Search in sources :

Example 1 with NotificationFilter

use of org.xwiki.notifications.filters.NotificationFilter in project xwiki-platform by xwiki.

the class QueryGenerator method handleEventPreferences.

/**
 * For each notification preference of the given user, add a constraint on the events to
 * - have one of the notification types that have been subscribed by the user;
 * - have a date superior to the start date corresponding to this type;
 * - match the custom defined user filters.
 *
 * @param user the current user
 * @param preferences a list of the user preferences
 * @return a list of maps that contains query parameters
 * @throws NotificationException if an error occurred
 */
private AbstractOperatorNode handleEventPreferences(DocumentReference user, List<NotificationPreference> preferences) throws NotificationException {
    AbstractOperatorNode preferencesNode = null;
    // Filter the notification preferences that are not bound to a specific EVENT_TYPE
    Iterator<NotificationPreference> it = preferences.stream().filter(pref -> pref.getProperties().containsKey(NotificationPreferenceProperty.EVENT_TYPE)).iterator();
    while (it.hasNext()) {
        NotificationPreference preference = it.next();
        AbstractOperatorNode preferenceTypeNode = new AndNode(new EqualsNode(value(EventProperty.TYPE), value((String) preference.getProperties().get(NotificationPreferenceProperty.EVENT_TYPE))), new GreaterThanNode(value(EventProperty.DATE), value(preference.getStartDate())));
        // Get the notification filters that can be applied to the current preference
        Collection<NotificationFilter> filters = notificationFilterManager.getFilters(user, preference);
        for (NotificationFilter filter : filters) {
            ExpressionNode node = filter.filterExpression(user, preference);
            if (node != null && node instanceof AbstractOperatorNode) {
                preferenceTypeNode = preferenceTypeNode.and((AbstractOperatorNode) node);
            }
        }
        if (preferencesNode == null) {
            preferencesNode = preferenceTypeNode;
        } else {
            preferencesNode = preferencesNode.or(preferenceTypeNode);
        }
    }
    return preferencesNode;
}
Also used : StringValueNode(org.xwiki.notifications.filters.expression.StringValueNode) BooleanValueNode(org.xwiki.notifications.filters.expression.BooleanValueNode) QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) Date(java.util.Date) Component(org.xwiki.component.annotation.Component) NotificationFilterManager(org.xwiki.notifications.filters.NotificationFilterManager) GreaterThanNode(org.xwiki.notifications.filters.expression.GreaterThanNode) NotificationFilterType(org.xwiki.notifications.filters.NotificationFilterType) NotificationFormat(org.xwiki.notifications.NotificationFormat) Singleton(javax.inject.Singleton) ArrayList(java.util.ArrayList) NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) Inject(javax.inject.Inject) Map(java.util.Map) ExpressionNode(org.xwiki.notifications.filters.expression.ExpressionNode) Named(javax.inject.Named) NotificationException(org.xwiki.notifications.NotificationException) AbstractOperatorNode(org.xwiki.notifications.filters.expression.generics.AbstractOperatorNode) NotificationFilter(org.xwiki.notifications.filters.NotificationFilter) InNode(org.xwiki.notifications.filters.expression.InNode) Iterator(java.util.Iterator) EventProperty(org.xwiki.notifications.filters.expression.EventProperty) NotNode(org.xwiki.notifications.filters.expression.NotNode) Collection(java.util.Collection) NotificationPreferenceManager(org.xwiki.notifications.preferences.NotificationPreferenceManager) LesserThanNode(org.xwiki.notifications.filters.expression.LesserThanNode) AndNode(org.xwiki.notifications.filters.expression.AndNode) QueryManager(org.xwiki.query.QueryManager) EqualsNode(org.xwiki.notifications.filters.expression.EqualsNode) NotificationPreferenceProperty(org.xwiki.notifications.preferences.NotificationPreferenceProperty) ExpressionBuilder.value(org.xwiki.notifications.filters.expression.generics.ExpressionBuilder.value) List(java.util.List) PropertyValueNode(org.xwiki.notifications.filters.expression.PropertyValueNode) NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) AbstractValueNode(org.xwiki.notifications.filters.expression.generics.AbstractValueNode) DocumentReference(org.xwiki.model.reference.DocumentReference) NotEqualsNode(org.xwiki.notifications.filters.expression.NotEqualsNode) ConfigurationSource(org.xwiki.configuration.ConfigurationSource) DateValueNode(org.xwiki.notifications.filters.expression.DateValueNode) EntityReferenceNode(org.xwiki.notifications.filters.expression.EntityReferenceNode) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) AbstractOperatorNode(org.xwiki.notifications.filters.expression.generics.AbstractOperatorNode) ExpressionNode(org.xwiki.notifications.filters.expression.ExpressionNode) AndNode(org.xwiki.notifications.filters.expression.AndNode) EqualsNode(org.xwiki.notifications.filters.expression.EqualsNode) NotEqualsNode(org.xwiki.notifications.filters.expression.NotEqualsNode) GreaterThanNode(org.xwiki.notifications.filters.expression.GreaterThanNode) NotificationFilter(org.xwiki.notifications.filters.NotificationFilter)

Example 2 with NotificationFilter

use of org.xwiki.notifications.filters.NotificationFilter in project xwiki-platform by xwiki.

the class QueryGeneratorTest method generateQueryWithNoRelevantFilters.

@Test
public void generateQueryWithNoRelevantFilters() throws Exception {
    // Mocks
    NotificationFilter notificationFilter1 = mock(NotificationFilter.class);
    when(notificationFilterManager.getFilters(any(DocumentReference.class), any(NotificationPreference.class))).thenReturn(Collections.singleton(notificationFilter1));
    when(notificationFilter1.filterExpression(any(DocumentReference.class), any(NotificationPreference.class))).thenReturn(new EmptyNode());
    when(notificationFilter1.matchesPreference(any(NotificationPreference.class))).thenReturn(true);
    // Test
    ExpressionNode node = mocker.getComponentUnderTest().generateQueryExpression(new DocumentReference("xwiki", "XWiki", "UserA"), NotificationFormat.ALERT, null, startDate, Arrays.asList("event1", "event2"));
    assertEquals("(((DATE >= \"Thu Jan 01 01:00:00 CET 1970\" " + "AND (TYPE = \"create\" AND DATE >= \"Fri Jan 02 04:46:40 CET 1970\")) " + "AND NOT (ID IN (\"event1\", \"event2\"))) " + "AND HIDDEN <> true) " + "ORDER BY DATE DESC", node.toString());
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) EmptyNode(org.xwiki.notifications.filters.expression.EmptyNode) ExpressionNode(org.xwiki.notifications.filters.expression.ExpressionNode) NotificationFilter(org.xwiki.notifications.filters.NotificationFilter) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 3 with NotificationFilter

use of org.xwiki.notifications.filters.NotificationFilter in project xwiki-platform by xwiki.

the class QueryGeneratorTest method generateQueryWithFilters.

@Test
public void generateQueryWithFilters() throws Exception {
    // Mocks
    NotificationFilter notificationFilter1 = mock(NotificationFilter.class);
    NotificationFilter notificationFilter2 = mock(NotificationFilter.class);
    when(notificationFilterManager.getFilters(any(DocumentReference.class), any(NotificationPreference.class))).thenReturn(Sets.newSet(notificationFilter1, notificationFilter2));
    when(notificationFilter1.filterExpression(any(DocumentReference.class), any(NotificationPreference.class))).thenReturn(new AndNode(new EqualsNode(new PropertyValueNode(EventProperty.PAGE), new StringValueNode("someValue1")), new EqualsNode(new StringValueNode("1"), new StringValueNode("1"))));
    when(notificationFilter2.filterExpression(any(DocumentReference.class), any(NotificationPreference.class))).thenReturn(new AndNode(new EqualsNode(new PropertyValueNode(EventProperty.TYPE), new StringValueNode("someValue2")), new EqualsNode(new StringValueNode("2"), new StringValueNode("2"))));
    when(notificationFilter1.matchesPreference(any(NotificationPreference.class))).thenReturn(true);
    when(notificationFilter2.matchesPreference(any(NotificationPreference.class))).thenReturn(true);
    // Test
    ExpressionNode node = mocker.getComponentUnderTest().generateQueryExpression(new DocumentReference("xwiki", "XWiki", "UserA"), NotificationFormat.ALERT, null, startDate, Arrays.asList("event1", "event2"));
    assertEquals("(((DATE >= \"Thu Jan 01 01:00:00 CET 1970\" " + "AND (((TYPE = \"create\" AND DATE >= \"Fri Jan 02 04:46:40 CET 1970\") " + "AND (PAGE = \"someValue1\" AND \"1\" = \"1\")) " + "AND (TYPE = \"someValue2\" AND \"2\" = \"2\"))) " + "AND NOT (ID IN (\"event1\", \"event2\"))) " + "AND HIDDEN <> true) " + "ORDER BY DATE DESC", node.toString());
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) ExpressionNode(org.xwiki.notifications.filters.expression.ExpressionNode) StringValueNode(org.xwiki.notifications.filters.expression.StringValueNode) AndNode(org.xwiki.notifications.filters.expression.AndNode) EqualsNode(org.xwiki.notifications.filters.expression.EqualsNode) PropertyValueNode(org.xwiki.notifications.filters.expression.PropertyValueNode) NotificationFilter(org.xwiki.notifications.filters.NotificationFilter) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 4 with NotificationFilter

use of org.xwiki.notifications.filters.NotificationFilter in project xwiki-platform by xwiki.

the class DefaultNotificationManager method filterEvent.

private boolean filterEvent(Event event, Parameters parameters) throws NotificationException {
    // Don't record events that have a target that don't include the current user
    if (!event.getTarget().isEmpty() && !event.getTarget().contains(parameters.userId)) {
        return true;
    }
    List<NotificationFilter> filters = new ArrayList<>(notificationFilterManager.getAllFilters(parameters.userReference));
    Collections.sort(filters);
    for (NotificationFilter filter : filters) {
        NotificationFilter.FilterPolicy policy = filter.filterEvent(event, parameters.userReference, parameters.format);
        switch(policy) {
            case FILTER:
                return true;
            case KEEP:
                return false;
            default:
        }
    }
    return false;
}
Also used : ArrayList(java.util.ArrayList) NotificationFilter(org.xwiki.notifications.filters.NotificationFilter)

Example 5 with NotificationFilter

use of org.xwiki.notifications.filters.NotificationFilter in project xwiki-platform by xwiki.

the class DefaultNotificationFilterManager method fetchAllFilters.

/**
 * Fetches every filter available to the user, without taking care of whether the filter is disabled by the user
 * or not.
 *
 * @param user the user to use
 * @return a set of filters
 * @throws NotificationException if an error occurs
 */
private Set<NotificationFilter> fetchAllFilters(DocumentReference user) throws NotificationException {
    // If the user is from the main wiki, get filters from all wikis
    if (user.getWikiReference().getName().equals(wikiDescriptorManager.getMainWikiId())) {
        String currentWikiId = wikiDescriptorManager.getCurrentWikiId();
        Map<String, NotificationFilter> filters = new HashMap<>();
        try {
            for (String wikiId : wikiDescriptorManager.getAllIds()) {
                modelContext.setCurrentEntityReference(new WikiReference(wikiId));
                filters.putAll(componentManager.getInstanceMap(NotificationFilter.class));
            }
        } catch (Exception e) {
            throw new NotificationException(ERROR_MESSAGE, e);
        } finally {
            modelContext.setCurrentEntityReference(new WikiReference(currentWikiId));
        }
        return new HashSet<>(filters.values());
    } else {
        // If the user is local, get filters from the current wiki only (we assume it's the wiki of the user).
        try {
            return new HashSet<>(componentManager.getInstanceList(NotificationFilter.class));
        } catch (Exception e) {
            throw new NotificationException(ERROR_MESSAGE, e);
        }
    }
}
Also used : HashMap(java.util.HashMap) NotificationException(org.xwiki.notifications.NotificationException) WikiReference(org.xwiki.model.reference.WikiReference) NotificationFilter(org.xwiki.notifications.filters.NotificationFilter) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) NotificationException(org.xwiki.notifications.NotificationException) HashSet(java.util.HashSet)

Aggregations

NotificationFilter (org.xwiki.notifications.filters.NotificationFilter)18 Test (org.junit.Test)10 NotificationFilterPreference (org.xwiki.notifications.filters.NotificationFilterPreference)5 NotificationPreference (org.xwiki.notifications.preferences.NotificationPreference)5 DocumentReference (org.xwiki.model.reference.DocumentReference)4 ArrayList (java.util.ArrayList)3 NotificationException (org.xwiki.notifications.NotificationException)3 ExpressionNode (org.xwiki.notifications.filters.expression.ExpressionNode)3 HashMap (java.util.HashMap)2 AndNode (org.xwiki.notifications.filters.expression.AndNode)2 EqualsNode (org.xwiki.notifications.filters.expression.EqualsNode)2 PropertyValueNode (org.xwiki.notifications.filters.expression.PropertyValueNode)2 StringValueNode (org.xwiki.notifications.filters.expression.StringValueNode)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 Collection (java.util.Collection)1 Date (java.util.Date)1