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