Search in sources :

Example 6 with CompositeEvent

use of org.xwiki.notifications.CompositeEvent in project xwiki-platform by xwiki.

the class DefaultNotificationManager method getEvents.

private List<CompositeEvent> getEvents(List<CompositeEvent> results, Parameters parameters) throws NotificationException {
    // Because the user might not be able to see all notifications because of the rights, we take from the database
    // more events than expected and we will filter afterwards.
    final int batchSize = parameters.expectedCount * 2;
    try {
        // Create the query
        Query query = queryGenerator.generateQuery(parameters.userReference, parameters.format, parameters.endDate, parameters.fromDate, parameters.blackList);
        if (query == null) {
            return Collections.emptyList();
        }
        query.setLimit(batchSize);
        // Get a batch of events
        List<Event> batch = eventStream.searchEvents(query);
        // Add to the results the events the user has the right to see
        for (Event event : batch) {
            DocumentReference document = event.getDocument();
            // Don't record events concerning a doc the user cannot see
            if (document != null && !authorizationManager.hasAccess(Right.VIEW, parameters.userReference, document)) {
                continue;
            }
            if (filterEvent(event, parameters)) {
                continue;
            }
            // Record this event
            recordEvent(results, event);
            // If the expected count is reached, stop now
            if (results.size() >= parameters.expectedCount) {
                return results;
            }
        }
        // If we haven't get the expected number of events, perform a new batch
        if (results.size() < parameters.expectedCount && batch.size() == batchSize) {
            parameters.blackList.addAll(getEventsIds(batch));
            getEvents(results, parameters);
        }
        return results;
    } catch (Exception e) {
        throw new NotificationException("Fail to get the list of notifications.", e);
    }
}
Also used : Query(org.xwiki.query.Query) NotificationException(org.xwiki.notifications.NotificationException) CompositeEvent(org.xwiki.notifications.CompositeEvent) Event(org.xwiki.eventstream.Event) DocumentReference(org.xwiki.model.reference.DocumentReference) NotificationException(org.xwiki.notifications.NotificationException)

Example 7 with CompositeEvent

use of org.xwiki.notifications.CompositeEvent in project xwiki-platform by xwiki.

the class DefaultNotificationRSSRendererTest method testCompositeEventRendering.

@Test
public void testCompositeEventRendering() throws Exception {
    CompositeEvent testCompositeEvent = mock(CompositeEvent.class);
    this.mockEvent(testCompositeEvent);
    when(this.scriptContextManager.getCurrentScriptContext()).thenReturn(Mockito.mock(ScriptContext.class));
    SyndEntry resultEntry = this.mocker.getComponentUnderTest().renderNotification(testCompositeEvent);
    assertEquals("TranslatedEventTitle", resultEntry.getTitle());
    assertEquals(1, resultEntry.getAuthors().size());
    assertEquals("id1", resultEntry.getUri());
}
Also used : SyndEntry(com.rometools.rome.feed.synd.SyndEntry) ScriptContext(javax.script.ScriptContext) CompositeEvent(org.xwiki.notifications.CompositeEvent) Test(org.junit.Test)

Example 8 with CompositeEvent

use of org.xwiki.notifications.CompositeEvent in project xwiki-platform by xwiki.

the class DefaultNotificationRSSManager method renderFeed.

@Override
public SyndFeed renderFeed(List<CompositeEvent> events) {
    SyndFeed feed = new SyndFeedImpl();
    // Define the general properties of the rss
    feed.setFeedType("rss_2.0");
    feed.setTitle(this.contextualLocalizationManager.getTranslationPlain("notifications.rss.feedTitle"));
    // Set the RSS feed link to the service generating the feed
    feed.setLink(this.modelBridge.getDocumentURL(new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), Arrays.asList("XWiki", "Notifications", "Code"), "NotificationRSSService"), "get", "outputSyntax=plain"));
    // Set the feed description
    feed.setDescription(this.contextualLocalizationManager.getTranslationPlain("notifications.rss.feedDescription"));
    // Add every given CompositeEvent entry to the rss
    List<SyndEntry> entries = new ArrayList<>();
    for (CompositeEvent event : events) {
        try {
            NotificationRSSRenderer renderer = this.getRenderer(event);
            if (renderer != null) {
                entries.add(renderer.renderNotification(event));
            } else {
                entries.add(defaultNotificationRSSRenderer.renderNotification(event));
            }
        } catch (NotificationException e) {
            this.logger.warn("Unable to render RSS entry for CompositeEvent [{}] : [{}]", event, ExceptionUtils.getRootCauseMessage(e));
        }
    }
    feed.setEntries(entries);
    return feed;
}
Also used : SyndFeed(com.rometools.rome.feed.synd.SyndFeed) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) NotificationRSSRenderer(org.xwiki.notifications.notifiers.rss.NotificationRSSRenderer) ArrayList(java.util.ArrayList) NotificationException(org.xwiki.notifications.NotificationException) SyndFeedImpl(com.rometools.rome.feed.synd.SyndFeedImpl) CompositeEvent(org.xwiki.notifications.CompositeEvent) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 9 with CompositeEvent

use of org.xwiki.notifications.CompositeEvent in project xwiki-platform by xwiki.

the class DefaultLiveMimeMessageIterator method retrieveCompositeEventList.

/**
 * For the given user, we will have to check that the composite event that we have to send matches the user
 * preferences.
 *
 * If, for any reason, one of the events of the original composite event is not meant for the user, we clone
 * the original composite event and remove the incriminated event.
 */
@Override
protected List<CompositeEvent> retrieveCompositeEventList(DocumentReference user) throws NotificationException {
    CompositeEvent resultCompositeEvent = new CompositeEvent(this.compositeEvent);
    if (this.hasCorrespondingNotificationPreference(user, resultCompositeEvent)) {
        // Apply the filters that the user has defined in its notification preferences
        // If one of the events present in the composite event does not match a user filter, remove the event
        List<NotificationFilter> filters = new ArrayList<>(notificationFilterManager.getAllFilters(user));
        Collections.sort(filters);
        Iterator<Event> it = resultCompositeEvent.getEvents().iterator();
        while (it.hasNext()) {
            Event event = it.next();
            if (isEventFiltered(filters, event, user)) {
                it.remove();
            }
        }
        if (resultCompositeEvent.getEvents().size() == 0) {
            return Collections.emptyList();
        }
        return Collections.singletonList(resultCompositeEvent);
    }
    return Collections.emptyList();
}
Also used : ArrayList(java.util.ArrayList) CompositeEvent(org.xwiki.notifications.CompositeEvent) Event(org.xwiki.eventstream.Event) CompositeEvent(org.xwiki.notifications.CompositeEvent) NotificationFilter(org.xwiki.notifications.filters.NotificationFilter)

Example 10 with CompositeEvent

use of org.xwiki.notifications.CompositeEvent in project xwiki-platform by xwiki.

the class DefaultNotificationManagerTest method getEventsWhenNoPreferences.

@Test
public void getEventsWhenNoPreferences() throws Exception {
    NotificationPreference pref1 = mock(NotificationPreference.class);
    when(pref1.getProperties()).thenReturn(Collections.singletonMap(NotificationPreferenceProperty.EVENT_TYPE, "create"));
    when(pref1.isNotificationEnabled()).thenReturn(false);
    when(notificationPreferenceManager.getAllPreferences(userReference)).thenReturn(Arrays.asList(pref1));
    // Test
    List<CompositeEvent> results = mocker.getComponentUnderTest().getEvents("xwiki:XWiki.UserA", 2);
    // Verify
    assertEquals(0, results.size());
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) CompositeEvent(org.xwiki.notifications.CompositeEvent) Test(org.junit.Test)

Aggregations

CompositeEvent (org.xwiki.notifications.CompositeEvent)23 DocumentReference (org.xwiki.model.reference.DocumentReference)15 Event (org.xwiki.eventstream.Event)14 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)5 Date (java.util.Date)3 NotificationException (org.xwiki.notifications.NotificationException)3 SyndEntry (com.rometools.rome.feed.synd.SyndEntry)2 SyndFeed (com.rometools.rome.feed.synd.SyndFeed)1 SyndFeedImpl (com.rometools.rome.feed.synd.SyndFeedImpl)1 Attachment (com.xpn.xwiki.api.Attachment)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 AddressException (javax.mail.internet.AddressException)1 InternetAddress (javax.mail.internet.InternetAddress)1 MimeMessage (javax.mail.internet.MimeMessage)1 ScriptContext (javax.script.ScriptContext)1 HashedMap (org.apache.commons.collections.map.HashedMap)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1