Search in sources :

Example 1 with CreateNeedFromMailEvent

use of won.bot.framework.eventbot.event.impl.mail.CreateNeedFromMailEvent 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 CreateNeedFromMailEvent

use of won.bot.framework.eventbot.event.impl.mail.CreateNeedFromMailEvent 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)

Example 3 with CreateNeedFromMailEvent

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

the class CreateNeedFromMailAction method doRun.

protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    if (event instanceof CreateNeedFromMailEvent && ctx.getBotContextWrapper() instanceof MailBotContextWrapper) {
        MailBotContextWrapper botContextWrapper = (MailBotContextWrapper) ctx.getBotContextWrapper();
        MimeMessage message = ((CreateNeedFromMailEvent) event).getMessage();
        try {
            NeedContentPropertyType type = mailContentExtractor.getNeedType(message);
            String title = mailContentExtractor.getTitle(message);
            String description = mailContentExtractor.getDescription(message);
            String[] tags = mailContentExtractor.getTags(message);
            boolean isUsedForTesting = mailContentExtractor.isUsedForTesting(message);
            boolean isDoNotMatch = mailContentExtractor.isDoNotMatch(message);
            WonNodeInformationService wonNodeInformationService = ctx.getWonNodeInformationService();
            final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
            final URI needURI = wonNodeInformationService.generateNeedURI(wonNodeUri);
            DefaultNeedModelWrapper needModelWrapper = new DefaultNeedModelWrapper(needURI.toString());
            needModelWrapper.setTitle(type, title);
            needModelWrapper.setDescription(type, description);
            for (String tag : tags) {
                needModelWrapper.addTag(type, tag);
            }
            for (URI facet : facets) {
                needModelWrapper.addFacetUri(facet.toString());
            }
            Dataset dataset = needModelWrapper.copyDataset();
            logger.debug("creating need on won node {} with content {} ", wonNodeUri, StringUtils.abbreviate(RdfUtils.toString(dataset), 150));
            WonMessage createNeedMessage = createWonMessage(wonNodeInformationService, needURI, wonNodeUri, dataset, isUsedForTesting, isDoNotMatch);
            EventBotActionUtils.rememberInList(ctx, needURI, uriListName);
            botContextWrapper.addUriMimeMessageRelation(needURI, message);
            EventListener successCallback = new EventListener() {

                @Override
                public void onEvent(Event event) throws Exception {
                    logger.debug("need creation successful, new need URI is {}", needURI);
                    String sender = MailContentExtractor.getFromAddressString(botContextWrapper.getMimeMessageForURI(needURI));
                    botContextWrapper.addMailAddressWonURIRelation(sender, new WonURI(needURI, UriType.NEED));
                    logger.debug("created need was from sender: " + sender);
                }
            };
            EventListener failureCallback = new EventListener() {

                @Override
                public void onEvent(Event event) throws Exception {
                    String textMessage = WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) event).getFailureMessage());
                    logger.debug("need creation failed for need URI {}, original message URI {}: {}", new Object[] { needURI, ((FailureResponseEvent) event).getOriginalMessageURI(), textMessage });
                    EventBotActionUtils.removeFromList(ctx, needURI, uriListName);
                    botContextWrapper.removeUriMimeMessageRelation(needURI);
                }
            };
            EventBotActionUtils.makeAndSubscribeResponseListener(createNeedMessage, successCallback, failureCallback, ctx);
            logger.debug("registered listeners for response to message URI {}", createNeedMessage.getMessageURI());
            ctx.getWonMessageSender().sendWonMessage(createNeedMessage);
            logger.debug("need creation message sent with message URI {}", createNeedMessage.getMessageURI());
        } catch (MessagingException me) {
            logger.error("messaging exception occurred: {}", me);
        }
    }
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) DefaultNeedModelWrapper(won.protocol.util.DefaultNeedModelWrapper) MailBotContextWrapper(won.bot.framework.bot.context.MailBotContextWrapper) MessagingException(javax.mail.MessagingException) Dataset(org.apache.jena.query.Dataset) WonNodeInformationService(won.protocol.service.WonNodeInformationService) NeedContentPropertyType(won.protocol.model.NeedContentPropertyType) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) URI(java.net.URI) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) MimeMessage(javax.mail.internet.MimeMessage) WonMessage(won.protocol.message.WonMessage) CreateNeedFromMailEvent(won.bot.framework.eventbot.event.impl.mail.CreateNeedFromMailEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) Event(won.bot.framework.eventbot.event.Event) EventListener(won.bot.framework.eventbot.listener.EventListener) CreateNeedFromMailEvent(won.bot.framework.eventbot.event.impl.mail.CreateNeedFromMailEvent)

Aggregations

MailBotContextWrapper (won.bot.framework.bot.context.MailBotContextWrapper)3 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)3 CreateNeedFromMailEvent (won.bot.framework.eventbot.event.impl.mail.CreateNeedFromMailEvent)3 MimeMessage (javax.mail.internet.MimeMessage)2 SubscribeStatus (won.bot.framework.eventbot.action.impl.mail.model.SubscribeStatus)2 EventBus (won.bot.framework.eventbot.bus.EventBus)2 URI (java.net.URI)1 MessagingException (javax.mail.MessagingException)1 Dataset (org.apache.jena.query.Dataset)1 WonURI (won.bot.framework.eventbot.action.impl.mail.model.WonURI)1 Event (won.bot.framework.eventbot.event.Event)1 SubscribeUnsubscribeEvent (won.bot.framework.eventbot.event.impl.mail.SubscribeUnsubscribeEvent)1 WelcomeMailEvent (won.bot.framework.eventbot.event.impl.mail.WelcomeMailEvent)1 FailureResponseEvent (won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent)1 EventListener (won.bot.framework.eventbot.listener.EventListener)1 WonMessage (won.protocol.message.WonMessage)1 NeedContentPropertyType (won.protocol.model.NeedContentPropertyType)1 WonNodeInformationService (won.protocol.service.WonNodeInformationService)1 DefaultNeedModelWrapper (won.protocol.util.DefaultNeedModelWrapper)1