Search in sources :

Example 21 with NotificationPreference

use of org.xwiki.notifications.preferences.NotificationPreference in project xwiki-platform by xwiki.

the class XWikiEventTypesEnabler method ensureXWikiNotificationsAreEnabled.

/**
 * Make sure that notification are enabled for at least one XWiki Event Types (create, update, delete,
 * addComment) for the current user.
 * @throws NotificationException if an error occurs
 */
public void ensureXWikiNotificationsAreEnabled() throws NotificationException {
    if (isNotificationDisabled()) {
        TargetableNotificationPreferenceBuilder builder = targetableNotificationPreferenceBuilderProvider.get();
        List<NotificationPreference> preferencesToCreate = new ArrayList<>();
        Date now = new Date();
        for (String eventType : XWIKI_EVENT_TYPES) {
            for (NotificationFormat format : NotificationFormat.values()) {
                preferencesToCreate.add(createNotificationPreference(builder, eventType, format, now));
            }
        }
        notificationPreferenceManager.savePreferences(preferencesToCreate);
    }
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) TargetableNotificationPreference(org.xwiki.notifications.preferences.TargetableNotificationPreference) TargetableNotificationPreferenceBuilder(org.xwiki.notifications.preferences.TargetableNotificationPreferenceBuilder) NotificationFormat(org.xwiki.notifications.NotificationFormat) ArrayList(java.util.ArrayList) Date(java.util.Date)

Example 22 with NotificationPreference

use of org.xwiki.notifications.preferences.NotificationPreference in project xwiki-platform by xwiki.

the class NotificationPreferenceScriptService method isEventTypeEnabled.

/**
 * @param eventType an event type
 * @param format a notification format
 * @param wiki id of the wiki
 * @return either or not the given event type is enabled by default on the given wiki in the given format
 * @throws NotificationException if an error happens
 * @throws AccessDeniedException if the current user has not the admin right on the wiki
 */
public boolean isEventTypeEnabled(String eventType, NotificationFormat format, String wiki) throws NotificationException, AccessDeniedException {
    WikiReference wikiReference = new WikiReference(wiki);
    authorizationManager.checkAccess(Right.ADMIN, wikiReference);
    for (NotificationPreference preference : notificationPreferenceManager.getAllPreferences(wikiReference)) {
        Object prefEventType = preference.getProperties().get(NotificationPreferenceProperty.EVENT_TYPE);
        if (prefEventType != null && StringUtils.equals((String) prefEventType, eventType) && preference.getFormat() == format) {
            return preference.isNotificationEnabled();
        }
    }
    return false;
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) TargetableNotificationPreference(org.xwiki.notifications.preferences.TargetableNotificationPreference) WikiReference(org.xwiki.model.reference.WikiReference)

Example 23 with NotificationPreference

use of org.xwiki.notifications.preferences.NotificationPreference in project xwiki-platform by xwiki.

the class NotificationPreferenceScriptService method saveNotificationPreferences.

private void saveNotificationPreferences(String json, String providerHint, EntityReference target, NotificationPreferenceCategory category) throws NotificationException {
    /*
            The JSON we get is a "snapshot" of the states of the buttons the user has in front of her eyes when she is
            managing her preferences.

            We did that so we can save several preferences in the same time without making too much requests (for
            example when the user enable an application, it actually enable all the app's event types).

            However, this snapshot also "freeze" the default preferences, ie the preferences set at the wiki level and
            that the user has not changed.

            Example:
            1. Wiki Administrator enables the application A by default, and doesn't touch the settings for the
               application B.
            2. John Doe disables the application A on HIS profile. He doesn't touch the setting for the application B.
               A "snapshot" of his preferences is saved.
            3. Wiki Administrator enables the application B by default.
            4. On the John Doe's preferences, application B is still disabled, because of the snapshot done on step 2.

            I don't think this situation is good. If a user did not EXPLICITLY change a setting, the default settings
            should be applied.

            For this reason, this code will only save the settings THAT ARE DIFFERENT FROM THE DEFAULT (INHERITED).

            Since this logic is totally related to the way the UI is built, I think it does not deserve its own
            component or API. In a way, it should even be internal.
        */
    List<NotificationPreference> existingPreferences = Collections.emptyList();
    if (target instanceof DocumentReference) {
        existingPreferences = notificationPreferenceManager.getAllPreferences((DocumentReference) target);
    } else if (target instanceof WikiReference) {
        existingPreferences = notificationPreferenceManager.getAllPreferences((WikiReference) target);
    }
    // Instantiate a new copy of TargetableNotificationPreferenceBuilder because this component is not thread-safe.
    TargetableNotificationPreferenceBuilder targetableNotificationPreferenceBuilder = targetableNotificationPreferenceBuilderProvider.get();
    List<NotificationPreference> toSave = new ArrayList<>();
    try {
        ObjectMapper objectMapper = new ObjectMapper();
        List<Map<String, Object>> preferences = objectMapper.reader().forType(List.class).readValue(json);
        for (Map<String, Object> item : preferences) {
            String eventType = (String) item.get("eventType");
            NotificationFormat format = NotificationFormat.valueOf(((String) item.get("format")).toUpperCase());
            boolean enabled = (Boolean) item.get("enabled");
            targetableNotificationPreferenceBuilder.prepare();
            targetableNotificationPreferenceBuilder.setEnabled(enabled);
            targetableNotificationPreferenceBuilder.setFormat(format);
            targetableNotificationPreferenceBuilder.setProviderHint(providerHint);
            targetableNotificationPreferenceBuilder.setProperties(Collections.singletonMap(NotificationPreferenceProperty.EVENT_TYPE, eventType));
            targetableNotificationPreferenceBuilder.setTarget(target);
            targetableNotificationPreferenceBuilder.setCategory(category);
            TargetableNotificationPreference newPreference = targetableNotificationPreferenceBuilder.build();
            // This part is explained by the long comment below
            NotificationPreference correspondingPreference = getCorrespondingPreference(existingPreferences, newPreference);
            if (correspondingPreference == null || correspondingPreference.isNotificationEnabled() != newPreference.isNotificationEnabled()) {
                toSave.add(newPreference);
            }
        }
        notificationPreferenceManager.savePreferences(toSave);
    } catch (Exception e) {
        throw new NotificationException("Failed to save preferences for notifications given as JSON.", e);
    }
}
Also used : TargetableNotificationPreferenceBuilder(org.xwiki.notifications.preferences.TargetableNotificationPreferenceBuilder) ArrayList(java.util.ArrayList) NotificationException(org.xwiki.notifications.NotificationException) NotificationException(org.xwiki.notifications.NotificationException) AccessDeniedException(org.xwiki.security.authorization.AccessDeniedException) NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) TargetableNotificationPreference(org.xwiki.notifications.preferences.TargetableNotificationPreference) NotificationFormat(org.xwiki.notifications.NotificationFormat) ArrayList(java.util.ArrayList) List(java.util.List) WikiReference(org.xwiki.model.reference.WikiReference) TargetableNotificationPreference(org.xwiki.notifications.preferences.TargetableNotificationPreference) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 24 with NotificationPreference

use of org.xwiki.notifications.preferences.NotificationPreference in project xwiki-platform by xwiki.

the class AbstractNotificationPreferenceTest method testPublicGetters.

@Test
public void testPublicGetters() throws Exception {
    Date testDate = new Date();
    NotificationPreference testPreference = new NotificationPreferenceImplementation(true, NotificationFormat.ALERT, NotificationPreferenceCategory.SYSTEM, testDate, "userProfile", Collections.singletonMap(NotificationPreferenceProperty.EVENT_TYPE, "eventType"));
    assertEquals("eventType", testPreference.getProperties().get(NotificationPreferenceProperty.EVENT_TYPE));
    assertEquals(NotificationFormat.ALERT, testPreference.getFormat());
    assertEquals(UserProfileNotificationPreferenceProvider.NAME, testPreference.getProviderHint());
    assertEquals(testDate, testPreference.getStartDate());
    assertEquals("userProfile", testPreference.getProviderHint());
    assertEquals(NotificationPreferenceCategory.SYSTEM, testPreference.getCategory());
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) Date(java.util.Date) Test(org.junit.Test)

Example 25 with NotificationPreference

use of org.xwiki.notifications.preferences.NotificationPreference in project xwiki-platform by xwiki.

the class DefaultNotificationPreferenceManagerTest method getNotificationPreferencesWithAdditionalParameters.

@Test
public void getNotificationPreferencesWithAdditionalParameters() throws Exception {
    DocumentReference user = new DocumentReference("xwiki", "test", "user");
    mockPreferenceProviders(user);
    List<NotificationPreference> preferences = mocker.getComponentUnderTest().getPreferences(user, true, NotificationFormat.ALERT);
    assertEquals(1, preferences.size());
    assertTrue(preferences.contains(mockPreference11));
    preferences = mocker.getComponentUnderTest().getPreferences(user, false, NotificationFormat.ALERT);
    assertEquals(1, preferences.size());
    assertTrue(preferences.contains(mockPreference21));
    preferences = mocker.getComponentUnderTest().getPreferences(user, true, NotificationFormat.EMAIL);
    assertEquals(0, preferences.size());
    preferences = mocker.getComponentUnderTest().getPreferences(user, false, NotificationFormat.EMAIL);
    assertEquals(2, preferences.size());
    assertTrue(preferences.contains(mockPreference22));
    assertTrue(preferences.contains(mockPreference23));
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

NotificationPreference (org.xwiki.notifications.preferences.NotificationPreference)32 Test (org.junit.Test)18 DocumentReference (org.xwiki.model.reference.DocumentReference)12 HashMap (java.util.HashMap)8 List (java.util.List)8 NotificationPreferenceProperty (org.xwiki.notifications.preferences.NotificationPreferenceProperty)8 Date (java.util.Date)7 ArrayList (java.util.ArrayList)6 WikiReference (org.xwiki.model.reference.WikiReference)5 NotificationException (org.xwiki.notifications.NotificationException)4 NotificationFormat (org.xwiki.notifications.NotificationFormat)4 NotificationFilterPreference (org.xwiki.notifications.filters.NotificationFilterPreference)4 TargetableNotificationPreference (org.xwiki.notifications.preferences.TargetableNotificationPreference)4 Map (java.util.Map)3 NotificationFilter (org.xwiki.notifications.filters.NotificationFilter)3 NotificationPreferenceManager (org.xwiki.notifications.preferences.NotificationPreferenceManager)3 Query (org.xwiki.query.Query)3 XWiki (com.xpn.xwiki.XWiki)2 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiException (com.xpn.xwiki.XWikiException)2