2021-01-08 16:59:06 +00:00
|
|
|
package streams
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AccountDataStreamProvider struct {
|
|
|
|
StreamProvider
|
|
|
|
userAPI userapi.UserInternalAPI
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *AccountDataStreamProvider) Setup() {
|
|
|
|
p.StreamProvider.Setup()
|
|
|
|
|
|
|
|
p.latestMutex.Lock()
|
|
|
|
defer p.latestMutex.Unlock()
|
|
|
|
|
|
|
|
id, err := p.DB.MaxStreamPositionForAccountData(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
p.latest = id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *AccountDataStreamProvider) CompleteSync(
|
|
|
|
ctx context.Context,
|
|
|
|
req *types.SyncRequest,
|
|
|
|
) types.StreamPosition {
|
2022-04-25 17:04:46 +00:00
|
|
|
return p.IncrementalSync(ctx, req, 0, p.LatestPosition(ctx))
|
2021-01-08 16:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *AccountDataStreamProvider) IncrementalSync(
|
|
|
|
ctx context.Context,
|
|
|
|
req *types.SyncRequest,
|
|
|
|
from, to types.StreamPosition,
|
|
|
|
) types.StreamPosition {
|
|
|
|
r := types.Range{
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
}
|
|
|
|
|
|
|
|
dataTypes, err := p.DB.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 {
|
|
|
|
// 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 {
|
2021-01-13 14:32:49 +00:00
|
|
|
joinData := *types.NewJoinResponse()
|
|
|
|
if existing, ok := req.Response.Rooms.Join[roomID]; ok {
|
|
|
|
joinData = existing
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return to
|
|
|
|
}
|