Skip to content

Commit 7b75c3a

Browse files
committed
channel: Implement modern group-based permission check for can-send-message
Fixes #1862.
1 parent 95dc22c commit 7b75c3a

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

lib/model/channel.dart

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,21 @@ mixin ChannelStore on UserStore {
191191
bool selfCanSendMessage({
192192
required ZulipStream inChannel,
193193
required DateTime byDate,
194+
}) {
195+
// (selfHasPermissionForGroupSetting isn't equipped to handle the old-server
196+
// fallback logic for this specific permission; it's dynamic and depends on
197+
// channelPostPolicy, so we do our own null check here.)
198+
if (inChannel.canSendMessageGroup != null) {
199+
return selfHasPermissionForGroupSetting(inChannel.canSendMessageGroup!,
200+
GroupSettingType.stream, 'can_send_message_group');
201+
} else {
202+
return _selfPassesLegacyChannelPostPolicy(inChannel: inChannel, atDate: byDate);
203+
}
204+
}
205+
206+
bool _selfPassesLegacyChannelPostPolicy({
207+
required ZulipStream inChannel,
208+
required DateTime atDate,
194209
}) {
195210
final role = selfUser.role;
196211
// We let the users with [unknown] role to send the message, then the server
@@ -202,7 +217,7 @@ mixin ChannelStore on UserStore {
202217
case ChannelPostPolicy.fullMembers: {
203218
if (!role.isAtLeast(UserRole.member)) return false;
204219
if (role == UserRole.member) {
205-
return selfHasPassedWaitingPeriod(byDate: byDate);
220+
return selfHasPassedWaitingPeriod(byDate: atDate);
206221
}
207222
return true;
208223
}

test/model/channel_test.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ import 'package:zulip/model/channel.dart';
99
import '../api/model/model_checks.dart';
1010
import '../example_data.dart' as eg;
1111
import '../stdlib_checks.dart';
12+
import 'binding.dart';
1213
import 'test_store.dart';
1314

1415
void main() {
16+
TestZulipBinding.ensureInitialized();
17+
1518
group('Unified stream/sub data', () {
1619
/// Check that `streams`, `streamsByName`, and `subscriptions` all agree
1720
/// and point to the same objects where applicable.
@@ -456,6 +459,32 @@ void main() {
456459
});
457460

458461
group('selfCanSendMessage', () {
462+
test('in group', () {
463+
addTearDown(testBinding.reset);
464+
final now = testBinding.utcNow();
465+
466+
final canSendMessageGroup = eg.groupSetting(members: [eg.selfUser.userId]);
467+
final channel = eg.stream(canSendMessageGroup: canSendMessageGroup);
468+
final store = eg.store(
469+
initialSnapshot: eg.initialSnapshot(streams: [channel]));
470+
check(store.selfCanSendMessage(inChannel: channel, byDate: now))
471+
.isTrue();
472+
});
473+
474+
test('not in group', () {
475+
addTearDown(testBinding.reset);
476+
final now = testBinding.utcNow();
477+
478+
final canSendMessageGroup = eg.groupSetting(members: []);
479+
final channel = eg.stream(canSendMessageGroup: canSendMessageGroup);
480+
final store = eg.store(
481+
initialSnapshot: eg.initialSnapshot(streams: [channel]));
482+
check(store.selfCanSendMessage(inChannel: channel, byDate: now))
483+
.isFalse();
484+
});
485+
});
486+
487+
group('selfCanSendMessage, legacy', () {
459488
final testCases = [
460489
(ChannelPostPolicy.unknown, UserRole.unknown, true),
461490
(ChannelPostPolicy.unknown, UserRole.guest, true),

0 commit comments

Comments
 (0)