2021-01-08 10:59:06 -06:00
|
|
|
package streams
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
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 SendToDeviceStreamProvider 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 *SendToDeviceStreamProvider) Setup(
|
|
|
|
ctx context.Context, snapshot storage.DatabaseTransaction,
|
|
|
|
) {
|
|
|
|
p.DefaultStreamProvider.Setup(ctx, snapshot)
|
2021-01-13 11:29:46 -06:00
|
|
|
|
2022-09-30 06:48:10 -05:00
|
|
|
p.latestMutex.Lock()
|
|
|
|
defer p.latestMutex.Unlock()
|
|
|
|
|
|
|
|
id, err := snapshot.MaxStreamPositionForSendToDeviceMessages(ctx)
|
2021-01-13 11:29:46 -06:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
p.latest = id
|
|
|
|
}
|
|
|
|
|
2021-01-08 10:59:06 -06:00
|
|
|
func (p *SendToDeviceStreamProvider) 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 *SendToDeviceStreamProvider) 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 {
|
|
|
|
// See if we have any new tasks to do for the send-to-device messaging.
|
2022-09-30 06:48:10 -05:00
|
|
|
lastPos, events, err := snapshot.SendToDeviceUpdatesForSync(req.Context, req.Device.UserID, req.Device.ID, from, to)
|
2021-01-08 10:59:06 -06:00
|
|
|
if err != nil {
|
|
|
|
req.Log.WithError(err).Error("p.DB.SendToDeviceUpdatesForSync failed")
|
2022-09-30 10:07:18 -05:00
|
|
|
_ = snapshot.Rollback()
|
2021-01-08 10:59:06 -06:00
|
|
|
return from
|
|
|
|
}
|
|
|
|
|
2022-08-09 03:40:46 -05:00
|
|
|
// Add the updates into the sync response.
|
|
|
|
for _, event := range events {
|
|
|
|
// skip ignored user events
|
|
|
|
if _, ok := req.IgnoredUsers.List[event.Sender]; ok {
|
|
|
|
continue
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|
2022-08-09 03:40:46 -05:00
|
|
|
req.Response.ToDevice.Events = append(req.Response.ToDevice.Events, event.SendToDeviceEvent)
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return lastPos
|
|
|
|
}
|