use of org.xwiki.watchlist.internal.api.WatchListEvent in project xwiki-platform by xwiki.
the class WatchListMessageDataExtractorTest method setup.
@Before
public void setup() throws Exception {
events = new ArrayList<>();
subscribers = new ArrayList<>();
EventsAndSubscribersSource source = new EventsAndSubscribersSource(events, subscribers);
parameters = new HashMap<>();
factoryParameters = new HashMap<>();
parameters.put(WatchListEventMimeMessageFactory.PARAMETERS_PARAMETER, factoryParameters);
mockEventMatcher = mocker.registerMockComponent(WatchListEventMatcher.class);
mockExecution = mocker.registerMockComponent(Execution.class);
mockExplicitDocumentReferenceResolver = mocker.registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "explicit");
extractor = new WatchListMessageDataExtractor(source, parameters, mockEventMatcher, mockExecution, mockExplicitDocumentReferenceResolver);
// Document and object
mockDocument = mock(XWikiDocument.class);
mockUserObject = mock(BaseObject.class);
// Context and wiki
mockWiki = mock(XWiki.class);
mockContext = mock(XWikiContext.class);
when(mockContext.getWiki()).thenReturn(mockWiki);
mockExecutionContext = mocker.registerMockComponent(ExecutionContext.class);
when(mockExecution.getContext()).thenReturn(mockExecutionContext);
when(mockExecutionContext.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY)).thenReturn(mockContext);
// Subscriber reference value
testSubscriberReference = new DocumentReference("wiki", "XWiki", "User");
testSubscriberStringReference = "wiki:XWiki.User";
when(mockDocument.getPrefixedFullName()).thenReturn(testSubscriberStringReference);
// Matched events
testMatchingEvents = new ArrayList<>();
WatchListEvent matchingEvent = mock(WatchListEvent.class);
testMatchingEvents.add(matchingEvent);
when(mockEventMatcher.getMatchingVisibleEvents(events, testSubscriberStringReference)).thenReturn(testMatchingEvents);
// User object field values
testFirstName = "U";
testLastName = "ser";
testEmail = "e@ma.il";
}
use of org.xwiki.watchlist.internal.api.WatchListEvent in project xwiki-platform by xwiki.
the class DefaultWatchListEventMatcher method getEventsSince.
@Override
public List<WatchListEvent> getEventsSince(Date start) {
List<WatchListEvent> events = new ArrayList<>();
XWikiContext context = getXWikiContext();
ActivityStream actStream = ((ActivityStreamPlugin) context.getWiki().getPlugin(ActivityStreamPlugin.PLUGIN_NAME, context)).getActivityStream();
List<Object> parameters = new ArrayList<Object>();
parameters.add(start);
try {
// FIXME: Watch out for memory usage here, since the list of events could be huge in some cases.
List<ActivityEvent> rawEvents = actStream.searchEvents("act.date > ? and act.type in ('" + StringUtils.join(MATCHING_EVENT_TYPES, "','") + "')", false, true, 0, 0, parameters, context);
// WatchListEvent#equals(WatchListEvent).
for (ActivityEvent rawEvent : rawEvents) {
WatchListEvent event = this.eventConverter.convert(rawEvent);
int existingIndex = events.indexOf(event);
if (existingIndex == -1) {
// An event on a new document, add the new event.
events.add(event);
} else {
// An event on an existing document, add to the events of that document.
WatchListEvent existingCompositeEvent = events.get(existingIndex);
existingCompositeEvent.addEvent(event);
}
}
} catch (Exception e) {
logger.error("Failed to retrieve updated documents from activity stream since [{}]", start, e);
}
return events;
}
use of org.xwiki.watchlist.internal.api.WatchListEvent in project xwiki-platform by xwiki.
the class WatchListJob method executeJob.
/**
* Method called from the scheduler.
*
* @param jobContext Context of the request
* @throws JobExecutionException if the job execution fails.
*/
@Override
public void executeJob(JobExecutionContext jobContext) throws JobExecutionException {
try {
// Don't go further if the application is disabled
if (!isWatchListEnabled()) {
return;
}
init(jobContext);
if (this.watchListJobObject == null) {
return;
}
Collection<String> subscribers = getSubscribers();
// Stop here if nobody is interested.
if (!hasSubscribers()) {
return;
}
// Determine what happened since the last execution for everybody.
Date previousFireTime = getPreviousFireTime();
WatchListEventMatcher eventMatcher = Utils.getComponent(WatchListEventMatcher.class);
List<WatchListEvent> events = eventMatcher.getEventsSince(previousFireTime);
setPreviousFireTime();
// Stop here if nothing happened in the meantime.
if (events.size() == 0) {
return;
}
// Notify all the interested subscribers of the events that occurred.
// When processing the events, a subscriber will only be notified of events that interest him.
Map<String, Object> notificationData = new HashMap<>();
notificationData.put(DefaultWatchListNotifier.PREVIOUS_FIRE_TIME_VARIABLE, previousFireTime);
String mailTemplate = this.watchListJobObject.getStringValue(WatchListJobClassDocumentInitializer.TEMPLATE_FIELD);
notificationData.put(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER, mailTemplate);
// Send the notification for processing.
this.watchlist.getNotifier().sendNotification(subscribers, events, notificationData);
} catch (Exception e) {
// We're in a job, we don't throw exceptions
LOGGER.error("Exception while running job", e);
}
}
use of org.xwiki.watchlist.internal.api.WatchListEvent in project xwiki-platform by xwiki.
the class WatchListEventMimeMessageIterator method updateFactoryParameters.
/**
* Update the factory's parameters with the values specific to the message we are going to send right now.
*
* @param watchListMessageData the messageData to use for the new message
* @param factoryParameters the factory parameters we wish to update
*/
private void updateFactoryParameters(Map<String, Object> factoryParameters, WatchListMessageData watchListMessageData) {
Map<String, Object> velocityVariables = (Map<String, Object>) factoryParameters.get("velocityVariables");
// Set the list of events, containing 1 event per document.
List<WatchListEvent> events = watchListMessageData.getEvents();
velocityVariables.put("events", events);
// Compute the list of modified documents.
List<String> modifiedDocuments = new ArrayList<>();
for (WatchListEvent event : events) {
if (!modifiedDocuments.contains(event.getPrefixedFullName())) {
modifiedDocuments.add(event.getPrefixedFullName());
}
}
velocityVariables.put("modifiedDocuments", modifiedDocuments);
velocityVariables.put(XWIKI_USER_CLASS_FIRST_NAME_PROP, watchListMessageData.getFirstName());
velocityVariables.put(XWIKI_USER_CLASS_LAST_NAME_PROP, watchListMessageData.getLastName());
velocityVariables.put(SUBSCRIBER_REFERENCE, watchListMessageData.getUserReference());
// Attach the avatars of the authors of the events we are notifying about.
if (parameters.get(WatchListEventMimeMessageFactory.ATTACH_AUTHOR_AVATARS_PARAMETER) == Boolean.TRUE) {
List<Attachment> templateExtraAttachments = getTemplateExtraAttachments(factoryParameters, events);
factoryParameters.put(TEMPLATE_FACTORY_ATTACHMENTS_PARAMETER, templateExtraAttachments);
}
}
use of org.xwiki.watchlist.internal.api.WatchListEvent in project xwiki-platform by xwiki.
the class WatchListEventMimeMessageIterator method getTemplateExtraAttachments.
private List<Attachment> getTemplateExtraAttachments(Map<String, Object> factoryParameters, List<WatchListEvent> events) {
// Append to any existing list of extra attachments specified by the caller.
List<Attachment> templateExtraAttachments = new ArrayList<>();
if (originalTemplateExtraParameters != null) {
templateExtraAttachments.addAll(originalTemplateExtraParameters);
}
Set<DocumentReference> processedAuthors = new HashSet<DocumentReference>();
for (WatchListEvent event : events) {
for (DocumentReference authorReference : event.getAuthorReferences()) {
// instead since that would also work across messages and would be much more useful.
if (!processedAuthors.contains(authorReference)) {
Attachment avatarAttachment = avatarExtractor.getUserAvatar(authorReference);
if (avatarAttachment != null) {
templateExtraAttachments.add(avatarAttachment);
}
processedAuthors.add(authorReference);
}
}
}
return templateExtraAttachments;
}
Aggregations