Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions lib/model/channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/widgets/compose_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2153,8 +2153,8 @@ class _ComposeBoxState extends State<ComposeBox> 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);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/share.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions lib/widgets/subscription_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -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.
Expand Down Expand Up @@ -120,9 +120,8 @@ class _SubscriptionListPageBodyState extends State<SubscriptionListPageBody> wit
final List<Subscription> 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;
}
}
Expand Down
34 changes: 22 additions & 12 deletions test/model/channel_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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();
});
Expand Down