Search in sources :

Example 6 with WatchListEvent

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;
}
Also used : WatchListEvent(org.xwiki.watchlist.internal.api.WatchListEvent) Address(javax.mail.Address) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 7 with WatchListEvent

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());
}
Also used : ActivityEvent(com.xpn.xwiki.plugin.activitystream.api.ActivityEvent) WatchListEvent(org.xwiki.watchlist.internal.api.WatchListEvent) ActivityEventImpl(com.xpn.xwiki.plugin.activitystream.impl.ActivityEventImpl) WikiReference(org.xwiki.model.reference.WikiReference) Date(java.util.Date) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 8 with WatchListEvent

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

Example 9 with WatchListEvent

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;
}
Also used : DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) WatchListEvent(org.xwiki.watchlist.internal.api.WatchListEvent) DocumentCreatedEvent(org.xwiki.bridge.event.DocumentCreatedEvent) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) DocumentReference(org.xwiki.model.reference.DocumentReference) Date(java.util.Date)

Example 10 with 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);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) WatchListEvent(org.xwiki.watchlist.internal.api.WatchListEvent) HashMap(java.util.HashMap) XWikiContext(com.xpn.xwiki.XWikiContext) InitializationException(org.xwiki.component.phase.InitializationException)

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