Support dynamic room predecessors in RoomNotificationStateStore (#10297)

* Tests for RoomNotificationStateStore emitting events

* Support dynamic room predecessors in RoomNotificationStateStore

* Remove unused arguments from emit call.

UPDATE_STATUS_INDICATOR is used in:
* SpacePanel
* MatrixChat
* RoomHeaderButtons

but these arguments are not used in any of those places. Remove them so
when I refactor I don't have to make up values for them.

* Fix broken test (wrong expected args to emit)

UPDATE_STATUS_INDICATOR is used in:
* SpacePanel
* MatrixChat
* RoomHeaderButtons

but these arguments are not used in any of those places. Remove them so
when I refactor I don't have to make up values for them.

* Update the RoomNotificationStore whenever the predecessor labs flag changes

* Fix type errors

* Fix other tests that trigger our new watcher
This commit is contained in:
Andy Balaam 2023-03-08 14:18:03 +00:00 committed by GitHub
parent 80fc0997a4
commit b8d502be2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 186 additions and 15 deletions

View file

@ -15,18 +15,19 @@ limitations under the License.
*/
import { Room } from "matrix-js-sdk/src/models/room";
import { ISyncStateData, SyncState } from "matrix-js-sdk/src/sync";
import { SyncState } from "matrix-js-sdk/src/sync";
import { ClientEvent } from "matrix-js-sdk/src/client";
import { ActionPayload } from "../../dispatcher/payloads";
import { AsyncStoreWithClient } from "../AsyncStoreWithClient";
import defaultDispatcher from "../../dispatcher/dispatcher";
import defaultDispatcher, { MatrixDispatcher } from "../../dispatcher/dispatcher";
import { DefaultTagID, TagID } from "../room-list/models";
import { FetchRoomFn, ListNotificationState } from "./ListNotificationState";
import { RoomNotificationState } from "./RoomNotificationState";
import { SummarizedNotificationState } from "./SummarizedNotificationState";
import { VisibilityProvider } from "../room-list/filters/VisibilityProvider";
import { PosthogAnalytics } from "../../PosthogAnalytics";
import SettingsStore from "../../settings/SettingsStore";
interface IState {}
@ -43,8 +44,22 @@ export class RoomNotificationStateStore extends AsyncStoreWithClient<IState> {
private listMap = new Map<TagID, ListNotificationState>();
private _globalState = new SummarizedNotificationState();
private constructor() {
super(defaultDispatcher, {});
private constructor(dispatcher = defaultDispatcher) {
super(dispatcher, {});
SettingsStore.watchSetting("feature_dynamic_room_predecessors", null, () => {
// We pass SyncState.Syncing here to "simulate" a sync happening.
// The code that receives these events actually doesn't care
// what state we pass, except that it behaves differently if we
// pass SyncState.Error.
this.emitUpdateIfStateChanged(SyncState.Syncing, false);
});
}
/**
* @internal Public for test only
*/
public static testInstance(dispatcher: MatrixDispatcher): RoomNotificationStateStore {
return new RoomNotificationStateStore();
}
/**
@ -93,11 +108,22 @@ export class RoomNotificationStateStore extends AsyncStoreWithClient<IState> {
return RoomNotificationStateStore.internalInstance;
}
private onSync = (state: SyncState, prevState: SyncState | null, data?: ISyncStateData): void => {
private onSync = (state: SyncState, prevState: SyncState | null): void => {
this.emitUpdateIfStateChanged(state, state !== prevState);
};
/**
* If the SummarizedNotificationState of this room has changed, or forceEmit
* is true, emit an UPDATE_STATUS_INDICATOR event.
*
* @internal public for test
*/
public emitUpdateIfStateChanged = (state: SyncState, forceEmit: boolean): void => {
// Only count visible rooms to not torment the user with notification counts in rooms they can't see.
// This will include highlights from the previous version of the room internally
const msc3946ProcessDynamicPredecessor = SettingsStore.getValue("feature_dynamic_room_predecessors");
const globalState = new SummarizedNotificationState();
const visibleRooms = this.matrixClient.getVisibleRooms();
const visibleRooms = this.matrixClient.getVisibleRooms(msc3946ProcessDynamicPredecessor);
let numFavourites = 0;
for (const room of visibleRooms) {
@ -115,10 +141,10 @@ export class RoomNotificationStateStore extends AsyncStoreWithClient<IState> {
this.globalState.count !== globalState.count ||
this.globalState.color !== globalState.color ||
this.globalState.numUnreadStates !== globalState.numUnreadStates ||
state !== prevState
forceEmit
) {
this._globalState = globalState;
this.emit(UPDATE_STATUS_INDICATOR, globalState, state, prevState, data);
this.emit(UPDATE_STATUS_INDICATOR, globalState, state);
}
};