2022-03-03 12:40:53 +01:00
|
|
|
package streams
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-10-14 10:38:12 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/eventutil"
|
2022-09-30 13:48:10 +02:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
2022-03-03 12:40:53 +01:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NotificationDataStreamProvider struct {
|
2022-09-30 13:48:10 +02:00
|
|
|
DefaultStreamProvider
|
2022-03-03 12:40:53 +01:00
|
|
|
}
|
|
|
|
|
2022-09-30 13:48:10 +02:00
|
|
|
func (p *NotificationDataStreamProvider) Setup(
|
|
|
|
ctx context.Context, snapshot storage.DatabaseTransaction,
|
|
|
|
) {
|
|
|
|
p.DefaultStreamProvider.Setup(ctx, snapshot)
|
2022-03-03 12:40:53 +01:00
|
|
|
|
2022-09-30 13:48:10 +02:00
|
|
|
p.latestMutex.Lock()
|
|
|
|
defer p.latestMutex.Unlock()
|
|
|
|
|
|
|
|
id, err := snapshot.MaxStreamPositionForNotificationData(ctx)
|
2022-03-03 12:40:53 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
p.latest = id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *NotificationDataStreamProvider) CompleteSync(
|
|
|
|
ctx context.Context,
|
2022-09-30 13:48:10 +02:00
|
|
|
snapshot storage.DatabaseTransaction,
|
2022-03-03 12:40:53 +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))
|
2022-03-03 12:40:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *NotificationDataStreamProvider) IncrementalSync(
|
|
|
|
ctx context.Context,
|
2022-09-30 13:48:10 +02:00
|
|
|
snapshot storage.DatabaseTransaction,
|
2022-03-03 12:40:53 +01:00
|
|
|
req *types.SyncRequest,
|
2022-09-27 15:01:34 +02:00
|
|
|
from, _ types.StreamPosition,
|
2022-03-03 12:40:53 +01:00
|
|
|
) types.StreamPosition {
|
2022-09-27 15:01:34 +02:00
|
|
|
// Get the unread notifications for rooms in our join response.
|
|
|
|
// This is to ensure clients always have an unread notification section
|
|
|
|
// and can display the correct numbers.
|
2022-09-30 13:48:10 +02:00
|
|
|
countsByRoom, err := snapshot.GetUserUnreadNotificationCountsForRooms(ctx, req.Device.UserID, req.Rooms)
|
2022-03-03 12:40:53 +01:00
|
|
|
if err != nil {
|
2022-09-27 15:01:34 +02:00
|
|
|
req.Log.WithError(err).Error("GetUserUnreadNotificationCountsForRooms failed")
|
2022-03-03 12:40:53 +01:00
|
|
|
return from
|
|
|
|
}
|
|
|
|
|
2022-09-27 15:01:34 +02:00
|
|
|
// We're merely decorating existing rooms.
|
2022-03-03 12:40:53 +01:00
|
|
|
for roomID, jr := range req.Response.Rooms.Join {
|
|
|
|
counts := countsByRoom[roomID]
|
|
|
|
if counts == nil {
|
2022-10-14 10:38:12 +02:00
|
|
|
counts = &eventutil.NotificationData{}
|
2022-03-03 12:40:53 +01:00
|
|
|
}
|
2022-09-27 15:01:34 +02:00
|
|
|
jr.UnreadNotifications = &types.UnreadNotifications{
|
|
|
|
HighlightCount: counts.UnreadHighlightCount,
|
|
|
|
NotificationCount: counts.UnreadNotificationCount,
|
|
|
|
}
|
2022-03-03 12:40:53 +01:00
|
|
|
req.Response.Rooms.Join[roomID] = jr
|
|
|
|
}
|
2022-09-27 15:01:34 +02:00
|
|
|
|
|
|
|
return p.LatestPosition(ctx)
|
2022-03-03 12:40:53 +01:00
|
|
|
}
|