@@ -176,9 +176,16 @@ public Mono<DeleteResult> deleteOneSidedRelationships(
176
176
.retryWhen (TRANSACTION_RETRY );
177
177
}
178
178
179
- public Mono <Void > deleteOneSidedRelationship (
179
+ /**
180
+ * @return true if the relationship existed and has been deleted;
181
+ * <p>
182
+ * false if the relationship does not exist, or still exists and has been deleted from a
183
+ * relationship group.
184
+ */
185
+ public Mono <Boolean > deleteOneSidedRelationship (
180
186
@ NotNull Long ownerId ,
181
187
@ NotNull Long relatedUserId ,
188
+ @ Nullable Integer groupIndex ,
182
189
@ Nullable ClientSession session ) {
183
190
try {
184
191
Validator .notNull (ownerId , "ownerId" );
@@ -187,45 +194,131 @@ public Mono<Void> deleteOneSidedRelationship(
187
194
return Mono .error (e );
188
195
}
189
196
if (session == null ) {
190
- return userRelationshipRepository .inTransaction (
191
- newSession -> deleteOneSidedRelationship (ownerId , relatedUserId , newSession ))
197
+ return userRelationshipRepository
198
+ .inTransaction (newSession -> deleteOneSidedRelationship (ownerId ,
199
+ relatedUserId ,
200
+ groupIndex ,
201
+ newSession ))
192
202
.retryWhen (TRANSACTION_RETRY );
193
203
}
194
204
UserRelationship .Key key = new UserRelationship .Key (ownerId , relatedUserId );
195
- return userRelationshipRepository .deleteById (key )
196
- .then (userRelationshipGroupService .deleteRelatedUserFromAllRelationshipGroups (
197
- ownerId ,
205
+ if (groupIndex == null ) {
206
+ return userRelationshipGroupService
207
+ .deleteRelatedUserFromAllRelationshipGroups (ownerId ,
208
+ relatedUserId ,
209
+ session ,
210
+ false )
211
+ .flatMap (deleteResult -> {
212
+ // because every relationship should exist in a relationship group at least,
213
+ // return immediately if no relationship found in any group.
214
+ if (deleteResult .getDeletedCount () == 0 ) {
215
+ return PublisherPool .FALSE ;
216
+ }
217
+ return userRelationshipRepository .deleteById (key , session )
218
+ .then (userVersionService
219
+ .updateSpecificVersion (ownerId ,
220
+ null ,
221
+ UserVersion .Fields .RELATIONSHIP_GROUP_MEMBERS ,
222
+ UserVersion .Fields .RELATIONSHIPS )
223
+ .onErrorResume (t -> {
224
+ LOGGER .error (
225
+ "Caught an error while updating the relationships version and relationship groups members version of "
226
+ + "the owner ({}) after deleting the relationship with the user ({})" ,
227
+ ownerId ,
228
+ relatedUserId ,
229
+ t );
230
+ return Mono .empty ();
231
+ }))
232
+ .then (Mono .fromCallable (() -> {
233
+ invalidateRelationshipCache (ownerId , relatedUserId );
234
+ return true ;
235
+ }));
236
+ });
237
+ }
238
+ return userRelationshipGroupService
239
+ .deleteRelatedUserFromRelationshipGroup (ownerId ,
198
240
relatedUserId ,
241
+ groupIndex ,
199
242
session ,
200
- false ))
201
- .then (userVersionService
202
- .updateSpecificVersion (ownerId ,
203
- session ,
204
- UserVersion .Fields .RELATIONSHIP_GROUP_MEMBERS ,
205
- UserVersion .Fields .RELATIONSHIPS )
206
- .onErrorResume (t -> {
207
- LOGGER .error (
208
- "Caught an error while updating the relationships version and relationships group members version of the owner ({}) after deleting a relationship" ,
209
- ownerId ,
210
- t );
211
- return Mono .empty ();
212
- }))
213
- .doOnSuccess (unused -> invalidateRelationshipCache (ownerId , relatedUserId ))
214
- .then ();
243
+ false )
244
+ .flatMap (deleteResult -> {
245
+ if (deleteResult .getDeletedCount () == 0 ) {
246
+ return PublisherPool .FALSE ;
247
+ }
248
+ return userRelationshipGroupService
249
+ .countRelationshipGroups (Set .of (ownerId ), Set .of (relatedUserId ))
250
+ .flatMap (relationshipGroupCount -> {
251
+ if (relationshipGroupCount > 0 ) {
252
+ return userVersionService
253
+ .updateSpecificVersion (ownerId ,
254
+ null ,
255
+ UserVersion .Fields .RELATIONSHIP_GROUP_MEMBERS )
256
+ .onErrorResume (t -> {
257
+ LOGGER .error (
258
+ "Caught an error while updating the relationship groups members version of "
259
+ + "the owner ({}) after deleting the relationship with the user ({}) from the group ({})" ,
260
+ ownerId ,
261
+ relatedUserId ,
262
+ groupIndex ,
263
+ t );
264
+ return Mono .empty ();
265
+ })
266
+ .thenReturn (false );
267
+ }
268
+ return userRelationshipRepository .deleteById (key , session )
269
+ .then (Mono .defer (() -> {
270
+ invalidateRelationshipCache (ownerId , relatedUserId );
271
+ return userVersionService .updateSpecificVersion (ownerId ,
272
+ null ,
273
+ UserVersion .Fields .RELATIONSHIP_GROUP_MEMBERS ,
274
+ UserVersion .Fields .RELATIONSHIPS )
275
+ .onErrorResume (t -> {
276
+ LOGGER .error (
277
+ "Caught an error while updating the relationships version and relationship group members version of "
278
+ + "the owner ({}) after deleting the relationship with the user ({}) from the group ({})" ,
279
+ ownerId ,
280
+ relatedUserId ,
281
+ groupIndex ,
282
+ t );
283
+ return Mono .empty ();
284
+ });
285
+ }))
286
+ .thenReturn (true );
287
+ });
288
+ });
215
289
}
216
290
217
- public Mono <Void > deleteTwoSidedRelationships (
218
- @ NotNull Long userOneId ,
219
- @ NotNull Long userTwoId ) {
291
+ public Mono <Void > tryDeleteTwoSidedRelationships (
292
+ @ NotNull Long requesterId ,
293
+ @ NotNull Long relatedUserId ,
294
+ @ Nullable Integer groupId ) {
220
295
try {
221
- Validator .notNull (userOneId , "userOneId " );
222
- Validator .notNull (userTwoId , "userTwoId " );
296
+ Validator .notNull (requesterId , "requesterId " );
297
+ Validator .notNull (relatedUserId , "relatedUserId " );
223
298
} catch (ResponseException e ) {
224
299
return Mono .error (e );
225
300
}
226
- return userRelationshipRepository
227
- .inTransaction (session -> deleteOneSidedRelationship (userOneId , userTwoId , session )
228
- .then (deleteOneSidedRelationship (userTwoId , userOneId , session ))
301
+ return userRelationshipRepository .inTransaction (
302
+ session -> deleteOneSidedRelationship (requesterId , relatedUserId , groupId , session )
303
+ .flatMap (deleted -> {
304
+ if (!deleted ) {
305
+ // If not deleted, meaning the relationship between the requester,
306
+ // and the related user still exists,
307
+ // so we don't delete the relationship between
308
+ // the related user and the requester.
309
+ return Mono .empty ();
310
+ }
311
+ return isBlocked (relatedUserId , requesterId , false )
312
+ .flatMap (isBlocked -> {
313
+ if (isBlocked ) {
314
+ return Mono .empty ();
315
+ }
316
+ return deleteOneSidedRelationship (relatedUserId ,
317
+ requesterId ,
318
+ groupId ,
319
+ session );
320
+ });
321
+ })
229
322
.then ())
230
323
.retryWhen (TRANSACTION_RETRY );
231
324
}
0 commit comments