mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-06 14:33:10 -06:00
Squashed commit of the following: commitb5c55faf98Author: Neil Alexander <neilalexander@users.noreply.github.com> Date: Fri Jul 1 12:00:32 2022 +0100 Version 0.8.9 (#2549) * Version 0.8.9 * Update changelog commitb50a24c666Author: Neil Alexander <neilalexander@users.noreply.github.com> Date: Fri Jul 1 10:54:07 2022 +0100 Roomserver producers package (#2546) * Give the roomserver a producers package * Change init point * Populate ACLs API * Fix build issues * `RoomEventProducer` naming commit89cd0e8fc1Author: Till <2353100+S7evinK@users.noreply.github.com> Date: Fri Jul 1 11:49:26 2022 +0200 Try to fix backfilling (#2548) * Try to fix backfilling * Return start/end to not confuse clients * Update GMSL * Update GMSL commit086f182e24Author: Neil Alexander <neilalexander@users.noreply.github.com> Date: Fri Jul 1 09:50:06 2022 +0100 Disable WebAssembly builds for now commit54bed4c593Author: Neil Alexander <neilalexander@users.noreply.github.com> Date: Fri Jul 1 09:37:54 2022 +0100 Blacklist `Guest users can join guest_access rooms` test until it can be investigated commit561c159ad7Author: Till <2353100+S7evinK@users.noreply.github.com> Date: Thu Jun 30 12:34:37 2022 +0200 Silence presence logs (#2547) commit519bc1124bAuthor: Neil Alexander <neilalexander@users.noreply.github.com> Date: Wed Jun 29 15:29:39 2022 +0100 Add `evacuateUser` endpoint, use it when deactivating accounts (#2545) * Add `evacuateUser` endpoint, use it when deactivating accounts * Populate the API * Clean up user devices when deactivating * Include invites, delete pushers commit2dea466685Author: Neil Alexander <neilalexander@users.noreply.github.com> Date: Wed Jun 29 12:32:24 2022 +0100 Return an error if trying to invite a malformed user ID (#2543) commit2086992cafAuthor: Till <2353100+S7evinK@users.noreply.github.com> Date: Wed Jun 29 10:49:12 2022 +0200 Don't return `end` if there are not more messages (#2542) * Be more spec compliant * Move lazyLoadMembers to own method commit920a20821bAuthor: Jean Lucas <jean@4ray.co> Date: Mon Jun 27 04:15:19 2022 -0400 Fix nats.go commit (#2540) Signed-off-by: Jean Lucas <jean@4ray.co> commit7120eb6bc9Author: Neil Alexander <neilalexander@users.noreply.github.com> Date: Wed Jun 15 14:27:07 2022 +0100 Add `InputDeviceListUpdate` to the keyserver, remove old input API (#2536) * Add `InputDeviceListUpdate` to the keyserver, remove old input API * Fix copyright * Log more information when a device list update fails commit1b90cc9536Author: Till <2353100+S7evinK@users.noreply.github.com> Date: Wed Jun 15 12:50:02 2022 +0200 Fix rare panic when returning user devices over federation (#2534) commit4c2a10f1a6Author: Neil Alexander <neilalexander@users.noreply.github.com> Date: Mon Jun 13 15:11:10 2022 +0100 Handle state before, send history visibility in output (#2532) * Check state before event * Tweaks * Refactor a bit, include in output events * Don't waste time if soft failed either * Tweak control flow, comments, use GMSL history visibility type commitc500958583Author: Emanuele Aliberti <dev@mtka.eu> Date: Mon Jun 13 13:08:46 2022 +0200 generic CaddyFile in front of Dendrite (monolith) (#2531) for Caddy 2.5.x Co-authored-by: emanuele.aliberti <emanuele.aliberti@mtka.eu> commite1136f4d3eAuthor: Till Faelligen <davidf@element.io> Date: Mon Jun 13 11:46:59 2022 +0200 Make the linter happy again commit0a7f7dc716Author: Neil Alexander <neilalexander@users.noreply.github.com> Date: Mon Jun 13 10:16:30 2022 +0100 Add `--difference` to `resolve-state` tool commit89d2adadbdAuthor: Neil Alexander <neilalexander@users.noreply.github.com> Date: Fri Jun 10 10:58:04 2022 +0100 Attempt to raise the file descriptor limit at startup (#2527) commit1030072285Author: Neil Alexander <neilalexander@users.noreply.github.com> Date: Fri Jun 10 10:18:32 2022 +0100 Rename the page to "Optimise your installation" commit16ed1633b6Author: Neil Alexander <neilalexander@users.noreply.github.com> Date: Fri Jun 10 10:15:14 2022 +0100 Highlighting in docs commite2a64773ceAuthor: Neil Alexander <neilalexander@users.noreply.github.com> Date: Fri Jun 10 10:14:15 2022 +0100 Add new next steps page to the documentation commit660f7839f5Author: Till <2353100+S7evinK@users.noreply.github.com> Date: Thu Jun 9 18:38:07 2022 +0200 Correctly redact events over federation (#2526) * Ensure we check powerlevel/origin before redacting an event * Add passing test * Use pl.UserLevel * Make check more readable, also check for the sender
183 lines
5.4 KiB
Go
183 lines
5.4 KiB
Go
// Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package streams
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"sync"
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/notifier"
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
)
|
|
|
|
type PresenceStreamProvider struct {
|
|
StreamProvider
|
|
// cache contains previously sent presence updates to avoid unneeded updates
|
|
cache sync.Map
|
|
notifier *notifier.Notifier
|
|
}
|
|
|
|
func (p *PresenceStreamProvider) Setup() {
|
|
p.StreamProvider.Setup()
|
|
|
|
id, err := p.DB.MaxStreamPositionForPresence(context.Background())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
p.latest = id
|
|
}
|
|
|
|
func (p *PresenceStreamProvider) CompleteSync(
|
|
ctx context.Context,
|
|
req *types.SyncRequest,
|
|
) types.StreamPosition {
|
|
return p.IncrementalSync(ctx, req, 0, p.LatestPosition(ctx))
|
|
}
|
|
|
|
func (p *PresenceStreamProvider) IncrementalSync(
|
|
ctx context.Context,
|
|
req *types.SyncRequest,
|
|
from, to types.StreamPosition,
|
|
) types.StreamPosition {
|
|
// We pull out a larger number than the filter asks for, since we're filtering out events later
|
|
presences, err := p.DB.PresenceAfter(ctx, from, gomatrixserverlib.EventFilter{Limit: 1000})
|
|
if err != nil {
|
|
req.Log.WithError(err).Error("p.DB.PresenceAfter failed")
|
|
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 {
|
|
// TODO: Check if this is working better than before.
|
|
if err = p.notifier.LoadRooms(ctx, p.DB, newlyJoined); err != nil {
|
|
req.Log.WithError(err).Error("unable to refresh notifier lists")
|
|
return from
|
|
}
|
|
NewlyJoinedLoop:
|
|
for _, roomID := range newlyJoined {
|
|
roomUsers := p.notifier.JoinedUsers(roomID)
|
|
for i := range roomUsers {
|
|
// we already got a presence from this user
|
|
if _, ok := presences[roomUsers[i]]; ok {
|
|
continue
|
|
}
|
|
// Bear in mind that this might return nil, but at least populating
|
|
// a nil means that there's a map entry so we won't repeat this call.
|
|
presences[roomUsers[i]], err = p.DB.GetPresence(ctx, roomUsers[i])
|
|
if err != nil {
|
|
req.Log.WithError(err).Error("unable to query presence for user")
|
|
return from
|
|
}
|
|
if len(presences) > req.Filter.Presence.Limit {
|
|
break NewlyJoinedLoop
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
lastPos := from
|
|
for _, presence := range presences {
|
|
if presence == nil {
|
|
continue
|
|
}
|
|
// Ignore users we don't share a room with
|
|
if req.Device.UserID != presence.UserID && !p.notifier.IsSharedUser(req.Device.UserID, presence.UserID) {
|
|
continue
|
|
}
|
|
cacheKey := req.Device.UserID + req.Device.ID + presence.UserID
|
|
pres, ok := p.cache.Load(cacheKey)
|
|
if ok {
|
|
// skip already sent presence
|
|
prevPresence := pres.(*types.PresenceInternal)
|
|
currentlyActive := prevPresence.CurrentlyActive()
|
|
skip := prevPresence.Equals(presence) && currentlyActive && req.Device.UserID != presence.UserID
|
|
if skip {
|
|
req.Log.Tracef("Skipping presence, no change (%s)", presence.UserID)
|
|
continue
|
|
}
|
|
}
|
|
|
|
if _, known := types.PresenceFromString(presence.ClientFields.Presence); known {
|
|
presence.ClientFields.LastActiveAgo = presence.LastActiveAgo()
|
|
if presence.ClientFields.Presence == "online" {
|
|
currentlyActive := presence.CurrentlyActive()
|
|
presence.ClientFields.CurrentlyActive = ¤tlyActive
|
|
}
|
|
} else {
|
|
presence.ClientFields.Presence = "offline"
|
|
}
|
|
|
|
content, err := json.Marshal(presence.ClientFields)
|
|
if err != nil {
|
|
return from
|
|
}
|
|
|
|
req.Response.Presence.Events = append(req.Response.Presence.Events, gomatrixserverlib.ClientEvent{
|
|
Content: content,
|
|
Sender: presence.UserID,
|
|
Type: gomatrixserverlib.MPresence,
|
|
})
|
|
if presence.StreamPos > lastPos {
|
|
lastPos = presence.StreamPos
|
|
}
|
|
if len(req.Response.Presence.Events) == req.Filter.Presence.Limit {
|
|
break
|
|
}
|
|
p.cache.Store(cacheKey, presence)
|
|
}
|
|
|
|
if len(req.Response.Presence.Events) == 0 {
|
|
return to
|
|
}
|
|
|
|
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 {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|