2021-01-08 10:59:06 -06:00
|
|
|
package streams
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-28 02:44:27 -05:00
|
|
|
"fmt"
|
2021-01-08 10:59:06 -06:00
|
|
|
|
2022-03-29 07:14:35 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
2022-09-30 10:07:18 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2021-01-08 10:59:06 -06:00
|
|
|
keyapi "github.com/matrix-org/dendrite/keyserver/api"
|
|
|
|
rsapi "github.com/matrix-org/dendrite/roomserver/api"
|
2022-04-06 06:11:19 -05:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/notifier"
|
2021-01-08 10:59:06 -06:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Streams struct {
|
2022-09-30 06:48:10 -05:00
|
|
|
PDUStreamProvider StreamProvider
|
|
|
|
TypingStreamProvider StreamProvider
|
|
|
|
ReceiptStreamProvider StreamProvider
|
|
|
|
InviteStreamProvider StreamProvider
|
|
|
|
SendToDeviceStreamProvider StreamProvider
|
|
|
|
AccountDataStreamProvider StreamProvider
|
|
|
|
DeviceListStreamProvider StreamProvider
|
|
|
|
NotificationDataStreamProvider StreamProvider
|
|
|
|
PresenceStreamProvider StreamProvider
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSyncStreamProviders(
|
2022-05-05 03:56:03 -05:00
|
|
|
d storage.Database, userAPI userapi.SyncUserAPI,
|
|
|
|
rsAPI rsapi.SyncRoomserverAPI, keyAPI keyapi.SyncKeyAPI,
|
2022-05-06 08:33:34 -05:00
|
|
|
eduCache *caching.EDUCache, lazyLoadCache caching.LazyLoadCache, notifier *notifier.Notifier,
|
2021-01-08 10:59:06 -06:00
|
|
|
) *Streams {
|
|
|
|
streams := &Streams{
|
|
|
|
PDUStreamProvider: &PDUStreamProvider{
|
2022-09-30 06:48:10 -05:00
|
|
|
DefaultStreamProvider: DefaultStreamProvider{DB: d},
|
|
|
|
lazyLoadCache: lazyLoadCache,
|
|
|
|
rsAPI: rsAPI,
|
|
|
|
notifier: notifier,
|
2021-01-08 10:59:06 -06:00
|
|
|
},
|
|
|
|
TypingStreamProvider: &TypingStreamProvider{
|
2022-09-30 06:48:10 -05:00
|
|
|
DefaultStreamProvider: DefaultStreamProvider{DB: d},
|
|
|
|
EDUCache: eduCache,
|
2021-01-08 10:59:06 -06:00
|
|
|
},
|
|
|
|
ReceiptStreamProvider: &ReceiptStreamProvider{
|
2022-09-30 06:48:10 -05:00
|
|
|
DefaultStreamProvider: DefaultStreamProvider{DB: d},
|
2021-01-08 10:59:06 -06:00
|
|
|
},
|
|
|
|
InviteStreamProvider: &InviteStreamProvider{
|
2022-09-30 06:48:10 -05:00
|
|
|
DefaultStreamProvider: DefaultStreamProvider{DB: d},
|
2021-01-08 10:59:06 -06:00
|
|
|
},
|
|
|
|
SendToDeviceStreamProvider: &SendToDeviceStreamProvider{
|
2022-09-30 06:48:10 -05:00
|
|
|
DefaultStreamProvider: DefaultStreamProvider{DB: d},
|
2021-01-08 10:59:06 -06:00
|
|
|
},
|
|
|
|
AccountDataStreamProvider: &AccountDataStreamProvider{
|
2022-09-30 06:48:10 -05:00
|
|
|
DefaultStreamProvider: DefaultStreamProvider{DB: d},
|
|
|
|
userAPI: userAPI,
|
2021-01-08 10:59:06 -06:00
|
|
|
},
|
2022-03-03 05:40:53 -06:00
|
|
|
NotificationDataStreamProvider: &NotificationDataStreamProvider{
|
2022-09-30 06:48:10 -05:00
|
|
|
DefaultStreamProvider: DefaultStreamProvider{DB: d},
|
2022-03-03 05:40:53 -06:00
|
|
|
},
|
2021-01-08 10:59:06 -06:00
|
|
|
DeviceListStreamProvider: &DeviceListStreamProvider{
|
2022-09-30 06:48:10 -05:00
|
|
|
DefaultStreamProvider: DefaultStreamProvider{DB: d},
|
|
|
|
rsAPI: rsAPI,
|
|
|
|
keyAPI: keyAPI,
|
2021-01-08 10:59:06 -06:00
|
|
|
},
|
2022-04-06 06:11:19 -05:00
|
|
|
PresenceStreamProvider: &PresenceStreamProvider{
|
2022-09-30 06:48:10 -05:00
|
|
|
DefaultStreamProvider: DefaultStreamProvider{DB: d},
|
|
|
|
notifier: notifier,
|
2022-04-06 06:11:19 -05:00
|
|
|
},
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|
|
|
|
|
2022-09-30 06:48:10 -05:00
|
|
|
ctx := context.TODO()
|
|
|
|
snapshot, err := d.NewDatabaseSnapshot(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-09-30 10:07:18 -05:00
|
|
|
var succeeded bool
|
|
|
|
defer sqlutil.EndTransactionWithCheck(snapshot, &succeeded, &err)
|
2022-09-30 06:48:10 -05:00
|
|
|
|
|
|
|
streams.PDUStreamProvider.Setup(ctx, snapshot)
|
|
|
|
streams.TypingStreamProvider.Setup(ctx, snapshot)
|
|
|
|
streams.ReceiptStreamProvider.Setup(ctx, snapshot)
|
|
|
|
streams.InviteStreamProvider.Setup(ctx, snapshot)
|
|
|
|
streams.SendToDeviceStreamProvider.Setup(ctx, snapshot)
|
|
|
|
streams.AccountDataStreamProvider.Setup(ctx, snapshot)
|
|
|
|
streams.NotificationDataStreamProvider.Setup(ctx, snapshot)
|
|
|
|
streams.DeviceListStreamProvider.Setup(ctx, snapshot)
|
|
|
|
streams.PresenceStreamProvider.Setup(ctx, snapshot)
|
2021-01-08 10:59:06 -06:00
|
|
|
|
2022-09-30 10:07:18 -05:00
|
|
|
succeeded = true
|
2021-01-08 10:59:06 -06:00
|
|
|
return streams
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Streams) Latest(ctx context.Context) types.StreamingToken {
|
|
|
|
return types.StreamingToken{
|
2022-03-03 05:40:53 -06:00
|
|
|
PDUPosition: s.PDUStreamProvider.LatestPosition(ctx),
|
|
|
|
TypingPosition: s.TypingStreamProvider.LatestPosition(ctx),
|
|
|
|
ReceiptPosition: s.ReceiptStreamProvider.LatestPosition(ctx),
|
|
|
|
InvitePosition: s.InviteStreamProvider.LatestPosition(ctx),
|
|
|
|
SendToDevicePosition: s.SendToDeviceStreamProvider.LatestPosition(ctx),
|
|
|
|
AccountDataPosition: s.AccountDataStreamProvider.LatestPosition(ctx),
|
|
|
|
NotificationDataPosition: s.NotificationDataStreamProvider.LatestPosition(ctx),
|
|
|
|
DeviceListPosition: s.DeviceListStreamProvider.LatestPosition(ctx),
|
2022-04-06 06:11:19 -05:00
|
|
|
PresencePosition: s.PresenceStreamProvider.LatestPosition(ctx),
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|
|
|
|
}
|
2022-10-28 02:44:27 -05:00
|
|
|
|
|
|
|
func ToToken(provider StreamProvider, position types.StreamPosition) types.StreamingToken {
|
|
|
|
switch t := provider.(type) {
|
|
|
|
case *PDUStreamProvider:
|
|
|
|
return types.StreamingToken{PDUPosition: position}
|
|
|
|
case *TypingStreamProvider:
|
|
|
|
return types.StreamingToken{TypingPosition: position}
|
|
|
|
case *ReceiptStreamProvider:
|
|
|
|
return types.StreamingToken{ReceiptPosition: position}
|
|
|
|
case *SendToDeviceStreamProvider:
|
|
|
|
return types.StreamingToken{SendToDevicePosition: position}
|
|
|
|
case *InviteStreamProvider:
|
|
|
|
return types.StreamingToken{InvitePosition: position}
|
|
|
|
case *AccountDataStreamProvider:
|
|
|
|
return types.StreamingToken{AccountDataPosition: position}
|
|
|
|
case *DeviceListStreamProvider:
|
|
|
|
return types.StreamingToken{DeviceListPosition: position}
|
|
|
|
case *NotificationDataStreamProvider:
|
|
|
|
return types.StreamingToken{NotificationDataPosition: position}
|
|
|
|
case *PresenceStreamProvider:
|
|
|
|
return types.StreamingToken{PresencePosition: position}
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown stream provider: %T", t))
|
|
|
|
}
|
|
|
|
return types.StreamingToken{}
|
|
|
|
}
|
2022-10-28 03:01:19 -05:00
|
|
|
|
|
|
|
func IncrementalPositions(provider StreamProvider, current, since types.StreamingToken) (types.StreamPosition, types.StreamPosition) {
|
|
|
|
switch t := provider.(type) {
|
|
|
|
case *PDUStreamProvider:
|
|
|
|
return current.PDUPosition, since.PDUPosition
|
|
|
|
case *TypingStreamProvider:
|
|
|
|
return current.TypingPosition, since.TypingPosition
|
|
|
|
case *ReceiptStreamProvider:
|
|
|
|
return current.ReceiptPosition, since.ReceiptPosition
|
|
|
|
case *SendToDeviceStreamProvider:
|
|
|
|
return current.SendToDevicePosition, since.SendToDevicePosition
|
|
|
|
case *InviteStreamProvider:
|
|
|
|
return current.InvitePosition, since.InvitePosition
|
|
|
|
case *AccountDataStreamProvider:
|
|
|
|
return current.AccountDataPosition, since.AccountDataPosition
|
|
|
|
case *DeviceListStreamProvider:
|
|
|
|
return current.DeviceListPosition, since.DeviceListPosition
|
|
|
|
case *NotificationDataStreamProvider:
|
|
|
|
return current.NotificationDataPosition, since.NotificationDataPosition
|
|
|
|
case *PresenceStreamProvider:
|
|
|
|
return current.PresencePosition, since.PresencePosition
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown stream provider: %T", t))
|
|
|
|
}
|
|
|
|
return 0, 0
|
|
|
|
}
|