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
|
|
|
|
2021-07-14 09:39:17 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2022-08-16 08:29:36 +00:00
|
|
|
|
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
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
for roomID, inviteEvent := range invites {
|
2022-04-07 14:08:19 +00:00
|
|
|
// skip ignored user events
|
|
|
|
if _, ok := req.IgnoredUsers.List[inviteEvent.Sender()]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2021-01-08 16:59:06 +00:00
|
|
|
ir := types.NewInviteResponse(inviteEvent)
|
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
|
|
|
|
if membership == gomatrixserverlib.Join ||
|
|
|
|
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[:]),
|
|
|
|
OriginServerTS: gomatrixserverlib.AsTimestamp(time.Now()),
|
|
|
|
RoomID: roomID,
|
|
|
|
Sender: req.Device.UserID,
|
|
|
|
StateKey: &req.Device.UserID,
|
|
|
|
Type: "m.room.member",
|
|
|
|
Content: gomatrixserverlib.RawJSON(`{"membership":"leave"}`),
|
|
|
|
})
|
|
|
|
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
|
|
|
}
|