Skip to content

Commit cdb98e0

Browse files
committed
refactor(user-preferences): update limit enforcement logic and dependencies
- Replace AppConfig with RemoteConfig for fetching user preference limits - Remove unused UserContentPreferencesRepository dependency - Simplify user role checking by using dashboardRole instead of roles - Update error handling and messaging - Rename `followedCategories` to `followedTopics` in error message
1 parent 2832ed5 commit cdb98e0

File tree

1 file changed

+47
-56
lines changed

1 file changed

+47
-56
lines changed

lib/src/services/default_user_preference_limit_service.dart

Lines changed: 47 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ import 'package:ht_shared/ht_shared.dart';
44

55
/// {@template default_user_preference_limit_service}
66
/// Default implementation of [UserPreferenceLimitService] that enforces limits
7-
/// based on user role and [AppConfig].
7+
/// based on user role and [RemoteConfig].
88
/// {@endtemplate}
99
class DefaultUserPreferenceLimitService implements UserPreferenceLimitService {
1010
/// {@macro default_user_preference_limit_service}
1111
const DefaultUserPreferenceLimitService({
12-
required HtDataRepository<AppConfig> appConfigRepository,
13-
// Removed unused UserContentPreferencesRepository
14-
}) : _appConfigRepository = appConfigRepository;
12+
required HtDataRepository<RemoteConfig> remoteConfigRepository,
13+
}) : _remoteConfigRepository = remoteConfigRepository;
1514

16-
final HtDataRepository<AppConfig> _appConfigRepository;
15+
final HtDataRepository<RemoteConfig> _remoteConfigRepository;
1716

18-
// Assuming a fixed ID for the AppConfig document
19-
static const String _appConfigId = 'app_config';
17+
// Assuming a fixed ID for the RemoteConfig document
18+
static const String _remoteConfigId = 'remote_config';
2019

2120
@override
2221
Future<void> checkAddItem(
@@ -25,39 +24,35 @@ class DefaultUserPreferenceLimitService implements UserPreferenceLimitService {
2524
int currentCount,
2625
) async {
2726
try {
28-
// 1. Fetch the application configuration to get limits
29-
final appConfig = await _appConfigRepository.read(id: _appConfigId);
30-
final limits = appConfig.userPreferenceLimits;
27+
// 1. Fetch the remote configuration to get limits
28+
final remoteConfig = await _remoteConfigRepository.read(id: _remoteConfigId);
29+
final limits = remoteConfig.userPreferenceLimits;
3130

3231
// Admins have no limits.
33-
if (user.roles.contains(UserRoles.admin)) {
32+
if (user.dashboardRole == DashboardUserRole.admin) {
3433
return;
3534
}
3635

37-
// 2. Determine the limit based on the user's highest role.
36+
// 2. Determine the limit based on the user's app role.
3837
int limit;
3938
String accountType;
4039

41-
if (user.roles.contains(UserRoles.premiumUser)) {
42-
accountType = 'premium';
43-
limit = (itemType == 'headline')
44-
? limits.premiumSavedHeadlinesLimit
45-
: limits.premiumFollowedItemsLimit;
46-
} else if (user.roles.contains(UserRoles.standardUser)) {
47-
accountType = 'standard';
48-
limit = (itemType == 'headline')
49-
? limits.authenticatedSavedHeadlinesLimit
50-
: limits.authenticatedFollowedItemsLimit;
51-
} else if (user.roles.contains(UserRoles.guestUser)) {
52-
accountType = 'guest';
53-
limit = (itemType == 'headline')
54-
? limits.guestSavedHeadlinesLimit
55-
: limits.guestFollowedItemsLimit;
56-
} else {
57-
// Fallback for users with unknown or no roles.
58-
throw const ForbiddenException(
59-
'Cannot determine preference limits for this user account.',
60-
);
40+
switch (user.appRole) {
41+
case AppUserRole.premiumUser:
42+
accountType = 'premium';
43+
limit = (itemType == 'headline')
44+
? limits.premiumSavedHeadlinesLimit
45+
: limits.premiumFollowedItemsLimit;
46+
case AppUserRole.standardUser:
47+
accountType = 'standard';
48+
limit = (itemType == 'headline')
49+
? limits.authenticatedSavedHeadlinesLimit
50+
: limits.authenticatedFollowedItemsLimit;
51+
case AppUserRole.guestUser:
52+
accountType = 'guest';
53+
limit = (itemType == 'headline')
54+
? limits.guestSavedHeadlinesLimit
55+
: limits.guestFollowedItemsLimit;
6156
}
6257

6358
// 3. Check if adding the item would exceed the limit
@@ -85,37 +80,33 @@ class DefaultUserPreferenceLimitService implements UserPreferenceLimitService {
8580
UserContentPreferences updatedPreferences,
8681
) async {
8782
try {
88-
// 1. Fetch the application configuration to get limits
89-
final appConfig = await _appConfigRepository.read(id: _appConfigId);
90-
final limits = appConfig.userPreferenceLimits;
83+
// 1. Fetch the remote configuration to get limits
84+
final remoteConfig = await _remoteConfigRepository.read(id: _remoteConfigId);
85+
final limits = remoteConfig.userPreferenceLimits;
9186

9287
// Admins have no limits.
93-
if (user.roles.contains(UserRoles.admin)) {
88+
if (user.dashboardRole == DashboardUserRole.admin) {
9489
return;
9590
}
9691

97-
// 2. Determine limits based on the user's highest role.
92+
// 2. Determine limits based on the user's app role.
9893
int followedItemsLimit;
9994
int savedHeadlinesLimit;
10095
String accountType;
10196

102-
if (user.roles.contains(UserRoles.premiumUser)) {
103-
accountType = 'premium';
104-
followedItemsLimit = limits.premiumFollowedItemsLimit;
105-
savedHeadlinesLimit = limits.premiumSavedHeadlinesLimit;
106-
} else if (user.roles.contains(UserRoles.standardUser)) {
107-
accountType = 'standard';
108-
followedItemsLimit = limits.authenticatedFollowedItemsLimit;
109-
savedHeadlinesLimit = limits.authenticatedSavedHeadlinesLimit;
110-
} else if (user.roles.contains(UserRoles.guestUser)) {
111-
accountType = 'guest';
112-
followedItemsLimit = limits.guestFollowedItemsLimit;
113-
savedHeadlinesLimit = limits.guestSavedHeadlinesLimit;
114-
} else {
115-
// Fallback for users with unknown or no roles.
116-
throw const ForbiddenException(
117-
'Cannot determine preference limits for this user account.',
118-
);
97+
switch (user.appRole) {
98+
case AppUserRole.premiumUser:
99+
accountType = 'premium';
100+
followedItemsLimit = limits.premiumFollowedItemsLimit;
101+
savedHeadlinesLimit = limits.premiumSavedHeadlinesLimit;
102+
case AppUserRole.standardUser:
103+
accountType = 'standard';
104+
followedItemsLimit = limits.authenticatedFollowedItemsLimit;
105+
savedHeadlinesLimit = limits.authenticatedSavedHeadlinesLimit;
106+
case AppUserRole.guestUser:
107+
accountType = 'guest';
108+
followedItemsLimit = limits.guestFollowedItemsLimit;
109+
savedHeadlinesLimit = limits.guestSavedHeadlinesLimit;
119110
}
120111

121112
// 3. Check if proposed preferences exceed limits
@@ -131,9 +122,9 @@ class DefaultUserPreferenceLimitService implements UserPreferenceLimitService {
131122
'for your account type ($accountType).',
132123
);
133124
}
134-
if (updatedPreferences.followedCategories.length > followedItemsLimit) {
125+
if (updatedPreferences.followedTopics.length > followedItemsLimit) {
135126
throw ForbiddenException(
136-
'You have reached the maximum number of followed categories allowed '
127+
'You have reached the maximum number of followed topics allowed '
137128
'for your account type ($accountType).',
138129
);
139130
}

0 commit comments

Comments
 (0)