This commit is contained in:
S7evinK 2022-03-31 21:15:17 +02:00
parent 68786e9d14
commit 237a539e5b
4 changed files with 32 additions and 16 deletions

View file

@ -123,13 +123,13 @@ func GetPresence(
} }
} }
lastActiveTS := gomatrixserverlib.Timestamp(lastActive) p := types.Presence{LastActiveTS: gomatrixserverlib.Timestamp(lastActive)}
currentlyActive := time.Since(lastActiveTS.Time()).Minutes() < 5 currentlyActive := p.CurrentlyActive()
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusOK, Code: http.StatusOK,
JSON: types.PresenceClientResponse{ JSON: types.PresenceClientResponse{
CurrentlyActive: &currentlyActive, CurrentlyActive: &currentlyActive,
LastActiveAgo: time.Since(lastActiveTS.Time()).Milliseconds(), LastActiveAgo: p.LastActiveAgo(),
Presence: presence.Header.Get("presence"), Presence: presence.Header.Get("presence"),
StatusMsg: &statusMsg, StatusMsg: &statusMsg,
}, },

View file

@ -18,7 +18,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"strconv" "strconv"
"time"
"github.com/matrix-org/dendrite/federationapi/queue" "github.com/matrix-org/dendrite/federationapi/queue"
"github.com/matrix-org/dendrite/federationapi/storage" "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/config"
"github.com/matrix-org/dendrite/setup/jetstream" "github.com/matrix-org/dendrite/setup/jetstream"
"github.com/matrix-org/dendrite/setup/process" "github.com/matrix-org/dendrite/setup/process"
"github.com/matrix-org/dendrite/syncapi/types"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/nats-io/nats.go" "github.com/nats-io/nats.go"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -92,8 +92,6 @@ func (t *OutputPresenceConsumer) onMessage(ctx context.Context, msg *nats.Msg) b
return true return true
} }
timestamp := gomatrixserverlib.Timestamp(ts)
joined, err := t.db.GetAllJoinedHosts(ctx) joined, err := t.db.GetAllJoinedHosts(ctx)
if err != nil { if err != nil {
log.WithError(err).Error("failed to get joined hosts") 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 newStatusMsg = nil
} }
p := types.Presence{LastActiveTS: gomatrixserverlib.Timestamp(ts)}
content := fedTypes.Presence{ content := fedTypes.Presence{
Push: []fedTypes.PresenceContent{ Push: []fedTypes.PresenceContent{
{ {
CurrentlyActive: time.Since(timestamp.Time()).Minutes() < 5, CurrentlyActive: p.CurrentlyActive(),
LastActiveAgo: time.Since(timestamp.Time()).Milliseconds(), LastActiveAgo: p.LastActiveAgo(),
Presence: presence, Presence: presence,
StatusMsg: newStatusMsg, StatusMsg: newStatusMsg,
UserID: userID, UserID: userID,

View file

@ -19,7 +19,6 @@ import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"sync" "sync"
"time"
"github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/dendrite/syncapi/types"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
@ -27,6 +26,7 @@ import (
type PresenceStreamProvider struct { type PresenceStreamProvider struct {
StreamProvider StreamProvider
// cache contains previously sent presence updates to avoid unneeded updates
cache sync.Map cache sync.Map
} }
@ -91,10 +91,10 @@ func (p *PresenceStreamProvider) IncrementalSync(
} }
presences[roomUsers[i]], err = p.DB.GetPresence(ctx, roomUsers[i]) presences[roomUsers[i]], err = p.DB.GetPresence(ctx, roomUsers[i])
if err != nil { if err != nil {
req.Log.WithError(err).Warn("unable to query presence for user")
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
continue continue
} }
req.Log.WithError(err).Error("unable to query presence for user")
return from return from
} }
} }
@ -111,18 +111,16 @@ func (p *PresenceStreamProvider) IncrementalSync(
if ok { if ok {
// skip already sent presence // skip already sent presence
prevPresence := pres.(*types.Presence) prevPresence := pres.(*types.Presence)
currentlyActive := time.Since(prevPresence.LastActiveTS.Time()).Minutes() < 5 currentlyActive := prevPresence.CurrentlyActive()
samePresence := prevPresence.ClientFields.Presence == presence.ClientFields.Presence && skip := prevPresence.Equals(presence) && currentlyActive && req.Device.UserID != presence.UserID
prevPresence.ClientFields.StatusMsg == presence.ClientFields.StatusMsg
skip := currentlyActive && samePresence && req.Device.UserID != presence.UserID
if skip { if skip {
req.Log.Debugf("Skipping presence, no change (%s)", presence.UserID) req.Log.Debugf("Skipping presence, no change (%s)", presence.UserID)
continue continue
} }
} }
presence.ClientFields.LastActiveAgo = time.Since(presence.LastActiveTS.Time()).Milliseconds() presence.ClientFields.LastActiveAgo = presence.LastActiveAgo()
currentlyActive := time.Since(presence.LastActiveTS.Time()).Minutes() < 5
if presence.ClientFields.Presence == "online" { if presence.ClientFields.Presence == "online" {
currentlyActive := presence.CurrentlyActive()
presence.ClientFields.CurrentlyActive = &currentlyActive presence.ClientFields.CurrentlyActive = &currentlyActive
} }

View file

@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
@ -521,6 +522,23 @@ type Presence struct {
LastActiveTS gomatrixserverlib.Timestamp `json:"-"` 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 { type PresenceClientResponse struct {
CurrentlyActive *bool `json:"currently_active,omitempty"` CurrentlyActive *bool `json:"currently_active,omitempty"`
LastActiveAgo int64 `json:"last_active_ago,omitempty"` LastActiveAgo int64 `json:"last_active_ago,omitempty"`