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);
}
}
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());
}
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;
}
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();
}
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());
}
Aggregations