Skip to content

Commit f09ec3e

Browse files
committed
model: Add hasPostingPermission helper method to ZulipStream
Also add `UserRole.isAtLeast` method.
1 parent 93ce93a commit f09ec3e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

lib/api/model/model.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,10 @@ enum UserRole{
314314
final int? apiValue;
315315

316316
int? toJson() => apiValue;
317+
318+
bool isAtLeast(UserRole role) {
319+
return (apiValue ?? 0) <= (role.apiValue ?? 0);
320+
}
317321
}
318322

319323
/// As in `streams` in the initial snapshot.
@@ -381,6 +385,19 @@ class ZulipStream {
381385
_$ZulipStreamFromJson(json);
382386

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

386403
/// Policy for which users can post to the stream.

0 commit comments

Comments
 (0)