@@ -176,9 +176,16 @@ public Mono<DeleteResult> deleteOneSidedRelationships(
176176 .retryWhen (TRANSACTION_RETRY );
177177 }
178178
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 (
180186 @ NotNull Long ownerId ,
181187 @ NotNull Long relatedUserId ,
188+ @ Nullable Integer groupIndex ,
182189 @ Nullable ClientSession session ) {
183190 try {
184191 Validator .notNull (ownerId , "ownerId" );
@@ -187,45 +194,131 @@ public Mono<Void> deleteOneSidedRelationship(
187194 return Mono .error (e );
188195 }
189196 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 ))
192202 .retryWhen (TRANSACTION_RETRY );
193203 }
194204 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 ,
198240 relatedUserId ,
241+ groupIndex ,
199242 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+ });
215289 }
216290
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 ) {
220295 try {
221- Validator .notNull (userOneId , "userOneId " );
222- Validator .notNull (userTwoId , "userTwoId " );
296+ Validator .notNull (requesterId , "requesterId " );
297+ Validator .notNull (relatedUserId , "relatedUserId " );
223298 } catch (ResponseException e ) {
224299 return Mono .error (e );
225300 }
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 didn't exist, or still exists,
307+ // we don't delete the relationship between
308+ // the related user and the requester for these cases.
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+ })
229322 .then ())
230323 .retryWhen (TRANSACTION_RETRY );
231324 }
0 commit comments