Skip to content

Commit d3665ae

Browse files
committed
per_account_store: reduce redundant arguments
1 parent a4879a9 commit d3665ae

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

lib/model/store.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import '../log.dart';
2121
import '../notifications/receive.dart';
2222
import 'actions.dart';
2323
import 'autocomplete.dart';
24+
import 'binding.dart';
2425
import 'database.dart';
2526
import 'emoji.dart';
2627
import 'localizations.dart';
@@ -678,7 +679,7 @@ class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStor
678679
///
679680
/// To determine if a user is a full member, callers must also check that the
680681
/// user's role is at least [UserRole.member].
681-
bool hasPassedWaitingPeriod(User user, {required DateTime byDate}) {
682+
bool hasPassedWaitingPeriod(User user) {
682683
// [User.dateJoined] is in UTC. For logged-in users, the format is:
683684
// YYYY-MM-DDTHH:mm+00:00, which includes the timezone offset for UTC.
684685
// For logged-out spectators, the format is: YYYY-MM-DD, which doesn't
@@ -690,7 +691,8 @@ class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStor
690691
// See the related discussion:
691692
// https://chat.zulip.org/#narrow/channel/412-api-documentation/topic/provide.20an.20explicit.20format.20for.20.60realm_user.2Edate_joined.60/near/1980194
692693
final dateJoined = DateTime.parse(user.dateJoined);
693-
return byDate.difference(dateJoined).inDays >= realmWaitingPeriodThreshold;
694+
final now = ZulipBinding.instance.utcNow();
695+
return now.difference(dateJoined).inDays >= realmWaitingPeriodThreshold;
694696
}
695697

696698
/// The given user's real email address, if known, for displaying in the UI.
@@ -741,7 +743,6 @@ class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStor
741743
bool hasPostingPermission({
742744
required ZulipStream inChannel,
743745
required User user,
744-
required DateTime byDate,
745746
}) {
746747
final role = user.role;
747748
// We let the users with [unknown] role to send the message, then the server
@@ -753,7 +754,7 @@ class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStor
753754
case ChannelPostPolicy.fullMembers: {
754755
if (!role.isAtLeast(UserRole.member)) return false;
755756
return role == UserRole.member
756-
? hasPassedWaitingPeriod(user, byDate: byDate)
757+
? hasPassedWaitingPeriod(user)
757758
: true;
758759
}
759760
case ChannelPostPolicy.moderators: return role.isAtLeast(UserRole.moderator);

lib/widgets/compose_box.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,8 +2064,7 @@ class _ComposeBoxState extends State<ComposeBox> with PerAccountStoreAwareStateM
20642064
case ChannelNarrow(:final streamId):
20652065
case TopicNarrow(:final streamId):
20662066
final channel = store.streams[streamId];
2067-
if (channel == null || !store.hasPostingPermission(inChannel: channel,
2068-
user: store.selfUser, byDate: ZulipBinding.instance.utcNow())) {
2067+
if (channel == null || !store.hasPostingPermission(inChannel: channel, user: store.selfUser)) {
20692068
return _ErrorBanner(getLabel: (zulipLocalizations) =>
20702069
zulipLocalizations.errorBannerCannotPostInChannelLabel);
20712070
}

test/model/store_test.dart

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:async';
22
import 'dart:io';
33

44
import 'package:checks/checks.dart';
5+
import 'package:clock/clock.dart';
56
import 'package:fake_async/fake_async.dart';
67
import 'package:flutter/foundation.dart';
78
import 'package:http/http.dart' as http;
@@ -480,9 +481,11 @@ void main() {
480481
for (final (String dateJoined, DateTime currentDate, bool hasPassedWaitingPeriod) in testCases) {
481482
test('user joined at $dateJoined ${hasPassedWaitingPeriod ? 'has' : "hasn't"} '
482483
'passed waiting period by $currentDate', () {
483-
final user = eg.user(dateJoined: dateJoined);
484-
check(store.hasPassedWaitingPeriod(user, byDate: currentDate))
485-
.equals(hasPassedWaitingPeriod);
484+
withClock(Clock.fixed(currentDate), () {
485+
final user = eg.user(dateJoined: dateJoined);
486+
check(store.hasPassedWaitingPeriod(user))
487+
.equals(hasPassedWaitingPeriod);
488+
});
486489
});
487490
}
488491
});
@@ -526,11 +529,10 @@ void main() {
526529
test('"${role.name}" user ${canPost ? 'can' : "can't"} post in channel '
527530
'with "${policy.name}" policy', () {
528531
final store = eg.store();
532+
// we don't use `withClock` because current time is not actually relevant for
533+
// these test cases; for the ones which it is, they're practiced below.
529534
final actual = store.hasPostingPermission(
530-
inChannel: eg.stream(channelPostPolicy: policy), user: eg.user(role: role),
531-
// [byDate] is not actually relevant for these test cases; for the
532-
// ones which it is, they're practiced below.
533-
byDate: DateTime.now());
535+
inChannel: eg.stream(channelPostPolicy: policy), user: eg.user(role: role));
534536
check(actual).equals(canPost);
535537
});
536538
}
@@ -544,21 +546,23 @@ void main() {
544546
role: UserRole.member, dateJoined: dateJoined);
545547

546548
test('a "full" member -> can post in the channel', () {
547-
final store = localStore(realmWaitingPeriodThreshold: 3);
548-
final hasPermission = store.hasPostingPermission(
549-
inChannel: eg.stream(channelPostPolicy: ChannelPostPolicy.fullMembers),
550-
user: memberUser(dateJoined: '2024-11-25T10:00+00:00'),
551-
byDate: DateTime.utc(2024, 11, 28, 10, 00));
552-
check(hasPermission).isTrue();
549+
withClock(Clock.fixed(DateTime.utc(2024, 11, 28, 10, 00)), () {
550+
final store = localStore(realmWaitingPeriodThreshold: 3);
551+
final hasPermission = store.hasPostingPermission(
552+
inChannel: eg.stream(channelPostPolicy: ChannelPostPolicy.fullMembers),
553+
user: memberUser(dateJoined: '2024-11-25T10:00+00:00'));
554+
check(hasPermission).isTrue();
555+
});
553556
});
554557

555558
test('not a "full" member -> cannot post in the channel', () {
556-
final store = localStore(realmWaitingPeriodThreshold: 3);
557-
final actual = store.hasPostingPermission(
558-
inChannel: eg.stream(channelPostPolicy: ChannelPostPolicy.fullMembers),
559-
user: memberUser(dateJoined: '2024-11-25T10:00+00:00'),
560-
byDate: DateTime.utc(2024, 11, 28, 09, 59));
561-
check(actual).isFalse();
559+
withClock(Clock.fixed(DateTime.utc(2024, 11, 28, 09, 59)), () {
560+
final store = localStore(realmWaitingPeriodThreshold: 3);
561+
final actual = store.hasPostingPermission(
562+
inChannel: eg.stream(channelPostPolicy: ChannelPostPolicy.fullMembers),
563+
user: memberUser(dateJoined: '2024-11-25T10:00+00:00'));
564+
check(actual).isFalse();
565+
});
562566
});
563567
});
564568
});

0 commit comments

Comments
 (0)