Skip to content

Commit cf83c9e

Browse files
committed
channel: Add selfHasContentAccess, using canSubscribeGroup et al
Fixes #814. Fixes part of #1786. The general implementation of group-based permissions (#814) was added in the commits leading up to this one. This commit demonstrates implementing a given such permission. Then we also use the new permission in one place where we already want to use it, toward #1786. (There's another part of #1786 which calls for a variation: determining whether the user would be able to re-subscribe to a channel if they unsubscribe. We'll leave that for a follow-up.) We'll also be adding a more prominent bit of UI that wants this permission soon: the list of channels, #188. To exercise this method on real data, I added some debugging prints that printed a list of the channels where this returned true, and a list of the channels where it returned false. I also manually tested the change for #1786, by visiting an unsubscribed private channel (via the link in a #-mention), long-pressing the app bar to get the channel action sheet, and then (as another user, on web) adding and removing permission for the test user to subscribe themself.
1 parent c3be229 commit cf83c9e

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

lib/model/channel.dart

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:flutter/foundation.dart';
55
import '../api/model/events.dart';
66
import '../api/model/initial_snapshot.dart';
77
import '../api/model/model.dart';
8+
import 'realm.dart';
89
import 'store.dart';
910
import 'user.dart';
1011

@@ -139,6 +140,44 @@ mixin ChannelStore on UserStore {
139140
}
140141
}
141142

143+
bool selfHasContentAccess(ZulipStream channel) {
144+
// Compare web's stream_data.has_content_access.
145+
if (channel.isWebPublic) return true;
146+
if (channel is Subscription) return true;
147+
// Here web calls has_metadata_access... but that always returns true,
148+
// as its comment says.
149+
if (selfUser.role == UserRole.guest) return false;
150+
if (!channel.inviteOnly) return true;
151+
return _selfHasContentAccessViaGroupPermissions(channel);
152+
}
153+
154+
bool _selfHasContentAccessViaGroupPermissions(ZulipStream channel) {
155+
// Compare web's stream_data.has_content_access_via_group_permissions.
156+
// TODO(#814) try to clean up this logic; perhaps record more explicitly
157+
// what default/fallback value to use for a given group-based permission
158+
// on older servers.
159+
160+
if (channel.canAddSubscribersGroup != null
161+
&& selfHasPermissionForGroupSetting(channel.canAddSubscribersGroup!,
162+
GroupSettingType.stream, 'can_add_subscribers_group')) {
163+
// The behavior before this permission was introduced was equivalent to
164+
// the "nobody" group.
165+
// TODO(server-10): simplify
166+
return true;
167+
}
168+
169+
if (channel.canSubscribeGroup != null
170+
&& selfHasPermissionForGroupSetting(channel.canSubscribeGroup!,
171+
GroupSettingType.stream, 'can_subscribe_group')) {
172+
// The behavior before this permission was introduced was equivalent to
173+
// the "nobody" group.
174+
// TODO(server-10): simplify
175+
return true;
176+
}
177+
178+
return false;
179+
}
180+
142181
bool hasPostingPermission({
143182
required ZulipStream inChannel,
144183
required User user,

lib/widgets/action_sheet.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,10 +445,11 @@ void showChannelActionSheet(BuildContext context, {
445445
&& messageListPageNarrow.streamId == channelId;
446446

447447
final unreadCount = store.unreads.countInChannelNarrow(channelId);
448-
final isSubscribed = store.subscriptions[channelId] != null;
448+
final channel = store.streams[channelId];
449+
final isSubscribed = channel is Subscription;
449450
final buttonSections = [
450-
if (!isSubscribed)
451-
// TODO(#1786) check group-based can-subscribe permission
451+
if (!isSubscribed
452+
&& channel != null && store.selfHasContentAccess(channel))
452453
[SubscribeButton(pageContext: pageContext, channelId: channelId)],
453454
[
454455
if (unreadCount > 0)

0 commit comments

Comments
 (0)