Skip to content
Snippets Groups Projects
Unverified Commit fe242d7d authored by Clemens Robbenhaar's avatar Clemens Robbenhaar Committed by GitHub
Browse files

XWIKI-15814 - Messages sent to followers only appear in the Notificat… (#1282)

- if "useUserPreferences" is false and we have a registered user
  then fetch a matching fiter preference to allow the filter
  "PersonalMessageStreamNotificationFilter" to figure out
  who is followed by the current user
- move the special user related code to the UsersParameterHandler
- add test for the "handleUsersParameter" when the "user" parameter
  is empty
- try to clarify the comments
- apply best practice: test methods should not be public
parent 326199c9
No related branches found
No related tags found
No related merge requests found
......@@ -29,14 +29,19 @@
import javax.inject.Singleton;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.xwiki.bridge.DocumentAccessBridge;
import org.xwiki.component.annotation.Component;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.DocumentReferenceResolver;
import org.xwiki.model.reference.EntityReferenceSerializer;
import org.xwiki.notifications.NotificationException;
import org.xwiki.notifications.NotificationFormat;
import org.xwiki.notifications.filters.NotificationFilterPreference;
import org.xwiki.notifications.filters.NotificationFilterPreferenceManager;
import org.xwiki.notifications.filters.NotificationFilterType;
import org.xwiki.notifications.filters.internal.DefaultNotificationFilterPreference;
import org.xwiki.notifications.filters.internal.user.EventUserFilter;
import org.xwiki.notifications.sources.NotificationParameters;
/**
......@@ -64,6 +69,13 @@ public class UsersParameterHandler
@Inject
private EntityReferenceSerializer<String> entityReferenceSerializer;
@Inject
private NotificationFilterPreferenceManager notificationFilterPreferenceManager;
@Inject
private Logger logger;
/**
* Handle the "users" parameters of the REST API.
*
......@@ -90,6 +102,26 @@ public void handleUsersParameter(String users, NotificationParameters parameters
parameters.filters.add(new FollowedUserOnlyEventFilter(entityReferenceSerializer, userList));
addFilterPreference(parameters, userList);
} else if (parameters.user != null) {
// If we have a user (but no "users") then we should also display personal messages from users
// followed by that user.
// The other types of messages get included in other places, but for personal messages the filter needs
// a matching filter preference so we loop though preferences to see if they have
// a preference for this (using a copy to guard against unwanted modifications).
// As result there should be a preference for every followed user in the parameters.filterPreferences list.
try {
for (NotificationFilterPreference filterPref
: notificationFilterPreferenceManager.getFilterPreferences(parameters.user)) {
if (EventUserFilter.FILTER_NAME.equals(filterPref.getFilterName())) {
DefaultNotificationFilterPreference personalPref
= new DefaultNotificationFilterPreference(filterPref);
parameters.filterPreferences.add(personalPref);
}
}
} catch (NotificationException e) {
logger.error("failed to fetch the notification preferences for user [{}]:",
entityReferenceSerializer.serialize(parameters.user), e);
}
}
}
......
......@@ -19,11 +19,11 @@
*/
package org.xwiki.notifications.sources.internal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import org.junit.jupiter.api.Test;
......@@ -31,9 +31,11 @@
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.DocumentReferenceResolver;
import org.xwiki.model.reference.EntityReferenceSerializer;
import org.xwiki.notifications.NotificationException;
import org.xwiki.notifications.NotificationFormat;
import org.xwiki.notifications.filters.NotificationFilter;
import org.xwiki.notifications.filters.NotificationFilterPreference;
import org.xwiki.notifications.filters.NotificationFilterPreferenceManager;
import org.xwiki.notifications.filters.NotificationFilterType;
import org.xwiki.notifications.filters.internal.DefaultNotificationFilterPreference;
import org.xwiki.notifications.sources.NotificationParameters;
......@@ -68,8 +70,11 @@ public class UsersParameterHandlerTest
@MockComponent
private EntityReferenceSerializer<String> entityReferenceSerializer;
@MockComponent
private NotificationFilterPreferenceManager notificationFilterPreferenceManager;
@Test
public void handlerUsersParameter()
void handlerUsersParameter()
{
NotificationParameters notificationParameters = new NotificationParameters();
this.usersParameterHandler.handleUsersParameter(null, notificationParameters);
......@@ -119,6 +124,46 @@ public void handlerUsersParameter()
assertEquals(expectedParameters, notificationParameters);
}
@Test
void handlerUsersParameterWhenOnlyTheUserParameterIsGiven() throws NotificationException
{
final DocumentReference testUser = new DocumentReference("currentwiki", "XWiki", "SomeUser");
NotificationParameters notificationParameters = notificationParametersForUser(testUser);
NotificationParameters expectedParameters = notificationParametersForUser(testUser);
expectedParameters.filters = Collections.emptyList();
expectedParameters.filterPreferences = Collections.emptyList();
this.usersParameterHandler.handleUsersParameter("", notificationParameters);
assertEquals(expectedParameters, notificationParameters);
List<NotificationFilterPreference> userFilterPreferences = new ArrayList<>();
userFilterPreferences.add(getFilterPreference("currentwiki:XWiki.SomeUser"));
NotificationFilterPreference userFollowingFilterPref1 = getFilterPreferenceWithName(
"currentwiki:XWiki.Alice", "eventUserNotificationFilter");
userFilterPreferences.add(userFollowingFilterPref1);
when(notificationFilterPreferenceManager.getFilterPreferences(testUser))
.thenReturn(userFilterPreferences);
expectedParameters.filterPreferences = Collections.singletonList(userFollowingFilterPref1);
notificationParameters = notificationParametersForUser(testUser);
this.usersParameterHandler.handleUsersParameter("", notificationParameters);
assertEquals(expectedParameters, notificationParameters);
userFilterPreferences.add(getFilterPreferenceWithName("currentwiki:XWiki.SomeOther", ""));
NotificationFilterPreference userFollowingFilterPref2 = getFilterPreferenceWithName(
"currentwiki:XWiki.Bob", "eventUserNotificationFilter");
userFilterPreferences.add(userFollowingFilterPref2);
userFilterPreferences.add(getFilterPreference("currentwiki:XWiki.SomeUser4"));
expectedParameters.filterPreferences = Arrays.asList(userFollowingFilterPref1, userFollowingFilterPref2);
notificationParameters = notificationParametersForUser(testUser);
this.usersParameterHandler.handleUsersParameter("", notificationParameters);
assertEquals(expectedParameters, notificationParameters);
}
private NotificationFilterPreference getFilterPreference(String userId)
{
DefaultNotificationFilterPreference pref = new DefaultNotificationFilterPreference();
......@@ -129,4 +174,20 @@ private NotificationFilterPreference getFilterPreference(String userId)
pref.setUser(userId);
return pref;
}
private NotificationFilterPreference getFilterPreferenceWithName(String userId, String filterName)
{
DefaultNotificationFilterPreference pref = (DefaultNotificationFilterPreference) getFilterPreference(userId);
pref.setFilterName(filterName);
return pref;
}
private NotificationParameters notificationParametersForUser(DocumentReference userRef)
{
NotificationParameters notificationParameters = new NotificationParameters();
notificationParameters.user = userRef;
notificationParameters.format = NotificationFormat.ALERT;
return notificationParameters;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment