2021-01-08 16:59:06 +00:00
|
|
|
package streams
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-14 10:39:17 +01:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
2021-01-08 16:59:06 +00:00
|
|
|
|
2021-07-14 10:39:17 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2022-08-16 09:29:36 +01:00
|
|
|
|
2022-09-30 12:48:10 +01:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
2022-08-16 09:29:36 +01:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
2021-01-08 16:59:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type InviteStreamProvider struct {
|
2022-09-30 12:48:10 +01:00
|
|
|
DefaultStreamProvider
|
2021-01-08 16:59:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-30 12:48:10 +01: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 12:48:10 +01: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 12:48:10 +01:00
|
|
|
snapshot storage.DatabaseTransaction,
|
2021-01-08 16:59:06 +00:00
|
|
|
req *types.SyncRequest,
|
|
|
|
) types.StreamPosition {
|
2022-09-30 12:48:10 +01: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 12:48:10 +01: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 12:48:10 +01: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 15:08:19 +01: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 13:47:13 +01:00
|
|
|
req.Response.Rooms.Invite[roomID] = ir
|
2021-01-08 16:59:06 +00:00
|
|
|
}
|
|
|
|
|
2022-08-16 09:29:36 +01: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 {
|
|
|
|
if _, ok := req.Response.Rooms.Join[roomID]; !ok {
|
|
|
|
lr := types.NewLeaveResponse()
|
2021-07-14 10:39:17 +01:00
|
|
|
h := sha256.Sum256(append([]byte(roomID), []byte(strconv.FormatInt(int64(to), 10))...))
|
|
|
|
lr.Timeline.Events = append(lr.Timeline.Events, gomatrixserverlib.ClientEvent{
|
|
|
|
// 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"}`),
|
|
|
|
})
|
2022-10-05 13:47:13 +01:00
|
|
|
req.Response.Rooms.Leave[roomID] = lr
|
2021-01-08 16:59:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 12:48:10 +01:00
|
|
|
return maxID
|
2021-01-08 16:59:06 +00:00
|
|
|
}
|