2021-01-08 17:59:06 +01:00
|
|
|
package streams
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-09-27 15:01:34 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
|
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"
|
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AccountDataStreamProvider struct {
|
2022-09-30 13:48:10 +02:00
|
|
|
DefaultStreamProvider
|
2022-05-05 10:56:03 +02:00
|
|
|
userAPI userapi.SyncUserAPI
|
2021-01-08 17:59:06 +01:00
|
|
|
}
|
|
|
|
|
2022-09-30 13:48:10 +02:00
|
|
|
func (p *AccountDataStreamProvider) Setup(
|
|
|
|
ctx context.Context, snapshot storage.DatabaseTransaction,
|
|
|
|
) {
|
|
|
|
p.DefaultStreamProvider.Setup(ctx, snapshot)
|
2021-01-08 17:59:06 +01:00
|
|
|
|
|
|
|
p.latestMutex.Lock()
|
|
|
|
defer p.latestMutex.Unlock()
|
|
|
|
|
2022-09-30 13:48:10 +02:00
|
|
|
id, err := snapshot.MaxStreamPositionForAccountData(ctx)
|
2021-01-08 17:59:06 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
p.latest = id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *AccountDataStreamProvider) 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 *AccountDataStreamProvider) 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 {
|
|
|
|
r := types.Range{
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
}
|
|
|
|
|
2022-09-30 13:48:10 +02:00
|
|
|
dataTypes, pos, err := snapshot.GetAccountDataInRange(
|
2022-04-25 19:04:46 +02:00
|
|
|
ctx, req.Device.UserID, r, &req.Filter.AccountData,
|
2021-01-08 17:59:06 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
req.Log.WithError(err).Error("p.DB.GetAccountDataInRange failed")
|
|
|
|
return from
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over the rooms
|
|
|
|
for roomID, dataTypes := range dataTypes {
|
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 && roomID != "" && !req.IsRoomPresent(roomID) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-01-08 17:59:06 +01:00
|
|
|
// Request the missing data from the database
|
|
|
|
for _, dataType := range dataTypes {
|
|
|
|
dataReq := userapi.QueryAccountDataRequest{
|
|
|
|
UserID: req.Device.UserID,
|
|
|
|
RoomID: roomID,
|
|
|
|
DataType: dataType,
|
|
|
|
}
|
|
|
|
dataRes := userapi.QueryAccountDataResponse{}
|
|
|
|
err = p.userAPI.QueryAccountData(ctx, &dataReq, &dataRes)
|
|
|
|
if err != nil {
|
|
|
|
req.Log.WithError(err).Error("p.userAPI.QueryAccountData failed")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if roomID == "" {
|
|
|
|
if globalData, ok := dataRes.GlobalAccountData[dataType]; ok {
|
|
|
|
req.Response.AccountData.Events = append(
|
|
|
|
req.Response.AccountData.Events,
|
|
|
|
gomatrixserverlib.ClientEvent{
|
|
|
|
Type: dataType,
|
|
|
|
Content: gomatrixserverlib.RawJSON(globalData),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if roomData, ok := dataRes.RoomAccountData[roomID][dataType]; ok {
|
2022-10-05 14:47:13 +02:00
|
|
|
joinData, ok := req.Response.Rooms.Join[roomID]
|
|
|
|
if !ok {
|
|
|
|
joinData = types.NewJoinResponse()
|
2021-01-13 15:32:49 +01:00
|
|
|
}
|
2021-01-08 17:59:06 +01:00
|
|
|
joinData.AccountData.Events = append(
|
|
|
|
joinData.AccountData.Events,
|
|
|
|
gomatrixserverlib.ClientEvent{
|
|
|
|
Type: dataType,
|
|
|
|
Content: gomatrixserverlib.RawJSON(roomData),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
req.Response.Rooms.Join[roomID] = joinData
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-26 15:50:56 +02:00
|
|
|
return pos
|
2021-01-08 17:59:06 +01:00
|
|
|
}
|