Search in sources :

Example 1 with SubscribeStatus

use of won.bot.framework.eventbot.action.impl.mail.model.SubscribeStatus in project webofneeds by researchstudio-sat.

the class MailParserAction method processCreateNeedMail.

private void processCreateNeedMail(MimeMessage message) throws MessagingException, IOException {
    EventListenerContext ctx = getEventListenerContext();
    EventBus bus = ctx.getEventBus();
    String senderMailAddress = MailContentExtractor.getMailSender(message);
    MailBotContextWrapper botContextWrapper = ((MailBotContextWrapper) ctx.getBotContextWrapper());
    SubscribeStatus subscribeStatus = botContextWrapper.getSubscribeStatusForMailAddress(senderMailAddress);
    // published as needs, discarded or cached
    if (SubscribeStatus.SUBSCRIBED.equals(subscribeStatus)) {
        logger.info("received a create mail from subscribed user '{}' with subject '{}' so publish as need", senderMailAddress, message.getSubject());
        bus.publish(new CreateNeedFromMailEvent(message));
    } else if (SubscribeStatus.UNSUBSCRIBED.equals(subscribeStatus)) {
        logger.info("received mail from unsubscribed user '{}' so discard mail with subject '{}'", senderMailAddress, message.getSubject());
    } else {
        logger.info("received a create mail from new user '{}' with subject '{}' so cache it and send welcome mail", senderMailAddress, message.getSubject());
        botContextWrapper.addCachedMailsForMailAddress(message);
        bus.publish(new WelcomeMailEvent(message));
    }
}
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) WelcomeMailEvent(won.bot.framework.eventbot.event.impl.mail.WelcomeMailEvent) EventBus(won.bot.framework.eventbot.bus.EventBus) CreateNeedFromMailEvent(won.bot.framework.eventbot.event.impl.mail.CreateNeedFromMailEvent)

Example 2 with SubscribeStatus

use of won.bot.framework.eventbot.action.impl.mail.model.SubscribeStatus 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

MailBotContextWrapper (won.bot.framework.bot.context.MailBotContextWrapper)2 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)2 SubscribeStatus (won.bot.framework.eventbot.action.impl.mail.model.SubscribeStatus)2 EventBus (won.bot.framework.eventbot.bus.EventBus)2 CreateNeedFromMailEvent (won.bot.framework.eventbot.event.impl.mail.CreateNeedFromMailEvent)2 MimeMessage (javax.mail.internet.MimeMessage)1 SubscribeUnsubscribeEvent (won.bot.framework.eventbot.event.impl.mail.SubscribeUnsubscribeEvent)1 WelcomeMailEvent (won.bot.framework.eventbot.event.impl.mail.WelcomeMailEvent)1