diff --git a/lib/model/channel.dart b/lib/model/channel.dart index 11d556b902..e4a40790d4 100644 --- a/lib/model/channel.dart +++ b/lib/model/channel.dart @@ -199,12 +199,11 @@ mixin ChannelStore on UserStore { return false; } - bool hasPostingPermission({ + bool selfCanSendMessage({ required ZulipStream inChannel, - required User user, required DateTime byDate, }) { - final role = user.role; + final role = selfUser.role; // We let the users with [unknown] role to send the message, then the server // will decide to accept it or not based on its actual role. if (role == UserRole.unknown) return true; @@ -214,7 +213,7 @@ mixin ChannelStore on UserStore { case ChannelPostPolicy.fullMembers: { if (!role.isAtLeast(UserRole.member)) return false; if (role == UserRole.member) { - return hasPassedWaitingPeriod(user, byDate: byDate); + return hasPassedWaitingPeriod(selfUser, byDate: byDate); } return true; } diff --git a/lib/widgets/compose_box.dart b/lib/widgets/compose_box.dart index 1644a6a038..76a4e36411 100644 --- a/lib/widgets/compose_box.dart +++ b/lib/widgets/compose_box.dart @@ -2153,8 +2153,8 @@ class _ComposeBoxState extends State with PerAccountStoreAwareStateM case ChannelNarrow(:final streamId): case TopicNarrow(:final streamId): final channel = store.streams[streamId]; - if (channel == null || !store.hasPostingPermission(inChannel: channel, - user: store.selfUser, byDate: DateTime.now())) { + if (channel == null || !store.selfCanSendMessage(inChannel: channel, + byDate: DateTime.now())) { return _ErrorBanner(getLabel: (zulipLocalizations) => zulipLocalizations.errorBannerCannotPostInChannelLabel); } diff --git a/lib/widgets/share.dart b/lib/widgets/share.dart index 083ed83bba..df776e4d1e 100644 --- a/lib/widgets/share.dart +++ b/lib/widgets/share.dart @@ -191,7 +191,7 @@ class SharePage extends StatelessWidget { body: TabBarView(children: [ SubscriptionListPageBody( showTopicListButtonInActionSheet: false, - hideChannelsIfUserCantPost: true, + hideChannelsIfUserCantSendMessage: true, allowGoToAllChannels: false, onChannelSelect: (narrow) => _handleNarrowSelect(context, narrow), // TODO(#412) add onTopicSelect, Currently when user lands on the diff --git a/lib/widgets/subscription_list.dart b/lib/widgets/subscription_list.dart index 50fd68cff0..0feaeeaa52 100644 --- a/lib/widgets/subscription_list.dart +++ b/lib/widgets/subscription_list.dart @@ -23,7 +23,7 @@ class SubscriptionListPageBody extends StatefulWidget { const SubscriptionListPageBody({ super.key, this.showTopicListButtonInActionSheet = true, - this.hideChannelsIfUserCantPost = false, + this.hideChannelsIfUserCantSendMessage = false, this.allowGoToAllChannels = true, this.onChannelSelect, }); @@ -34,7 +34,7 @@ class SubscriptionListPageBody extends StatefulWidget { // See discussion: // https://github.com/zulip/zulip-flutter/pull/1774#discussion_r2249032503 final bool showTopicListButtonInActionSheet; - final bool hideChannelsIfUserCantPost; + final bool hideChannelsIfUserCantSendMessage; final bool allowGoToAllChannels; /// Callback to invoke when the user selects a channel from the list. @@ -120,9 +120,8 @@ class _SubscriptionListPageBodyState extends State wit final List unpinned = []; final now = DateTime.now(); for (final subscription in store.subscriptions.values) { - if (widget.hideChannelsIfUserCantPost) { - if (!store.hasPostingPermission(inChannel: subscription, - user: store.selfUser, byDate: now)) { + if (widget.hideChannelsIfUserCantSendMessage) { + if (!store.selfCanSendMessage(inChannel: subscription, byDate: now)) { continue; } } diff --git a/test/model/channel_test.dart b/test/model/channel_test.dart index 07e542e6a1..00dbfb10a2 100644 --- a/test/model/channel_test.dart +++ b/test/model/channel_test.dart @@ -493,9 +493,12 @@ void main() { for (final (ChannelPostPolicy policy, UserRole role, bool canPost) in testCases) { test('"${role.name}" user ${canPost ? 'can' : "can't"} post in channel ' 'with "${policy.name}" policy', () { - final store = eg.store(); - final actual = store.hasPostingPermission( - inChannel: eg.stream(channelPostPolicy: policy), user: eg.user(role: role), + final selfUserWithRole = eg.user(role: role); + final store = eg.store( + selfUser: selfUserWithRole, + initialSnapshot: eg.initialSnapshot(realmUsers: [selfUserWithRole])); + final actual = store.selfCanSendMessage( + inChannel: eg.stream(channelPostPolicy: policy), // [byDate] is not actually relevant for these test cases; for the // ones which it is, they're practiced below. byDate: DateTime.now()); @@ -504,27 +507,34 @@ void main() { } group('"member" user posting in a channel with "fullMembers" policy', () { - PerAccountStore localStore({required int realmWaitingPeriodThreshold}) => - eg.store(initialSnapshot: eg.initialSnapshot( - realmWaitingPeriodThreshold: realmWaitingPeriodThreshold)); + PerAccountStore localStore({ + required User selfUser, + required int realmWaitingPeriodThreshold, + }) => eg.store( + selfUser: selfUser, + initialSnapshot: eg.initialSnapshot( + realmWaitingPeriodThreshold: realmWaitingPeriodThreshold, + realmUsers: [selfUser])); User memberUser({required String dateJoined}) => eg.user( role: UserRole.member, dateJoined: dateJoined); test('a "full" member -> can post in the channel', () { - final store = localStore(realmWaitingPeriodThreshold: 3); - final hasPermission = store.hasPostingPermission( + final store = localStore( + selfUser: memberUser(dateJoined: '2024-11-25T10:00+00:00'), + realmWaitingPeriodThreshold: 3); + final hasPermission = store.selfCanSendMessage( inChannel: eg.stream(channelPostPolicy: ChannelPostPolicy.fullMembers), - user: memberUser(dateJoined: '2024-11-25T10:00+00:00'), byDate: DateTime.utc(2024, 11, 28, 10, 00)); check(hasPermission).isTrue(); }); test('not a "full" member -> cannot post in the channel', () { - final store = localStore(realmWaitingPeriodThreshold: 3); - final actual = store.hasPostingPermission( + final store = localStore( + selfUser: memberUser(dateJoined: '2024-11-25T10:00+00:00'), + realmWaitingPeriodThreshold: 3); + final actual = store.selfCanSendMessage( inChannel: eg.stream(channelPostPolicy: ChannelPostPolicy.fullMembers), - user: memberUser(dateJoined: '2024-11-25T10:00+00:00'), byDate: DateTime.utc(2024, 11, 28, 09, 59)); check(actual).isFalse(); });