From 237a539e5b847ee40c882fe072175aaacfe0164c Mon Sep 17 00:00:00 2001 From: S7evinK Date: Thu, 31 Mar 2022 21:15:17 +0200 Subject: [PATCH] Tweaks --- clientapi/routing/presence.go | 6 +++--- federationapi/consumers/presence.go | 10 +++++----- syncapi/streams/stream_presence.go | 14 ++++++-------- syncapi/types/types.go | 18 ++++++++++++++++++ 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/clientapi/routing/presence.go b/clientapi/routing/presence.go index 09f8372da..076c7c571 100644 --- a/clientapi/routing/presence.go +++ b/clientapi/routing/presence.go @@ -123,13 +123,13 @@ func GetPresence( } } - lastActiveTS := gomatrixserverlib.Timestamp(lastActive) - currentlyActive := time.Since(lastActiveTS.Time()).Minutes() < 5 + p := types.Presence{LastActiveTS: gomatrixserverlib.Timestamp(lastActive)} + currentlyActive := p.CurrentlyActive() return util.JSONResponse{ Code: http.StatusOK, JSON: types.PresenceClientResponse{ CurrentlyActive: ¤tlyActive, - LastActiveAgo: time.Since(lastActiveTS.Time()).Milliseconds(), + LastActiveAgo: p.LastActiveAgo(), Presence: presence.Header.Get("presence"), StatusMsg: &statusMsg, }, diff --git a/federationapi/consumers/presence.go b/federationapi/consumers/presence.go index 939abc3b5..2c5cc5e9c 100644 --- a/federationapi/consumers/presence.go +++ b/federationapi/consumers/presence.go @@ -18,7 +18,6 @@ import ( "context" "encoding/json" "strconv" - "time" "github.com/matrix-org/dendrite/federationapi/queue" "github.com/matrix-org/dendrite/federationapi/storage" @@ -26,6 +25,7 @@ import ( "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/jetstream" "github.com/matrix-org/dendrite/setup/process" + "github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/gomatrixserverlib" "github.com/nats-io/nats.go" log "github.com/sirupsen/logrus" @@ -92,8 +92,6 @@ func (t *OutputPresenceConsumer) onMessage(ctx context.Context, msg *nats.Msg) b return true } - timestamp := gomatrixserverlib.Timestamp(ts) - joined, err := t.db.GetAllJoinedHosts(ctx) if err != nil { log.WithError(err).Error("failed to get joined hosts") @@ -108,11 +106,13 @@ func (t *OutputPresenceConsumer) onMessage(ctx context.Context, msg *nats.Msg) b newStatusMsg = nil } + p := types.Presence{LastActiveTS: gomatrixserverlib.Timestamp(ts)} + content := fedTypes.Presence{ Push: []fedTypes.PresenceContent{ { - CurrentlyActive: time.Since(timestamp.Time()).Minutes() < 5, - LastActiveAgo: time.Since(timestamp.Time()).Milliseconds(), + CurrentlyActive: p.CurrentlyActive(), + LastActiveAgo: p.LastActiveAgo(), Presence: presence, StatusMsg: newStatusMsg, UserID: userID, diff --git a/syncapi/streams/stream_presence.go b/syncapi/streams/stream_presence.go index 8af23803f..0262178b2 100644 --- a/syncapi/streams/stream_presence.go +++ b/syncapi/streams/stream_presence.go @@ -19,7 +19,6 @@ import ( "database/sql" "encoding/json" "sync" - "time" "github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/gomatrixserverlib" @@ -27,6 +26,7 @@ import ( type PresenceStreamProvider struct { StreamProvider + // cache contains previously sent presence updates to avoid unneeded updates cache sync.Map } @@ -91,10 +91,10 @@ func (p *PresenceStreamProvider) IncrementalSync( } presences[roomUsers[i]], err = p.DB.GetPresence(ctx, roomUsers[i]) if err != nil { - req.Log.WithError(err).Warn("unable to query presence for user") if err == sql.ErrNoRows { continue } + req.Log.WithError(err).Error("unable to query presence for user") return from } } @@ -111,18 +111,16 @@ func (p *PresenceStreamProvider) IncrementalSync( if ok { // skip already sent presence prevPresence := pres.(*types.Presence) - currentlyActive := time.Since(prevPresence.LastActiveTS.Time()).Minutes() < 5 - samePresence := prevPresence.ClientFields.Presence == presence.ClientFields.Presence && - prevPresence.ClientFields.StatusMsg == presence.ClientFields.StatusMsg - skip := currentlyActive && samePresence && req.Device.UserID != presence.UserID + currentlyActive := prevPresence.CurrentlyActive() + skip := prevPresence.Equals(presence) && currentlyActive && req.Device.UserID != presence.UserID if skip { req.Log.Debugf("Skipping presence, no change (%s)", presence.UserID) continue } } - presence.ClientFields.LastActiveAgo = time.Since(presence.LastActiveTS.Time()).Milliseconds() - currentlyActive := time.Since(presence.LastActiveTS.Time()).Minutes() < 5 + presence.ClientFields.LastActiveAgo = presence.LastActiveAgo() if presence.ClientFields.Presence == "online" { + currentlyActive := presence.CurrentlyActive() presence.ClientFields.CurrentlyActive = ¤tlyActive } diff --git a/syncapi/types/types.go b/syncapi/types/types.go index af0e6e969..01fc303ef 100644 --- a/syncapi/types/types.go +++ b/syncapi/types/types.go @@ -20,6 +20,7 @@ import ( "fmt" "strconv" "strings" + "time" "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/gomatrixserverlib" @@ -521,6 +522,23 @@ type Presence struct { LastActiveTS gomatrixserverlib.Timestamp `json:"-"` } +// Equals compares p1 with p2. +func (p1 *Presence) Equals(p2 *Presence) bool { + return p1.ClientFields.Presence == p2.ClientFields.Presence && + p1.ClientFields.StatusMsg == p2.ClientFields.StatusMsg && + p1.UserID == p2.UserID +} + +// CurrentlyActive returns the current active state. +func (p *Presence) CurrentlyActive() bool { + return time.Since(p.LastActiveTS.Time()).Minutes() < 5 +} + +// LastActiveAgo returns the time since the LastActiveTS in milliseconds. +func (p *Presence) LastActiveAgo() int64 { + return time.Since(p.LastActiveTS.Time()).Milliseconds() +} + type PresenceClientResponse struct { CurrentlyActive *bool `json:"currently_active,omitempty"` LastActiveAgo int64 `json:"last_active_ago,omitempty"`