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:
2026-01-17 14:46:11 +01:00
parent 32063f046c
commit ea30907bfe
13 changed files with 306 additions and 51 deletions

View File

@@ -2,10 +2,10 @@
package de.srsoftware.umbrella.core.constants;
import java.time.format.DateTimeFormatter;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.time.format.DateTimeFormatter;
public class Constants {
private Constants(){

View File

@@ -67,8 +67,10 @@ public class Field {
public static final String HASH = "hash";
public static final String HEAD = "head";
public static final String HOURS = "hours";
public static final String ID = "id";
public static final String INSTANTLY = "instantly";
public static final String ITEM = "item";
public static final String ITEM_CODE = "item_code";
@@ -123,6 +125,7 @@ public class Field {
public static final String SENDER = "sender";
public static final String SETTINGS = "settings";
public static final String SHOW_CLOSED = "show_closed";
public static final String SILENT = "silent";
public static final String SOURCE = "source";
public static final String START_DATE = "start_date";
public static final String START_TIME = "start_time";

View File

@@ -9,7 +9,6 @@ import static de.srsoftware.umbrella.core.model.Translatable.t;
import static java.text.MessageFormat.format;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;

View File

@@ -1,7 +1,162 @@
<script>
import { t } from '../../translations.svelte';
import { api, patch } from '../../urls.svelte';
import { error, yikes } from '../../warn.svelte';
let instant = false;
let silent = false;
let at8 = false;
let at9 = false;
let at10 = false;
let at11 = false;
let at12 = false;
let at13 = false;
let at14 = true;
let at15 = false;
let at16 = false;
let at17 = false;
let at18 = false;
let at19 = false;
let at20 = false;
let at21 = false;
let at22 = false;
function delivery(mode){
if (mode){
silent = false;
} else {
instant = false;
}
at8 = at9 = at10 = at11 = at12 = at13 = at14 = at15 = at16 = at17 = at18 = at19 = at20 = at21 = at22 = false;
saveTimes();
}
async function saveTimes(){
let data = {
instantly : instant,
hours : []
}
if (at8) data.hours.push(8);
if (at9) data.hours.push(9);
if (at10) data.hours.push(10);
if (at11) data.hours.push(11);
if (at12) data.hours.push(12);
if (at13) data.hours.push(13);
if (at14) data.hours.push(14);
if (at15) data.hours.push(15);
if (at16) data.hours.push(16);
if (at17) data.hours.push(17);
if (at18) data.hours.push(18);
if (at19) data.hours.push(19);
if (at20) data.hours.push(20);
if (at21) data.hours.push(21);
if (at22) data.hours.push(22);
let url = api('message/settings');
let res = await patch(url,data);
if (res.ok){
yikes();
} else {
error(res);
}
}
function selectTime(ev){
instant = false;
silent = !(at8 || at9 || at10 || at11 || at12 || at13 || at14 || at15 || at16 || at17 || at18 || at19 || at20 || at21 || at22);
saveTimes();
}
</script>
<fieldset>
<legend>{t('message settingss')}</legend>
<fieldset class="message settings">
<legend>{t('message settings')}</legend>
<p>{t('When shall messages be delivered?')}</p>
<table>
<tbody>
<tr>
<td colspan="3">
<label>
<input type="checkbox" onchange={ev => delivery(true)} bind:checked={instant} />{t('instantly')}
</label>
</td>
</tr>
<tr>
<td>
{t('collect messages and send them at')}
</td>
<td>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
</td>
<td>
<label>
<input type="checkbox" onchange={selectTime} bind:checked={at8} />{t('8 am')}
</label>
<label>
<input type="checkbox" onchange={selectTime} bind:checked={at9} />{t('9:00 am')}
</label>
<label>
<input type="checkbox" onchange={selectTime} bind:checked={at10} />{t('10:00 am')}
</label>
<label>
<input type="checkbox" onchange={selectTime} bind:checked={at11} />{t('11:00 am')}
</label>
<label>
<input type="checkbox" onchange={selectTime} bind:checked={at12} />{t('noon')}
</label>
<label>
<input type="checkbox" onchange={selectTime} bind:checked={at13} />{t('1:00 pm')}
</label>
<label>
<input type="checkbox" onchange={selectTime} bind:checked={at14} />{t('2:00 pm')}
</label>
<label>
<input type="checkbox" onchange={selectTime} bind:checked={at15} />{t('3:00 pm')}
</label>
<label>
<input type="checkbox" onchange={selectTime} bind:checked={at16} />{t('4:00 pm')}
</label>
<label>
<input type="checkbox" onchange={selectTime} bind:checked={at17} />{t('5:00 pm')}
</label>
<label>
<input type="checkbox" onchange={selectTime} bind:checked={at18} />{t('6:00 pm')}
</label>
<label>
<input type="checkbox" onchange={selectTime} bind:checked={at19} />{t('7:00 pm')}
</label>
<label>
<input type="checkbox" onchange={selectTime} bind:checked={at20} />{t('8:00 pm')}
</label>
<label>
<input type="checkbox" onchange={selectTime} bind:checked={at21} />{t('9:00 pm')}
</label>
<label>
<input type="checkbox" onchange={selectTime} bind:checked={at22} />{t('10:00 pm')}
</label>
</td>
</tr>
<tr>
<td colspan="3">
<label>
<input type="checkbox" onchange={ev => delivery(false)} bind:checked={silent} />{t('i don`t want to receive email notifications!')}
</label>
</td>
</tr>
</tbody>
</table>
</fieldset>

View File

@@ -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);

View File

@@ -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);

View File

@@ -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;
}
}

View File

@@ -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(","));
}
}

View File

@@ -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);
}

View File

@@ -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";
}
}

View File

@@ -188,6 +188,7 @@
"members": "Mitarbeiter",
"message": "Nachricht",
"messages": "Benachrichtigungen",
"message settings": "Benachrichtigungs-Einstellungen",
"miscellaneous_settings": "sonstige Einstellungen",
"missing_new_item_id": "Alter Artikel-ID ({0}) wurde keine neue ID zugeordnet!",
"mismatch": "ungleich",

View File

@@ -188,6 +188,7 @@
"members": "members",
"message": "message",
"messages": "messages",
"message settings": "message settings",
"miscellaneous_settings": "miscellaneous settings",
"missing_new_item_id": "Old item id ({0}) has no new counterpart!",
"mismatch": "mismatch",

View File

@@ -323,6 +323,13 @@ textarea{
max-height: unset;
}
.message.settings label{
display: block;
}
.message.settings td{
vertical-align: middle;
}
.project th,
.task th{
text-align: right;