Skip to content

Commit 2cf110b

Browse files
committed
refactor(push-notification): update breaking news subscription query
- Replace interest-based query with user preferences-based query - Improve efficiency by directly targeting embedded interests array - Simplify user ID collection process - Update logging to reflect new query approach
1 parent 918de42 commit 2cf110b

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

lib/src/services/push_notification_service.dart

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,23 @@ class DefaultPushNotificationService implements IPushNotificationService {
3232
DefaultPushNotificationService({
3333
required DataRepository<PushNotificationDevice>
3434
pushNotificationDeviceRepository,
35-
required DataRepository<Interest> interestRepository,
35+
required DataRepository<UserContentPreferences>
36+
userContentPreferencesRepository,
3637
required DataRepository<RemoteConfig> remoteConfigRepository,
3738
required IPushNotificationClient? firebaseClient,
3839
required IPushNotificationClient? oneSignalClient,
3940
required Logger log,
4041
}) : _pushNotificationDeviceRepository = pushNotificationDeviceRepository,
41-
_interestRepository = interestRepository,
42+
_userContentPreferencesRepository = userContentPreferencesRepository,
4243
_remoteConfigRepository = remoteConfigRepository,
4344
_firebaseClient = firebaseClient,
4445
_oneSignalClient = oneSignalClient,
4546
_log = log;
4647

4748
final DataRepository<PushNotificationDevice>
4849
_pushNotificationDeviceRepository;
49-
final DataRepository<Interest> _interestRepository;
50+
final DataRepository<UserContentPreferences>
51+
_userContentPreferencesRepository;
5052
final DataRepository<RemoteConfig> _remoteConfigRepository;
5153
final IPushNotificationClient? _firebaseClient;
5254
final IPushNotificationClient? _oneSignalClient;
@@ -115,33 +117,34 @@ class DefaultPushNotificationService implements IPushNotificationService {
115117
return;
116118
}
117119

118-
// 2. Find all interests subscribed to breaking news.
119-
// The query now correctly finds interests where the 'deliveryTypes'
120-
// set *contains* the 'breakingOnly' value.
121-
final breakingNewsInterests = await _interestRepository.readAll(
122-
filter: {
123-
'deliveryTypes': {
124-
r'$in': [
125-
PushNotificationSubscriptionDeliveryType.breakingOnly.name,
126-
],
127-
},
128-
},
129-
);
120+
// 2. Find all user preferences that contain an interest subscribed to
121+
// breaking news. This query targets the embedded 'interests' array.
122+
final subscribedUserPreferences = await _userContentPreferencesRepository
123+
.readAll(
124+
filter: {
125+
'interests.deliveryTypes': {
126+
r'$in': [
127+
PushNotificationSubscriptionDeliveryType.breakingOnly.name,
128+
],
129+
},
130+
},
131+
);
130132

131-
if (breakingNewsInterests.items.isEmpty) {
133+
if (subscribedUserPreferences.items.isEmpty) {
132134
_log.info('No users subscribed to breaking news. Aborting.');
133135
return;
134136
}
135137

136-
// 3. Collect all unique user IDs from the subscriptions.
138+
// 3. Collect all unique user IDs from the preference documents.
137139
// Using a Set automatically handles deduplication.
138-
final userIds = breakingNewsInterests.items
139-
.map((interest) => interest.userId)
140+
// The ID of the UserContentPreferences document is the user's ID.
141+
final userIds = subscribedUserPreferences.items
142+
.map((preference) => preference.id)
140143
.toSet();
141144

142145
_log.info(
143-
'Found ${breakingNewsInterests.items.length} interests subscribed to '
144-
'breaking news, corresponding to ${userIds.length} unique users.',
146+
'Found ${subscribedUserPreferences.items.length} users with '
147+
'subscriptions to breaking news.',
145148
);
146149

147150
// 4. Fetch all devices for all subscribed users in a single bulk query.

0 commit comments

Comments
 (0)