implemented state change for messages:

messages are marked as read upon display

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2026-01-18 11:54:40 +01:00
parent 8422dce031
commit d9f88af6b7
3 changed files with 94 additions and 39 deletions

View File

@@ -5,7 +5,10 @@ import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
import static de.srsoftware.umbrella.core.constants.Constants.TIME_FORMATTER;
import static de.srsoftware.umbrella.core.constants.Constants.UTF8;
import static de.srsoftware.umbrella.core.constants.Field.*;
import static de.srsoftware.umbrella.core.constants.Path.READ;
import static de.srsoftware.umbrella.core.constants.Path.SETTINGS;
import static de.srsoftware.umbrella.core.constants.Text.LONG;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.invalidField;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingConfig;
import static de.srsoftware.umbrella.core.model.Translatable.t;
import static de.srsoftware.umbrella.message.Constants.*;
@@ -131,25 +134,6 @@ public class MessageSystem extends BaseHandler implements PostBox, EventListener
}
}
private boolean getMessage(HttpExchange ex, UmbrellaUser user, int hash) throws IOException {
var envelope = queue.stream()
.filter(msg -> msg.isFor(user))
.filter(msg -> msg.hashCode() == hash)
.findFirst();
if (envelope.isPresent()) {
var message = envelope.get().message();
var sender = message.sender().name();
var subject = message.subject().translate(user.language());
var body = message.body().translate(user.language());
return sendContent(ex,Map.of(
SENDER,sender,
SUBJECT,subject,
BODY,body
));
}
return notFound(ex);
}
@Override
public boolean doPatch(Path path, HttpExchange ex) throws IOException {
addCors(ex);
@@ -160,6 +144,7 @@ public class MessageSystem extends BaseHandler implements PostBox, EventListener
var head = path.pop();
return switch (head){
case SETTINGS -> patchSettings(ex,user.get());
case READ -> patchState(ex,user.get(),path.pop());
default -> super.doGet(path,ex);
};
} catch (NumberFormatException e){
@@ -169,6 +154,15 @@ public class MessageSystem extends BaseHandler implements PostBox, EventListener
}
}
private boolean getMessage(HttpExchange ex, UmbrellaUser user, int hash) throws IOException {
var envelope = queue.stream()
.filter(msg -> msg.isFor(user))
.filter(msg -> msg.hashCode() == hash)
.findFirst();
if (envelope.isPresent()) return sendMessage(ex, user, envelope.get());
return notFound(ex);
}
private boolean getSettings(HttpExchange ex, UmbrellaUser user) throws IOException {
return sendContent(ex,db.getSettings(user));
}
@@ -178,6 +172,16 @@ public class MessageSystem extends BaseHandler implements PostBox, EventListener
return sendContent(ex,messages);
}
@Override
public void onEvent(Event<?> event) {
for (var user : event.audience()){
if (debugAddress != null && !debugAddress.equals(user.email().toString())) continue;
var message = new de.srsoftware.umbrella.core.model.Message(event.initiator(),event.subject(),event.describe(),null);
var envelope = new Envelope(message,user);
send(envelope);
}
}
private boolean patchSettings(HttpExchange ex, UmbrellaUser user) throws IOException {
var json = json(ex);
Settings settings = null;
@@ -192,25 +196,20 @@ public class MessageSystem extends BaseHandler implements PostBox, EventListener
return sendContent(ex,db.update(user,settings));
}
private static JSONObject summary(Envelope envelope, String lang) {
var sender = envelope.message().sender().name();
var subject = envelope.message().subject().translate(lang);
var time = envelope.time().format(TIME_FORMATTER);
var hash = envelope.hashCode();
return new JSONObject(Map.of(SENDER,sender,SUBJECT,subject,TIMESTAMP,time,HASH,hash));
}
@Override
public void onEvent(Event<?> event) {
for (var user : event.audience()){
if (debugAddress != null && !debugAddress.equals(user.email().toString())) continue;
var message = new de.srsoftware.umbrella.core.model.Message(event.initiator(),event.subject(),event.describe(),null);
var envelope = new Envelope(message,user);
send(envelope);
private boolean patchState(HttpExchange ex, UmbrellaUser user, String path) {
try {
var hash = Integer.parseInt(path);
var envelope = queue.stream().filter(env -> env.hashCode() == hash).findFirst().orElse(null);
if (envelope != null){
envelope.receivers().remove(user);
return sendMessage(ex,user,envelope);
}
return notFound(ex);
} catch (NumberFormatException | IOException e) {
throw invalidField(HASH,LONG);
}
}
private synchronized void processMessages(Integer scheduledHour) {
LOG.log(INFO,"Running {0}…",scheduledHour == null ? "instantly" : "scheduled at "+scheduledHour);
var queue = new ArrayList<>(this.queue);
@@ -252,6 +251,27 @@ public class MessageSystem extends BaseHandler implements PostBox, EventListener
if (scheduledHour != null) new SubmissionTask(scheduledHour).schedule();
}
private boolean sendMessage(HttpExchange ex, UmbrellaUser user, Envelope envelope) throws IOException {
var message = envelope.message();
var sender = message.sender().name();
var subject = message.subject().translate(user.language());
var body = message.body().translate(user.language());
return sendContent(ex,Map.of(
SENDER,sender,
SUBJECT,subject,
BODY,body
));
}
private static JSONObject summary(Envelope envelope, String lang) {
var sender = envelope.message().sender().name();
var subject = envelope.message().subject().translate(lang);
var time = envelope.time().format(TIME_FORMATTER);
var hash = envelope.hashCode();
return new JSONObject(Map.of(SENDER,sender,SUBJECT,subject,TIMESTAMP,time,HASH,hash));
}
private void send(CombinedMessage message, Date date) throws MessagingException {
var receiver = message.receiver();
LOG.log(TRACE,"Sending combined message to {0}…",receiver);