package syncapi import ( "context" "slices" ) // pushRulesType is the account data push rules travel as. The push server // renders it rather than the account data store, because a user who never // changed a rule has none stored and still has the server defaults. const pushRulesType = "m.push_rules" // UnreadNotifications is a joined room's unread counts for the user // syncing. type UnreadNotifications struct { NotificationCount int `json:"notification_count"` HighlightCount int `json:"highlight_count"` } // unreadNotifications fills in the counts of every joined room in the // response, in one read. // // Every joined room reported gets its counts, not only the ones whose // counts moved: a room is in a response because something in it changed, // and a client showing a new message wants the count beside it whether or // not this particular sync is the one that moved it. func (s *API) unreadNotifications(ctx context.Context, userID string, resp *Response) error { if s.push == nil || len(resp.Rooms.Join) == 0 { return nil } roomIDs := make([]string, 0, len(resp.Rooms.Join)) for roomID := range resp.Rooms.Join { roomIDs = append(roomIDs, roomID) } slices.Sort(roomIDs) counts, err := s.push.NotificationCounts(ctx, userID, roomIDs) if err != nil { return err } for _, roomID := range roomIDs { room := resp.Rooms.Join[roomID] c := counts[roomID] room.UnreadNotifications = &UnreadNotifications{ NotificationCount: c.Notifications, HighlightCount: c.Highlights, } resp.Rooms.Join[roomID] = room } return nil }