use of org.xwiki.notifications.filters.NotificationFilterPreference in project xwiki-platform by xwiki.
the class DefaultModelBridge method getFilterPreferences.
@Override
public Set<NotificationFilterPreference> getFilterPreferences(DocumentReference user) throws NotificationException {
XWikiContext context = contextProvider.get();
XWiki xwiki = context.getWiki();
final DocumentReference notificationFilterPreferenceClass = NOTIFICATION_FILTER_PREFERENCE_CLASS.setWikiReference(user.getWikiReference());
Set<NotificationFilterPreference> preferences = new HashSet<>();
try {
XWikiDocument doc = xwiki.getDocument(user, context);
List<BaseObject> preferencesObj = doc.getXObjects(notificationFilterPreferenceClass);
if (preferencesObj != null) {
for (BaseObject obj : preferencesObj) {
if (obj != null) {
Map<NotificationFilterProperty, List<String>> filterPreferenceProperties = createNotificationFilterPropertiesMap(obj);
NotificationFilterType filterType = NotificationFilterType.valueOf(obj.getStringValue(FIELD_FILTER_TYPE).toUpperCase());
Set<NotificationFormat> filterFormats = new HashSet<>();
for (String format : (List<String>) obj.getListValue(FIELD_FILTER_FORMATS)) {
filterFormats.add(NotificationFormat.valueOf(format.toUpperCase()));
}
// Create the new filter preference and add it to the list of preferences
DefaultNotificationFilterPreference notificationFilterPreference = new DefaultNotificationFilterPreference(obj.getStringValue(FILTER_PREFERENCE_NAME));
notificationFilterPreference.setProviderHint("userProfile");
notificationFilterPreference.setFilterName(obj.getStringValue(FIELD_FILTER_NAME));
notificationFilterPreference.setEnabled(obj.getIntValue(FIELD_IS_ENABLED, 1) == 1);
notificationFilterPreference.setActive(obj.getIntValue(FIELD_IS_ACTIVE, 1) == 1);
notificationFilterPreference.setFilterType(filterType);
notificationFilterPreference.setNotificationFormats(filterFormats);
notificationFilterPreference.setPreferenceProperties(filterPreferenceProperties);
preferences.add(notificationFilterPreference);
}
}
}
} catch (Exception e) {
throw new NotificationException(String.format("Failed to get the notification preferences scope for the user [%s].", user), e);
}
return preferences;
}
use of org.xwiki.notifications.filters.NotificationFilterPreference in project xwiki-platform by xwiki.
the class DefaultModelBridge method saveFilterPreferences.
@Override
public void saveFilterPreferences(DocumentReference user, Collection<NotificationFilterPreference> filterPreferences) throws NotificationException {
if (user == null) {
return;
}
// Convert the collection of preferences to save to a Map sorted by filter names
Map<String, NotificationFilterPreference> toSave = filterPreferences.stream().collect(Collectors.toMap(NotificationFilterPreference::getFilterPreferenceName, Function.identity()));
// Usual XWiki objects
XWikiContext context = contextProvider.get();
XWiki xwiki = context.getWiki();
final DocumentReference notificationFilterPreferenceClass = NOTIFICATION_FILTER_PREFERENCE_CLASS.setWikiReference(user.getWikiReference());
try {
XWikiDocument doc = xwiki.getDocument(user, context);
// Update existing objects if they match the filter preferences to save
updateExistingObjects(toSave, notificationFilterPreferenceClass, doc);
// Create objects from the remaining filter preferences to save
createNewObjects(toSave, notificationFilterPreferenceClass, doc, context);
// Make this change a minor edit so it's not displayed, by default, in notifications
xwiki.saveDocument(doc, "Save notification filter preferences.", true, context);
} catch (Exception e) {
throw new NotificationException(String.format("Failed to save the notification preferences scope for the user [%s].", user), e);
}
}
use of org.xwiki.notifications.filters.NotificationFilterPreference in project xwiki-platform by xwiki.
the class DefaultWatchedEntitiesManager method handleEntity.
private void handleEntity(WatchedEntityReference entity, DocumentReference user, boolean shouldBeWatched) throws NotificationException {
if (entityIsAlreadyInDesiredState(entity, user, shouldBeWatched)) {
return;
}
// If the notifications for the XWiki app (create, update, delete, addComment) are not enabled but autowatch is
// on, then we need to enable the notifications in the user preferences.
// We do that because it has no sense for a user to use the "AutoWatch" feature when the notifications are not
// enabled.
// Moreover, it makes the notifications feature discoverable. It means that, by default, all pages where the
// user has made a contribution will generate notifications. That's probably what users expect from a
// notification area.
xwikiEventTypesEnabler.ensureXWikiNotificationsAreEnabled();
Iterator<NotificationFilterPreference> filterPreferences = getAllEventsFilterPreferences(user).iterator();
boolean thereIsAMatch = false;
// Look if an existing filter match the entity
while (filterPreferences.hasNext()) {
NotificationFilterPreference notificationFilterPreference = filterPreferences.next();
if (entity.matchExactly(notificationFilterPreference)) {
thereIsAMatch = true;
if (notificationFilterPreference.getFilterType() == NotificationFilterType.INCLUSIVE && notificationFilterPreference.isEnabled() != shouldBeWatched) {
enableOrDeleteFilter(shouldBeWatched, notificationFilterPreference);
} else if (notificationFilterPreference.getFilterType() == NotificationFilterType.EXCLUSIVE && notificationFilterPreference.isEnabled() == shouldBeWatched) {
enableOrDeleteFilter(!shouldBeWatched, notificationFilterPreference);
}
}
}
// But it might been still unwatched because of an other filter!
if (!thereIsAMatch || entity.isWatched(user) != shouldBeWatched) {
notificationFilterManager.saveFilterPreferences(Sets.newHashSet(createFilterPreference(entity, shouldBeWatched)));
}
}
use of org.xwiki.notifications.filters.NotificationFilterPreference in project xwiki-platform by xwiki.
the class DefaultWatchedEntitiesManagerTest method unwatchWhen2OppositeFilters.
@Test
public void unwatchWhen2OppositeFilters() throws Exception {
// Mocks
WatchedEntityReference watchedEntityReference = mock(WatchedEntityReference.class);
DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
// Filters
NotificationFilterPreference pref1 = mock(NotificationFilterPreference.class);
when(pref1.getProperties(NotificationFilterProperty.EVENT_TYPE)).thenReturn(Collections.emptyList());
when(pref1.getFilterFormats()).thenReturn(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
when(watchedEntityReference.matchExactly(pref1)).thenReturn(true);
when(pref1.getFilterType()).thenReturn(NotificationFilterType.INCLUSIVE);
when(pref1.isEnabled()).thenReturn(true);
when(pref1.getFilterPreferenceName()).thenReturn("pref1");
NotificationFilterPreference pref2 = mock(NotificationFilterPreference.class);
when(pref2.getProperties(NotificationFilterProperty.EVENT_TYPE)).thenReturn(Collections.emptyList());
when(pref2.getFilterFormats()).thenReturn(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
when(watchedEntityReference.matchExactly(pref2)).thenReturn(true);
when(pref2.getFilterType()).thenReturn(NotificationFilterType.EXCLUSIVE);
when(pref2.isEnabled()).thenReturn(true);
when(pref2.getFilterPreferenceName()).thenReturn("pref2");
when(notificationFilterManager.getFilterPreferences(user)).thenReturn(Sets.newSet(pref1, pref2));
when(watchedEntityReference.isWatched(user)).thenReturn(true, false);
// Test
mocker.getComponentUnderTest().unwatchEntity(watchedEntityReference, user);
// Checks
verify(notificationFilterManager).deleteFilterPreference("pref1");
verify(watchedEntityReference, never()).createExclusiveFilterPreference();
}
use of org.xwiki.notifications.filters.NotificationFilterPreference in project xwiki-platform by xwiki.
the class DefaultWatchedEntitiesManagerTest method unwatchWhenExclusiveFilter.
@Test
public void unwatchWhenExclusiveFilter() throws Exception {
// Mocks
WatchedEntityReference watchedEntityReference = mock(WatchedEntityReference.class);
DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
// Filters
NotificationFilterPreference pref1 = mock(NotificationFilterPreference.class);
when(pref1.getProperties(NotificationFilterProperty.EVENT_TYPE)).thenReturn(Collections.emptyList());
when(pref1.getFilterFormats()).thenReturn(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
when(watchedEntityReference.matchExactly(pref1)).thenReturn(true);
when(pref1.getFilterType()).thenReturn(NotificationFilterType.EXCLUSIVE);
when(pref1.isEnabled()).thenReturn(false);
when(pref1.getFilterPreferenceName()).thenReturn("pref1");
when(notificationFilterManager.getFilterPreferences(user)).thenReturn(Sets.newSet(pref1));
when(watchedEntityReference.isWatched(user)).thenReturn(true, false);
// Test
mocker.getComponentUnderTest().unwatchEntity(watchedEntityReference, user);
// Checks
verify(notificationFilterManager).setFilterPreferenceEnabled("pref1", true);
verify(watchedEntityReference, never()).createExclusiveFilterPreference();
}
Aggregations