Search in sources :

Example 1 with SubscribeUnsubscribeEvent

use of won.bot.framework.eventbot.event.impl.mail.SubscribeUnsubscribeEvent in project webofneeds by researchstudio-sat.

the class MailCommandAction method processNonReferenceMailCommand.

private void processNonReferenceMailCommand(MimeMessage message) throws IOException, MessagingException {
    EventBus bus = getEventListenerContext().getEventBus();
    ActionType mailAction = mailContentExtractor.getMailAction(message);
    switch(mailAction) {
        case SUBSCRIBE:
            bus.publish(new SubscribeUnsubscribeEvent(message, SubscribeStatus.SUBSCRIBED));
            break;
        case UNSUBSCRIBE:
            bus.publish(new SubscribeUnsubscribeEvent(message, SubscribeStatus.UNSUBSCRIBED));
            break;
        case CLOSE_NEED:
            /*A need can be closed with a mail that matches the takenCmdPattern in its subject and has the same title
                 as a previously created need by the user*/
            URI needUri = retrieveCorrespondingNeedUriFromMailByTitle(message);
            if (needUri != null) {
                bus.publish(new DeactivateNeedCommandEvent(needUri));
            }
            break;
        case NO_ACTION:
        default:
            // INVALID COMMAND
            logger.error("No command was given or assumed");
            break;
    }
}
Also used : ActionType(won.bot.framework.eventbot.action.impl.mail.model.ActionType) SubscribeUnsubscribeEvent(won.bot.framework.eventbot.event.impl.mail.SubscribeUnsubscribeEvent) DeactivateNeedCommandEvent(won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateNeedCommandEvent) EventBus(won.bot.framework.eventbot.bus.EventBus) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) URI(java.net.URI)

Example 2 with SubscribeUnsubscribeEvent

use of won.bot.framework.eventbot.event.impl.mail.SubscribeUnsubscribeEvent in project webofneeds by researchstudio-sat.

the class SubscribeUnsubscribeAction method doRun.

@Override
protected void doRun(final Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    if (event instanceof SubscribeUnsubscribeEvent && ctx.getBotContextWrapper() instanceof MailBotContextWrapper) {
        MailBotContextWrapper botContextWrapper = (MailBotContextWrapper) ctx.getBotContextWrapper();
        // save the new subscription status of the user to the bot context
        SubscribeUnsubscribeEvent subscribeEvent = (SubscribeUnsubscribeEvent) event;
        SubscribeStatus subscribeStatus = subscribeEvent.getSubscribeStatus();
        String senderMailAddress = MailContentExtractor.getMailSender(subscribeEvent.getMessage());
        botContextWrapper.setSubscribeStatusForMailAddress(senderMailAddress, subscribeStatus);
        // depending on the new subscribe status of the user publish his cached mails as needs or delete the cache
        if (SubscribeStatus.SUBSCRIBED.equals(subscribeStatus)) {
            EventBus bus = getEventListenerContext().getEventBus();
            Collection<MimeMessage> cachedMessages = botContextWrapper.loadCachedMailsForMailAddress(senderMailAddress);
            cachedMessages.stream().forEach(message -> bus.publish(new CreateNeedFromMailEvent(message)));
        } else if (SubscribeStatus.UNSUBSCRIBED.equals(subscribeStatus)) {
            botContextWrapper.removeCachedMailsForMailAddress(senderMailAddress);
        }
    }
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) SubscribeStatus(won.bot.framework.eventbot.action.impl.mail.model.SubscribeStatus) MailBotContextWrapper(won.bot.framework.bot.context.MailBotContextWrapper) MimeMessage(javax.mail.internet.MimeMessage) SubscribeUnsubscribeEvent(won.bot.framework.eventbot.event.impl.mail.SubscribeUnsubscribeEvent) EventBus(won.bot.framework.eventbot.bus.EventBus) CreateNeedFromMailEvent(won.bot.framework.eventbot.event.impl.mail.CreateNeedFromMailEvent)

Aggregations

EventBus (won.bot.framework.eventbot.bus.EventBus)2 SubscribeUnsubscribeEvent (won.bot.framework.eventbot.event.impl.mail.SubscribeUnsubscribeEvent)2 URI (java.net.URI)1 MimeMessage (javax.mail.internet.MimeMessage)1 MailBotContextWrapper (won.bot.framework.bot.context.MailBotContextWrapper)1 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)1 ActionType (won.bot.framework.eventbot.action.impl.mail.model.ActionType)1 SubscribeStatus (won.bot.framework.eventbot.action.impl.mail.model.SubscribeStatus)1 WonURI (won.bot.framework.eventbot.action.impl.mail.model.WonURI)1 DeactivateNeedCommandEvent (won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateNeedCommandEvent)1 CreateNeedFromMailEvent (won.bot.framework.eventbot.event.impl.mail.CreateNeedFromMailEvent)1