Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compose_box: Replace compose box with a banner when cannot post in a channel #886

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
18 changes: 18 additions & 0 deletions lib/api/model/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ enum UserRole{
final int? apiValue;

int? toJson() => apiValue;

bool isAtLeast(UserRole threshold) {
// Roles with more privilege have lower [apiValue].
return (apiValue ?? 0) <= (threshold.apiValue ?? 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this, I was thinking about why 0 is chosen as the default value for both operands. Considering the context this helper is used, I suppose that we want to fallback to the behavior that, when the role has an unknown value, we allow the user to post.

This consideration is specific to the posting permission feature we are implementing here. I think the best thing to do is to assert that both api values are non-null; then we handle the case when either of them is null in hasPostingPermission.

}
}

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

Map<String, dynamic> toJson() => _$ZulipStreamToJson(this);

bool hasPostingPermission(User user, {required DateTime byDate, required int realmWaitingPeriodThreshold}) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: let's break this into multiple lines

final role = user.role;
return switch (channelPostPolicy) {
ChannelPostPolicy.any => true,
ChannelPostPolicy.fullMembers => role.isAtLeast(UserRole.member) && (role == UserRole.member
Copy link
Member

@PIG208 PIG208 Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: by turning this into || we can replace the ... ? ... : true clause with ... && ...

? user.hasPassedWaitingPeriod(byDate, realmWaitingPeriodThreshold)
: true),
ChannelPostPolicy.moderators => role.isAtLeast(UserRole.moderator),
ChannelPostPolicy.administrators => role.isAtLeast(UserRole.administrator),
ChannelPostPolicy.unknown => true,
};
}
}

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