Skip to content

Commit

Permalink
Implement the missing feature: Delete a relationship from a specific …
Browse files Browse the repository at this point in the history
…relationship group #1365
  • Loading branch information
JamesChenX committed Jan 1, 2024
1 parent 4cd2b1f commit 1766a8f
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,21 @@ public ClientRequestHandler handleDeleteRelationshipRequest() {
return clientRequest -> {
DeleteRelationshipRequest request = clientRequest.turmsRequest()
.getDeleteRelationshipRequest();
Mono<Void> deleteMono;
Mono<?> deleteMono;
if (deleteTwoSidedRelationships) {
deleteMono = userRelationshipService
.deleteTwoSidedRelationships(clientRequest.userId(), request.getUserId());
deleteMono = userRelationshipService.tryDeleteTwoSidedRelationships(
clientRequest.userId(),
request.getUserId(),
request.hasGroupIndex()
? request.getGroupIndex()
: null);
} else {
deleteMono =
userRelationshipService.deleteOneSidedRelationship(clientRequest.userId(),
request.getUserId(),
request.hasGroupIndex()
? request.getGroupIndex()
: null,
null);
}
return deleteMono.then(Mono.fromCallable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ public UserRelationshipGroupMemberRepository(
super(mongoClient, UserRelationshipGroupMember.class);
}

public Mono<DeleteResult> deleteRelatedUserFromRelationshipGroup(
Long ownerId,
Long relatedUserId,
Integer groupIndex,
@Nullable ClientSession session) {
Filter filter = Filter.newBuilder(3)
.eq(UserRelationshipGroupMember.Fields.ID_OWNER_ID, ownerId)
.eq(UserRelationshipGroupMember.Fields.ID_RELATED_USER_ID, relatedUserId)
.eq(UserRelationshipGroupMember.Fields.ID_GROUP_INDEX, groupIndex);
return mongoClient.deleteOne(session, entityClass, filter);
}

public Mono<DeleteResult> deleteRelatedUsersFromAllRelationshipGroups(
Long ownerId,
Collection<Long> relatedUserIds,
Expand All @@ -56,6 +68,15 @@ public Mono<DeleteResult> deleteRelatedUsersFromAllRelationshipGroups(
return mongoClient.deleteMany(session, entityClass, filter);
}

public Mono<Long> countGroups(
@Nullable Collection<Long> ownerIds,
@Nullable Collection<Long> relatedUserIds) {
Filter filter = Filter.newBuilder(2)
.inIfNotNull(UserRelationshipGroupMember.Fields.ID_OWNER_ID, ownerIds)
.inIfNotNull(UserRelationshipGroupMember.Fields.ID_RELATED_USER_ID, relatedUserIds);
return mongoClient.count(entityClass, filter);
}

public Mono<Long> countMembers(
@Nullable Set<Long> ownerIds,
@Nullable Set<Integer> groupIndexes) {
Expand Down Expand Up @@ -112,4 +133,4 @@ public Flux<UserRelationshipGroupMember> findRelationshipGroupMembers(
return mongoClient.findMany(entityClass, filterMember);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,41 @@ public Mono<DeleteResult> deleteAllRelationshipGroups(
return deleteMono;
}

public Mono<DeleteResult> deleteRelatedUserFromRelationshipGroup(
@NotNull Long ownerId,
@NotNull Long relatedUserId,
@NotNull Integer groupIndex,
@Nullable ClientSession session,
boolean updateRelationshipGroupsMembersVersion) {
try {
Validator.notNull(ownerId, "ownerId");
Validator.notNull(relatedUserId, "relatedUserId");
Validator.notNull(groupIndex, "groupIndex");
} catch (ResponseException e) {
return Mono.error(e);
}
Mono<DeleteResult> deleteMono = userRelationshipGroupMemberRepository
.deleteRelatedUserFromRelationshipGroup(ownerId,
relatedUserId,
groupIndex,
session);
if (updateRelationshipGroupsMembersVersion) {
return deleteMono.flatMap(
result -> userVersionService.updateRelationshipGroupsMembersVersion(ownerId)
.onErrorResume(t -> {
LOGGER.error(
"Caught an error while updating the relationship groups members version of the owner ({}) after deleting the user ({}) from the group ({})",
ownerId,
relatedUserId,
groupIndex,
t);
return Mono.empty();
})
.thenReturn(result));
}
return deleteMono;
}

public Mono<DeleteResult> deleteRelatedUserFromAllRelationshipGroups(
@NotNull Long ownerId,
@NotNull Long relatedUserId,
Expand Down Expand Up @@ -557,6 +592,12 @@ public Mono<Long> countRelationshipGroups(
.countRelationshipGroups(ownerIds, indexes, names, creationDateRange);
}

public Mono<Long> countRelationshipGroups(
@Nullable Set<Long> ownerIds,
@Nullable Set<Long> relatedUserIds) {
return userRelationshipGroupMemberRepository.countGroups(ownerIds, relatedUserIds);
}

public Mono<Long> countRelationshipGroupMembers(
@Nullable Set<Long> ownerIds,
@Nullable Set<Integer> groupIndexes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,16 @@ public Mono<DeleteResult> deleteOneSidedRelationships(
.retryWhen(TRANSACTION_RETRY);
}

public Mono<Void> deleteOneSidedRelationship(
/**
* @return true if the relationship existed and has been deleted;
* <p>
* false if the relationship does not exist, or still exists and has been deleted from a
* relationship group.
*/
public Mono<Boolean> deleteOneSidedRelationship(
@NotNull Long ownerId,
@NotNull Long relatedUserId,
@Nullable Integer groupIndex,
@Nullable ClientSession session) {
try {
Validator.notNull(ownerId, "ownerId");
Expand All @@ -187,45 +194,131 @@ public Mono<Void> deleteOneSidedRelationship(
return Mono.error(e);
}
if (session == null) {
return userRelationshipRepository.inTransaction(
newSession -> deleteOneSidedRelationship(ownerId, relatedUserId, newSession))
return userRelationshipRepository
.inTransaction(newSession -> deleteOneSidedRelationship(ownerId,
relatedUserId,
groupIndex,
newSession))
.retryWhen(TRANSACTION_RETRY);
}
UserRelationship.Key key = new UserRelationship.Key(ownerId, relatedUserId);
return userRelationshipRepository.deleteById(key)
.then(userRelationshipGroupService.deleteRelatedUserFromAllRelationshipGroups(
ownerId,
if (groupIndex == null) {
return userRelationshipGroupService
.deleteRelatedUserFromAllRelationshipGroups(ownerId,
relatedUserId,
session,
false)
.flatMap(deleteResult -> {
// because every relationship should exist in a relationship group at least,
// return immediately if no relationship found in any group.
if (deleteResult.getDeletedCount() == 0) {
return PublisherPool.FALSE;
}
return userRelationshipRepository.deleteById(key, session)
.then(userVersionService
.updateSpecificVersion(ownerId,
null,
UserVersion.Fields.RELATIONSHIP_GROUP_MEMBERS,
UserVersion.Fields.RELATIONSHIPS)
.onErrorResume(t -> {
LOGGER.error(
"Caught an error while updating the relationships version and relationship groups members version of "
+ "the owner ({}) after deleting the relationship with the user ({})",
ownerId,
relatedUserId,
t);
return Mono.empty();
}))
.then(Mono.fromCallable(() -> {
invalidateRelationshipCache(ownerId, relatedUserId);
return true;
}));
});
}
return userRelationshipGroupService
.deleteRelatedUserFromRelationshipGroup(ownerId,
relatedUserId,
groupIndex,
session,
false))
.then(userVersionService
.updateSpecificVersion(ownerId,
session,
UserVersion.Fields.RELATIONSHIP_GROUP_MEMBERS,
UserVersion.Fields.RELATIONSHIPS)
.onErrorResume(t -> {
LOGGER.error(
"Caught an error while updating the relationships version and relationships group members version of the owner ({}) after deleting a relationship",
ownerId,
t);
return Mono.empty();
}))
.doOnSuccess(unused -> invalidateRelationshipCache(ownerId, relatedUserId))
.then();
false)
.flatMap(deleteResult -> {
if (deleteResult.getDeletedCount() == 0) {
return PublisherPool.FALSE;
}
return userRelationshipGroupService
.countRelationshipGroups(Set.of(ownerId), Set.of(relatedUserId))
.flatMap(relationshipGroupCount -> {
if (relationshipGroupCount > 0) {
return userVersionService
.updateSpecificVersion(ownerId,
null,
UserVersion.Fields.RELATIONSHIP_GROUP_MEMBERS)
.onErrorResume(t -> {
LOGGER.error(
"Caught an error while updating the relationship groups members version of "
+ "the owner ({}) after deleting the relationship with the user ({}) from the group ({})",
ownerId,
relatedUserId,
groupIndex,
t);
return Mono.empty();
})
.thenReturn(false);
}
return userRelationshipRepository.deleteById(key, session)
.then(Mono.defer(() -> {
invalidateRelationshipCache(ownerId, relatedUserId);
return userVersionService.updateSpecificVersion(ownerId,
null,
UserVersion.Fields.RELATIONSHIP_GROUP_MEMBERS,
UserVersion.Fields.RELATIONSHIPS)
.onErrorResume(t -> {
LOGGER.error(
"Caught an error while updating the relationships version and relationship group members version of "
+ "the owner ({}) after deleting the relationship with the user ({}) from the group ({})",
ownerId,
relatedUserId,
groupIndex,
t);
return Mono.empty();
});
}))
.thenReturn(true);
});
});
}

public Mono<Void> deleteTwoSidedRelationships(
@NotNull Long userOneId,
@NotNull Long userTwoId) {
public Mono<Void> tryDeleteTwoSidedRelationships(
@NotNull Long requesterId,
@NotNull Long relatedUserId,
@Nullable Integer groupId) {
try {
Validator.notNull(userOneId, "userOneId");
Validator.notNull(userTwoId, "userTwoId");
Validator.notNull(requesterId, "requesterId");
Validator.notNull(relatedUserId, "relatedUserId");
} catch (ResponseException e) {
return Mono.error(e);
}
return userRelationshipRepository
.inTransaction(session -> deleteOneSidedRelationship(userOneId, userTwoId, session)
.then(deleteOneSidedRelationship(userTwoId, userOneId, session))
return userRelationshipRepository.inTransaction(
session -> deleteOneSidedRelationship(requesterId, relatedUserId, groupId, session)
.flatMap(deleted -> {
if (!deleted) {
// If not deleted, meaning the relationship between the requester,
// and the related user still exists,
// so we don't delete the relationship between
// the related user and the requester.
return Mono.empty();
}
return isBlocked(relatedUserId, requesterId, false)
.flatMap(isBlocked -> {
if (isBlocked) {
return Mono.empty();
}
return deleteOneSidedRelationship(relatedUserId,
requesterId,
groupId,
session);
});
})
.then())
.retryWhen(TRANSACTION_RETRY);
}
Expand Down

0 comments on commit 1766a8f

Please sign in to comment.