2021-01-08 10:59:06 -06:00
|
|
|
package streams
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-14 04:39:17 -05:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
2021-01-08 10:59:06 -06:00
|
|
|
|
2021-07-14 04:39:17 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2022-08-16 03:29:36 -05:00
|
|
|
|
2022-09-30 06:48:10 -05:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
2022-08-16 03:29:36 -05:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
2021-01-08 10:59:06 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type InviteStreamProvider struct {
|
2022-09-30 06:48:10 -05:00
|
|
|
DefaultStreamProvider
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|
|
|
|
|
2022-09-30 06:48:10 -05:00
|
|
|
func (p *InviteStreamProvider) Setup(
|
|
|
|
ctx context.Context, snapshot storage.DatabaseTransaction,
|
|
|
|
) {
|
|
|
|
p.DefaultStreamProvider.Setup(ctx, snapshot)
|
2021-01-08 10:59:06 -06:00
|
|
|
|
|
|
|
p.latestMutex.Lock()
|
|
|
|
defer p.latestMutex.Unlock()
|
|
|
|
|
2022-09-30 06:48:10 -05:00
|
|
|
id, err := snapshot.MaxStreamPositionForInvites(ctx)
|
2021-01-08 10:59:06 -06:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
p.latest = id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *InviteStreamProvider) CompleteSync(
|
|
|
|
ctx context.Context,
|
2022-09-30 06:48:10 -05:00
|
|
|
snapshot storage.DatabaseTransaction,
|
2021-01-08 10:59:06 -06:00
|
|
|
req *types.SyncRequest,
|
|
|
|
) types.StreamPosition {
|
2022-09-30 06:48:10 -05:00
|
|
|
return p.IncrementalSync(ctx, snapshot, req, 0, p.LatestPosition(ctx))
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *InviteStreamProvider) IncrementalSync(
|
|
|
|
ctx context.Context,
|
2022-09-30 06:48:10 -05:00
|
|
|
snapshot storage.DatabaseTransaction,
|
2021-01-08 10:59:06 -06:00
|
|
|
req *types.SyncRequest,
|
|
|
|
from, to types.StreamPosition,
|
|
|
|
) types.StreamPosition {
|
|
|
|
r := types.Range{
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
}
|
|
|
|
|
2022-09-30 06:48:10 -05:00
|
|
|
invites, retiredInvites, maxID, err := snapshot.InviteEventsInRange(
|
2021-01-08 10:59:06 -06: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 09:08:19 -05:00
|
|
|
// skip ignored user events
|
|
|
|
if _, ok := req.IgnoredUsers.List[inviteEvent.Sender()]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2021-01-08 10:59:06 -06:00
|
|
|
ir := types.NewInviteResponse(inviteEvent)
|
2022-10-05 07:47:13 -05:00
|
|
|
req.Response.Rooms.Invite[roomID] = ir
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|
|
|
|
|
2022-08-16 03:29:36 -05: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 10:59:06 -06:00
|
|
|
for roomID := range retiredInvites {
|
2022-10-21 03:26:22 -05:00
|
|
|
if _, ok := req.Response.Rooms.Invite[roomID]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := req.Response.Rooms.Join[roomID]; ok {
|
|
|
|
continue
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|
2022-10-28 03:50:33 -05:00
|
|
|
|
2022-10-21 03:26:22 -05:00
|
|
|
lr := types.NewLeaveResponse()
|
|
|
|
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"}`),
|
|
|
|
})
|
|
|
|
req.Response.Rooms.Leave[roomID] = lr
|
|
|
|
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|
|
|
|
|
2022-09-30 06:48:10 -05:00
|
|
|
return maxID
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|