2021-01-08 16:59:06 +00:00
|
|
|
package streams
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-09-27 13:01:34 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
|
2022-09-30 11:48:10 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
2021-01-08 16:59:06 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AccountDataStreamProvider struct {
|
2022-09-30 11:48:10 +00:00
|
|
|
DefaultStreamProvider
|
2022-05-05 08:56:03 +00:00
|
|
|
userAPI userapi.SyncUserAPI
|
2021-01-08 16:59:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-30 11:48:10 +00:00
|
|
|
func (p *AccountDataStreamProvider) Setup(
|
|
|
|
ctx context.Context, snapshot storage.DatabaseTransaction,
|
|
|
|
) {
|
|
|
|
p.DefaultStreamProvider.Setup(ctx, snapshot)
|
2021-01-08 16:59:06 +00:00
|
|
|
|
|
|
|
p.latestMutex.Lock()
|
|
|
|
defer p.latestMutex.Unlock()
|
|
|
|
|
2022-09-30 11:48:10 +00:00
|
|
|
id, err := snapshot.MaxStreamPositionForAccountData(ctx)
|
2021-01-08 16:59:06 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
p.latest = id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *AccountDataStreamProvider) 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 *AccountDataStreamProvider) 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 {
|
|
|
|
r := types.Range{
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
}
|
|
|
|
|
2022-09-30 11:48:10 +00:00
|
|
|
dataTypes, pos, err := snapshot.GetAccountDataInRange(
|
2022-04-25 17:04:46 +00:00
|
|
|
ctx, req.Device.UserID, r, &req.Filter.AccountData,
|
2021-01-08 16:59:06 +00: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 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 && roomID != "" && !req.IsRoomPresent(roomID) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-01-08 16:59:06 +00: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 12:47:13 +00:00
|
|
|
joinData, ok := req.Response.Rooms.Join[roomID]
|
|
|
|
if !ok {
|
|
|
|
joinData = types.NewJoinResponse()
|
2021-01-13 14:32:49 +00:00
|
|
|
}
|
2021-01-08 16:59:06 +00: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 13:50:56 +00:00
|
|
|
return pos
|
2021-01-08 16:59:06 +00:00
|
|
|
}
|