From 9b5a6fa8f8eba670a6dc090301eed78b8cda12b4 Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Wed, 5 Aug 2015 14:07:01 +0000 Subject: [PATCH] fix for Bug #476 Social Mail Replies: vacation notice auto reply to post is treated as "Favorite" git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portal/social-mail-servlet@117670 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../gcube/portal/socialmail/PeriodicTask.java | 79 +++++++++++-------- 1 file changed, 46 insertions(+), 33 deletions(-) diff --git a/src/main/java/org/gcube/portal/socialmail/PeriodicTask.java b/src/main/java/org/gcube/portal/socialmail/PeriodicTask.java index f163377..0dad946 100644 --- a/src/main/java/org/gcube/portal/socialmail/PeriodicTask.java +++ b/src/main/java/org/gcube/portal/socialmail/PeriodicTask.java @@ -54,6 +54,9 @@ import com.sun.mail.util.MailSSLSocketFactory; public class PeriodicTask implements Runnable { private static final Logger _log = LoggerFactory.getLogger(PeriodicTask.class); + private static final String ACCEPTED_SUBJECT_1 = "shared a news"; + private static final String ACCEPTED_SUBJECT_2 = "commented on"; + private DatabookStore socialStore; private String host; private String mailserver_username; @@ -106,13 +109,14 @@ public class PeriodicTask implements Runnable { } catch (PortalException | SystemException e) { _log.error("Exception while trying to get the user from email address: " + e.getMessage()); + return null; } return SessionManager.getInstance().getASLSession(sessionID, username); } - - + + public void check(String portalName, String host, String user, String password) { try { MailSSLSocketFactory sf = null; @@ -151,37 +155,46 @@ public class PeriodicTask implements Runnable { Message message = messages[i]; _log.info("--------------- FOUND EMAIL ------------------"); String subject = message.getSubject(); - _log.debug("Parsing email of " + message.getFrom()[0] + " with subject: " + subject); + _log.info("Parsing email of " + message.getFrom()[0] + " with subject: " + subject); + if (subject.contains(ACCEPTED_SUBJECT_1) || subject.contains(ACCEPTED_SUBJECT_2)) { - String feedId = extractFeedId(message); - String commentText = extractText(portalName, feedId, message); - _log.info("Extracted id: " + feedId + " text=" + commentText); - String escapedCommentText = Utils.escapeHtmlAndTransformUrl(commentText); - - Address[] froms = message.getFrom(); - String emailAddress = froms == null ? null : ((InternetAddress) froms[0]).getAddress(); + String feedId = extractFeedId(message); + String commentText = extractText(portalName, feedId, message); + _log.info("Extracted id: " + feedId + " text=" + commentText); + String escapedCommentText = Utils.escapeHtmlAndTransformUrl(commentText); - ASLSession fakeSession = getFakeASLSession(emailAddress); - if (escapedCommentText.trim().compareTo("") == 0) {//it is a favorite/subscription - _log.debug("Found favorite/subscription for feed with subject: " + subject); - favoriteFeed(feedId, fakeSession); + Address[] froms = message.getFrom(); + String emailAddress = froms == null ? null : ((InternetAddress) froms[0]).getAddress(); + + ASLSession fakeSession = getFakeASLSession(emailAddress); + if (fakeSession != null) { + if (escapedCommentText.trim().compareTo("") == 0) {//it is a favorite/subscription + _log.debug("Found favorite/subscription for feed with subject: " + subject); + favoriteFeed(feedId, fakeSession); + } + else { + Comment comment = new Comment(UUID.randomUUID().toString(), fakeSession.getUsername(), + new Date(), feedId, escapedCommentText, fakeSession.getUserFullName(), fakeSession.getUserAvatarId()); + + _log.debug("The EscapedCommentText =>" + escapedCommentText); + boolean commentCommitResult = false; + try { + if (socialStore.addComment(comment)) + commentCommitResult = true; + } catch (FeedIDNotFoundException e) { + _log.error("Related post not found for this comment " + e.getMessage()); + e.printStackTrace(); + } + if (commentCommitResult) { //the notifications should start + notifyUsersInvolved(comment, escapedCommentText, feedId, fakeSession); + } + } + } else { + _log.warn("User Not Recognized, going to discard Message from emailAddress = " + emailAddress); + } } else { - Comment comment = new Comment(UUID.randomUUID().toString(), fakeSession.getUsername(), - new Date(), feedId, escapedCommentText, fakeSession.getUserFullName(), fakeSession.getUserAvatarId()); - - _log.debug("The EscapedCommentText =>" + escapedCommentText); - boolean commentCommitResult = false; - try { - if (socialStore.addComment(comment)) - commentCommitResult = true; - } catch (FeedIDNotFoundException e) { - _log.error("Related post not found for this comment " + e.getMessage()); - e.printStackTrace(); - } - if (commentCommitResult) { //the notifications should start - notifyUsersInvolved(comment, escapedCommentText, feedId, fakeSession); - } + _log.warn("Message Subject is not one of the accepted ones, going to discard Message with subject = " + subject); } //delete this message message.setFlag(Flags.Flag.DELETED, true); @@ -210,11 +223,11 @@ public class PeriodicTask implements Runnable { _log.warn("Found email with no feedId from " + fakeSession.getUserEmailAddress() + ". Going to trash it"); return; } - - + + Like like = new Like(UUID.randomUUID().toString(), fakeSession.getUsername(), new Date(), feedId, fakeSession.getUserFullName(), fakeSession.getUserAvatarId()); - + boolean likeCommitResult = false; try { if (socialStore.like(like)); @@ -329,7 +342,7 @@ public class PeriodicTask implements Runnable { break; } } - + StringBuilder sb = new StringBuilder(); /* * this is needed to remove the text added by the email client like when you reply, e.g. On wrote ...