2021-01-08 16:59:06 +00:00
|
|
|
package streams
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
2022-10-05 12:47:13 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
|
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"
|
2021-01-08 16:59:06 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ReceiptStreamProvider 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 *ReceiptStreamProvider) Setup(
|
|
|
|
ctx context.Context, snapshot storage.DatabaseTransaction,
|
|
|
|
) {
|
|
|
|
p.DefaultStreamProvider.Setup(ctx, snapshot)
|
2021-01-08 16:59:06 +00:00
|
|
|
|
2022-09-30 11:48:10 +00:00
|
|
|
p.latestMutex.Lock()
|
|
|
|
defer p.latestMutex.Unlock()
|
|
|
|
|
|
|
|
id, err := snapshot.MaxStreamPositionForReceipts(ctx)
|
2021-01-08 16:59:06 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
p.latest = id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ReceiptStreamProvider) 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 *ReceiptStreamProvider) 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 {
|
|
|
|
var joinedRooms []string
|
|
|
|
for roomID, membership := range req.Rooms {
|
|
|
|
if membership == gomatrixserverlib.Join {
|
|
|
|
joinedRooms = append(joinedRooms, roomID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 11:48:10 +00:00
|
|
|
lastPos, receipts, err := snapshot.RoomReceiptsAfter(ctx, joinedRooms, from)
|
2021-01-08 16:59:06 +00: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 12:14:35 +00:00
|
|
|
receiptsByRoom := make(map[string][]types.OutputReceiptEvent)
|
2021-01-08 16:59:06 +00:00
|
|
|
for _, receipt := range receipts {
|
2022-04-07 14:08:19 +00:00
|
|
|
// skip ignored user events
|
|
|
|
if _, ok := req.IgnoredUsers.List[receipt.UserID]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2022-10-11 11:27:21 +00: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 16:59:06 +00:00
|
|
|
receiptsByRoom[receipt.RoomID] = append(receiptsByRoom[receipt.RoomID], receipt)
|
|
|
|
}
|
|
|
|
|
|
|
|
for roomID, receipts := range receiptsByRoom {
|
2022-04-27 11:03:34 +00: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 10:57:13 +00:00
|
|
|
jr, ok := req.Response.Rooms.Join[roomID]
|
|
|
|
if !ok {
|
|
|
|
jr = types.NewJoinResponse()
|
2021-01-13 14:32:49 +00:00
|
|
|
}
|
2021-01-08 16:59:06 +00:00
|
|
|
|
2023-04-04 17:16:53 +00:00
|
|
|
ev := synctypes.ClientEvent{
|
2022-11-29 14:46:28 +00:00
|
|
|
Type: gomatrixserverlib.MReceipt,
|
2021-01-08 16:59:06 +00:00
|
|
|
}
|
2022-03-29 12:14:35 +00:00
|
|
|
content := make(map[string]ReceiptMRead)
|
2021-01-08 16:59:06 +00:00
|
|
|
for _, receipt := range receipts {
|
2022-03-01 16:59:11 +00:00
|
|
|
read, ok := content[receipt.EventID]
|
|
|
|
if !ok {
|
2022-03-29 12:14:35 +00:00
|
|
|
read = ReceiptMRead{
|
|
|
|
User: make(map[string]ReceiptTS),
|
2021-01-08 16:59:06 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-29 12:14:35 +00:00
|
|
|
read.User[receipt.UserID] = ReceiptTS{TS: receipt.Timestamp}
|
2021-01-08 16:59:06 +00: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 12:14:35 +00:00
|
|
|
|
|
|
|
type ReceiptMRead struct {
|
|
|
|
User map[string]ReceiptTS `json:"m.read"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ReceiptTS struct {
|
|
|
|
TS gomatrixserverlib.Timestamp `json:"ts"`
|
|
|
|
}
|