Tweaks/fixes to presence

This commit is contained in:
Till Faelligen 2022-10-18 10:09:43 +02:00
parent 50a4f51f2f
commit 6f7c57e6bd
No known key found for this signature in database
GPG key ID: 3DF82D8AB9211D4E
3 changed files with 14 additions and 43 deletions

View file

@ -185,6 +185,7 @@ func (p *PDUStreamProvider) IncrementalSync(
// If this room was joined in this sync, try to fetch
// as much timeline events as allowed by the filter.
if delta.NewlyJoined {
req.NewlyJoined[delta.RoomID] = struct{}{}
// Reverse the range, so we get the most recent first.
// This will be limited by the eventFilter.
newRange = types.Range{

View file

@ -20,7 +20,6 @@ import (
"sync"
"github.com/matrix-org/gomatrixserverlib"
"github.com/tidwall/gjson"
"github.com/matrix-org/dendrite/syncapi/notifier"
"github.com/matrix-org/dendrite/syncapi/storage"
@ -70,20 +69,19 @@ func (p *PresenceStreamProvider) IncrementalSync(
return from
}
if len(presences) == 0 {
return to
}
// add newly joined rooms user presences
newlyJoined := joinedRooms(req.Response, req.Device.UserID)
if len(newlyJoined) > 0 {
if len(req.NewlyJoined) > 0 {
newlyJoinedRoomIDs := make([]string, 0, len(req.NewlyJoined))
for roomID := range req.NewlyJoined {
newlyJoinedRoomIDs = append(newlyJoinedRoomIDs, roomID)
}
// TODO: Check if this is working better than before.
if err = p.notifier.LoadRooms(ctx, p.DB, newlyJoined); err != nil {
if err = p.notifier.LoadRooms(ctx, p.DB, newlyJoinedRoomIDs); err != nil {
req.Log.WithError(err).Error("unable to refresh notifier lists")
return from
}
NewlyJoinedLoop:
for _, roomID := range newlyJoined {
for _, roomID := range newlyJoinedRoomIDs {
roomUsers := p.notifier.JoinedUsers(roomID)
for i := range roomUsers {
// we already got a presence from this user
@ -103,6 +101,8 @@ func (p *PresenceStreamProvider) IncrementalSync(
}
}
}
} else {
return to
}
lastPos := from
@ -162,36 +162,3 @@ func (p *PresenceStreamProvider) IncrementalSync(
return lastPos
}
func joinedRooms(res *types.Response, userID string) []string {
var roomIDs []string
for roomID, join := range res.Rooms.Join {
// we would expect to see our join event somewhere if we newly joined the room.
// Normal events get put in the join section so it's not enough to know the room ID is present in 'join'.
newlyJoined := membershipEventPresent(join.State.Events, userID)
if newlyJoined {
roomIDs = append(roomIDs, roomID)
continue
}
newlyJoined = membershipEventPresent(join.Timeline.Events, userID)
if newlyJoined {
roomIDs = append(roomIDs, roomID)
}
}
return roomIDs
}
func membershipEventPresent(events []gomatrixserverlib.ClientEvent, userID string) bool {
for _, ev := range events {
// it's enough to know that we have our member event here, don't need to check membership content
// as it's implied by being in the respective section of the sync response.
if ev.Type == gomatrixserverlib.MRoomMember && ev.StateKey != nil && *ev.StateKey == userID {
// ignore e.g. join -> join changes
if gjson.GetBytes(ev.Unsigned, "prev_content.membership").Str == gjson.GetBytes(ev.Content, "membership").Str {
continue
}
return true
}
}
return false
}

View file

@ -4,9 +4,10 @@ import (
"context"
"time"
userapi "github.com/matrix-org/dendrite/userapi/api"
"github.com/matrix-org/gomatrixserverlib"
"github.com/sirupsen/logrus"
userapi "github.com/matrix-org/dendrite/userapi/api"
)
type SyncRequest struct {
@ -22,6 +23,8 @@ type SyncRequest struct {
// Updated by the PDU stream.
Rooms map[string]string
// Updated by the PDU stream.
NewlyJoined map[string]struct{}
// Updated by the PDU stream.
IgnoredUsers IgnoredUsers
}