2021-01-08 17:59:06 +01:00
|
|
|
package streams
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
2023-04-19 16:50:33 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-10-05 14:47:13 +02:00
|
|
|
|
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"
|
2021-01-08 17:59:06 +01:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2023-04-19 16:50:33 +02:00
|
|
|
if membership == spec.Join {
|
2021-01-08 17:59:06 +01:00
|
|
|
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")
|
|
|
|
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
|
|
|
|
}
|
2022-10-11 13:27:21 +02:00
|
|
|
// Don't send private read receipts to other users
|
|
|
|
if receipt.Type == "m.read.private" && req.Device.UserID != receipt.UserID {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-06 12:57:13 +02:00
|
|
|
jr, ok := req.Response.Rooms.Join[roomID]
|
|
|
|
if !ok {
|
|
|
|
jr = types.NewJoinResponse()
|
2021-01-13 15:32:49 +01:00
|
|
|
}
|
2021-01-08 17:59:06 +01:00
|
|
|
|
2023-04-04 19:16:53 +02:00
|
|
|
ev := synctypes.ClientEvent{
|
2023-04-19 16:50:33 +02:00
|
|
|
Type: spec.MReceipt,
|
2021-01-08 17:59:06 +01:00
|
|
|
}
|
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 {
|
2023-04-19 16:50:33 +02:00
|
|
|
TS spec.Timestamp `json:"ts"`
|
2022-03-29 14:14:35 +02:00
|
|
|
}
|