Browse Source

tries looking for email message by id first and name as fallback

master r5.4.0
Massimiliano Assante 6 months ago
parent
commit
f769c3d1ce
  1. 62
      src/main/java/org/gcube/portal/socialmail/PeriodicTask.java

62
src/main/java/org/gcube/portal/socialmail/PeriodicTask.java

@ -4,6 +4,7 @@ import static org.gcube.common.authorization.client.Constants.authorizationServi
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Properties;
@ -39,6 +40,8 @@ import org.gcube.common.portal.PortalContext;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.storagehub.client.plugins.AbstractPlugin;
import org.gcube.common.storagehub.client.proxies.MessageManagerClient;
import org.gcube.common.storagehub.model.exceptions.IdNotFoundException;
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
import org.gcube.portal.databook.server.DatabookStore;
import org.gcube.portal.databook.shared.Comment;
import org.gcube.portal.databook.shared.Feed;
@ -270,27 +273,46 @@ public class PeriodicTask implements Runnable {
String token = authorizationService().generateUserToken(new UserInfo(username, userRoles), scope);
SecurityTokenProvider.instance.set(token);
MessageManagerClient mmClient = AbstractPlugin.messages().build();
org.gcube.common.storagehub.model.messages.Message theMessage = mmClient.get(messageId);
String[] addresses = theMessage.getAddresses();
for (int i = 0; i < addresses.length; i++) {
recipientIds.add(addresses[i]);
org.gcube.common.storagehub.model.messages.Message theMessage = null;
try {
theMessage = mmClient.get(messageId);
}
//add the sender and remove the person who is replying from the recipients
String sender = theMessage.getSender().getUserName();
String originalText = theMessage.getBody();
Date originalSentTime = theMessage.getCreationTime().getTime();
recipientIds.add(sender);
recipientIds.remove(fakeSession.getUsername());
_log.debug("Message Recipients:");
for (String rec : recipientIds) {
_log.debug(rec);
catch (IdNotFoundException e) {
_log.warn("Message with id "+ messageId + " could not be found, trying by name ...");
List<org.gcube.common.storagehub.model.messages.Message> messages = mmClient.getReceivedMessages();
Collections.reverse(messages); //the messages are returned in chronological order, but the messages we look for is one of the newests
_log.warn("Messages size="+messages.size());
for(org.gcube.common.storagehub.model.messages.Message msg: messages) {
if (msg.getName().equals(messageId)) {
theMessage = msg;
break;
}
}
}
_log.debug("Constructing reply message");
messageText += getReplyHeaderMessage(sender, originalSentTime, originalText);
if (theMessage != null) {
String[] addresses = theMessage.getAddresses();
for (int i = 0; i < addresses.length; i++) {
recipientIds.add(addresses[i]);
}
//add the sender and remove the person who is replying from the recipients
String sender = theMessage.getSender().getUserName();
String originalText = theMessage.getBody();
Date originalSentTime = theMessage.getCreationTime().getTime();
recipientIds.add(sender);
recipientIds.remove(fakeSession.getUsername());
_log.debug("Message Recipients:");
for (String rec : recipientIds) {
_log.debug(rec);
}
_log.debug("Constructing reply message");
messageText += getReplyHeaderMessage(sender, originalSentTime, originalText);
_log.debug("Trying to send message with subject: " + subject + " to: " + recipientIds.toString());
newMessageId = mmClient.sendMessage(recipientIds, subject, messageText, new ArrayList<String>());
_log.debug("Trying to send message with subject: " + subject + " to: " + recipientIds.toString());
newMessageId = mmClient.sendMessage(recipientIds, subject, messageText, new ArrayList<String>());
} else {
_log.error("Message with id "+ messageId + " could not be found even iterating by name, exiting.");
}
} catch (Exception e) {
e.printStackTrace();
@ -605,7 +627,7 @@ public class PeriodicTask implements Runnable {
sb.append("\n");
}
String toReturn = sb.toString().trim();
//check if the reply is from iOS which add illegal character
String sanytisedLine = StringEscapeUtils.escapeJava(toReturn);
@ -618,6 +640,6 @@ public class PeriodicTask implements Runnable {
return toReturn;
}
}

Loading…
Cancel
Save