Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow to send a Kudos to a Space - MEED-7568 - Meeds-io/MIPs158 #534

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package io.meeds.kudos.activity;

import static io.meeds.kudos.service.utils.Utils.SPACE_ACCOUNT_TYPE;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -60,6 +62,7 @@ public boolean isEnableNotification(ExoSocialActivity activity, String username)
} else {
Kudos kudos = this.kudosService.getKudosByActivityId(Long.parseLong(activity.getId().replace("comment", "")));
return kudos != null
&& !SPACE_ACCOUNT_TYPE.equals(kudos.getReceiverType())
&& !StringUtils.equals(kudos.getReceiverId(), username)
&& !StringUtils.equals(kudos.getSenderId(), username);
}
Expand Down
10 changes: 5 additions & 5 deletions kudos-services/src/main/java/io/meeds/kudos/dao/KudosDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ List<KudosEntity> findByCreatedDateBetweenAndEntityTypeOrderByCreatedDateDesc(lo

List<KudosEntity> findByEntityTypeAndEntityIdOrderByCreatedDateDesc(int entityType, long entityId, Limit limit);

List<KudosEntity> findByCreatedDateBetweenAndReceiverIdAndIsReceiverUserOrderByCreatedDateDesc(long startDateInSeconds,
long endDateInSeconds,
long receiverId,
boolean isReceiverUser,
Limit limit);
List<KudosEntity> findByCreatedDateBetweenAndReceiverIdAndIsReceiverUserOrderByIdDesc(long startDateInSeconds,
long endDateInSeconds,
long receiverId,
boolean isReceiverUser,
Limit limit);

List<KudosEntity> findByCreatedDateBetweenAndSenderIdOrderByCreatedDateDesc(long startDateInSeconds,
long endDateInSeconds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.exoplatform.services.listener.Event;
import org.exoplatform.services.listener.Listener;
import org.exoplatform.services.listener.ListenerService;
import org.exoplatform.social.core.identity.provider.OrganizationIdentityProvider;

import io.meeds.kudos.model.Kudos;
import io.meeds.kudos.service.KudosService;
Expand Down Expand Up @@ -70,15 +71,19 @@ public void onEvent(Event<KudosService, Kudos> event) {
private void saveSendKudosAchievement(Kudos kudos, String eventName) {
Map<String, String> gam = buildGamificationEventDetails(GAMIFICATION_SEND_KUDOS_EVENT_NAME,
kudos.getSenderId(),
OrganizationIdentityProvider.NAME,
kudos.getReceiverId(),
kudos.getReceiverType(),
String.valueOf(kudos.getActivityId()));
listenerService.broadcast(getGamificationEventName(eventName), gam, String.valueOf(kudos.getTechnicalId()));
}

private void saveRecieveKudosAchievement(Kudos kudos, String eventName) {
Map<String, String> gam = buildGamificationEventDetails(GAMIFICATION_RECEIVE_KUDOS_EVENT_NAME,
kudos.getReceiverId(),
kudos.getReceiverType(),
kudos.getSenderId(),
OrganizationIdentityProvider.NAME,
String.valueOf(kudos.getActivityId()));
listenerService.broadcast(getGamificationEventName(eventName), gam, String.valueOf(kudos.getTechnicalId()));
}
Expand All @@ -93,15 +98,18 @@ private String getGamificationEventName(String eventName) {

private Map<String, String> buildGamificationEventDetails(String gamificationEventName,
String earnerId,
String earnerType,
String receiverId,
String receiverType,
String objectId) {
Map<String, String> gam = new HashMap<>();
gam.put("objectType", GAMIFICATION_OBJECT_TYPE);
gam.put("objectId", objectId);
gam.put("ruleTitle", gamificationEventName);
gam.put("senderId", earnerId); // matches the gamification's
// earner id
gam.put("senderId", earnerId); // matches the gamification's earner id
gam.put("senderType", earnerType);
gam.put("receiverId", receiverId);
gam.put("receiverType", receiverType);
return gam;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static io.meeds.kudos.service.utils.Utils.KUDOS_SCOPE;
import static io.meeds.kudos.service.utils.Utils.KUDOS_SENT_EVENT;
import static io.meeds.kudos.service.utils.Utils.SETTINGS_KEY_NAME;
import static io.meeds.kudos.service.utils.Utils.SPACE_ACCOUNT_TYPE;
import static io.meeds.kudos.service.utils.Utils.USER_ACCOUNT_TYPE;
import static io.meeds.kudos.service.utils.Utils.getActivityId;
import static io.meeds.kudos.service.utils.Utils.getCurrentPeriod;
Expand Down Expand Up @@ -189,7 +190,14 @@ public Kudos createKudos(Kudos kudos, String currentUser) throws IllegalAccessEx
throw new ObjectNotFoundException("Space not found");
} else if (!canSendKudosInSpace(kudos, space, currentUser)) {
throw new IllegalAccessException("User cannot redact on space");
} else if (!isActivityComment(kudos)
&& SPACE_ACCOUNT_TYPE.equals(kudos.getReceiverType())
&& !isReceiverSpaceTargetAudience(kudos, space)) {
throw new IllegalAccessException("Target space isn't the space receiving the kudos");
}
} else if (!isActivityComment(kudos)
&& SPACE_ACCOUNT_TYPE.equals(kudos.getReceiverType())) {
throw new IllegalAccessException("Target space isn't the space receiving the kudos");
}
KudosPeriod currentPeriod = getCurrentKudosPeriod();

Expand Down Expand Up @@ -232,8 +240,8 @@ public Kudos createKudos(Kudos kudos, String currentUser) throws IllegalAccessEx
* @return true if can redact on space or if is a comment/reply on an existing activity
*/
public boolean canSendKudosInSpace(Kudos kudos, Space space, String username) {
return (isActivityComment(kudos) && spaceService.canViewSpace(space, username))
|| spaceService.canRedactOnSpace(space, username);
return spaceService.canRedactOnSpace(space, username)
|| (isActivityComment(kudos) && spaceService.canViewSpace(space, username));
}

/**
Expand Down Expand Up @@ -571,6 +579,13 @@ public KudosPeriod getKudosPeriodOfTime(KudosPeriodType periodType, long dateInS
return periodType.getPeriodOfTime(timeFromSeconds(dateInSeconds));
}

private boolean isReceiverSpaceTargetAudience(Kudos kudos, Space space) {
String receiverId = kudos.getReceiverId();
Space targetSpaceAudience = getSpace(receiverId);
return targetSpaceAudience != null
&& StringUtils.equals(space.getId(), targetSpaceAudience.getId());
}

private Object checkStatusAndGetReceiver(String type, String id) {
if (USER_ACCOUNT_TYPE.equals(type) || OrganizationIdentityProvider.NAME.equals(type)) {
Identity identity = identityManager.getOrCreateIdentity(OrganizationIdentityProvider.NAME, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ public List<Kudos> getKudosByPeriodAndReceiver(KudosPeriod kudosPeriod, String r
return Collections.emptyList();
}
List<KudosEntity> kudosEntities =
kudosDAO.findByCreatedDateBetweenAndReceiverIdAndIsReceiverUserOrderByCreatedDateDesc(kudosPeriod.getStartDateInSeconds(),
kudosPeriod.getEndDateInSeconds(),
identityId,
isReceiverUser,
Limit.of(limit));
kudosDAO.findByCreatedDateBetweenAndReceiverIdAndIsReceiverUserOrderByIdDesc(kudosPeriod.getStartDateInSeconds(),
kudosPeriod.getEndDateInSeconds(),
identityId,
isReceiverUser,
Limit.of(limit));
if (kudosEntities != null) {
List<Kudos> kudosList = new ArrayList<>();
for (KudosEntity kudosEntity : kudosEntities) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Notification.title.KudosReceiverNotificationPlugin=New kudos received
Notification.kudos.onActivity=on activity
Notification.kudos.received=You have received a kudos from {0}
Notification.kudos.spaceReceived={0} has received a Kudos from {1}
Notification.kudos.spaceReceived.web=The space {1} has received a Kudos from {0}
Notification.label.SayHello=Hi
Notification.label.ViewFullDiscussion=View the full discussion
Notification.plugin.NewUserKudosButtonTitle=Say welcome
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,21 @@ public void testSendKudosToSpace() {
assertEquals(0, list.size());

SpaceServiceMock.setRedactor(SENDER_REMOTE_ID);
assertThrows(IllegalAccessException.class, () -> kudosService.createKudos(kudosToSend, SENDER_REMOTE_ID));

kudosToSend.setSpacePrettyName(spaceRemoteId);

kudosToSend.setReceiverId("space2");
assertThrows(IllegalAccessException.class, () -> kudosService.createKudos(kudosToSend, SENDER_REMOTE_ID));

kudosToSend.setReceiverId("NotFoundSpace");
assertThrows(IllegalAccessException.class, () -> kudosService.createKudos(kudosToSend, SENDER_REMOTE_ID));

kudosToSend.setReceiverId(spaceRemoteId);
Kudos kudos;
try {
kudos = kudosService.createKudos(kudosToSend, SENDER_REMOTE_ID);
assertNotNull(kudos);
} finally {
SpaceServiceMock.setRedactor(null);
}
Expand All @@ -401,17 +413,18 @@ public void testSendKudosToSpace() {
assertEquals(kudos.getTimeInSeconds(), retrievedKudos.getTimeInSeconds());
assertEquals(kudos.hashCode(), retrievedKudos.hashCode());

restartTransaction();
Kudos kudosToSendOnActivity = newKudosDTO();
kudosToSendOnActivity.setReceiverType(SpaceIdentityProvider.NAME);
kudosToSendOnActivity.setReceiverId(spaceRemoteId);
kudosToSendOnActivity.setReceiverIdentityId(null);
kudosToSendOnActivity.setReceiverIdentityId(null);
kudosToSendOnActivity.setEntityType(KudosEntityType.ACTIVITY.name());
kudosToSendOnActivity.setEntityId(String.valueOf(retrievedKudos.getActivityId()));

SpaceServiceMock.setMember(SENDER_REMOTE_ID);
try {
kudosService.createKudos(kudosToSendOnActivity, SENDER_REMOTE_ID);
kudos = kudosService.createKudos(kudosToSendOnActivity, SENDER_REMOTE_ID);
assertNotNull(kudos);
} finally {
SpaceServiceMock.setMember(null);
}
Expand Down
62 changes: 48 additions & 14 deletions kudos-webapps/src/main/webapp/vue-app/js/Kudos.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export function getPeriodDates(date, periodType) {
}

export function registerExternalExtensions(title) {
const profileExtensionAction = {
extensionRegistry.registerExtension('profile-extension', 'action', {
id: 'profile-kudos',
title: title,
icon: 'fa fa-award uiIconKudos uiIconLightBlue',
Expand All @@ -206,8 +206,26 @@ export function registerExternalExtensions(title) {
}}));
}
},
};
extensionRegistry.registerExtension('profile-extension', 'action', profileExtensionAction);
});
extensionRegistry.registerExtension('profile-extension', 'action', {
id: 'space-kudos',
title: title,
icon: 'fa-award',
order: 10,
iconOnly: true,
enabled: space => !space.username && space.canRedactOnSpace,
click: space => {
document.dispatchEvent(new CustomEvent('exo-kudos-open-send-modal', {detail: {
id: space.id,
type: 'SPACE_PROFILE',
parentId: '',
readOnlySpace: true,
owner: eXo.env.portal.userName,
spacePrettyName: space.prettyName,
spaceId: space.id,
}}));
},
});
}

export function registerOverviewExtension() {
Expand Down Expand Up @@ -280,6 +298,12 @@ export function registerActivityActionExtension() {
rank: 30,
});

extensionRegistry.registerComponent('SpacePopover', 'space-popover-action', {
id: 'kudos',
vueComponent: Vue.options.components['popover-kudos-button'],
rank: 30,
});

// Register predefined activity types
extensionRegistry.registerExtension('activity', 'type', {
type: 'exokudos:activity',
Expand All @@ -302,19 +326,29 @@ export function registerActivityActionExtension() {
getTitle: activityOrComment => {
const kudos = activityOrComment && activityOrComment.kudos;
if (kudos) {
const receiverIdentity = {
'id': kudos.receiverIdentityId,
'username': kudos.receiverId,
'fullName': kudos.receiverFullName,
'avatar': kudos.receiverAvatar,
'position': kudos.receiverPosition,
'external': String(kudos.externalReceiver),
'enabled': String(kudos.enabledReceiver),
const receiverIdentity = kudos.receiverType === 'user' && {
id: kudos.receiverIdentityId,
username: kudos.receiverId,
fullName: kudos.receiverFullName,
avatar: kudos.receiverAvatar,
position: kudos.receiverPosition,
external: String(kudos.externalReceiver),
enabled: String(kudos.enabledReceiver),
identityType: kudos.receiverType,
} || {
id: kudos.receiverIdentityId,
prettyName: kudos.receiverId,
displayName: kudos.receiverFullName,
avatarUrl: kudos.receiverAvatar,
external: String(kudos.externalReceiver),
enabled: String(kudos.enabledReceiver),
identityType: kudos.receiverType,
};
const url = kudos.receiverType === 'user' ? `${eXo.env.portal.context}/${eXo.env.portal.metaPortalName}/profile/${kudos.receiverId}` : `${eXo.env.portal.context}/s/${kudos.receiverIdentityId}`;
return {
key: 'NewKudosSentActivityComment.activity_kudos_title',
params: {
0: `<a class="primary--text" href="${eXo.env.portal.context}/${eXo.env.portal.portalName}/profile/${kudos.receiverId}" v-identity-popover="${JSON.stringify(receiverIdentity).replace(/'/g, '\\\'').replace(/"/g, '\'')}">${kudos.receiverFullName}</a>`
0: `<a class="primary--text" href="${url}" v-identity-popover="${JSON.stringify(receiverIdentity).replace(/'/g, '\\\'').replace(/"/g, '\'')}">${kudos.receiverFullName}</a>`
},
};
}
Expand All @@ -325,8 +359,8 @@ export function registerActivityActionExtension() {
return activityOrComment.body;
},
getSummary: activityOrComment => {
const summary = (activityOrComment.templateParams && activityOrComment.templateParams.kudosMessage)
|| (activityOrComment.kudos && activityOrComment.kudos.message)
const summary = activityOrComment?.templateParams?.kudosMessage
|| activityOrComment?.kudos?.message
|| '';
return summary.includes('<oembed>') && summary.split('<oembed>')[0] || summary;
},
Expand Down
6 changes: 4 additions & 2 deletions kudos-webapps/src/main/webapp/vue-app/js/KudosIdentity.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function getIdentityDetails(urlId, type, remoteId) {
ownerDetails.avatar = identityDetails.avatar;
ownerDetails.username = identityDetails.username;
ownerDetails.position = identityDetails.position;
ownerDetails.external = identityDetails.external;
ownerDetails.external = identityDetails.external === 'true';
ownerDetails.enabled = identityDetails.enabled;
} else {
ownerDetails.notAuthorized = true;
Expand All @@ -80,7 +80,9 @@ export function getIdentityDetails(urlId, type, remoteId) {
if (identityDetails) {
ownerDetails.identityId = identityDetails.id;
ownerDetails.fullname = identityDetails.displayName;
ownerDetails.avatar = identityDetails.avatar;
ownerDetails.avatar = identityDetails.avatarUrl;
ownerDetails.external = false;
ownerDetails.enabled = true;
} else {
ownerDetails.notAuthorized = true;
}
Expand Down
Loading