implemented configurable fallback-sender for collected mails with several senders
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -5,6 +5,7 @@ public class Constants {
|
|||||||
public static final String AUTH = "mail.smtp.auth";
|
public static final String AUTH = "mail.smtp.auth";
|
||||||
|
|
||||||
public static final String CONFIG_DB = "umbrella.modules.message.database";
|
public static final String CONFIG_DB = "umbrella.modules.message.database";
|
||||||
|
public static final String CONFIG_SMTP_FROM = "umbrella.modules.message.smtp.from";
|
||||||
public static final String CONFIG_SMTP_HOST = "umbrella.modules.message.smtp.host";
|
public static final String CONFIG_SMTP_HOST = "umbrella.modules.message.smtp.host";
|
||||||
public static final String CONFIG_SMTP_PASS = "umbrella.modules.message.smtp.pass";
|
public static final String CONFIG_SMTP_PASS = "umbrella.modules.message.smtp.pass";
|
||||||
public static final String CONFIG_SMTP_PORT = "umbrella.modules.message.smtp.port";
|
public static final String CONFIG_SMTP_PORT = "umbrella.modules.message.smtp.port";
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package de.srsoftware.umbrella.message;
|
|||||||
|
|
||||||
import static de.srsoftware.tools.PathHandler.CONTENT_TYPE;
|
import static de.srsoftware.tools.PathHandler.CONTENT_TYPE;
|
||||||
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
||||||
|
import static de.srsoftware.umbrella.core.ModuleRegistry.translator;
|
||||||
import static de.srsoftware.umbrella.core.constants.Constants.UTF8;
|
import static de.srsoftware.umbrella.core.constants.Constants.UTF8;
|
||||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingConfig;
|
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingConfig;
|
||||||
import static de.srsoftware.umbrella.message.Constants.*;
|
import static de.srsoftware.umbrella.message.Constants.*;
|
||||||
@@ -12,6 +13,7 @@ import de.srsoftware.configuration.Configuration;
|
|||||||
import de.srsoftware.umbrella.core.ModuleRegistry;
|
import de.srsoftware.umbrella.core.ModuleRegistry;
|
||||||
import de.srsoftware.umbrella.core.api.PostBox;
|
import de.srsoftware.umbrella.core.api.PostBox;
|
||||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||||
|
import de.srsoftware.umbrella.core.model.EmailAddress;
|
||||||
import de.srsoftware.umbrella.core.model.Envelope;
|
import de.srsoftware.umbrella.core.model.Envelope;
|
||||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||||
import de.srsoftware.umbrella.core.model.User;
|
import de.srsoftware.umbrella.core.model.User;
|
||||||
@@ -76,10 +78,10 @@ public class MessageSystem implements PostBox {
|
|||||||
db = new SqliteMessageDb(connect(dbFile));
|
db = new SqliteMessageDb(connect(dbFile));
|
||||||
debugAddress = config.get(DEBUG_ADDREESS).map(Object::toString).orElse(null);
|
debugAddress = config.get(DEBUG_ADDREESS).map(Object::toString).orElse(null);
|
||||||
port = config.get(CONFIG_SMTP_PORT,587);
|
port = config.get(CONFIG_SMTP_PORT,587);
|
||||||
host = config.get(CONFIG_SMTP_HOST).map(Object::toString).orElseThrow(() -> new RuntimeException("umbrella.modules.message.smtp.host not configured!"));
|
host = config.get(CONFIG_SMTP_HOST).map(Object::toString).orElseThrow(() -> missingConfig(CONFIG_SMTP_HOST));
|
||||||
user = config.get(CONFIG_SMTP_USER).map(Object::toString).orElseThrow(() -> new RuntimeException("umbrella.modules.message.smtp.user not configured!"));
|
user = config.get(CONFIG_SMTP_USER).map(Object::toString).orElseThrow(() -> missingConfig(CONFIG_SMTP_USER));
|
||||||
pass = config.get(CONFIG_SMTP_PASS).map(Object::toString).orElseThrow(() -> new RuntimeException("umbrella.modules.message.smtp.pass not configured!"));
|
pass = config.get(CONFIG_SMTP_PASS).map(Object::toString).orElseThrow(() -> missingConfig(CONFIG_SMTP_PASS));
|
||||||
from = user;
|
from = config.get(CONFIG_SMTP_FROM).map(Object::toString).orElseThrow(() -> missingConfig(CONFIG_SMTP_FROM));
|
||||||
ModuleRegistry.add(this);
|
ModuleRegistry.add(this);
|
||||||
new SubmissionTask(8).schedule();
|
new SubmissionTask(8).schedule();
|
||||||
new SubmissionTask(10).schedule();
|
new SubmissionTask(10).schedule();
|
||||||
@@ -116,9 +118,9 @@ public class MessageSystem implements PostBox {
|
|||||||
var date = new Date();
|
var date = new Date();
|
||||||
|
|
||||||
for (var receiver : dueRecipients){
|
for (var receiver : dueRecipients){
|
||||||
BiFunction<String,Map<String,String>,String> translateFunction = (text,fills) -> ModuleRegistry.translator().translate(receiver.language(),text,fills);
|
BiFunction<String,Map<String,String>,String> translateFunction = (text,fills) -> translator().translate(receiver.language(),text,fills);
|
||||||
|
var fallbackSender = new User("Umbrella",new EmailAddress(from),null);
|
||||||
var combined = new CombinedMessage("Collected messages",translateFunction);
|
var combined = new CombinedMessage("Collected messages",translateFunction,fallbackSender);
|
||||||
var envelopes = queue.stream().filter(env -> env.isFor(receiver)).toList();
|
var envelopes = queue.stream().filter(env -> env.isFor(receiver)).toList();
|
||||||
for (var envelope : envelopes) combined.merge(envelope.message());
|
for (var envelope : envelopes) combined.merge(envelope.message());
|
||||||
|
|
||||||
|
|||||||
@@ -5,9 +5,7 @@ import static java.lang.System.Logger.Level.DEBUG;
|
|||||||
import static java.lang.System.Logger.Level.TRACE;
|
import static java.lang.System.Logger.Level.TRACE;
|
||||||
import static java.text.MessageFormat.format;
|
import static java.text.MessageFormat.format;
|
||||||
|
|
||||||
import de.srsoftware.umbrella.core.model.Attachment;
|
import de.srsoftware.umbrella.core.model.*;
|
||||||
import de.srsoftware.umbrella.core.model.Message;
|
|
||||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
@@ -16,15 +14,17 @@ public class CombinedMessage {
|
|||||||
|
|
||||||
private final Set<Attachment> attachments = new HashSet<>();
|
private final Set<Attachment> attachments = new HashSet<>();
|
||||||
private final StringBuilder combinedBody = new StringBuilder();
|
private final StringBuilder combinedBody = new StringBuilder();
|
||||||
|
private final User fallbackSender;
|
||||||
private String combinedSubject = null;
|
private String combinedSubject = null;
|
||||||
private final List<Message> mergedMessages = new ArrayList<>();
|
private final List<Message> mergedMessages = new ArrayList<>();
|
||||||
private final String subjectForCombinedMessage;
|
private final String subjectForCombinedMessage;
|
||||||
private final BiFunction<String,Map<String,String>,String> translate;
|
private final BiFunction<String,Map<String,String>,String> translate;
|
||||||
private UmbrellaUser sender = null;
|
private User sender = null;
|
||||||
|
|
||||||
public CombinedMessage(String subjectForCombinedMessage, BiFunction<String, Map<String,String>,String> translateFunction){
|
public CombinedMessage(String subjectForCombinedMessage, BiFunction<String, Map<String,String>,String> translateFunction, User fallbackSender){
|
||||||
LOG.log(DEBUG,"Creating combined message…");
|
LOG.log(DEBUG,"Creating combined message…");
|
||||||
this.subjectForCombinedMessage = subjectForCombinedMessage;
|
this.subjectForCombinedMessage = translateFunction.apply(subjectForCombinedMessage,null);
|
||||||
|
this.fallbackSender = fallbackSender;
|
||||||
translate = translateFunction;
|
translate = translateFunction;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,6 +39,7 @@ public class CombinedMessage {
|
|||||||
combinedSubject = subject;
|
combinedSubject = subject;
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
|
if (!sender.equals(message.sender())) sender = fallbackSender;
|
||||||
combinedBody.insert(0,format("# {0}:\n# {1}:\n\n",sender,subject)); // insert sender and subject of first message right before the body of the first message
|
combinedBody.insert(0,format("# {0}:\n# {1}:\n\n",sender,subject)); // insert sender and subject of first message right before the body of the first message
|
||||||
combinedSubject = subjectForCombinedMessage;
|
combinedSubject = subjectForCombinedMessage;
|
||||||
// no break here, we need to append the subject and content
|
// no break here, we need to append the subject and content
|
||||||
@@ -59,7 +60,7 @@ public class CombinedMessage {
|
|||||||
return combinedBody.toString();
|
return combinedBody.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public UmbrellaUser sender() {
|
public User sender() {
|
||||||
return sender;
|
return sender;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user