refaturing message system, step 1: making message abstract and add TranslatableMessage and TranslatedMessage
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -17,16 +17,16 @@ import java.util.stream.Collectors;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class Envelope {
|
||||
private final Message message;
|
||||
public class Envelope<T> {
|
||||
private final Message<T> message;
|
||||
private final Set<User> receivers;
|
||||
private final LocalDateTime time;
|
||||
|
||||
public Envelope(Message message, User receiver){
|
||||
public Envelope(Message<T> message, User receiver){
|
||||
this(message,new HashSet<>(Set.of(receiver)));
|
||||
}
|
||||
|
||||
public Envelope(Message message, HashSet<User> receivers) {
|
||||
public Envelope(Message<T> message, HashSet<User> receivers) {
|
||||
this.message = message;
|
||||
this.receivers = receivers;
|
||||
time = LocalDateTime.now();
|
||||
@@ -40,7 +40,7 @@ public class Envelope {
|
||||
*/
|
||||
public static Envelope from(JSONObject json) throws UmbrellaException {
|
||||
if (!json.has(RECEIVERS)) throw missingField(RECEIVERS);
|
||||
var message = Message.from(json);
|
||||
var message = TranslatedMessage.from(json);
|
||||
var obj = json.get(RECEIVERS);
|
||||
if (obj instanceof JSONObject) obj = new JSONArray(List.of(obj));
|
||||
if (!(obj instanceof JSONArray receiverList)) throw invalidField(RECEIVERS, t(JSONARRAY));
|
||||
@@ -67,7 +67,7 @@ public class Envelope {
|
||||
return receivers.contains(receiver);
|
||||
}
|
||||
|
||||
public Message message(){
|
||||
public Message<T> message(){
|
||||
return message;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,50 +1,35 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.core.model;
|
||||
|
||||
import static de.srsoftware.tools.Optionals.isSet;
|
||||
import static de.srsoftware.umbrella.core.constants.Constants.JSONOBJECT;
|
||||
import static de.srsoftware.umbrella.core.constants.Field.*;
|
||||
import static de.srsoftware.umbrella.core.constants.Text.STRING;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||
import static de.srsoftware.umbrella.core.model.Translatable.t;
|
||||
import static java.text.MessageFormat.format;
|
||||
|
||||
import de.srsoftware.tools.Mappable;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import java.util.*;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import java.util.Collection;
|
||||
import java.util.Objects;
|
||||
|
||||
public record Message(UmbrellaUser sender, Translatable subject, Translatable body, List<Attachment> attachments) {
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (!(o instanceof Message message)) return false;
|
||||
return Objects.equals(sender, message.sender) && Objects.equals(subject, message.subject) && Objects.equals(body, message.body) && Objects.equals(attachments, message.attachments);
|
||||
public abstract class Message<T> {
|
||||
private final Collection<Attachment> attachments;
|
||||
private final T body, subject;
|
||||
private final UmbrellaUser sender;
|
||||
|
||||
public Message(UmbrellaUser sender, T subject, T body, Collection<Attachment> attachments){
|
||||
this.sender = sender;
|
||||
this.subject = subject;
|
||||
this.body = body;
|
||||
this.attachments = attachments;
|
||||
}
|
||||
|
||||
public static Message from(JSONObject json) throws UmbrellaException {
|
||||
for (var key : Set.of(SENDER, SUBJECT, BODY)) {
|
||||
if (!json.has(key)) throw missingField(key);
|
||||
}
|
||||
if (!(json.get(SENDER) instanceof JSONObject senderObject)) throw invalidField(SENDER, t(JSONOBJECT));
|
||||
if (!(json.get(SUBJECT) instanceof String subject && isSet(subject))) throw invalidField(SUBJECT,t(STRING));
|
||||
if (!(json.get(BODY) instanceof String body && isSet(body))) throw invalidField(BODY,t(STRING));
|
||||
public Collection<Attachment> attachments(){
|
||||
return attachments;
|
||||
}
|
||||
|
||||
var user = UmbrellaUser.of(senderObject);
|
||||
if (!(user instanceof UmbrellaUser sender)) throw new UmbrellaException(400, t("Sender is not an umbrella user!"));
|
||||
var attachments = new ArrayList<Attachment>();
|
||||
if (json.has(ATTACHMENTS)){
|
||||
var jsonAttachments = json.get(ATTACHMENTS);
|
||||
if (jsonAttachments instanceof JSONObject obj) jsonAttachments = new JSONArray(List.of(obj));
|
||||
if (jsonAttachments instanceof JSONArray arr){
|
||||
for (var att : arr){
|
||||
if (!(att instanceof JSONObject o)) throw new UmbrellaException(400, t("Attachments contains entry that is not an object: {entry}","entry",att));
|
||||
var attachment = Attachment.of(o);
|
||||
attachments.add(attachment);
|
||||
}
|
||||
}
|
||||
}
|
||||
return new Message(sender,new UnTranslatable(subject),new UnTranslatable(body),attachments);
|
||||
public T body(){
|
||||
return body;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (!(o instanceof Message<?> message)) return false;
|
||||
return Objects.equals(sender, message.sender) && Objects.equals(subject, message.subject) && Objects.equals(body, message.body) && Objects.equals(attachments, message.attachments);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -52,6 +37,14 @@ public record Message(UmbrellaUser sender, Translatable subject, Translatable bo
|
||||
return Objects.hash(subject, body, attachments);
|
||||
}
|
||||
|
||||
public UmbrellaUser sender(){
|
||||
return sender;
|
||||
}
|
||||
|
||||
public T subject(){
|
||||
return subject;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return format("{0}(from: {1}), subject: {2}",getClass().getSimpleName(),sender,subject);
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.core.model;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class TranslatableMessage extends Message<Translatable> {
|
||||
public TranslatableMessage(UmbrellaUser sender, Translatable subject, Translatable body, Collection<Attachment> attachments) {
|
||||
super(sender, subject, body, attachments);
|
||||
}
|
||||
|
||||
public TranslatedMessage translate(String lang){
|
||||
return new TranslatedMessage(sender(),subject().translate(lang),body().translate(lang),attachments());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.core.model;
|
||||
|
||||
import static de.srsoftware.tools.Optionals.isSet;
|
||||
import static de.srsoftware.umbrella.core.constants.Constants.JSONOBJECT;
|
||||
import static de.srsoftware.umbrella.core.constants.Field.*;
|
||||
import static de.srsoftware.umbrella.core.constants.Field.ATTACHMENTS;
|
||||
import static de.srsoftware.umbrella.core.constants.Text.STRING;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.invalidField;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingField;
|
||||
import static de.srsoftware.umbrella.core.model.Translatable.t;
|
||||
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class TranslatedMessage extends Message<String> {
|
||||
public TranslatedMessage(UmbrellaUser sender, String subject, String body, Collection<Attachment> attachments) {
|
||||
super(sender, subject, body, attachments);
|
||||
}
|
||||
|
||||
public static TranslatedMessage from(JSONObject json) throws UmbrellaException {
|
||||
for (var key : Set.of(SENDER, SUBJECT, BODY)) {
|
||||
if (!json.has(key)) throw missingField(key);
|
||||
}
|
||||
if (!(json.get(SENDER) instanceof JSONObject senderObject)) throw invalidField(SENDER, t(JSONOBJECT));
|
||||
if (!(json.get(SUBJECT) instanceof String subject && isSet(subject))) throw invalidField(SUBJECT,t(STRING));
|
||||
if (!(json.get(BODY) instanceof String body && isSet(body))) throw invalidField(BODY,t(STRING));
|
||||
|
||||
var user = UmbrellaUser.of(senderObject);
|
||||
if (!(user instanceof UmbrellaUser sender)) throw new UmbrellaException(400, t("Sender is not an umbrella user!"));
|
||||
var attachments = new ArrayList<Attachment>();
|
||||
if (json.has(ATTACHMENTS)){
|
||||
var jsonAttachments = json.get(ATTACHMENTS);
|
||||
if (jsonAttachments instanceof JSONObject obj) jsonAttachments = new JSONArray(List.of(obj));
|
||||
if (jsonAttachments instanceof JSONArray arr){
|
||||
for (var att : arr){
|
||||
if (!(att instanceof JSONObject o)) throw new UmbrellaException(400, t("Attachments contains entry that is not an object: {entry}","entry",att));
|
||||
var attachment = Attachment.of(o);
|
||||
attachments.add(attachment);
|
||||
}
|
||||
}
|
||||
}
|
||||
return new TranslatedMessage(sender,subject,body,attachments);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user