working on message settings:
- created form - implemented storing of settings - implemented loading of settings next: pre-filling from with settings loaded from DB
This commit is contained in:
@@ -5,9 +5,11 @@ 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.SETTINGS;
|
||||
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.*;
|
||||
import static de.srsoftware.umbrella.message.model.Schedule.schedule;
|
||||
import static de.srsoftware.umbrella.messagebus.MessageBus.messageBus;
|
||||
import static java.lang.System.Logger.Level.*;
|
||||
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
|
||||
@@ -24,7 +26,7 @@ import de.srsoftware.umbrella.core.model.Envelope;
|
||||
import de.srsoftware.umbrella.core.model.Token;
|
||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||
import de.srsoftware.umbrella.core.model.User;
|
||||
import de.srsoftware.umbrella.message.model.CombinedMessage;
|
||||
import de.srsoftware.umbrella.message.model.*;
|
||||
import de.srsoftware.umbrella.messagebus.EventListener;
|
||||
import de.srsoftware.umbrella.messagebus.events.Event;
|
||||
import jakarta.activation.DataHandler;
|
||||
@@ -36,12 +38,13 @@ import jakarta.mail.internet.MimeBodyPart;
|
||||
import jakarta.mail.internet.MimeMessage;
|
||||
import jakarta.mail.internet.MimeMultipart;
|
||||
import jakarta.mail.util.ByteArrayDataSource;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class MessageSystem extends BaseHandler implements PostBox, EventListener {
|
||||
public static final System.Logger LOG = System.getLogger(MessageSystem.class.getSimpleName());
|
||||
private final Timer timer = new Timer();
|
||||
@@ -120,11 +123,44 @@ public class MessageSystem extends BaseHandler implements PostBox, EventListener
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doPatch(Path path, HttpExchange ex) throws IOException {
|
||||
addCors(ex);
|
||||
try {
|
||||
Optional<Token> token = SessionToken.from(ex).map(Token::of);
|
||||
var user = ModuleRegistry.userService().loadUser(token);
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var head = path.pop();
|
||||
return switch (head){
|
||||
case SETTINGS -> patchSettings(ex,user.get());
|
||||
default -> super.doGet(path,ex);
|
||||
};
|
||||
} catch (NumberFormatException e){
|
||||
return sendContent(ex,HTTP_BAD_REQUEST,"Invalid project id");
|
||||
} catch (UmbrellaException e){
|
||||
return send(ex,e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean listMessages(HttpExchange ex, UmbrellaUser user) throws IOException {
|
||||
var messages = queue.stream().filter(e -> e.isFor(user)).map(e -> summary(e, user.language())).toList();
|
||||
return sendContent(ex,messages);
|
||||
}
|
||||
|
||||
private boolean patchSettings(HttpExchange ex, UmbrellaUser user) throws IOException {
|
||||
var json = json(ex);
|
||||
Settings settings = null;
|
||||
if (json.has(INSTANTLY) && json.get(INSTANTLY) instanceof Boolean b && b){
|
||||
settings = new Instantly();
|
||||
} else {
|
||||
if (json.has(HOURS) && json.get(HOURS) instanceof JSONArray hrs){
|
||||
var hours = hrs.toList().stream().filter(v -> v instanceof Integer).map(Integer.class::cast).toList();
|
||||
settings = schedule(hours);
|
||||
} else settings = new Silent();
|
||||
}
|
||||
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);
|
||||
|
||||
@@ -8,19 +8,19 @@ import static de.srsoftware.umbrella.core.constants.Constants.TABLE_SETTINGS;
|
||||
import static de.srsoftware.umbrella.core.constants.Field.*;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||
import static de.srsoftware.umbrella.core.model.Translatable.t;
|
||||
import static de.srsoftware.umbrella.message.model.Settings.Times;
|
||||
import static java.lang.System.Logger.Level.WARNING;
|
||||
import static de.srsoftware.umbrella.message.model.Schedule.schedule;
|
||||
import static java.text.MessageFormat.format;
|
||||
|
||||
import de.srsoftware.umbrella.core.constants.Text;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||
import de.srsoftware.umbrella.message.model.Instantly;
|
||||
import de.srsoftware.umbrella.message.model.Settings;
|
||||
import de.srsoftware.umbrella.message.model.Silent;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashSet;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class SqliteMessageDb implements MessageDb{
|
||||
private static final System.Logger LOG = System.getLogger(SqliteMessageDb.class.getSimpleName());
|
||||
@@ -100,21 +100,19 @@ CREATE TABLE IF NOT EXISTS {0} ( {1} VARCHAR(255) PRIMARY KEY, {2} VARCHAR(255)
|
||||
|
||||
private Settings toSettings(ResultSet rs) throws SQLException {
|
||||
var submission = rs.getString(VALUE);
|
||||
var parts = submission.split(",");
|
||||
var times = new HashSet<Times>();
|
||||
for (var part : parts) try {
|
||||
times.add(Times.valueOf(part));
|
||||
} catch (IllegalArgumentException e) {
|
||||
LOG.log(WARNING,"encountered {0}, which is not a valid Times enumeration value!",part);
|
||||
if (submission.trim().equalsIgnoreCase(INSTANTLY)) return new Instantly();
|
||||
try {
|
||||
var times = Arrays.stream(submission.split(",")).map(Integer::parseInt).toList();
|
||||
return schedule(times);
|
||||
} catch (NumberFormatException nfe){
|
||||
return new Silent();
|
||||
}
|
||||
return new Settings(times);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Settings update(UmbrellaUser user, Settings settings) throws UmbrellaException {
|
||||
var times = settings.times().stream().map(Times::toString).collect(Collectors.joining(","));
|
||||
try {
|
||||
replaceInto(TABLE_SUBMISSIONS, USER_ID, VALUE).values(user.id(),times).execute(db).close();
|
||||
replaceInto(TABLE_SUBMISSIONS, USER_ID, VALUE).values(user.id(),settings.toString()).execute(db).close();
|
||||
return settings;
|
||||
} catch (SQLException e) {
|
||||
throw failedToStoreObject("submission data").causedBy(e);
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.message.model;
|
||||
|
||||
import static de.srsoftware.umbrella.core.constants.Field.INSTANTLY;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class Instantly implements Settings{
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
return Map.of(INSTANTLY,true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sendAt(int scheduledHour) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return INSTANTLY;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.message.model;
|
||||
|
||||
import static de.srsoftware.umbrella.core.constants.Field.HOURS;
|
||||
import static java.util.stream.Collectors.joining;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
|
||||
public class Schedule implements Settings{
|
||||
|
||||
private final HashSet<Integer> hours;
|
||||
|
||||
private Schedule(Collection<Integer> hours){
|
||||
this.hours = new HashSet<>(hours);
|
||||
}
|
||||
|
||||
public static Settings schedule(Collection<Integer> hours){
|
||||
return hours == null || hours.isEmpty() ? new Silent() : new Schedule(hours);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sendAt(int scheduledHour) {
|
||||
return hours.contains(scheduledHour);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
return Map.of(HOURS,hours);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return hours.stream().map(Object::toString).collect(joining(","));
|
||||
}
|
||||
}
|
||||
@@ -1,38 +1,10 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.message.model;
|
||||
|
||||
import static de.srsoftware.umbrella.message.Constants.SUBMISSION;
|
||||
|
||||
import de.srsoftware.tools.Mappable;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public record Settings(Set<Times> times) implements Mappable {
|
||||
|
||||
public enum Times{
|
||||
INSTANTLY,
|
||||
AT8,
|
||||
AT10,
|
||||
AT12,
|
||||
AT14,
|
||||
AT16,
|
||||
AT18,
|
||||
AT20;
|
||||
|
||||
public boolean matches(int hour){
|
||||
if (this == INSTANTLY) return false;
|
||||
return Integer.parseInt(toString().substring(2)) == hour;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean sendAt(Integer scheduledHour) {
|
||||
return times.contains(Times.INSTANTLY) || (scheduledHour != null && times.stream().anyMatch(time -> time.matches(scheduledHour)));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
return Map.of(SUBMISSION,times);
|
||||
}
|
||||
|
||||
public interface Settings extends Mappable {
|
||||
boolean sendAt(int scheduledHour);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.message.model;
|
||||
|
||||
import static de.srsoftware.umbrella.core.constants.Field.SILENT;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class Silent implements Settings{
|
||||
@Override
|
||||
public boolean sendAt(int scheduledHour) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
return Map.of(SILENT,true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "silent";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user