Skip to content

Commit

Permalink
Feature #13791
Browse files Browse the repository at this point in the history
A light improvment in the message building within the Notification API.
From now on, the modification of the message by the Notification API for
HTML rendering is removed as it's not the goal of this API to decide how
the text has to be rendered at the place of the templating; the layout
and the way the message is rendered is the goal of the templates used to
build any messages to send.
  • Loading branch information
mmoqui committed Oct 26, 2023
1 parent c3132d3 commit 1bc4bae
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@ public class NotificationManagerSettings {
private static final int DEFAULT_SSE_ASYNC_TIMEOUT = 180;
private static final int DEFAULT_SSE_STORE_EVENT_LIFETIME = 40;
private static final int MS = 1000;
private static SettingBundle settings = ResourceLocator.getSettingBundle(
private static final SettingBundle DEFAULT_SETTINGS = ResourceLocator.getSettingBundle(
"org.silverpeas.notificationManager.settings.notificationManagerSettings");

private static SettingBundle silvermailIconsSettings = ResourceLocator.getSettingBundle(
private static final SettingBundle DEFAULT_ICON_SETTINGS = ResourceLocator.getSettingBundle(
"org.silverpeas.notificationserver.channel.silvermail.settings.silvermailIcons");

private static SettingBundle settings = DEFAULT_SETTINGS;
private static SettingBundle iconSettings = DEFAULT_ICON_SETTINGS;

/**
* Hidden constructor.
*/
Expand Down Expand Up @@ -90,7 +93,7 @@ public static Set<DelayedNotificationFrequency> getDelayedNotificationFrequencyC
// The parameter value
final String frequencyChoiceList =
settings.getString("DELAYED_NOTIFICATION_FREQUENCY_CHOICE_LIST", "")
.replaceAll("[ ]+", ",");
.replaceAll(" +", ",");

// The possible frequencies
if (StringUtils.isNotBlank(frequencyChoiceList)) {
Expand Down Expand Up @@ -172,12 +175,12 @@ public static boolean isMultiChannelNotificationEnabled() {
* then
* returns only one among the channels set up as default. In the case no default channels are set
* up, then the previous behaviour is used; the SMTP is used as default channel.
* @return a set of default notification channels.
* @return a list of default notification channels.
*/
static List<NotifChannel> getDefaultChannels() {
final String defaultChannelSetting = settings.getString("notif.defaultChannels", "");
final boolean isMultiChannelSupported = isMultiChannelNotificationEnabled();
final String[] defaultChannels = defaultChannelSetting.replaceAll("[ ]{2,}", " ").split(" ");
final String[] defaultChannels = defaultChannelSetting.replaceAll(" {2,}", " ").split(" ");
final List<NotifChannel> channels;
final Stream<NotifChannel> streamOfChannels = Stream.of(defaultChannels)
.map(NotifChannel::decode)
Expand Down Expand Up @@ -314,7 +317,7 @@ public static int sendEveryAmountOfSecondsFor(final ServerEventName serverEventN
* @return url as string without the application context.
*/
public static String getUserNotificationDesktopIconUrl() {
return silvermailIconsSettings
return iconSettings
.getString("silvermail.desktop.url", "/util/icons/desktop-user-notification.png");
}

Expand All @@ -337,4 +340,19 @@ public static boolean isComponentInstanceLabelInNotificationSource() {
return settings.getBoolean("notification.source.componentLabel");
}

/**
* Setter dedicated to tests.
* @param newSettings the settings to use instead of the default one.
*/
static void setSettings(final SettingBundle newSettings) {
settings = newSettings;
}

/**
* Setters dedicated to tests.
* @param newIconSettings the icon settings to use instead of the default one.
*/
static void setIconSettings(final SettingBundle newIconSettings) {
iconSettings = newIconSettings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.silverpeas.core.ui.DisplayI18NHelper;
import org.silverpeas.core.util.Link;
import org.silverpeas.core.util.StringUtil;
import org.silverpeas.core.util.WebEncodeHelper;
import org.silverpeas.core.util.logging.SilverLogger;

import java.util.*;
Expand Down Expand Up @@ -258,7 +257,7 @@ public String getContent(String language) {
String messageFooter =
templateMessageFooter.applyFileTemplate("messageFooter" + '_' + language)
.replaceAll(BREAK_LINE_REGEXP, "");
if (messageFooter.length() > 0) {
if (!messageFooter.isEmpty()) {
result.append(messageFooter);
}
}
Expand All @@ -267,7 +266,7 @@ public String getContent(String language) {
result.append(AFTER_MESSAGE_FOOTER_TAG);


return WebEncodeHelper.convertBlanksForHtml(result.toString());
return result.toString();
}

private void appendMessageContent(final StringBuilder result, final String language) {
Expand Down Expand Up @@ -481,19 +480,6 @@ public void addUserRecipients(Collection<UserRecipient> users) {
}
}

/**
* Set message user recipients to exclude
* @param users the user ids that must not receive this message
*/
public void setUserRecipientsToExclude(Collection<UserRecipient> users) {
this.userRecipientsToExclude.clear();
if (users != null) {
for(UserRecipient recipient: users) {
addUserRecipient(recipient);
}
}
}

/**
* Get message user recipients to exclude
* @return the message user recipients
Expand All @@ -510,33 +496,13 @@ public void addUserRecipientToExclude(UserRecipient user) {
userRecipientsToExclude.add(user);
}

/**
* Add a user recipient to user recipients to exclude
* @param users recipient that must not be notified
*/
public void addUserRecipientsToExclude(UserRecipient[] users) {
if (users != null) {
this.userRecipientsToExclude.addAll(Arrays.asList(users));
}
}

/**
* Add a user recipient to user recipients to exclude
* @param users recipient that must not be notified
*/
public void addUserRecipientsToExclude(Collection<UserRecipient> users) {
if (users != null) {
this.userRecipientsToExclude.addAll(users);
}
}

public String getExternalLanguage() {
return Optional.ofNullable(externalLanguage).orElse(DisplayI18NHelper.getDefaultLanguage());
}

/**
* Sets language to use for external receivers.
* @param externalLanguage a lenguage as string.
* @param externalLanguage a language as string.
*/
public void setExternalLanguage(final String externalLanguage) {
this.externalLanguage = externalLanguage;
Expand Down Expand Up @@ -717,15 +683,14 @@ public Set<UserRecipient> getAllUserRecipients() throws NotificationException {
public Set<UserRecipient> getAllUserRecipients(boolean updateInternalUserRecipientsToExclude)
throws NotificationException {

Set<UserRecipient> allUniqueUserRecipients = new HashSet<>();
Collection<UserRecipient> users = getUserRecipients();
Collection<GroupRecipient> groups = getGroupRecipients();
Collection<UserRecipient> usersToExclude =
updateInternalUserRecipientsToExclude ? getUserRecipientsToExclude() :
new HashSet<>(getUserRecipientsToExclude());

// First get direct users
allUniqueUserRecipients.addAll(users);
Set<UserRecipient> allUniqueUserRecipients = new HashSet<>(users);

// Then get users included in groups
final NotificationManager notificationManager = getNotificationManager();
Expand All @@ -742,8 +707,7 @@ public Set<UserRecipient> getAllUserRecipients(boolean updateInternalUserRecipie

private Set<UserRecipient> getUsersForReceiverBlock()
throws NotificationException {
HashSet<UserRecipient> usersSet = new HashSet<>();
usersSet.addAll(getUserRecipients());
HashSet<UserRecipient> usersSet = new HashSet<>(getUserRecipients());
final NotificationManager notificationManager = getNotificationManager();
for (GroupRecipient group : getGroupRecipients()) {
if (!displayGroup(group.getGroupId())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
<p>Fichiers joints&nbsp;:</p>
$resource.attachmentLinks:{attachment | $attachmentLinks_fr(attachment)$}$
$endif$

$if(resource.url)$<div style="height:40px;"><a target="_blank" href="$resource.url$" style="display:inline-block; float:right; margin:0;border-radius:10px; border:1px solid #ccc; font-size:12px; color:#333; font-weight:bold;text-decoration:none;padding:10px;background: linear-gradient(#fff, #eee) repeat scroll 0 0 #eee;"> &#9658; $resource.linkLabel$</a></div>$endif$
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ $if(notification_receiver_users)$<div style="padding:10px 0 0 0;">
$elseif(notification_receiver_groups)$<div style="padding:10px 0 0 0;">
$endif$
$if(notification_receiver_users)$
<i>Diese Nachricht wurde gesendet nutzer : $notification_receiver_users$</i>
<i>Diese Nachricht wurde gesendet nutzer: $notification_receiver_users$</i>
$endif$
$if(notification_receiver_groups)$
$if(!notification_receiver_users)$
<i>Diese Nachricht wurde gesendet gruppen : $notification_receiver_groups$</i>
<i>Diese Nachricht wurde gesendet gruppen: $notification_receiver_groups$</i>
$endif$
$if(notification_receiver_users)$
<br/><i>gruppen : $notification_receiver_groups$</i>
<br/><i>gruppen: $notification_receiver_groups$</i>
$endif$
$endif$
$if(notification_receiver_users)$</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ $if(notification_receiver_users)$<div style="padding:10px 0 0 0;">
$elseif(notification_receiver_groups)$<div style="padding:10px 0 0 0;">
$endif$
$if(notification_receiver_users)$
<i>This message has been sent to users : $notification_receiver_users$</i>
<i>This message has been sent to users: $notification_receiver_users$</i>
$endif$
$if(notification_receiver_groups)$
$if(!notification_receiver_users)$
<i>This message has been sent to groups : $notification_receiver_groups$</i>
<i>This message has been sent to groups: $notification_receiver_groups$</i>
$endif$
$if(notification_receiver_users)$
<br/><i>to groups : $notification_receiver_groups$</i>
<br/><i>to groups: $notification_receiver_groups$</i>
$endif$
$endif$
$if(notification_receiver_users)$</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ $if(notification_receiver_users)$<div style="padding:10px 0 0 0;">
$elseif(notification_receiver_groups)$<div style="padding:10px 0 0 0;">
$endif$
$if(notification_receiver_users)$
<i>Ce message a été envoyé aux utilisateurs : $notification_receiver_users$</i>
<i>Ce message a été envoyé aux utilisateurs&nbsp;: $notification_receiver_users$</i>
$endif$
$if(notification_receiver_groups)$
$if(!notification_receiver_users)$
<i>Ce message a été envoyé aux groupes : $notification_receiver_groups$</i>
<i>Ce message a été envoyé aux groupes&nbsp;: $notification_receiver_groups$</i>
$endif$
$if(notification_receiver_users)$
<br/><i>aux groupes : $notification_receiver_groups$</i>
<br/><i>aux groupes&nbsp;: $notification_receiver_groups$</i>
$endif$
$endif$
$if(notification_receiver_users)$</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,12 @@
*/
public class DefaultUserNotification implements UserNotification {

private NotificationMetaData notification;
private final NotificationMetaData notification;

public DefaultUserNotification() {
this(null, null);
}

public DefaultUserNotification(final NotificationMetaData metaData) {
this.notification = metaData;
}

public DefaultUserNotification(final String title, final String content) {
if (StringUtils.isNotBlank(title) && StringUtils.isNotBlank(content)) {
notification = new NotificationMetaData(NotifMessageType.NORMAL.getId(), title, content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ private Pair<Boolean, String> getRootTemplatePath() {
}

private SilverpeasTemplate createTemplate() {
final Boolean fromComponent = getRootTemplatePath().getFirst();
final boolean fromComponent = getRootTemplatePath().getFirst();
final String templatePath = getRootTemplatePath().getSecond();
if (fromComponent) {
return SilverpeasTemplates.createSilverpeasTemplateOnComponents(templatePath);
Expand Down Expand Up @@ -267,7 +267,7 @@ protected String getContributionAccessLinkLabelBundleKey() {
/**
* Handles the date formats into notification building context.
*/
public class NotificationTemporal {
public static class NotificationTemporal {
final Temporal temporal;
final ZoneId zoneIdReference;
final String language;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public final UserNotification build() {
/**
* A collection of user identifiers. All the users in this collection will be notified. This
* method requires to be implemented.
* @return
* @return the identifier of all the users to notify
*/
protected abstract Collection<String> getUserIdsToNotify();

Expand Down Expand Up @@ -381,7 +381,7 @@ protected void stop() {
* Used to stop the treatment at any time
* @author Yohann Chastagnier
*/
private class Stop extends RuntimeException {
private static class Stop extends RuntimeException {
private static final long serialVersionUID = 1L;
// Nothing to do
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.silverpeas.core.admin.user.constant.UserAccessLevel;
import org.silverpeas.core.admin.user.model.UserDetail;
import org.silverpeas.core.notification.user.client.NotificationManagerSettings;
import org.silverpeas.core.notification.user.client.NotificationSettingsMocker;
import org.silverpeas.core.test.unit.extention.EnableSilverTestEnv;
import org.silverpeas.core.test.unit.extention.FieldMocker;
import org.silverpeas.core.util.SettingBundle;

import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -44,16 +42,12 @@ class UserManualNotificationUserReceiverLimitUserDetailTest {
private static final int NOT_LIMITED = 0;
private static final int LIMITED = 5;

@RegisterExtension
FieldMocker mocker = new FieldMocker();

private SettingBundle mockedSettings;
private UserDetail currentUser;

@BeforeEach
public void setup() {
mockedSettings = mocker.mockField(NotificationManagerSettings.class,
SettingBundle.class, "settings");
mockedSettings = NotificationSettingsMocker.mockSettings();
currentUser = spy(new UserDetail());
// By default, a user is not an anonymous one
assertThat(currentUser.isAnonymous(), is(false));
Expand Down

0 comments on commit 1bc4bae

Please sign in to comment.