use of org.xwiki.watchlist.internal.api.WatchListEvent in project xwiki-platform by xwiki.
the class WatchListMessageDataExtractor method extract.
@Override
public WatchListMessageData extract(DocumentReference subscriberReference, XWikiDocument document, BaseObject userObject) {
WatchListMessageData result = null;
try {
if (skipContextUser && subscriberReference.equals(getXWikiContext().getUserReference())) {
// If the current context user should not be notified of events that apparently interest him, stop.
return null;
}
// Get only the events that the current subscriber is interested in.
List<WatchListEvent> matchingEvents = eventMatcher.getMatchingVisibleEvents(source.getEvents(), document.getPrefixedFullName());
if (matchingEvents.size() == 0) {
// If there are no interesting events, stop.
return null;
}
String firstName = userObject.getStringValue("first_name");
String lastName = userObject.getStringValue("last_name");
Address address = addressExtractor.extract(subscriberReference, document, userObject);
if (address == null || processedAddresses.contains(address)) {
// Make sure we skip users with no email set or emails we have already sent to.
return null;
}
// Remember emails we have already sent to.
processedAddresses.add(address);
DocumentReference templateReference = getTemplateReference(subscriberReference);
result = new WatchListMessageData(subscriberReference, templateReference, firstName, lastName, address, matchingEvents);
} catch (Exception e) {
LOGGER.error("Failed to retrieve information for user [{}]", subscriberReference, e);
}
return result;
}
use of org.xwiki.watchlist.internal.api.WatchListEvent in project xwiki-platform by xwiki.
the class ActivityEventWatchListEventConverterTest method testConversion.
@Test
public void testConversion() throws Exception {
// Input.
ActivityEvent activityEvent = new ActivityEventImpl();
activityEvent.setWiki("xwiki");
activityEvent.setPage("Space1.Space2.Page");
activityEvent.setType("update");
activityEvent.setUser("xwiki:XWiki.SomeUser");
activityEvent.setVersion("1.3");
activityEvent.setDate(new Date());
// Mocks
DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Space1", "Space2"), "Page");
WikiReference wikiReference = new WikiReference(activityEvent.getWiki());
EntityReferenceResolver<String> resolver = mocker.getInstance(EntityReferenceResolver.TYPE_STRING, "explicit");
// Note: cheating a bit, it should return an entityReference instead of documentReference, but we are fine.
when(resolver.resolve(activityEvent.getPage(), EntityType.DOCUMENT, wikiReference)).thenReturn(documentReference);
DocumentReference userReference = new DocumentReference("xwiki", "XWiki", "SomeUser");
when(resolver.resolve(activityEvent.getUser(), EntityType.DOCUMENT, wikiReference)).thenReturn(userReference);
// Convert.
WatchListEvent watchListEvent = mocker.getComponentUnderTest().convert(activityEvent);
// Test the output.
assertEquals(documentReference, watchListEvent.getDocumentReference());
assertEquals(activityEvent.getWiki(), watchListEvent.getDocumentReference().getWikiReference().getName());
assertEquals(activityEvent.getType(), watchListEvent.getType());
assertEquals(userReference, watchListEvent.getAuthorReference());
assertEquals(1, watchListEvent.getAuthorReferences().size());
assertEquals(activityEvent.getVersion(), watchListEvent.getVersion());
assertEquals(1, watchListEvent.getVersions().size());
assertEquals(activityEvent.getDate(), watchListEvent.getDate());
assertEquals(1, watchListEvent.getDates().size());
}
use of org.xwiki.watchlist.internal.api.WatchListEvent in project xwiki-platform by xwiki.
the class DefaultWatchListEventMatcher method getMatchingVisibleEvents.
@Override
public List<WatchListEvent> getMatchingVisibleEvents(List<WatchListEvent> events, String subscriber) {
List<WatchListEvent> result = new ArrayList<WatchListEvent>();
for (WatchListEvent event : events) {
if (isEventSkipped(event)) {
// Skip events that are on a blacklist for various reasons (performance, security, etc.)
continue;
}
if (!isEventViewable(event, subscriber)) {
// Skip events on documents that are not visible to the subscriber.
continue;
}
if (!isEventMatching(event, subscriber)) {
// Skip events that are not interesting to the subscriber.
continue;
}
result.add(event);
}
// Sort the matching events by document.
Collections.sort(result);
return result;
}
use of org.xwiki.watchlist.internal.api.WatchListEvent in project xwiki-platform by xwiki.
the class RealtimeNotificationGenerator method getWatchListEvent.
/**
* @param event the current event
* @param currentDoc the affected document
* @param context the context of the event
* @return the {@link WatchListEvent} to use to notify watchers of the current document
*/
private WatchListEvent getWatchListEvent(Event event, XWikiDocument currentDoc, XWikiContext context) {
String type = null;
DocumentReference documentReference = currentDoc.getDocumentReference();
DocumentReference userReference = context.getUserReference();
String version = currentDoc.getVersion();
Date date = currentDoc.getDate();
if (event instanceof DocumentCreatedEvent) {
type = WatchListEventType.CREATE;
} else if (event instanceof DocumentUpdatedEvent) {
type = WatchListEventType.UPDATE;
} else if (event instanceof DocumentDeletedEvent) {
version = currentDoc.getOriginalDocument().getVersion();
type = WatchListEventType.DELETE;
}
WatchListEvent watchListEvent = new WatchListEvent(documentReference, type, userReference, version, date);
return watchListEvent;
}
use of org.xwiki.watchlist.internal.api.WatchListEvent in project xwiki-platform by xwiki.
the class RealtimeNotificationGenerator method onEvent.
@Override
public void onEvent(Event event, Object source, Object data) {
// Early check if event should be processed.
if (this.remoteObservationManagerContext.isRemoteState() && !this.allowRemote) {
// Don't handle remote events to avoid duplicated processing.
return;
}
XWikiDocument currentDoc = (XWikiDocument) source;
XWikiContext context = (XWikiContext) data;
// Skip evens that are executed in the context of other event, thus not directly generated by a user.
if (observationContext.isIn(AutomaticWatchModeListener.SKIPPED_EVENTS)) {
return;
}
try {
// Get a corresponding watchlist event.
WatchListEvent watchListEvent = getWatchListEvent(event, currentDoc, context);
// Early optimization since this is not related to a user but to the event itself.
if (watchlistEventMatcher.isEventSkipped(watchListEvent)) {
// Stop here if the event is skipped.
return;
}
// Get all the realtime notification subscribers.
Collection<String> subscribers = store.getSubscribers(DefaultWatchListNotificationCache.REALTIME_INTERVAL_ID);
if (subscribers.size() == 0) {
// Stop here if no one is interested.
return;
}
// Build the notification parameters.
Map<String, Object> notificationData = new HashMap<>();
notificationData.put(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER, REALTIME_EMAIL_TEMPLATE);
notificationData.put(WatchListEventMimeMessageFactory.SKIP_CONTEXT_USER_PARAMETER, true);
notificationData.put(WatchListEventMimeMessageFactory.ATTACH_AUTHOR_AVATARS_PARAMETER, true);
// Send the notification for processing.
notifier.sendNotification(subscribers, Arrays.asList(watchListEvent), notificationData);
} catch (Exception e) {
logger.error("Failed to send realtime notification to user [{}]", context.getUserReference(), e);
}
}
Aggregations