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)
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: &currentlyActive,
LastActiveAgo: time.Since(lastActiveTS.Time()).Milliseconds(),
LastActiveAgo: p.LastActiveAgo(),
Presence: presence.Header.Get("presence"),
StatusMsg: &statusMsg,
},

View file

@ -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,

View file

@ -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 = &currentlyActive
}

View file

@ -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"`