Skip to content

Commit d1c16a9

Browse files
committed
model: Add hasPostingPermission helper method to ZulipStream
Also add `UserRole.isAtLeast` method.
1 parent 6cf3dd9 commit d1c16a9

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

lib/api/model/model.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ enum UserRole{
315315
final int? apiValue;
316316

317317
int? toJson() => apiValue;
318+
319+
bool isAtLeast(UserRole threshold) {
320+
// Roles with more privilege have lower [apiValue].
321+
return (apiValue ?? 0) <= (threshold.apiValue ?? 0);
322+
}
318323
}
319324

320325
/// As in `streams` in the initial snapshot.
@@ -382,6 +387,19 @@ class ZulipStream {
382387
_$ZulipStreamFromJson(json);
383388

384389
Map<String, dynamic> toJson() => _$ZulipStreamToJson(this);
390+
391+
bool hasPostingPermission(User user, {required DateTime byDate, required int realmWaitingPeriodThreshold}) {
392+
final role = user.role;
393+
return switch (channelPostPolicy) {
394+
ChannelPostPolicy.any => true,
395+
ChannelPostPolicy.fullMembers => role.isAtLeast(UserRole.member) && (role == UserRole.member
396+
? user.hasPassedWaitingPeriod(byDate, realmWaitingPeriodThreshold)
397+
: true),
398+
ChannelPostPolicy.moderators => role.isAtLeast(UserRole.moderator),
399+
ChannelPostPolicy.administrators => role.isAtLeast(UserRole.administrator),
400+
ChannelPostPolicy.unknown => true,
401+
};
402+
}
385403
}
386404

387405
/// The name of a property of [ZulipStream] that gets updated

0 commit comments

Comments
 (0)