11/*
2- Copyright 2020 The Matrix.org Foundation C.I.C.
2+ Copyright 2020, 2023 The Matrix.org Foundation C.I.C.
33
44Licensed under the Apache License, Version 2.0 (the "License");
55you may not use this file except in compliance with the License.
@@ -14,21 +14,19 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17- import { MatrixEvent , MatrixEventEvent } from "matrix-js-sdk/src/models/event" ;
18- import { NotificationCountType , Room , RoomEvent } from "matrix-js-sdk/src/models/room" ;
17+ import { MatrixEventEvent } from "matrix-js-sdk/src/models/event" ;
18+ import { RoomEvent } from "matrix-js-sdk/src/models/room" ;
1919import { ClientEvent } from "matrix-js-sdk/src/client" ;
2020import { Feature , ServerSupport } from "matrix-js-sdk/src/feature" ;
2121
22- import { NotificationColor } from "./NotificationColor" ;
23- import { IDestroyable } from "../../utils/IDestroyable" ;
22+ import type { Room } from "matrix-js-sdk/src/models/room" ;
23+ import type { MatrixEvent } from "matrix-js-sdk/src/models/event" ;
24+ import type { IDestroyable } from "../../utils/IDestroyable" ;
2425import { MatrixClientPeg } from "../../MatrixClientPeg" ;
25- import { EffectiveMembership , getEffectiveMembership } from "../../utils/membership" ;
2626import { readReceiptChangeIsFor } from "../../utils/read-receipts" ;
2727import * as RoomNotifs from "../../RoomNotifs" ;
28- import * as Unread from "../../Unread" ;
2928import { NotificationState , NotificationStateEvents } from "./NotificationState" ;
30- import { getUnsentMessages } from "../../components/structures/RoomStatusBar" ;
31- import { ThreadsRoomNotificationState } from "./ThreadsRoomNotificationState" ;
29+ import type { ThreadsRoomNotificationState } from "./ThreadsRoomNotificationState" ;
3230
3331export class RoomNotificationState extends NotificationState implements IDestroyable {
3432 public constructor ( public readonly room : Room , private readonly threadsState ?: ThreadsRoomNotificationState ) {
@@ -49,10 +47,6 @@ export class RoomNotificationState extends NotificationState implements IDestroy
4947 this . updateNotificationState ( ) ;
5048 }
5149
52- private get roomIsInvite ( ) : boolean {
53- return getEffectiveMembership ( this . room . getMyMembership ( ) ) === EffectiveMembership . Invite ;
54- }
55-
5650 public destroy ( ) : void {
5751 super . destroy ( ) ;
5852 const cli = this . room . client ;
@@ -112,58 +106,10 @@ export class RoomNotificationState extends NotificationState implements IDestroy
112106 private updateNotificationState ( ) : void {
113107 const snapshot = this . snapshot ( ) ;
114108
115- if ( getUnsentMessages ( this . room ) . length > 0 ) {
116- // When there are unsent messages we show a red `!`
117- this . _color = NotificationColor . Unsent ;
118- this . _symbol = "!" ;
119- this . _count = 1 ; // not used, technically
120- } else if (
121- RoomNotifs . getRoomNotifsState ( this . room . client , this . room . roomId ) === RoomNotifs . RoomNotifState . Mute
122- ) {
123- // When muted we suppress all notification states, even if we have context on them.
124- this . _color = NotificationColor . None ;
125- this . _symbol = null ;
126- this . _count = 0 ;
127- } else if ( this . roomIsInvite ) {
128- this . _color = NotificationColor . Red ;
129- this . _symbol = "!" ;
130- this . _count = 1 ; // not used, technically
131- } else {
132- const redNotifs = RoomNotifs . getUnreadNotificationCount ( this . room , NotificationCountType . Highlight ) ;
133- const greyNotifs = RoomNotifs . getUnreadNotificationCount ( this . room , NotificationCountType . Total ) ;
134-
135- // For a 'true count' we pick the grey notifications first because they include the
136- // red notifications. If we don't have a grey count for some reason we use the red
137- // count. If that count is broken for some reason, assume zero. This avoids us showing
138- // a badge for 'NaN' (which formats as 'NaNB' for NaN Billion).
139- const trueCount = greyNotifs ? greyNotifs : redNotifs ? redNotifs : 0 ;
140-
141- // Note: we only set the symbol if we have an actual count. We don't want to show
142- // zero on badges.
143-
144- if ( redNotifs > 0 ) {
145- this . _color = NotificationColor . Red ;
146- this . _count = trueCount ;
147- this . _symbol = null ; // symbol calculated by component
148- } else if ( greyNotifs > 0 ) {
149- this . _color = NotificationColor . Grey ;
150- this . _count = trueCount ;
151- this . _symbol = null ; // symbol calculated by component
152- } else {
153- // We don't have any notified messages, but we might have unread messages. Let's
154- // find out.
155- const hasUnread = Unread . doesRoomHaveUnreadMessages ( this . room ) ;
156- if ( hasUnread ) {
157- this . _color = NotificationColor . Bold ;
158- } else {
159- this . _color = NotificationColor . None ;
160- }
161-
162- // no symbol or count for this state
163- this . _count = 0 ;
164- this . _symbol = null ;
165- }
166- }
109+ const { color, symbol, count } = RoomNotifs . determineUnreadState ( this . room ) ;
110+ this . _color = color ;
111+ this . _symbol = symbol ;
112+ this . _count = count ;
167113
168114 // finally, publish an update if needed
169115 this . emitIfUpdated ( snapshot ) ;
0 commit comments