Skip to content

Commit da1d968

Browse files
chrisbobbegnprice
authored andcommitted
channel [nfc]: s/hasPostingPermission/selfCanSendMessage/; assume self-user
This new name aligns better with the API's naming of the modern group-based permission, can_send_message_group, which we might implement soon.
1 parent 7cab2ad commit da1d968

File tree

5 files changed

+32
-24
lines changed

5 files changed

+32
-24
lines changed

lib/model/channel.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,11 @@ mixin ChannelStore on UserStore {
199199
return false;
200200
}
201201

202-
bool hasPostingPermission({
202+
bool selfCanSendMessage({
203203
required ZulipStream inChannel,
204-
required User user,
205204
required DateTime byDate,
206205
}) {
207-
final role = user.role;
206+
final role = selfUser.role;
208207
// We let the users with [unknown] role to send the message, then the server
209208
// will decide to accept it or not based on its actual role.
210209
if (role == UserRole.unknown) return true;
@@ -214,7 +213,7 @@ mixin ChannelStore on UserStore {
214213
case ChannelPostPolicy.fullMembers: {
215214
if (!role.isAtLeast(UserRole.member)) return false;
216215
if (role == UserRole.member) {
217-
return hasPassedWaitingPeriod(user, byDate: byDate);
216+
return hasPassedWaitingPeriod(selfUser, byDate: byDate);
218217
}
219218
return true;
220219
}

lib/widgets/compose_box.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,8 +2153,8 @@ class _ComposeBoxState extends State<ComposeBox> with PerAccountStoreAwareStateM
21532153
case ChannelNarrow(:final streamId):
21542154
case TopicNarrow(:final streamId):
21552155
final channel = store.streams[streamId];
2156-
if (channel == null || !store.hasPostingPermission(inChannel: channel,
2157-
user: store.selfUser, byDate: DateTime.now())) {
2156+
if (channel == null || !store.selfCanSendMessage(inChannel: channel,
2157+
byDate: DateTime.now())) {
21582158
return _ErrorBanner(getLabel: (zulipLocalizations) =>
21592159
zulipLocalizations.errorBannerCannotPostInChannelLabel);
21602160
}

lib/widgets/share.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class SharePage extends StatelessWidget {
191191
body: TabBarView(children: [
192192
SubscriptionListPageBody(
193193
showTopicListButtonInActionSheet: false,
194-
hideChannelsIfUserCantPost: true,
194+
hideChannelsIfUserCantSendMessage: true,
195195
allowGoToAllChannels: false,
196196
onChannelSelect: (narrow) => _handleNarrowSelect(context, narrow),
197197
// TODO(#412) add onTopicSelect, Currently when user lands on the

lib/widgets/subscription_list.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class SubscriptionListPageBody extends StatefulWidget {
2323
const SubscriptionListPageBody({
2424
super.key,
2525
this.showTopicListButtonInActionSheet = true,
26-
this.hideChannelsIfUserCantPost = false,
26+
this.hideChannelsIfUserCantSendMessage = false,
2727
this.allowGoToAllChannels = true,
2828
this.onChannelSelect,
2929
});
@@ -34,7 +34,7 @@ class SubscriptionListPageBody extends StatefulWidget {
3434
// See discussion:
3535
// https://github.com/zulip/zulip-flutter/pull/1774#discussion_r2249032503
3636
final bool showTopicListButtonInActionSheet;
37-
final bool hideChannelsIfUserCantPost;
37+
final bool hideChannelsIfUserCantSendMessage;
3838
final bool allowGoToAllChannels;
3939

4040
/// Callback to invoke when the user selects a channel from the list.
@@ -120,9 +120,8 @@ class _SubscriptionListPageBodyState extends State<SubscriptionListPageBody> wit
120120
final List<Subscription> unpinned = [];
121121
final now = DateTime.now();
122122
for (final subscription in store.subscriptions.values) {
123-
if (widget.hideChannelsIfUserCantPost) {
124-
if (!store.hasPostingPermission(inChannel: subscription,
125-
user: store.selfUser, byDate: now)) {
123+
if (widget.hideChannelsIfUserCantSendMessage) {
124+
if (!store.selfCanSendMessage(inChannel: subscription, byDate: now)) {
126125
continue;
127126
}
128127
}

test/model/channel_test.dart

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,12 @@ void main() {
493493
for (final (ChannelPostPolicy policy, UserRole role, bool canPost) in testCases) {
494494
test('"${role.name}" user ${canPost ? 'can' : "can't"} post in channel '
495495
'with "${policy.name}" policy', () {
496-
final store = eg.store();
497-
final actual = store.hasPostingPermission(
498-
inChannel: eg.stream(channelPostPolicy: policy), user: eg.user(role: role),
496+
final selfUserWithRole = eg.user(role: role);
497+
final store = eg.store(
498+
selfUser: selfUserWithRole,
499+
initialSnapshot: eg.initialSnapshot(realmUsers: [selfUserWithRole]));
500+
final actual = store.selfCanSendMessage(
501+
inChannel: eg.stream(channelPostPolicy: policy),
499502
// [byDate] is not actually relevant for these test cases; for the
500503
// ones which it is, they're practiced below.
501504
byDate: DateTime.now());
@@ -504,27 +507,34 @@ void main() {
504507
}
505508

506509
group('"member" user posting in a channel with "fullMembers" policy', () {
507-
PerAccountStore localStore({required int realmWaitingPeriodThreshold}) =>
508-
eg.store(initialSnapshot: eg.initialSnapshot(
509-
realmWaitingPeriodThreshold: realmWaitingPeriodThreshold));
510+
PerAccountStore localStore({
511+
required User selfUser,
512+
required int realmWaitingPeriodThreshold,
513+
}) => eg.store(
514+
selfUser: selfUser,
515+
initialSnapshot: eg.initialSnapshot(
516+
realmWaitingPeriodThreshold: realmWaitingPeriodThreshold,
517+
realmUsers: [selfUser]));
510518

511519
User memberUser({required String dateJoined}) => eg.user(
512520
role: UserRole.member, dateJoined: dateJoined);
513521

514522
test('a "full" member -> can post in the channel', () {
515-
final store = localStore(realmWaitingPeriodThreshold: 3);
516-
final hasPermission = store.hasPostingPermission(
523+
final store = localStore(
524+
selfUser: memberUser(dateJoined: '2024-11-25T10:00+00:00'),
525+
realmWaitingPeriodThreshold: 3);
526+
final hasPermission = store.selfCanSendMessage(
517527
inChannel: eg.stream(channelPostPolicy: ChannelPostPolicy.fullMembers),
518-
user: memberUser(dateJoined: '2024-11-25T10:00+00:00'),
519528
byDate: DateTime.utc(2024, 11, 28, 10, 00));
520529
check(hasPermission).isTrue();
521530
});
522531

523532
test('not a "full" member -> cannot post in the channel', () {
524-
final store = localStore(realmWaitingPeriodThreshold: 3);
525-
final actual = store.hasPostingPermission(
533+
final store = localStore(
534+
selfUser: memberUser(dateJoined: '2024-11-25T10:00+00:00'),
535+
realmWaitingPeriodThreshold: 3);
536+
final actual = store.selfCanSendMessage(
526537
inChannel: eg.stream(channelPostPolicy: ChannelPostPolicy.fullMembers),
527-
user: memberUser(dateJoined: '2024-11-25T10:00+00:00'),
528538
byDate: DateTime.utc(2024, 11, 28, 09, 59));
529539
check(actual).isFalse();
530540
});

0 commit comments

Comments
 (0)