2021-01-08 17:59:06 +01:00
|
|
|
package streams
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
2022-09-30 13:48:10 +02:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
2021-01-08 17:59:06 +01:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ReceiptStreamProvider struct {
|
2022-09-30 13:48:10 +02:00
|
|
|
DefaultStreamProvider
|
2021-01-08 17:59:06 +01:00
|
|
|
}
|
|
|
|
|
2022-09-30 13:48:10 +02:00
|
|
|
func (p *ReceiptStreamProvider) Setup(
|
|
|
|
ctx context.Context, snapshot storage.DatabaseTransaction,
|
|
|
|
) {
|
|
|
|
p.DefaultStreamProvider.Setup(ctx, snapshot)
|
2021-01-08 17:59:06 +01:00
|
|
|
|
2022-09-30 13:48:10 +02:00
|
|
|
p.latestMutex.Lock()
|
|
|
|
defer p.latestMutex.Unlock()
|
|
|
|
|
|
|
|
id, err := snapshot.MaxStreamPositionForReceipts(ctx)
|
2021-01-08 17:59:06 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
p.latest = id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ReceiptStreamProvider) 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 *ReceiptStreamProvider) 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 {
|
|
|
|
var joinedRooms []string
|
|
|
|
for roomID, membership := range req.Rooms {
|
|
|
|
if membership == gomatrixserverlib.Join {
|
|
|
|
joinedRooms = append(joinedRooms, roomID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 13:48:10 +02:00
|
|
|
lastPos, receipts, err := snapshot.RoomReceiptsAfter(ctx, joinedRooms, from)
|
2021-01-08 17:59:06 +01:00
|
|
|
if err != nil {
|
|
|
|
req.Log.WithError(err).Error("p.DB.RoomReceiptsAfter failed")
|
2022-09-30 17:07:18 +02:00
|
|
|
_ = snapshot.Rollback()
|
2021-01-08 17:59:06 +01:00
|
|
|
return from
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(receipts) == 0 || lastPos == 0 {
|
|
|
|
return to
|
|
|
|
}
|
|
|
|
|
|
|
|
// Group receipts by room, so we can create one ClientEvent for every room
|
2022-03-29 14:14:35 +02:00
|
|
|
receiptsByRoom := make(map[string][]types.OutputReceiptEvent)
|
2021-01-08 17:59:06 +01:00
|
|
|
for _, receipt := range receipts {
|
2022-04-07 16:08:19 +02:00
|
|
|
// skip ignored user events
|
|
|
|
if _, ok := req.IgnoredUsers.List[receipt.UserID]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2021-01-08 17:59:06 +01:00
|
|
|
receiptsByRoom[receipt.RoomID] = append(receiptsByRoom[receipt.RoomID], receipt)
|
|
|
|
}
|
|
|
|
|
|
|
|
for roomID, receipts := range receiptsByRoom {
|
2022-04-27 13:03:34 +02:00
|
|
|
// For a complete sync, make sure we're only including this room if
|
|
|
|
// that room was present in the joined rooms.
|
|
|
|
if from == 0 && !req.IsRoomPresent(roomID) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-01-13 15:32:49 +01:00
|
|
|
jr := *types.NewJoinResponse()
|
|
|
|
if existing, ok := req.Response.Rooms.Join[roomID]; ok {
|
|
|
|
jr = existing
|
|
|
|
}
|
2021-01-08 17:59:06 +01:00
|
|
|
|
|
|
|
ev := gomatrixserverlib.ClientEvent{
|
|
|
|
Type: gomatrixserverlib.MReceipt,
|
|
|
|
RoomID: roomID,
|
|
|
|
}
|
2022-03-29 14:14:35 +02:00
|
|
|
content := make(map[string]ReceiptMRead)
|
2021-01-08 17:59:06 +01:00
|
|
|
for _, receipt := range receipts {
|
2022-03-01 17:59:11 +01:00
|
|
|
read, ok := content[receipt.EventID]
|
|
|
|
if !ok {
|
2022-03-29 14:14:35 +02:00
|
|
|
read = ReceiptMRead{
|
|
|
|
User: make(map[string]ReceiptTS),
|
2021-01-08 17:59:06 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-29 14:14:35 +02:00
|
|
|
read.User[receipt.UserID] = ReceiptTS{TS: receipt.Timestamp}
|
2021-01-08 17:59:06 +01:00
|
|
|
content[receipt.EventID] = read
|
|
|
|
}
|
|
|
|
ev.Content, err = json.Marshal(content)
|
|
|
|
if err != nil {
|
|
|
|
req.Log.WithError(err).Error("json.Marshal failed")
|
|
|
|
return from
|
|
|
|
}
|
|
|
|
|
|
|
|
jr.Ephemeral.Events = append(jr.Ephemeral.Events, ev)
|
|
|
|
req.Response.Rooms.Join[roomID] = jr
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastPos
|
|
|
|
}
|
2022-03-29 14:14:35 +02:00
|
|
|
|
|
|
|
type ReceiptMRead struct {
|
|
|
|
User map[string]ReceiptTS `json:"m.read"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ReceiptTS struct {
|
|
|
|
TS gomatrixserverlib.Timestamp `json:"ts"`
|
|
|
|
}
|