@@ -120,7 +120,7 @@ class PushManagerImpl @Inject constructor(
120
120
onMessageReceived(notification)
121
121
}
122
122
123
- @Suppress(" LongMethod" , " CyclomaticComplexMethod" , " ReturnCount " )
123
+ @Suppress(" LongMethod" , " CyclomaticComplexMethod" )
124
124
private fun onMessageReceived (notification : BitwardenNotification ) {
125
125
if (authDiskSource.uniqueAppId == notification.contextId) return
126
126
@@ -130,62 +130,67 @@ class PushManagerImpl @Inject constructor(
130
130
NotificationType .AUTH_REQUEST ,
131
131
NotificationType .AUTH_REQUEST_RESPONSE ,
132
132
-> {
133
- val payload: NotificationPayload .PasswordlessRequestNotification =
134
- json.decodeFromString(string = notification.payload)
135
- mutablePasswordlessRequestSharedFlow.tryEmit(
136
- PasswordlessRequestData (
137
- loginRequestId = payload.id,
138
- userId = payload.userId,
139
- ),
140
- )
133
+ json
134
+ .decodeFromString<NotificationPayload .PasswordlessRequestNotification >(
135
+ string = notification.payload,
136
+ )
137
+ .takeIf { it.loginRequestId != null && it.userId != null }
138
+ ?.let {
139
+ mutablePasswordlessRequestSharedFlow.tryEmit(
140
+ PasswordlessRequestData (
141
+ loginRequestId = requireNotNull(it.loginRequestId),
142
+ userId = requireNotNull(it.userId),
143
+ ),
144
+ )
145
+ }
141
146
}
142
147
143
148
NotificationType .LOG_OUT -> {
144
- val payload: NotificationPayload .UserNotification =
145
- json.decodeFromString(notification.payload)
146
- mutableLogoutSharedFlow.tryEmit(
147
- NotificationLogoutData (payload.userId),
148
- )
149
+ json
150
+ .decodeFromString<NotificationPayload .UserNotification >(
151
+ string = notification.payload,
152
+ )
153
+ .userId
154
+ ?.let { mutableLogoutSharedFlow.tryEmit(NotificationLogoutData (it)) }
149
155
}
150
156
151
157
NotificationType .SYNC_CIPHER_CREATE ,
152
158
NotificationType .SYNC_CIPHER_UPDATE ,
153
159
-> {
154
- val payload: NotificationPayload .SyncCipherNotification =
155
- json.decodeFromString(notification.payload)
156
- @Suppress(" ComplexCondition" )
157
- if (payload.id == null ||
158
- payload.revisionDate == null ||
159
- ! isLoggedIn(userId) ||
160
- ! payload.userMatchesNotification(userId)
161
- ) {
162
- return
163
- }
164
- mutableSyncCipherUpsertSharedFlow.tryEmit(
165
- SyncCipherUpsertData (
166
- cipherId = payload.id,
167
- revisionDate = payload.revisionDate,
168
- organizationId = payload.organizationId,
169
- collectionIds = payload.collectionIds,
170
- isUpdate = type == NotificationType .SYNC_CIPHER_UPDATE ,
171
- ),
172
- )
160
+ json
161
+ .decodeFromString<NotificationPayload .SyncCipherNotification >(
162
+ string = notification.payload,
163
+ )
164
+ .takeIf { isLoggedIn(userId) && it.userMatchesNotification(userId) }
165
+ ?.takeIf {
166
+ it.cipherId != null &&
167
+ it.revisionDate != null &&
168
+ it.organizationId != null &&
169
+ it.collectionIds != null
170
+ }
171
+ ?.let {
172
+ mutableSyncCipherUpsertSharedFlow.tryEmit(
173
+ SyncCipherUpsertData (
174
+ cipherId = requireNotNull(it.cipherId),
175
+ revisionDate = requireNotNull(it.revisionDate),
176
+ organizationId = requireNotNull(it.organizationId),
177
+ collectionIds = requireNotNull(it.collectionIds),
178
+ isUpdate = type == NotificationType .SYNC_CIPHER_UPDATE ,
179
+ ),
180
+ )
181
+ }
173
182
}
174
183
175
184
NotificationType .SYNC_CIPHER_DELETE ,
176
185
NotificationType .SYNC_LOGIN_DELETE ,
177
186
-> {
178
- val payload: NotificationPayload .SyncCipherNotification =
179
- json.decodeFromString(notification.payload)
180
- if (payload.id == null ||
181
- ! isLoggedIn(userId) ||
182
- ! payload.userMatchesNotification(userId)
183
- ) {
184
- return
185
- }
186
- mutableSyncCipherDeleteSharedFlow.tryEmit(
187
- SyncCipherDeleteData (payload.id),
188
- )
187
+ json
188
+ .decodeFromString<NotificationPayload .SyncCipherNotification >(
189
+ string = notification.payload,
190
+ )
191
+ .takeIf { isLoggedIn(userId) && it.userMatchesNotification(userId) }
192
+ ?.cipherId
193
+ ?.let { mutableSyncCipherDeleteSharedFlow.tryEmit(SyncCipherDeleteData (it)) }
189
194
}
190
195
191
196
NotificationType .SYNC_CIPHERS ,
@@ -198,55 +203,67 @@ class PushManagerImpl @Inject constructor(
198
203
NotificationType .SYNC_FOLDER_CREATE ,
199
204
NotificationType .SYNC_FOLDER_UPDATE ,
200
205
-> {
201
- val payload: NotificationPayload .SyncFolderNotification =
202
- json.decodeFromString(notification.payload)
203
- if (! isLoggedIn(userId) || ! payload.userMatchesNotification(userId)) return
204
- mutableSyncFolderUpsertSharedFlow.tryEmit(
205
- SyncFolderUpsertData (
206
- folderId = payload.id,
207
- revisionDate = payload.revisionDate,
208
- isUpdate = type == NotificationType .SYNC_FOLDER_UPDATE ,
209
- ),
210
- )
206
+ json
207
+ .decodeFromString<NotificationPayload .SyncFolderNotification >(
208
+ string = notification.payload,
209
+ )
210
+ .takeIf { isLoggedIn(userId) && it.userMatchesNotification(userId) }
211
+ ?.takeIf { it.folderId != null && it.revisionDate != null }
212
+ ?.let {
213
+ mutableSyncFolderUpsertSharedFlow.tryEmit(
214
+ SyncFolderUpsertData (
215
+ folderId = requireNotNull(it.folderId),
216
+ revisionDate = requireNotNull(it.revisionDate),
217
+ isUpdate = type == NotificationType .SYNC_FOLDER_UPDATE ,
218
+ ),
219
+ )
220
+ }
211
221
}
212
222
213
223
NotificationType .SYNC_FOLDER_DELETE -> {
214
- val payload : NotificationPayload . SyncFolderNotification =
215
- json .decodeFromString(notification.payload)
216
- if ( ! isLoggedIn(userId) || ! payload.userMatchesNotification(userId)) return
217
-
218
- mutableSyncFolderDeleteSharedFlow.tryEmit(
219
- SyncFolderDeleteData (payload.id),
220
- )
224
+ json
225
+ .decodeFromString< NotificationPayload . SyncFolderNotification >(
226
+ string = notification.payload,
227
+ )
228
+ . takeIf { isLoggedIn(userId) && it.userMatchesNotification(userId) }
229
+ ?.folderId
230
+ ?. let { mutableSyncFolderDeleteSharedFlow.tryEmit( SyncFolderDeleteData (it)) }
221
231
}
222
232
223
233
NotificationType .SYNC_ORG_KEYS -> {
224
- if (! isLoggedIn(userId)) return
225
- mutableSyncOrgKeysSharedFlow.tryEmit(Unit )
234
+ if (isLoggedIn(userId)) {
235
+ mutableSyncOrgKeysSharedFlow.tryEmit(Unit )
236
+ }
226
237
}
227
238
228
239
NotificationType .SYNC_SEND_CREATE ,
229
240
NotificationType .SYNC_SEND_UPDATE ,
230
241
-> {
231
- val payload: NotificationPayload .SyncSendNotification =
232
- json.decodeFromString(notification.payload)
233
- if (! isLoggedIn(userId) || ! payload.userMatchesNotification(userId)) return
234
- mutableSyncSendUpsertSharedFlow.tryEmit(
235
- SyncSendUpsertData (
236
- sendId = payload.id,
237
- revisionDate = payload.revisionDate,
238
- isUpdate = type == NotificationType .SYNC_SEND_UPDATE ,
239
- ),
240
- )
242
+ json
243
+ .decodeFromString<NotificationPayload .SyncSendNotification >(
244
+ string = notification.payload,
245
+ )
246
+ .takeIf { isLoggedIn(userId) && it.userMatchesNotification(userId) }
247
+ ?.takeIf { it.sendId != null && it.revisionDate != null }
248
+ ?.let {
249
+ mutableSyncSendUpsertSharedFlow.tryEmit(
250
+ SyncSendUpsertData (
251
+ sendId = requireNotNull(it.sendId),
252
+ revisionDate = requireNotNull(it.revisionDate),
253
+ isUpdate = type == NotificationType .SYNC_SEND_UPDATE ,
254
+ ),
255
+ )
256
+ }
241
257
}
242
258
243
259
NotificationType .SYNC_SEND_DELETE -> {
244
- val payload: NotificationPayload .SyncSendNotification =
245
- json.decodeFromString(notification.payload)
246
- if (! isLoggedIn(userId) || ! payload.userMatchesNotification(userId)) return
247
- mutableSyncSendDeleteSharedFlow.tryEmit(
248
- SyncSendDeleteData (payload.id),
249
- )
260
+ json
261
+ .decodeFromString<NotificationPayload .SyncSendNotification >(
262
+ string = notification.payload,
263
+ )
264
+ .takeIf { isLoggedIn(userId) && it.userMatchesNotification(userId) }
265
+ ?.sendId
266
+ ?.let { mutableSyncSendDeleteSharedFlow.tryEmit(SyncSendDeleteData (it)) }
250
267
}
251
268
}
252
269
}
@@ -290,15 +307,15 @@ class PushManagerImpl @Inject constructor(
290
307
if (token == currentToken) {
291
308
// Our token is up-to-date, so just update the last registration date
292
309
pushDiskSource.storeLastPushTokenRegistrationDate(
293
- userId,
294
- ZonedDateTime .ofInstant(clock.instant(), ZoneOffset .UTC ),
310
+ userId = userId ,
311
+ registrationDate = ZonedDateTime .ofInstant(clock.instant(), ZoneOffset .UTC ),
295
312
)
296
313
return
297
314
}
298
315
299
316
pushService
300
317
.putDeviceToken(
301
- PushTokenRequest (token),
318
+ body = PushTokenRequest (token),
302
319
)
303
320
.fold(
304
321
onSuccess = {
0 commit comments