Skip to content

Commit 4add691

Browse files
committed
model: Add hasPassedWaitingPeriod helper method to PerAccountStore
Also add `UserRole.isAtLeast` method.
1 parent 97485ab commit 4add691

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

lib/api/model/model.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@ enum UserRole{
303303
final int? apiValue;
304304

305305
int? toJson() => apiValue;
306+
307+
bool isAtLeast(UserRole threshold) {
308+
// Roles with more privilege have lower [apiValue].
309+
return apiValue! <= threshold.apiValue!;
310+
}
306311
}
307312

308313
/// As in `streams` in the initial snapshot.

lib/model/store.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,19 @@ class PerAccountStore extends ChangeNotifier with EmojiStore, ChannelStore, Mess
434434

435435
final TypingStatus typingStatus;
436436

437+
/// Whether [user] has passed the realm's waiting period to be a full member.
438+
///
439+
/// See:
440+
/// https://zulip.com/api/roles-and-permissions#determining-if-a-user-is-a-full-member
441+
///
442+
/// To determine if a user is a full member, callers must also check that the
443+
/// user's role is at least `UserRole.member`.
444+
bool hasPassedWaitingPeriod(User user, {required DateTime byDate}) {
445+
// dateJoined is in UTC, in the following format: 2024-02-24T11:18+00:00.
446+
final dateJoined = DateTime.parse(user.dateJoined);
447+
return byDate.difference(dateJoined).inDays >= realmWaitingPeriodThreshold;
448+
}
449+
437450
////////////////////////////////
438451
// Streams, topics, and stuff about them.
439452

test/model/store_test.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,28 @@ void main() {
247247
});
248248
});
249249

250+
group('PerAccountStore.hasPassedWaitingPeriod', () {
251+
final store = eg.store(initialSnapshot:
252+
eg.initialSnapshot(realmWaitingPeriodThreshold: 2));
253+
254+
final testCases = [
255+
('2024-11-25T10:00+00:00', DateTime.utc(2024, 11, 25 + 0, 10, 00), false),
256+
('2024-11-25T10:00+00:00', DateTime.utc(2024, 11, 25 + 1, 10, 00), false),
257+
('2024-11-25T10:00+00:00', DateTime.utc(2024, 11, 25 + 2, 09, 59), false),
258+
('2024-11-25T10:00+00:00', DateTime.utc(2024, 11, 25 + 2, 10, 00), true),
259+
('2024-11-25T10:00+00:00', DateTime.utc(2024, 11, 25 + 1000, 07, 00), true),
260+
];
261+
262+
for (final (String dateJoined, DateTime currentDate, bool hasPassedWaitingPeriod) in testCases) {
263+
test('user joined at $dateJoined ${hasPassedWaitingPeriod ? 'has' : "hasn't"} '
264+
'passed waiting period by $currentDate', () {
265+
final user = eg.user(dateJoined: dateJoined);
266+
check(store.hasPassedWaitingPeriod(user, byDate: currentDate))
267+
.equals(hasPassedWaitingPeriod);
268+
});
269+
}
270+
});
271+
250272
group('PerAccountStore.handleEvent', () {
251273
// Mostly this method just dispatches to ChannelStore and MessageStore etc.,
252274
// and so most of the tests live in the test files for those

0 commit comments

Comments
 (0)