From ddc52d8920947a2293223262132eaad2f0ee0f18 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sun, 20 Feb 2022 13:36:36 +0100 Subject: [PATCH] [overlays] Add parameter to limit replay message age Signed-off-by: Knut Ahlers --- internal/apimodules/overlays/default/eventclient.mjs | 6 +++++- internal/apimodules/overlays/overlays.go | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/internal/apimodules/overlays/default/eventclient.mjs b/internal/apimodules/overlays/default/eventclient.mjs index 9cc7b2b..b48e5d9 100644 --- a/internal/apimodules/overlays/default/eventclient.mjs +++ b/internal/apimodules/overlays/default/eventclient.mjs @@ -3,6 +3,7 @@ * @typedef {Object} EventClient~Options * @prop {string} [channel] - Filter for specific channel events (format: `#channel`) * @prop {Object} handlers - Map event types to callback functions `(event, fields, time, live) => {...}` + * @prop {number} [maxReplayAge=-1] - Number of hours to replay the events for (-1 = infinite) * @prop {boolean} replay - Request a replay at connect (requires channel to be set to a channel name) * @prop {string} [token] - API access token to use to connect to the WebSocket */ @@ -63,7 +64,10 @@ export default class EventClient { // Auth was confirmed, request replay if wanted by client if (this.paramOptionFallback('replay', false) && this.paramOptionFallback('channel')) { this.socket.send(JSON.stringify({ - fields: { channel: this.paramOptionFallback('channel') }, + fields: { + channel: this.paramOptionFallback('channel'), + maxage: Number(this.paramOptionFallback('maxReplayAge', -1)), + }, type: '_replay', })) } diff --git a/internal/apimodules/overlays/overlays.go b/internal/apimodules/overlays/overlays.go index 4f14064..bbd8b1c 100644 --- a/internal/apimodules/overlays/overlays.go +++ b/internal/apimodules/overlays/overlays.go @@ -49,6 +49,7 @@ var ( fsStack httpFSStack + ptrIntMinusOne = func(v int64) *int64 { return &v }(-1) ptrStringEmpty = func(v string) *string { return &v }("") store plugins.StorageManager @@ -246,7 +247,13 @@ func handleSocketSubscription(w http.ResponseWriter, r *http.Request) { case msgTypeRequestReplay: go func() { + maxAge := time.Duration(recvMsg.Fields.MustInt64("maxage", ptrIntMinusOne)) * time.Hour + log.Errorf("DEBUG %s %T", maxAge, recvMsg.Fields.Data()["maxage"]) for _, msg := range storedObject.GetChannelEvents(recvMsg.Fields.MustString("channel", ptrStringEmpty)) { + if maxAge > 0 && time.Since(msg.Time) > maxAge { + continue + } + sendMsgC <- msg } }()