2021-01-08 17:59:06 +01:00
|
|
|
package streams
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-14 11:39:17 +02:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
2022-11-02 11:02:23 +01:00
|
|
|
"math"
|
2021-07-14 11:39:17 +02:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2021-01-08 17:59:06 +01:00
|
|
|
|
2023-04-19 16:50:33 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-08-16 10:29:36 +02:00
|
|
|
|
2023-06-06 22:55:18 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2022-09-30 13:48:10 +02:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
2023-04-04 19:16:53 +02:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/synctypes"
|
2022-08-16 10:29:36 +02:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
2021-01-08 17:59:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type InviteStreamProvider struct {
|
2022-09-30 13:48:10 +02:00
|
|
|
DefaultStreamProvider
|
2023-06-06 22:55:18 +02:00
|
|
|
rsAPI api.SyncRoomserverAPI
|
2021-01-08 17:59:06 +01:00
|
|
|
}
|
|
|
|
|
2022-09-30 13:48:10 +02:00
|
|
|
func (p *InviteStreamProvider) Setup(
|
|
|
|
ctx context.Context, snapshot storage.DatabaseTransaction,
|
|
|
|
) {
|
|
|
|
p.DefaultStreamProvider.Setup(ctx, snapshot)
|
2021-01-08 17:59:06 +01:00
|
|
|
|
|
|
|
p.latestMutex.Lock()
|
|
|
|
defer p.latestMutex.Unlock()
|
|
|
|
|
2022-09-30 13:48:10 +02:00
|
|
|
id, err := snapshot.MaxStreamPositionForInvites(ctx)
|
2021-01-08 17:59:06 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
p.latest = id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *InviteStreamProvider) CompleteSync(
|
|
|
|
ctx context.Context,
|
2022-09-30 13:48:10 +02:00
|
|
|
snapshot storage.DatabaseTransaction,
|
2021-01-08 17:59:06 +01:00
|
|
|
req *types.SyncRequest,
|
|
|
|
) types.StreamPosition {
|
2022-09-30 13:48:10 +02:00
|
|
|
return p.IncrementalSync(ctx, snapshot, req, 0, p.LatestPosition(ctx))
|
2021-01-08 17:59:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *InviteStreamProvider) IncrementalSync(
|
|
|
|
ctx context.Context,
|
2022-09-30 13:48:10 +02:00
|
|
|
snapshot storage.DatabaseTransaction,
|
2021-01-08 17:59:06 +01:00
|
|
|
req *types.SyncRequest,
|
|
|
|
from, to types.StreamPosition,
|
|
|
|
) types.StreamPosition {
|
|
|
|
r := types.Range{
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
}
|
|
|
|
|
2022-09-30 13:48:10 +02:00
|
|
|
invites, retiredInvites, maxID, err := snapshot.InviteEventsInRange(
|
2021-01-08 17:59:06 +01:00
|
|
|
ctx, req.Device.UserID, r,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
req.Log.WithError(err).Error("p.DB.InviteEventsInRange failed")
|
|
|
|
return from
|
|
|
|
}
|
|
|
|
|
|
|
|
for roomID, inviteEvent := range invites {
|
2023-06-06 22:55:18 +02:00
|
|
|
user := spec.UserID{}
|
2023-06-14 16:23:46 +02:00
|
|
|
validRoomID, err := spec.NewRoomID(inviteEvent.RoomID())
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sender, err := p.rsAPI.QueryUserIDForSender(ctx, *validRoomID, inviteEvent.SenderID())
|
2023-06-06 22:55:18 +02:00
|
|
|
if err == nil && sender != nil {
|
|
|
|
user = *sender
|
|
|
|
}
|
|
|
|
|
2023-06-12 13:19:25 +02:00
|
|
|
sk := inviteEvent.StateKey()
|
|
|
|
if sk != nil && *sk != "" {
|
2023-06-14 16:23:46 +02:00
|
|
|
skUserID, err := p.rsAPI.QueryUserIDForSender(ctx, *validRoomID, spec.SenderID(*inviteEvent.StateKey()))
|
2023-06-12 13:19:25 +02:00
|
|
|
if err == nil && skUserID != nil {
|
|
|
|
skString := skUserID.String()
|
|
|
|
sk = &skString
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 16:08:19 +02:00
|
|
|
// skip ignored user events
|
2023-06-06 22:55:18 +02:00
|
|
|
if _, ok := req.IgnoredUsers.List[user.String()]; ok {
|
2022-04-07 16:08:19 +02:00
|
|
|
continue
|
|
|
|
}
|
2023-06-12 13:19:25 +02:00
|
|
|
ir := types.NewInviteResponse(inviteEvent, user, sk)
|
2022-10-05 14:47:13 +02:00
|
|
|
req.Response.Rooms.Invite[roomID] = ir
|
2021-01-08 17:59:06 +01:00
|
|
|
}
|
|
|
|
|
2022-08-16 10:29:36 +02: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 17:59:06 +01:00
|
|
|
for roomID := range retiredInvites {
|
2022-11-02 11:02:23 +01: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 16:50:33 +02:00
|
|
|
if membership == spec.Join ||
|
2022-11-02 11:02:23 +01:00
|
|
|
err != nil {
|
2022-10-21 10:26:22 +02:00
|
|
|
continue
|
2021-01-08 17:59:06 +01:00
|
|
|
}
|
2022-11-02 11:02:23 +01:00
|
|
|
|
2022-10-21 10:26:22 +02:00
|
|
|
lr := types.NewLeaveResponse()
|
|
|
|
h := sha256.Sum256(append([]byte(roomID), []byte(strconv.FormatInt(int64(to), 10))...))
|
2023-04-04 19:16:53 +02:00
|
|
|
lr.Timeline.Events = append(lr.Timeline.Events, synctypes.ClientEvent{
|
2022-10-21 10:26:22 +02:00
|
|
|
// fake event ID which muxes in the to position
|
|
|
|
EventID: "$" + base64.RawURLEncoding.EncodeToString(h[:]),
|
2023-04-19 16:50:33 +02:00
|
|
|
OriginServerTS: spec.AsTimestamp(time.Now()),
|
2022-10-21 10:26:22 +02:00
|
|
|
RoomID: roomID,
|
|
|
|
Sender: req.Device.UserID,
|
|
|
|
StateKey: &req.Device.UserID,
|
|
|
|
Type: "m.room.member",
|
2023-04-19 16:50:33 +02:00
|
|
|
Content: spec.RawJSON(`{"membership":"leave"}`),
|
2022-10-21 10:26:22 +02:00
|
|
|
})
|
|
|
|
req.Response.Rooms.Leave[roomID] = lr
|
2021-01-08 17:59:06 +01:00
|
|
|
}
|
|
|
|
|
2022-09-30 13:48:10 +02:00
|
|
|
return maxID
|
2021-01-08 17:59:06 +01:00
|
|
|
}
|