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