Skip to content

Commit d71f081

Browse files
committed
user [nfc]: Move hasPassedWaitingPeriod here
1 parent d1b0812 commit d71f081

File tree

4 files changed

+44
-44
lines changed

4 files changed

+44
-44
lines changed

lib/model/store.dart

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -618,28 +618,6 @@ class PerAccountStore extends PerAccountStoreBase with
618618

619619
final Presence presence;
620620

621-
/// Whether [user] has passed the realm's waiting period to be a full member.
622-
///
623-
/// See:
624-
/// https://zulip.com/api/roles-and-permissions#determining-if-a-user-is-a-full-member
625-
///
626-
/// To determine if a user is a full member, callers must also check that the
627-
/// user's role is at least [UserRole.member].
628-
bool hasPassedWaitingPeriod(User user, {required DateTime byDate}) {
629-
// [User.dateJoined] is in UTC. For logged-in users, the format is:
630-
// YYYY-MM-DDTHH:mm+00:00, which includes the timezone offset for UTC.
631-
// For logged-out spectators, the format is: YYYY-MM-DD, which doesn't
632-
// include the timezone offset. In the later case, [DateTime.parse] will
633-
// interpret it as the client's local timezone, which could lead to
634-
// incorrect results; but that's acceptable for now because the app
635-
// doesn't support viewing as a spectator.
636-
//
637-
// See the related discussion:
638-
// https://chat.zulip.org/#narrow/channel/412-api-documentation/topic/provide.20an.20explicit.20format.20for.20.60realm_user.2Edate_joined.60/near/1980194
639-
final dateJoined = DateTime.parse(user.dateJoined);
640-
return byDate.difference(dateJoined).inDays >= realmWaitingPeriodThreshold;
641-
}
642-
643621
/// The user's real email address, if known, for displaying in the UI.
644622
///
645623
/// Returns null if self-user isn't able to see the user's real email address,

lib/model/user.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,28 @@ mixin UserStore on PerAccountStoreBase, RealmStore {
8383
return getUser(senderId)?.fullName ?? message.senderFullName;
8484
}
8585

86+
/// Whether [user] has passed the realm's waiting period to be a full member.
87+
///
88+
/// See:
89+
/// https://zulip.com/api/roles-and-permissions#determining-if-a-user-is-a-full-member
90+
///
91+
/// To determine if a user is a full member, callers must also check that the
92+
/// user's role is at least [UserRole.member].
93+
bool hasPassedWaitingPeriod(User user, {required DateTime byDate}) {
94+
// [User.dateJoined] is in UTC. For logged-in users, the format is:
95+
// YYYY-MM-DDTHH:mm+00:00, which includes the timezone offset for UTC.
96+
// For logged-out spectators, the format is: YYYY-MM-DD, which doesn't
97+
// include the timezone offset. In the later case, [DateTime.parse] will
98+
// interpret it as the client's local timezone, which could lead to
99+
// incorrect results; but that's acceptable for now because the app
100+
// doesn't support viewing as a spectator.
101+
//
102+
// See the related discussion:
103+
// https://chat.zulip.org/#narrow/channel/412-api-documentation/topic/provide.20an.20explicit.20format.20for.20.60realm_user.2Edate_joined.60/near/1980194
104+
final dateJoined = DateTime.parse(user.dateJoined);
105+
return byDate.difference(dateJoined).inDays >= realmWaitingPeriodThreshold;
106+
}
107+
86108
/// Whether the user with [userId] is muted by the self-user.
87109
///
88110
/// Looks for [userId] in a private [Set],

test/model/store_test.dart

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -465,28 +465,6 @@ void main() {
465465
});
466466
});
467467

468-
group('PerAccountStore.hasPassedWaitingPeriod', () {
469-
final store = eg.store(initialSnapshot:
470-
eg.initialSnapshot(realmWaitingPeriodThreshold: 2));
471-
472-
final testCases = [
473-
('2024-11-25T10:00+00:00', DateTime.utc(2024, 11, 25 + 0, 10, 00), false),
474-
('2024-11-25T10:00+00:00', DateTime.utc(2024, 11, 25 + 1, 10, 00), false),
475-
('2024-11-25T10:00+00:00', DateTime.utc(2024, 11, 25 + 2, 09, 59), false),
476-
('2024-11-25T10:00+00:00', DateTime.utc(2024, 11, 25 + 2, 10, 00), true),
477-
('2024-11-25T10:00+00:00', DateTime.utc(2024, 11, 25 + 1000, 07, 00), true),
478-
];
479-
480-
for (final (String dateJoined, DateTime currentDate, bool hasPassedWaitingPeriod) in testCases) {
481-
test('user joined at $dateJoined ${hasPassedWaitingPeriod ? 'has' : "hasn't"} '
482-
'passed waiting period by $currentDate', () {
483-
final user = eg.user(dateJoined: dateJoined);
484-
check(store.hasPassedWaitingPeriod(user, byDate: currentDate))
485-
.equals(hasPassedWaitingPeriod);
486-
});
487-
}
488-
});
489-
490468
group('PerAccountStore.hasPostingPermission', () {
491469
final testCases = [
492470
(ChannelPostPolicy.unknown, UserRole.unknown, true),

test/model/user_test.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,28 @@ void main() {
5858
});
5959
});
6060

61+
group('hasPassedWaitingPeriod', () {
62+
final store = eg.store(initialSnapshot:
63+
eg.initialSnapshot(realmWaitingPeriodThreshold: 2));
64+
65+
final testCases = [
66+
('2024-11-25T10:00+00:00', DateTime.utc(2024, 11, 25 + 0, 10, 00), false),
67+
('2024-11-25T10:00+00:00', DateTime.utc(2024, 11, 25 + 1, 10, 00), false),
68+
('2024-11-25T10:00+00:00', DateTime.utc(2024, 11, 25 + 2, 09, 59), false),
69+
('2024-11-25T10:00+00:00', DateTime.utc(2024, 11, 25 + 2, 10, 00), true),
70+
('2024-11-25T10:00+00:00', DateTime.utc(2024, 11, 25 + 1000, 07, 00), true),
71+
];
72+
73+
for (final (String dateJoined, DateTime currentDate, bool hasPassedWaitingPeriod) in testCases) {
74+
test('user joined at $dateJoined ${hasPassedWaitingPeriod ? 'has' : "hasn't"} '
75+
'passed waiting period by $currentDate', () {
76+
final user = eg.user(dateJoined: dateJoined);
77+
check(store.hasPassedWaitingPeriod(user, byDate: currentDate))
78+
.equals(hasPassedWaitingPeriod);
79+
});
80+
}
81+
});
82+
6183
group('RealmUserUpdateEvent', () {
6284
// TODO write more tests for handling RealmUserUpdateEvent
6385

0 commit comments

Comments
 (0)