Search in sources :

Example 1 with WatchListEvent

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";
}
Also used : XWiki(com.xpn.xwiki.XWiki) XWikiContext(com.xpn.xwiki.XWikiContext) WatchListEventMatcher(org.xwiki.watchlist.internal.WatchListEventMatcher) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) WatchListEvent(org.xwiki.watchlist.internal.api.WatchListEvent) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Example 2 with WatchListEvent

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;
}
Also used : WatchListEvent(org.xwiki.watchlist.internal.api.WatchListEvent) ActivityEvent(com.xpn.xwiki.plugin.activitystream.api.ActivityEvent) ActivityStream(com.xpn.xwiki.plugin.activitystream.api.ActivityStream) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) ActivityStreamPlugin(com.xpn.xwiki.plugin.activitystream.plugin.ActivityStreamPlugin)

Example 3 with WatchListEvent

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);
    }
}
Also used : WatchListEvent(org.xwiki.watchlist.internal.api.WatchListEvent) HashMap(java.util.HashMap) BaseObject(com.xpn.xwiki.objects.BaseObject) WatchListEventMatcher(org.xwiki.watchlist.internal.WatchListEventMatcher) Date(java.util.Date) XWikiException(com.xpn.xwiki.XWikiException) JobExecutionException(org.quartz.JobExecutionException)

Example 4 with WatchListEvent

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);
    }
}
Also used : WatchListEvent(org.xwiki.watchlist.internal.api.WatchListEvent) ArrayList(java.util.ArrayList) Attachment(com.xpn.xwiki.api.Attachment) Map(java.util.Map)

Example 5 with WatchListEvent

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;
}
Also used : WatchListEvent(org.xwiki.watchlist.internal.api.WatchListEvent) ArrayList(java.util.ArrayList) Attachment(com.xpn.xwiki.api.Attachment) DocumentReference(org.xwiki.model.reference.DocumentReference) HashSet(java.util.HashSet)

Aggregations

WatchListEvent (org.xwiki.watchlist.internal.api.WatchListEvent)11 DocumentReference (org.xwiki.model.reference.DocumentReference)6 ArrayList (java.util.ArrayList)4 XWikiContext (com.xpn.xwiki.XWikiContext)3 Date (java.util.Date)3 Attachment (com.xpn.xwiki.api.Attachment)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 BaseObject (com.xpn.xwiki.objects.BaseObject)2 ActivityEvent (com.xpn.xwiki.plugin.activitystream.api.ActivityEvent)2 HashMap (java.util.HashMap)2 WikiReference (org.xwiki.model.reference.WikiReference)2 WatchListEventMatcher (org.xwiki.watchlist.internal.WatchListEventMatcher)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiException (com.xpn.xwiki.XWikiException)1 ActivityStream (com.xpn.xwiki.plugin.activitystream.api.ActivityStream)1 ActivityEventImpl (com.xpn.xwiki.plugin.activitystream.impl.ActivityEventImpl)1 ActivityStreamPlugin (com.xpn.xwiki.plugin.activitystream.plugin.ActivityStreamPlugin)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Address (javax.mail.Address)1