Skip to content

Commit

Permalink
feat: Translate I18N of Kudos Message for Role Mentioning - MEED-3117 -
Browse files Browse the repository at this point in the history
Meeds-io/MIPs#109 (#455)

This change will allow to switch Role Mentioning Language from default
language to user language before accessing the Kudos massage.
  • Loading branch information
boubaker authored and SaraBoutej committed Feb 5, 2024
1 parent ef8322c commit e292c20
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.exoplatform.kudos.service.KudosService;
import org.exoplatform.social.core.ActivityTypePlugin;
import org.exoplatform.social.core.activity.model.ExoSocialActivity;
import org.exoplatform.social.core.utils.MentionUtils;

public class KudosActivityTypePlugin extends ActivityTypePlugin {

Expand All @@ -50,7 +51,7 @@ public boolean isEnableNotification(ExoSocialActivity activity, String username)
@Override
public String getActivityTitle(ExoSocialActivity activity) {
Kudos kudos = this.kudosService.getKudosByActivityId(Long.parseLong(activity.getId().replace("comment", "")));
return kudos == null ? activity.getTitle() : kudos.getMessage();
return kudos == null ? activity.getTitle() : MentionUtils.substituteUsernames(kudos.getMessage());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ActivityKudosProcessor(KudosService kudosService,
InitParams initParams) {
super(initParams);
this.kudosService = kudosService;
this.defaultPortal = userPortalConfigService.getDefaultPortal();
this.defaultPortal = userPortalConfigService.getMetaPortal();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private ExoSocialActivity createActivity(Kudos kudos, String parentCommentId) {
ExoSocialActivityImpl activity = new ExoSocialActivityImpl();
activity.setParentCommentId(parentCommentId);
activity.setType(org.exoplatform.kudos.service.utils.Utils.KUDOS_ACTIVITY_COMMENT_TYPE);
activity.setTitle("Kudos to " + kudos.getReceiverFullName());
activity.setTitle(kudos.getMessage());
activity.setBody("Kudos to " + kudos.getReceiverFullName());
activity.setUserId(kudos.getSenderIdentityId());
org.exoplatform.kudos.service.utils.Utils.computeKudosActivityProperties(activity, kudos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.exoplatform.social.core.processor.I18NActivityProcessor;
import org.exoplatform.social.core.service.LinkProvider;
import org.exoplatform.social.core.space.model.Space;
import org.exoplatform.social.core.utils.MentionUtils;
import org.exoplatform.social.notification.LinkProviderUtils;
import org.exoplatform.social.notification.Utils;
import org.exoplatform.social.notification.plugin.SocialNotificationUtils;
Expand Down Expand Up @@ -128,10 +129,10 @@ protected MessageInfo makeMessage(NotificationContext ctx) { // NOSONAR
// body construction must be made after subject building
if (activity != null) {
if (StringUtils.isNotBlank(activity.getParentCommentId())) {
String parentCommentTitle = Utils.getActivityManager().getActivityTitle(activity.getParentCommentId());
String parentCommentTitle = getActivityTitle(activity.getParentCommentId(), language);
templateContext.put("ACTIVITY", parentCommentTitle);
} else if (StringUtils.isNotBlank(activity.getParentId())) {
String parentActivityTitle = Utils.getActivityManager().getActivityTitle(activity.getParentId());
String parentActivityTitle = getActivityTitle(activity.getParentId(), language);
templateContext.put("ACTIVITY", parentActivityTitle);
} else {
templateContext.put("ACTIVITY", "");
Expand Down Expand Up @@ -159,4 +160,9 @@ protected ExoSocialActivity getI18N(ExoSocialActivity activity, Locale locale) {
return activity;
}

private String getActivityTitle(String activityId, String language) {
return MentionUtils.substituteRoleWithLocale(Utils.getActivityManager().getActivityTitle(activityId),
Locale.forLanguageTag(language));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import static org.exoplatform.kudos.service.utils.Utils.KUDOS_ACTIVITY_COMMENT_TYPE;

import java.util.Locale;

import org.exoplatform.commons.api.notification.NotificationContext;
import org.exoplatform.commons.api.notification.model.NotificationInfo;
import org.exoplatform.commons.api.notification.plugin.AbstractNotificationChildPlugin;
Expand All @@ -27,6 +29,7 @@
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.kudos.model.Kudos;
import org.exoplatform.kudos.service.KudosService;
import org.exoplatform.social.core.utils.MentionUtils;
import org.exoplatform.social.notification.plugin.SocialNotificationUtils;

public class KudosActivityChildPlugin extends AbstractNotificationChildPlugin {
Expand All @@ -50,8 +53,10 @@ public String makeContent(NotificationContext ctx) {
if (kudos == null) {
return "";
}
TemplateContext templateContext = new TemplateContext(getId(), getLanguage(notification));
templateContext.put("MESSAGE", kudos.getMessage());
String language = getLanguage(notification);
String message = MentionUtils.substituteUsernames(kudos.getMessage(), Locale.forLanguageTag(language));
TemplateContext templateContext = new TemplateContext(getId(), language);
templateContext.put("MESSAGE", message);
return TemplateUtils.processGroovy(templateContext);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.exoplatform.kudos.model.Kudos;
import org.exoplatform.social.core.activity.model.ExoSocialActivity;
import org.exoplatform.social.core.manager.ActivityManager;
import org.exoplatform.social.core.utils.MentionUtils;
import org.exoplatform.social.notification.plugin.SocialNotificationUtils;

public class KudosReceiverNotificationPlugin extends BaseNotificationPlugin {
Expand Down Expand Up @@ -75,7 +76,8 @@ public NotificationInfo makeNotification(NotificationContext ctx) {
.with("RECEIVER_ID", receiverId)
.with("RECEIVER_TYPE", receiverType)
.with("KUDOS_ID", String.valueOf(kudos.getTechnicalId()))
.with("KUDOS_MESSAGE", kudos.getMessage() == null ? "" : kudos.getMessage())
.with("KUDOS_MESSAGE",
kudos.getMessage() == null ? "" : MentionUtils.substituteUsernames(kudos.getMessage()))
.key(getId())
.end();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.exoplatform.kudos.service.utils.Utils.timeFromSeconds;

import java.util.List;
import java.util.Locale;

import javax.annotation.security.RolesAllowed;
import javax.ws.rs.*;
Expand All @@ -33,19 +34,23 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import org.exoplatform.commons.exception.ObjectNotFoundException;
import org.exoplatform.kudos.exception.KudosAlreadyLinkedException;
import org.exoplatform.kudos.model.*;
import org.exoplatform.kudos.service.KudosService;
import org.exoplatform.portal.application.localization.LocalizationFilter;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.services.rest.resource.ResourceContainer;
import org.exoplatform.services.security.ConversationState;
import org.exoplatform.social.core.identity.model.Identity;
import org.exoplatform.social.core.identity.provider.OrganizationIdentityProvider;
import org.exoplatform.social.core.manager.IdentityManager;
import org.exoplatform.social.core.utils.MentionUtils;


@Path("/kudos/api/kudos")
Expand Down Expand Up @@ -82,6 +87,7 @@ public Response getKudosByPeriodOfDate(@Parameter(description = "Timestamp in se
}
try {
List<Kudos> allKudosByPeriod = kudosService.getKudosByPeriodOfDate(dateInSeconds, getLimit(limit));
translateRoleMentions(allKudosByPeriod.toArray(new Kudos[0]));
return Response.ok(allKudosByPeriod).build();
} catch (Exception e) {
LOG.warn("Error getting kudos list of period with date {}", dateInSeconds, e);
Expand All @@ -108,6 +114,7 @@ public Response getEntityKudos(@Parameter(description = "kudos entity type (for
}
try {
List<Kudos> allKudosByEntity = kudosService.getKudosByEntity(entityType, entityId, getLimit(limit));
translateRoleMentions(allKudosByEntity.toArray(new Kudos[0]));
return Response.ok(allKudosByEntity).build();
} catch (Exception e) {
LOG.warn("Error getting kudos entity of entity {}/{}", entityType, entityId, e);
Expand Down Expand Up @@ -137,6 +144,7 @@ public Response getKudosByActivityId(
org.exoplatform.services.security.Identity currentUser = ConversationState.getCurrent().getIdentity();
try {
Kudos kudos = kudosService.getKudosByActivityId(getActivityId(activityId), currentUser);
translateRoleMentions(kudos);
return Response.ok(kudos).build();
} catch (IllegalAccessException e) {
LOG.error("Access denied to user {} to access Kudos of activity by id {}", currentUser.getUserId(), activityId);
Expand Down Expand Up @@ -171,6 +179,7 @@ public Response getKudosListOfActivity(@Parameter(description = "kudos parent ac
org.exoplatform.services.security.Identity currentUser = ConversationState.getCurrent().getIdentity();
try {
List<Kudos> kudosList = kudosService.getKudosListOfActivity(activityId, currentUser);
translateRoleMentions(kudosList.toArray(new Kudos[0]));
return Response.ok(kudosList).build();
} catch (IllegalAccessException e) {
LOG.error("Access denied to user {} to access Kudos of parent activity by id {}", currentUser.getUserId(), activityId);
Expand Down Expand Up @@ -247,6 +256,7 @@ public Response getKudosByDates(@QueryParam("startDateInSeconds") long startDate
}
try {
List<Kudos> allKudosByPeriod = kudosService.getKudosByPeriod(startDateInSeconds, endDateInSeconds, getLimit(limit));
translateRoleMentions(allKudosByPeriod.toArray(new Kudos[0]));
return Response.ok(allKudosByPeriod).build();
} catch (Exception e) {
LOG.warn("Error getting kudos list of period: from {} to {}", startDateInSeconds, endDateInSeconds, e);
Expand Down Expand Up @@ -322,6 +332,7 @@ public Response getReceivedKudosByPeriod(@Parameter(description = "User or space
period.getStartDateInSeconds(),
period.getEndDateInSeconds(),
getLimit(limit));
translateRoleMentions(kudos.toArray(new Kudos[0]));
kudosList.setKudos(kudos);
return Response.ok(kudosList).build();
}
Expand Down Expand Up @@ -394,6 +405,7 @@ public Response getSentKudosByPeriod(@Parameter(description = "User or space ide
period.getStartDateInSeconds(),
period.getEndDateInSeconds(),
getLimit(limit));
translateRoleMentions(kudos.toArray(new Kudos[0]));
kudosList.setKudos(kudos);
return Response.ok(kudosList).build();
}
Expand Down Expand Up @@ -432,6 +444,7 @@ public Response createKudos(@RequestBody(description = "Kudos object to create",
try {
kudos.setSenderId(getCurrentUserId());
Kudos kudosSent = kudosService.createKudos(kudos, getCurrentUserId());
translateRoleMentions(kudosSent);
return Response.ok(kudosSent).build();
} catch (Exception e) {
LOG.warn("Error saving kudos: {}", kudos, e);
Expand Down Expand Up @@ -513,8 +526,20 @@ private int getLimit(int limit) {
return limit;
}


private Long getActivityId(String commentId) {
return (commentId == null || commentId.trim().isEmpty()) ? null : Long.valueOf(commentId.replace("comment", ""));
}

private void translateRoleMentions(Kudos ...kudosList) {
if (ArrayUtils.isEmpty(kudosList)) {
return;
}
Locale userLocale = LocalizationFilter.getCurrentLocale();
for (Kudos kudos : kudosList) {
if (kudos != null) {
kudos.setMessage(MentionUtils.substituteUsernames(kudos.getMessage(), userLocale));
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
package org.exoplatform.kudos.service.utils;

import java.time.*;
import java.util.*;
import static org.exoplatform.social.core.manager.ActivityManagerImpl.REMOVABLE;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.TimeZone;

import org.apache.commons.text.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;

import org.exoplatform.commons.api.notification.model.ArgumentLiteral;
import org.exoplatform.commons.api.settings.data.Context;
import org.exoplatform.commons.api.settings.data.Scope;
import org.exoplatform.commons.utils.CommonsUtils;
import org.exoplatform.kudos.entity.KudosEntity;
import org.exoplatform.kudos.model.*;
import org.exoplatform.kudos.model.GlobalSettings;
import org.exoplatform.kudos.model.Kudos;
import org.exoplatform.kudos.model.KudosEntityType;
import org.exoplatform.kudos.model.KudosPeriod;
import org.exoplatform.kudos.model.KudosPeriodType;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.services.security.ConversationState;
Expand All @@ -28,8 +39,6 @@
import org.exoplatform.social.core.space.spi.SpaceService;
import org.exoplatform.social.core.utils.MentionUtils;

import static org.exoplatform.social.core.manager.ActivityManagerImpl.REMOVABLE;

public class Utils {
private static final Log LOG = ExoLogger.getLogger(Utils.class);

Expand Down Expand Up @@ -141,17 +150,13 @@ public static String getReceiverType(String receiverType) {
return SPACE_ACCOUNT_TYPE.equals(receiverType) ? SPACE_ACCOUNT_TYPE : USER_ACCOUNT_TYPE;
}

public static Kudos fromEntity(KudosEntity kudosEntity, String defaultPortal) {
public static Kudos fromEntity(KudosEntity kudosEntity) {
if (kudosEntity == null) {
return null;
}
Kudos kudos = new Kudos();
kudos.setTechnicalId(kudosEntity.getId());
if (defaultPortal == null) {
kudos.setMessage(kudosEntity.getMessage());
} else {
kudos.setMessage(MentionUtils.substituteUsernames(defaultPortal, kudosEntity.getMessage()));
}
kudos.setMessage(kudosEntity.getMessage());
kudos.setEntityId(String.valueOf(kudosEntity.getEntityId()));
kudos.setActivityId(kudosEntity.getActivityId());
if (kudosEntity.getParentEntityId() != null && kudosEntity.getParentEntityId() != 0) {
Expand Down Expand Up @@ -254,7 +259,7 @@ public static void computeKudosActivityProperties(ExoSocialActivity activity, Ku
String receiverLink = "<a href='" + kudos.getReceiverURL() + "'>" + kudos.getReceiverFullName() + "</a>";
receiverLink = StringEscapeUtils.unescapeHtml4(receiverLink);

String kudosMessage = kudos.getMessage();
String kudosMessage = MentionUtils.substituteUsernames(kudos.getMessage());
String message = StringUtils.isBlank(kudosMessage) ? "." : ": " + kudosMessage;

if (activity.getTemplateParams() != null) {
Expand Down
Loading

0 comments on commit e292c20

Please sign in to comment.