2022-03-03 11:40:53 +00:00
|
|
|
package streams
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NotificationDataStreamProvider struct {
|
|
|
|
StreamProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *NotificationDataStreamProvider) Setup() {
|
|
|
|
p.StreamProvider.Setup()
|
|
|
|
|
|
|
|
id, err := p.DB.MaxStreamPositionForNotificationData(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
p.latest = id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *NotificationDataStreamProvider) CompleteSync(
|
|
|
|
ctx context.Context,
|
|
|
|
req *types.SyncRequest,
|
|
|
|
) types.StreamPosition {
|
|
|
|
return p.IncrementalSync(ctx, req, 0, p.LatestPosition(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *NotificationDataStreamProvider) IncrementalSync(
|
|
|
|
ctx context.Context,
|
|
|
|
req *types.SyncRequest,
|
2022-09-27 14:01:34 +01:00
|
|
|
from, _ types.StreamPosition,
|
2022-03-03 11:40:53 +00:00
|
|
|
) types.StreamPosition {
|
2022-09-27 14:01:34 +01: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.
|
|
|
|
countsByRoom, err := p.DB.GetUserUnreadNotificationCountsForRooms(ctx, req.Device.UserID, req.Rooms)
|
2022-03-03 11:40:53 +00:00
|
|
|
if err != nil {
|
2022-09-27 14:01:34 +01:00
|
|
|
req.Log.WithError(err).Error("GetUserUnreadNotificationCountsForRooms failed")
|
2022-03-03 11:40:53 +00:00
|
|
|
return from
|
|
|
|
}
|
|
|
|
|
2022-09-27 14:01:34 +01:00
|
|
|
// We're merely decorating existing rooms.
|
2022-03-03 11:40:53 +00:00
|
|
|
for roomID, jr := range req.Response.Rooms.Join {
|
|
|
|
counts := countsByRoom[roomID]
|
|
|
|
if counts == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-09-27 14:01:34 +01:00
|
|
|
jr.UnreadNotifications = &types.UnreadNotifications{
|
|
|
|
HighlightCount: counts.UnreadHighlightCount,
|
|
|
|
NotificationCount: counts.UnreadNotificationCount,
|
|
|
|
}
|
2022-03-03 11:40:53 +00:00
|
|
|
req.Response.Rooms.Join[roomID] = jr
|
|
|
|
}
|
2022-09-27 14:01:34 +01:00
|
|
|
|
|
|
|
return p.LatestPosition(ctx)
|
2022-03-03 11:40:53 +00:00
|
|
|
}
|