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:
@@ -28,6 +28,7 @@ public class Path {
|
||||
public static final String PROPERTIES = "properties";
|
||||
public static final String PROPERTY = "property";
|
||||
|
||||
public static final String READ = "read";
|
||||
public static final String REDIRECT = "redirect";
|
||||
|
||||
public static final String SEARCH = "search";
|
||||
|
||||
@@ -2,19 +2,28 @@
|
||||
import { onMount } from 'svelte';
|
||||
import { useTinyRouter } from 'svelte-tiny-router';
|
||||
import { t } from '../../translations.svelte';
|
||||
import { api, get } from '../../urls.svelte';
|
||||
import { api, get, patch } from '../../urls.svelte';
|
||||
import { error, yikes } from '../../warn.svelte';
|
||||
|
||||
let router = useTinyRouter();
|
||||
let messages = [];
|
||||
let timer = null;
|
||||
|
||||
async function display(hash){
|
||||
const url = api(`message/${hash}`);
|
||||
const res = get(url);
|
||||
const res = await get(url);
|
||||
if (res.ok){
|
||||
yikes();
|
||||
const json = await res.json();
|
||||
console.log(json);
|
||||
if (timer) clearTimeout(timer);
|
||||
for (let i in messages){
|
||||
if (messages[i].hash == hash){
|
||||
messages[i].body = json.body;
|
||||
timer = setTimeout(() => {mark_read(hash)}, 2000);
|
||||
} else {
|
||||
delete messages[i].body;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
error(res);
|
||||
}
|
||||
@@ -31,6 +40,20 @@
|
||||
}
|
||||
}
|
||||
|
||||
async function mark_read(hash){
|
||||
timer = null;
|
||||
const url = api(`message/read/${hash}`);
|
||||
const res = await patch(url);
|
||||
if (res.ok) {
|
||||
for (let i in messages){
|
||||
if (messages[i].hash == hash) messages[i].read = true;
|
||||
}
|
||||
yikes();
|
||||
} else {
|
||||
error(res);
|
||||
}
|
||||
}
|
||||
|
||||
function showSettings(){
|
||||
router.navigate("message/settings");
|
||||
}
|
||||
@@ -51,10 +74,21 @@
|
||||
<tbody>
|
||||
{#each messages as message}
|
||||
<tr class="message-{message.hash}" onclick={ev => display(message.hash)}>
|
||||
<td>{message.timestamp}</td>
|
||||
<td>
|
||||
{#if message.read}→ {/if}
|
||||
{message.timestamp}
|
||||
</td>
|
||||
<td>{message.sender}</td>
|
||||
<td>{message.subject}</td>
|
||||
</tr>
|
||||
{#if message.body}
|
||||
<tr class="message-{message.hash} body">
|
||||
<td></td>
|
||||
<td colspan="2">
|
||||
<pre>{message.body.trim()}</pre>
|
||||
</td>
|
||||
</tr>
|
||||
{/if}
|
||||
{/each}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
@@ -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));
|
||||
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);
|
||||
}
|
||||
|
||||
@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);
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user