2021-01-08 16:59:06 +00:00
|
|
|
package streams
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-14 09:39:17 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
2022-11-02 10:02:23 +00:00
|
|
|
"math"
|
2021-07-14 09:39:17 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2021-01-08 16:59:06 +00:00
|
|
|
|
2023-04-19 14:50:33 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-08-16 08:29:36 +00:00
|
|
|
|
2023-06-06 20:55:18 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2022-09-30 11:48:10 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
2023-04-04 17:16:53 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/synctypes"
|
2022-08-16 08:29:36 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
2021-01-08 16:59:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type InviteStreamProvider struct {
|
2022-09-30 11:48:10 +00:00
|
|
|
DefaultStreamProvider
|
2023-06-06 20:55:18 +00:00
|
|
|
rsAPI api.SyncRoomserverAPI
|
2021-01-08 16:59:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-30 11:48:10 +00:00
|
|
|
func (p *InviteStreamProvider) Setup(
|
|
|
|
ctx context.Context, snapshot storage.DatabaseTransaction,
|
|
|
|
) {
|
|
|
|
p.DefaultStreamProvider.Setup(ctx, snapshot)
|
2021-01-08 16:59:06 +00:00
|
|
|
|
|
|
|
p.latestMutex.Lock()
|
|
|
|
defer p.latestMutex.Unlock()
|
|
|
|
|
2022-09-30 11:48:10 +00:00
|
|
|
id, err := snapshot.MaxStreamPositionForInvites(ctx)
|
2021-01-08 16:59:06 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
p.latest = id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *InviteStreamProvider) CompleteSync(
|
|
|
|
ctx context.Context,
|
2022-09-30 11:48:10 +00:00
|
|
|
snapshot storage.DatabaseTransaction,
|
2021-01-08 16:59:06 +00:00
|
|
|
req *types.SyncRequest,
|
|
|
|
) types.StreamPosition {
|
2022-09-30 11:48:10 +00:00
|
|
|
return p.IncrementalSync(ctx, snapshot, req, 0, p.LatestPosition(ctx))
|
2021-01-08 16:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *InviteStreamProvider) IncrementalSync(
|
|
|
|
ctx context.Context,
|
2022-09-30 11:48:10 +00:00
|
|
|
snapshot storage.DatabaseTransaction,
|
2021-01-08 16:59:06 +00:00
|
|
|
req *types.SyncRequest,
|
|
|
|
from, to types.StreamPosition,
|
|
|
|
) types.StreamPosition {
|
|
|
|
r := types.Range{
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
}
|
|
|
|
|
2022-09-30 11:48:10 +00:00
|
|
|
invites, retiredInvites, maxID, err := snapshot.InviteEventsInRange(
|
2021-01-08 16:59:06 +00:00
|
|
|
ctx, req.Device.UserID, r,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
req.Log.WithError(err).Error("p.DB.InviteEventsInRange failed")
|
|
|
|
return from
|
|
|
|
}
|
|
|
|
|
2023-08-31 15:33:38 +00:00
|
|
|
eventFormat := synctypes.FormatSync
|
|
|
|
if req.Filter.EventFormat == synctypes.EventFormatFederation {
|
|
|
|
eventFormat = synctypes.FormatSyncFederation
|
|
|
|
}
|
|
|
|
|
2021-01-08 16:59:06 +00:00
|
|
|
for roomID, inviteEvent := range invites {
|
2023-06-06 20:55:18 +00:00
|
|
|
user := spec.UserID{}
|
2023-09-15 14:39:06 +00:00
|
|
|
sender, err := p.rsAPI.QueryUserIDForSender(ctx, inviteEvent.RoomID(), inviteEvent.SenderID())
|
2023-06-06 20:55:18 +00:00
|
|
|
if err == nil && sender != nil {
|
|
|
|
user = *sender
|
|
|
|
}
|
|
|
|
|
2022-04-07 14:08:19 +00:00
|
|
|
// skip ignored user events
|
2023-06-06 20:55:18 +00:00
|
|
|
if _, ok := req.IgnoredUsers.List[user.String()]; ok {
|
2022-04-07 14:08:19 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-09-15 15:25:09 +00:00
|
|
|
ir, err := types.NewInviteResponse(ctx, p.rsAPI, inviteEvent, eventFormat)
|
|
|
|
if err != nil {
|
|
|
|
req.Log.WithError(err).Error("failed creating invite response")
|
|
|
|
continue
|
|
|
|
}
|
2022-10-05 12:47:13 +00:00
|
|
|
req.Response.Rooms.Invite[roomID] = ir
|
2021-01-08 16:59:06 +00:00
|
|
|
}
|
|
|
|
|
2022-08-16 08:29:36 +00:00
|
|
|
// When doing an initial sync, we don't want to add retired invites, as this
|
|
|
|
// can add rooms we were invited to, but already left.
|
|
|
|
if from == 0 {
|
|
|
|
return to
|
|
|
|
}
|
2021-01-08 16:59:06 +00:00
|
|
|
for roomID := range retiredInvites {
|
2022-11-02 10:02:23 +00:00
|
|
|
membership, _, err := snapshot.SelectMembershipForUser(ctx, roomID, req.Device.UserID, math.MaxInt64)
|
|
|
|
// Skip if the user is an existing member of the room.
|
|
|
|
// Otherwise, the NewLeaveResponse will eject the user from the room unintentionally
|
2023-04-19 14:50:33 +00:00
|
|
|
if membership == spec.Join ||
|
2022-11-02 10:02:23 +00:00
|
|
|
err != nil {
|
2022-10-21 08:26:22 +00:00
|
|
|
continue
|
2021-01-08 16:59:06 +00:00
|
|
|
}
|
2022-11-02 10:02:23 +00:00
|
|
|
|
2022-10-21 08:26:22 +00:00
|
|
|
lr := types.NewLeaveResponse()
|
|
|
|
h := sha256.Sum256(append([]byte(roomID), []byte(strconv.FormatInt(int64(to), 10))...))
|
2023-04-04 17:16:53 +00:00
|
|
|
lr.Timeline.Events = append(lr.Timeline.Events, synctypes.ClientEvent{
|
2022-10-21 08:26:22 +00:00
|
|
|
// fake event ID which muxes in the to position
|
|
|
|
EventID: "$" + base64.RawURLEncoding.EncodeToString(h[:]),
|
2023-04-19 14:50:33 +00:00
|
|
|
OriginServerTS: spec.AsTimestamp(time.Now()),
|
2022-10-21 08:26:22 +00:00
|
|
|
RoomID: roomID,
|
|
|
|
Sender: req.Device.UserID,
|
|
|
|
StateKey: &req.Device.UserID,
|
|
|
|
Type: "m.room.member",
|
2023-04-19 14:50:33 +00:00
|
|
|
Content: spec.RawJSON(`{"membership":"leave"}`),
|
2022-10-21 08:26:22 +00:00
|
|
|
})
|
|
|
|
req.Response.Rooms.Leave[roomID] = lr
|
2021-01-08 16:59:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-30 11:48:10 +00:00
|
|
|
return maxID
|
2021-01-08 16:59:06 +00:00
|
|
|
}
|