From 0c1141e451d6f4da022d4d67140ff95436b861cb Mon Sep 17 00:00:00 2001 From: Ali Hamdi Date: Mon, 20 Nov 2023 16:41:12 +0100 Subject: [PATCH] fix: add activity/comment link for push notification - EXO-67587 --- .../exoplatform/push/channel/PushChannel.java | 6 ++ .../template/PushTemplateProvider.java | 61 +++++++++++-------- .../push/service/fcm/FCMMessagePublisher.java | 4 ++ 3 files changed, 47 insertions(+), 24 deletions(-) diff --git a/service/src/main/java/org/exoplatform/push/channel/PushChannel.java b/service/src/main/java/org/exoplatform/push/channel/PushChannel.java index e49294c1..a3e01ece 100644 --- a/service/src/main/java/org/exoplatform/push/channel/PushChannel.java +++ b/service/src/main/java/org/exoplatform/push/channel/PushChannel.java @@ -16,6 +16,7 @@ */ package org.exoplatform.push.channel; +import org.apache.commons.lang.StringUtils; import org.exoplatform.commons.api.notification.NotificationContext; import org.exoplatform.commons.api.notification.channel.AbstractChannel; import org.exoplatform.commons.api.notification.channel.template.AbstractTemplateBuilder; @@ -94,6 +95,11 @@ public void dispatch(NotificationContext ctx, String userId) { String maskedToken = StringUtil.mask(device.getToken(), 4); LOG.info("Sending push notification to user {} (token={})", userId, maskedToken); + if(StringUtils.isBlank(messageInfo.getSubject())) { + if(LOG.isDebugEnabled()) { + LOG.warn("No URL provided for notification type {}", messageInfo.getPluginId()); + } + } Message message = new Message(userId, device.getToken(), device.getType(), brandingService.getCompanyName(), messageInfo.getBody(), messageInfo.getSubject()); messagePublisher.send(message); long sendMessageExecutionTime = System.currentTimeMillis() - startTimeSendingMessage; diff --git a/service/src/main/java/org/exoplatform/push/channel/template/PushTemplateProvider.java b/service/src/main/java/org/exoplatform/push/channel/template/PushTemplateProvider.java index 657e4d52..5b22bd32 100644 --- a/service/src/main/java/org/exoplatform/push/channel/template/PushTemplateProvider.java +++ b/service/src/main/java/org/exoplatform/push/channel/template/PushTemplateProvider.java @@ -174,14 +174,15 @@ protected MessageInfo makeMessage(NotificationContext ctx) { } // boolean notHighLightComment = Boolean.parseBoolean(notification.getValueOwnerParameter(NotificationMessageUtils.NOT_HIGHLIGHT_COMMENT_PORPERTY.getKey())); - templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", LinkProvider.getSingleActivityUrl(notHighLightComment ? activity.getId() : activity.getId() + "#comment-" + commentActivity.getId())); + String notificationLink = CommonsUtils.getCurrentDomain() + LinkProvider.getSingleActivityUrl(notHighLightComment ? activity.getId() : activity.getId() + "#comment-" + commentActivity.getId()); + templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", notificationLink); // String body = TemplateUtils.processGroovy(templateContext); //binding the exception throws by processing template ctx.setException(templateContext.getException()); MessageInfo messageInfo = new MessageInfo(); - return messageInfo.body(body).end(); + return messageInfo.body(body).subject(notificationLink).end(); } private String cutStringByMaxLength(String st, int maxLength) { @@ -346,14 +347,15 @@ protected MessageInfo makeMessage(NotificationContext ctx) { } // - templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", LinkProvider.getSingleActivityUrl(activity.getId() + "#comment-" + replyToCommentActivity.getId())); + String notificationLink = CommonsUtils.getCurrentDomain() + LinkProvider.getSingleActivityUrl(activity.getId() + "#comment-" + replyToCommentActivity.getId()); + templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", notificationLink); // String body = TemplateUtils.processGroovy(templateContext); //binding the exception throws by processing template ctx.setException(templateContext.getException()); MessageInfo messageInfo = new MessageInfo(); - return messageInfo.body(body).end(); + return messageInfo.body(body).subject(notificationLink).end(); } private String cutStringByMaxLength(String st, int maxLength) { @@ -401,21 +403,23 @@ protected MessageInfo makeMessage(NotificationContext ctx) { // In case of mention on a comment, we need provide the id of the activity, not of the comment String activityTitle = getActivityTitle(activity); + String activityLink; if (activity.isComment()) { ExoSocialActivity parentActivity = Utils.getActivityManager().getParentActivity(activity); activityTitle = getActivityTitle(parentActivity); activityId = parentActivity.getId(); - templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", LinkProvider.getSingleActivityUrl(activityId + "#comment-" + activity.getId())); + activityLink = CommonsUtils.getCurrentDomain() + LinkProvider.getSingleActivityUrl(activityId + "#comment-" + activity.getId()); } else { - templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", LinkProvider.getSingleActivityUrl(activityId)); + activityLink = CommonsUtils.getCurrentDomain() + LinkProvider.getSingleActivityUrl(activityId); } + templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", activityLink); templateContext.put("ACTIVITY", NotificationUtils.getNotificationActivityTitle(activityTitle, activity.getType())); // String body = TemplateUtils.processGroovy(templateContext); //binding the exception throws by processing template ctx.setException(templateContext.getException()); MessageInfo messageInfo = new MessageInfo(); - return messageInfo.body(body).end(); + return messageInfo.body(body).subject(activityLink).end(); } @Override @@ -462,17 +466,20 @@ protected MessageInfo makeMessage(NotificationContext ctx) { templateContext.put("ACTIVITY", NotificationUtils.getNotificationActivityTitle(getActivityTitle(activity), activity.getType())); templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", LinkProvider.getSingleActivityUrl(activity.getId())); + String notificationLink; if(activity.isComment()) { ExoSocialActivity activityOfComment = Utils.getActivityManager().getParentActivity(activity); - templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", LinkProvider.getSingleActivityUrl(activityOfComment.getId() + "#comment-" + activity.getId())); + notificationLink = CommonsUtils.getCurrentDomain() + LinkProvider.getSingleActivityUrl(activityOfComment.getId() + "#comment-" + activity.getId()); + } else { - templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", LinkProvider.getSingleActivityUrl(activity.getId())); + notificationLink = CommonsUtils.getCurrentDomain() + LinkProvider.getSingleActivityUrl(activity.getId()); } + templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", notificationLink); List users = SocialNotificationUtils.mergeUsers(notification, SocialNotificationUtils.LIKER.getKey(), activity.getId(), notification.getValueOwnerParameter(SocialNotificationUtils.LIKER.getKey())); // int nbUsers = users.size(); if (nbUsers > 0) { - Identity lastIdentity = Utils.getIdentityManager().getOrCreateIdentity(OrganizationIdentityProvider.NAME, users.get(nbUsers - 1), true); + Identity lastIdentity = Utils.getIdentityManager().getOrCreateIdentity(OrganizationIdentityProvider.NAME, users.get(nbUsers - 1)); Profile profile = lastIdentity.getProfile(); templateContext.put("USER", Utils.addExternalFlag(lastIdentity)); templateContext.put("AVATAR", profile.getAvatarUrl() != null ? profile.getAvatarUrl() : LinkProvider.PROFILE_DEFAULT_AVATAR_URL); @@ -480,7 +487,7 @@ protected MessageInfo makeMessage(NotificationContext ctx) { templateContext.put("NB_USERS", nbUsers); // if (nbUsers >= 2) { - Identity beforeLastIdentity = Utils.getIdentityManager().getOrCreateIdentity(OrganizationIdentityProvider.NAME, users.get(nbUsers - 2), true); + Identity beforeLastIdentity = Utils.getIdentityManager().getOrCreateIdentity(OrganizationIdentityProvider.NAME, users.get(nbUsers - 2)); templateContext.put("LAST_USER", Utils.addExternalFlag(beforeLastIdentity)); if (nbUsers > 2) { templateContext.put("COUNT", nbUsers - 2); @@ -493,7 +500,7 @@ protected MessageInfo makeMessage(NotificationContext ctx) { //binding the exception throws by processing template ctx.setException(templateContext.getException()); MessageInfo messageInfo = new MessageInfo(); - return messageInfo.body(body).end(); + return messageInfo.body(body).subject(notificationLink).end(); } @Override @@ -523,14 +530,15 @@ protected MessageInfo makeMessage(NotificationContext ctx) { templateContext.put("LAST_UPDATED_TIME", TimeConvertUtils.convertXTimeAgoByTimeServer(cal.getTime(), "EE, dd yyyy", new Locale(language), TimeConvertUtils.YEAR)); templateContext.put("USER", Utils.addExternalFlag(identity)); templateContext.put("PORTAL_NAME", NotificationPluginUtils.getBrandingPortalName()); - templateContext.put("PROFILE_URL", LinkProvider.getUserProfileUri(identity.getRemoteId())); + String profileUrl = CommonsUtils.getCurrentDomain() + LinkProvider.getUserProfileUri(identity.getRemoteId()); + templateContext.put("PROFILE_URL", profileUrl); templateContext.put("AVATAR", userProfile.getAvatarUrl() != null ? userProfile.getAvatarUrl() : LinkProvider.PROFILE_DEFAULT_AVATAR_URL); // String body = TemplateUtils.processGroovy(templateContext); //binding the exception throws by processing template ctx.setException(templateContext.getException()); MessageInfo messageInfo = new MessageInfo(); - return messageInfo.body(body).end(); + return messageInfo.body(body).subject(profileUrl).end(); } @Override @@ -571,13 +579,14 @@ protected MessageInfo makeMessage(NotificationContext ctx) { templateContext.put("ACTIVITY", NotificationUtils.getNotificationActivityTitle(getActivityTitle(activity), activity.getType())); templateContext.put("USER", Utils.addExternalFlag(identity)); templateContext.put("PROFILE_URL", LinkProvider.getUserProfileUri(identity.getRemoteId())); - templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", LinkProvider.getSingleActivityUrl(activity.getId())); + String activityLink = CommonsUtils.getCurrentDomain() + LinkProvider.getSingleActivityUrl(activity.getId()); + templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", activityLink) ; // String body = TemplateUtils.processGroovy(templateContext); //binding the exception throws by processing template ctx.setException(templateContext.getException()); MessageInfo messageInfo = new MessageInfo(); - return messageInfo.body(body).end(); + return messageInfo.body(body).subject(activityLink).end(); } @Override @@ -625,13 +634,14 @@ protected MessageInfo makeMessage(NotificationContext ctx) { templateContext.put("SPACE", space.getDisplayName()); templateContext.put("SPACE_URL", LinkProvider.getActivityUriForSpace(space.getPrettyName(), space.getGroupId().replace("/spaces/", ""))); templateContext.put("PROFILE_URL", LinkProvider.getUserProfileUri(identity.getRemoteId())); - templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", LinkProvider.getSingleActivityUrl(activity.getId())); + String activityLink = CommonsUtils.getCurrentDomain() + LinkProvider.getSingleActivityUrl(activity.getId()); + templateContext.put("VIEW_FULL_DISCUSSION_ACTION_URL", activityLink); // String body = TemplateUtils.processGroovy(templateContext); //binding the exception throws by processing template ctx.setException(templateContext.getException()); MessageInfo messageInfo = new MessageInfo(); - return messageInfo.body(body).end(); + return messageInfo.body(body).subject(activityLink).end(); } @Override @@ -663,7 +673,8 @@ protected MessageInfo makeMessage(NotificationContext ctx) { templateContext.put("STATUS", status != null && status.equals("accepted") ? "ACCEPTED" : "PENDING"); templateContext.put("LAST_UPDATED_TIME", TimeConvertUtils.convertXTimeAgoByTimeServer(cal.getTime(), "EE, dd yyyy", new Locale(language), TimeConvertUtils.YEAR)); templateContext.put("USER", Utils.addExternalFlag(identity)); - templateContext.put("PROFILE_URL", LinkProvider.getUserProfileUri(identity.getRemoteId())); + String senderProfileUrl = CommonsUtils.getCurrentDomain() + LinkProvider.getUserProfileUri(identity.getRemoteId()); + templateContext.put("PROFILE_URL", senderProfileUrl); templateContext.put("AVATAR", userProfile.getAvatarUrl() != null ? userProfile.getAvatarUrl() : LinkProvider.PROFILE_DEFAULT_AVATAR_URL); templateContext.put("ACCEPT_CONNECTION_REQUEST_ACTION_URL", LinkProviderUtils.getWebNotificationRestUrl(ACCEPT_INVITATION_TO_CONNECT, sender, toUser, notification.getId(), MESSAGE_JSON_FILE_NAME)); templateContext.put("REFUSE_CONNECTION_REQUEST_ACTION_URL", LinkProviderUtils.getWebNotificationRestUrl(REFUSE_INVITATION_TO_CONNECT, sender, toUser, notification.getId(), MESSAGE_JSON_FILE_NAME)); @@ -672,7 +683,7 @@ protected MessageInfo makeMessage(NotificationContext ctx) { //binding the exception throws by processing template ctx.setException(templateContext.getException()); MessageInfo messageInfo = new MessageInfo(); - return messageInfo.body(body).end(); + return messageInfo.body(body).subject(senderProfileUrl).end(); } @Override @@ -710,7 +721,8 @@ protected MessageInfo makeMessage(NotificationContext ctx) { templateContext.put("LAST_UPDATED_TIME", TimeConvertUtils.convertXTimeAgoByTimeServer(cal.getTime(), "EE, dd yyyy", new Locale(language), TimeConvertUtils.YEAR)); templateContext.put("SPACE", space.getDisplayName()); templateContext.put("USER", Utils.addExternalFlag(identity)); - templateContext.put("SPACE_URL", LinkProvider.getActivityUriForSpace(space.getPrettyName(), space.getGroupId().replace("/spaces/", ""))); + String spaceUrl = CommonsUtils.getCurrentDomain() + LinkProvider.getActivityUriForSpace(space.getPrettyName(), space.getGroupId().replace("/spaces/", "")); + templateContext.put("SPACE_URL", spaceUrl); templateContext.put("AVATAR", userProfile.getAvatarUrl() != null ? userProfile.getAvatarUrl() : LinkProvider.PROFILE_DEFAULT_AVATAR_URL); templateContext.put("VALIDATE_SPACE_REQUEST_ACTION_URL", LinkProviderUtils.getWebNotificationRestUrl(VALIDATE_SPACE_REQUEST, space.getId(), identity.getRemoteId()) + "/" + notification.getTo() + "/" + notification.getId() + "/" + MESSAGE_JSON_FILE_NAME); templateContext.put("REFUSE_SPACE_REQUEST_ACTION_URL", LinkProviderUtils.getWebNotificationRestUrl(REFUSE_SPACE_REQUEST, space.getId(), identity.getRemoteId()) + "/" + notification.getTo() + "/" + notification.getId() + "/" + MESSAGE_JSON_FILE_NAME); @@ -719,7 +731,7 @@ protected MessageInfo makeMessage(NotificationContext ctx) { //binding the exception throws by processing template ctx.setException(templateContext.getException()); MessageInfo messageInfo = new MessageInfo(); - return messageInfo.body(body).end(); + return messageInfo.body(body).subject(spaceUrl).end(); } @Override @@ -756,7 +768,8 @@ protected MessageInfo makeMessage(NotificationContext ctx) { templateContext.put("LAST_UPDATED_TIME", TimeConvertUtils.convertXTimeAgoByTimeServer(cal.getTime(), "EE, dd yyyy", new Locale(language), TimeConvertUtils.YEAR)); templateContext.put("SPACE", space.getDisplayName()); templateContext.put("SENDER_NAME", sender); - templateContext.put("SPACE_URL", LinkProvider.getActivityUriForSpace(space.getPrettyName(), space.getGroupId().replace("/spaces/", ""))); + String spaceUrl = CommonsUtils.getCurrentDomain() + LinkProvider.getActivityUriForSpace(space.getPrettyName(), space.getGroupId().replace("/spaces/", "")); + templateContext.put("SPACE_URL", spaceUrl); templateContext.put("SPACE_AVATAR", space.getAvatarUrl() != null ? space.getAvatarUrl() : LinkProvider.SPACE_DEFAULT_AVATAR_URL); templateContext.put("ACCEPT_SPACE_INVITATION_ACTION_URL", LinkProviderUtils.getWebNotificationRestUrl(ACCEPT_SPACE_INVITATION, space.getId(), notification.getTo(), notification.getId(), MESSAGE_JSON_FILE_NAME)); templateContext.put("REFUSE_SPACE_INVITATION_ACTION_URL", LinkProviderUtils.getWebNotificationRestUrl(REFUSE_SPACE_INVITATION, space.getId(), notification.getTo(), notification.getId(), MESSAGE_JSON_FILE_NAME)); @@ -765,7 +778,7 @@ protected MessageInfo makeMessage(NotificationContext ctx) { //binding the exception throws by processing template ctx.setException(templateContext.getException()); MessageInfo messageInfo = new MessageInfo(); - return messageInfo.body(body).end(); + return messageInfo.body(body).subject(spaceUrl).end(); } @Override diff --git a/service/src/main/java/org/exoplatform/push/service/fcm/FCMMessagePublisher.java b/service/src/main/java/org/exoplatform/push/service/fcm/FCMMessagePublisher.java index d4ed3d09..6bfd40ea 100644 --- a/service/src/main/java/org/exoplatform/push/service/fcm/FCMMessagePublisher.java +++ b/service/src/main/java/org/exoplatform/push/service/fcm/FCMMessagePublisher.java @@ -34,6 +34,7 @@ import org.apache.http.impl.client.HttpClientBuilder; import org.exoplatform.commons.api.notification.plugin.NotificationPluginUtils; import org.exoplatform.commons.api.notification.service.WebNotificationService; +import org.exoplatform.commons.utils.CommonsUtils; import org.exoplatform.container.xml.InitParams; import org.exoplatform.container.xml.ValueParam; import org.exoplatform.push.domain.Message; @@ -147,6 +148,9 @@ public void send(Message message) throws Exception { post.setHeader(HttpHeaders.CONTENT_TYPE, "application/json"); String messageBody = processBody(message); + if(StringUtils.isEmpty(message.getUrl())) { + message.setUrl(CommonsUtils.getCurrentDomain()); + } StringBuilder requestBody = new StringBuilder() .append("{")