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;
}
}
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);
}
}
}
Aggregations