Skip to content

Commit

Permalink
feat: Compute Kudos properties instead of relying on given parameters…
Browse files Browse the repository at this point in the history
… in HTTP call - MEED-7636 - Meeds-io/meeds#2485

Prior to this change, when sending a kudos to a user or space using an id instead of remoteId, the Kudos creation succeeds while the activity generation fails. This change ensures to store in kudos attributes the right value for Kudos fields and to not rely on properties sent with kudos object as is which will ensure data consistency.
  • Loading branch information
boubaker committed Oct 11, 2024
1 parent d13ba52 commit e1ea970
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,25 @@ public Kudos createKudos(Kudos kudos, String currentUser) throws IllegalAccessEx
throw new IllegalAccessException("User having username'" + currentUser + "' is not authorized to send more kudos");
}

if (kudos.getSenderIdentityId() == null) {
kudos.setSenderIdentityId(senderIdentity.getId());
}
kudos.setSenderId(senderIdentity.getRemoteId());
kudos.setSenderIdentityId(senderIdentity.getId());
Object receiverObject = checkStatusAndGetReceiver(kudos.getReceiverType(), kudos.getReceiverId());

if (kudos.getReceiverIdentityId() == null) {
if (receiverObject instanceof Identity identity) {
if (receiverObject instanceof Identity identity && identity.isUser()) {
kudos.setReceiverId(identity.getRemoteId());
kudos.setReceiverType(USER_ACCOUNT_TYPE);
kudos.setReceiverIdentityId(identity.getId());
} else if (receiverObject instanceof Identity identity && identity.isSpace()) {
Space space = getSpace(identity.getRemoteId());
kudos.setReceiverIdentityId(space.getId());
kudos.setReceiverId(space.getPrettyName());
kudos.setReceiverType(SPACE_ACCOUNT_TYPE);
} else if (receiverObject instanceof Space space) {
if (canSendKudosInSpace(kudos, space, currentUser)) {
kudos.setReceiverId(space.getPrettyName());
kudos.setReceiverIdentityId(space.getId());
kudos.setReceiverType(SPACE_ACCOUNT_TYPE);
} else {
throw new IllegalAccessException("User cannot redact on space");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,45 @@ public void testSendKudosToSpace() {
assertEquals(kudosToSendOnActivity.getEntityId(), retrievedKudos.getEntityId());
}

@Test
@SneakyThrows
public void testSendKudosUsingSpaceId() {
String spaceRemoteId = "space4";

Identity spaceIdentity = identityManager.getOrCreateSpaceIdentity(spaceRemoteId);
KudosPeriod currentKudosPeriod = kudosService.getCurrentKudosPeriod();

Kudos kudosToSend = newKudosDTO();
kudosToSend.setReceiverType(SpaceIdentityProvider.NAME);
kudosToSend.setReceiverId(spaceService.getSpaceByPrettyName(spaceIdentity.getRemoteId()).getId());
kudosToSend.setReceiverIdentityId(null);
kudosToSend.setSpacePrettyName(spaceRemoteId);

restartTransaction();

List<Kudos> list = kudosService.getKudosByPeriodAndReceiver(Long.parseLong(spaceIdentity.getId()),
currentKudosPeriod.getStartDateInSeconds(),
currentKudosPeriod.getEndDateInSeconds(),
10);
assertNotNull(list);
assertEquals(0, list.size());

SpaceServiceMock.setRedactor(SENDER_REMOTE_ID);
try {
Kudos kudos = kudosService.createKudos(kudosToSend, SENDER_REMOTE_ID);
assertNotNull(kudos);
} finally {
SpaceServiceMock.setRedactor(null);
}

list = kudosService.getKudosByPeriodAndReceiver(Long.parseLong(spaceIdentity.getId()),
currentKudosPeriod.getStartDateInSeconds(),
currentKudosPeriod.getEndDateInSeconds(),
10);
assertNotNull(list);
assertEquals(1, list.size());
}

@Test
public void testGetKudosByPeriodType() {
long startTime = getCurrentTimeInSeconds();
Expand Down

0 comments on commit e1ea970

Please sign in to comment.