@@ -4,19 +4,18 @@ import 'package:ht_shared/ht_shared.dart';
4
4
5
5
/// {@template default_user_preference_limit_service}
6
6
/// Default implementation of [UserPreferenceLimitService] that enforces limits
7
- /// based on user role and [AppConfig ] .
7
+ /// based on user role and [RemoteConfig ] .
8
8
/// {@endtemplate}
9
9
class DefaultUserPreferenceLimitService implements UserPreferenceLimitService {
10
10
/// {@macro default_user_preference_limit_service}
11
11
const DefaultUserPreferenceLimitService ({
12
- required HtDataRepository <AppConfig > appConfigRepository,
13
- // Removed unused UserContentPreferencesRepository
14
- }) : _appConfigRepository = appConfigRepository;
12
+ required HtDataRepository <RemoteConfig > remoteConfigRepository,
13
+ }) : _remoteConfigRepository = remoteConfigRepository;
15
14
16
- final HtDataRepository <AppConfig > _appConfigRepository ;
15
+ final HtDataRepository <RemoteConfig > _remoteConfigRepository ;
17
16
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 ' ;
20
19
21
20
@override
22
21
Future <void > checkAddItem (
@@ -25,39 +24,35 @@ class DefaultUserPreferenceLimitService implements UserPreferenceLimitService {
25
24
int currentCount,
26
25
) async {
27
26
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;
31
30
32
31
// Admins have no limits.
33
- if (user.roles. contains ( UserRoles . admin) ) {
32
+ if (user.dashboardRole == DashboardUserRole . admin) {
34
33
return ;
35
34
}
36
35
37
- // 2. Determine the limit based on the user's highest role.
36
+ // 2. Determine the limit based on the user's app role.
38
37
int limit;
39
38
String accountType;
40
39
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;
61
56
}
62
57
63
58
// 3. Check if adding the item would exceed the limit
@@ -85,37 +80,33 @@ class DefaultUserPreferenceLimitService implements UserPreferenceLimitService {
85
80
UserContentPreferences updatedPreferences,
86
81
) async {
87
82
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;
91
86
92
87
// Admins have no limits.
93
- if (user.roles. contains ( UserRoles . admin) ) {
88
+ if (user.dashboardRole == DashboardUserRole . admin) {
94
89
return ;
95
90
}
96
91
97
- // 2. Determine limits based on the user's highest role.
92
+ // 2. Determine limits based on the user's app role.
98
93
int followedItemsLimit;
99
94
int savedHeadlinesLimit;
100
95
String accountType;
101
96
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;
119
110
}
120
111
121
112
// 3. Check if proposed preferences exceed limits
@@ -131,9 +122,9 @@ class DefaultUserPreferenceLimitService implements UserPreferenceLimitService {
131
122
'for your account type ($accountType ).' ,
132
123
);
133
124
}
134
- if (updatedPreferences.followedCategories .length > followedItemsLimit) {
125
+ if (updatedPreferences.followedTopics .length > followedItemsLimit) {
135
126
throw ForbiddenException (
136
- 'You have reached the maximum number of followed categories allowed '
127
+ 'You have reached the maximum number of followed topics allowed '
137
128
'for your account type ($accountType ).' ,
138
129
);
139
130
}
0 commit comments