Search in sources :

Example 11 with NotificationFilterPreference

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;
}
Also used : NotificationFilterProperty(org.xwiki.notifications.filters.NotificationFilterProperty) NotificationException(org.xwiki.notifications.NotificationException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiException(com.xpn.xwiki.XWikiException) NotificationException(org.xwiki.notifications.NotificationException) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) NotificationFilterType(org.xwiki.notifications.filters.NotificationFilterType) NotificationFormat(org.xwiki.notifications.NotificationFormat) NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) ArrayList(java.util.ArrayList) List(java.util.List) DocumentReference(org.xwiki.model.reference.DocumentReference) HashSet(java.util.HashSet)

Example 12 with NotificationFilterPreference

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);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) NotificationException(org.xwiki.notifications.NotificationException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) NotificationException(org.xwiki.notifications.NotificationException)

Example 13 with NotificationFilterPreference

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)));
    }
}
Also used : NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference)

Example 14 with NotificationFilterPreference

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();
}
Also used : NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) WatchedEntityReference(org.xwiki.notifications.filters.watch.WatchedEntityReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 15 with NotificationFilterPreference

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();
}
Also used : NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) WatchedEntityReference(org.xwiki.notifications.filters.watch.WatchedEntityReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

NotificationFilterPreference (org.xwiki.notifications.filters.NotificationFilterPreference)29 Test (org.junit.Test)14 DocumentReference (org.xwiki.model.reference.DocumentReference)13 WatchedEntityReference (org.xwiki.notifications.filters.watch.WatchedEntityReference)8 BaseObject (com.xpn.xwiki.objects.BaseObject)4 HashSet (java.util.HashSet)4 XWiki (com.xpn.xwiki.XWiki)3 XWikiContext (com.xpn.xwiki.XWikiContext)3 XWikiException (com.xpn.xwiki.XWikiException)3 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)3 HashMap (java.util.HashMap)3 NotificationException (org.xwiki.notifications.NotificationException)3 NotificationPreference (org.xwiki.notifications.preferences.NotificationPreference)3 Set (java.util.Set)2 Event (org.xwiki.eventstream.Event)2 SpaceReference (org.xwiki.model.reference.SpaceReference)2 WikiReference (org.xwiki.model.reference.WikiReference)2 NotificationFilter (org.xwiki.notifications.filters.NotificationFilter)2 NotificationFilterProperty (org.xwiki.notifications.filters.NotificationFilterProperty)2 NotificationPreferenceProperty (org.xwiki.notifications.preferences.NotificationPreferenceProperty)2