-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathRoomListItemViewModel.ts
More file actions
444 lines (382 loc) · 17.9 KB
/
RoomListItemViewModel.ts
File metadata and controls
444 lines (382 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/
import {
BaseViewModel,
RoomNotifState,
type RoomListItemViewSnapshot,
type RoomListItemViewActions,
type Section,
} from "@element-hq/web-shared-components";
import { RoomEvent } from "matrix-js-sdk/src/matrix";
import { CallType } from "matrix-js-sdk/src/webrtc/call";
import type { Room, MatrixClient, RoomMember } from "matrix-js-sdk/src/matrix";
import type { RoomNotificationState } from "../../stores/notifications/RoomNotificationState";
import { RoomNotificationStateStore } from "../../stores/notifications/RoomNotificationStateStore";
import { NotificationStateEvents } from "../../stores/notifications/NotificationState";
import { MessagePreviewStore } from "../../stores/message-preview";
import { DefaultTagID } from "../../stores/room-list-v3/skip-list/tag";
import DMRoomMap from "../../utils/DMRoomMap";
import SettingsStore from "../../settings/SettingsStore";
import { NotificationLevel } from "../../stores/notifications/NotificationLevel";
import { hasAccessToNotificationMenu, hasAccessToOptionsMenu } from "./utils";
import { EchoChamber } from "../../stores/local-echo/EchoChamber";
import { RoomNotifState as ElementRoomNotifState } from "../../RoomNotifs";
import { shouldShowComponent } from "../../customisations/helpers/UIComponents";
import { UIComponent } from "../../settings/UIFeature";
import { CallStore, CallStoreEvent } from "../../stores/CallStore";
import { clearRoomNotification, setMarkedUnreadState } from "../../utils/notifications";
import { tagRoom } from "../../utils/room/tagRoom";
import { keepIfSame } from "../../utils/keepIfSame";
import dispatcher from "../../dispatcher/dispatcher";
import { Action } from "../../dispatcher/actions";
import type { ViewRoomPayload } from "../../dispatcher/payloads/ViewRoomPayload";
import PosthogTrackers from "../../PosthogTrackers";
import { type Call, CallEvent } from "../../models/Call";
import RoomListStoreV3, { CHATS_TAG } from "../../stores/room-list-v3/RoomListStoreV3";
import { _t } from "../../languageHandler";
interface RoomItemProps {
room: Room;
client: MatrixClient;
}
/**
* View model for an individual room list item.
* Manages per-room subscriptions and updates only when this specific room's data changes.
* Implements RoomListItemViewActions to provide interaction callbacks.
*/
export class RoomListItemViewModel
extends BaseViewModel<RoomListItemViewSnapshot, RoomItemProps>
implements RoomListItemViewActions
{
private notifState: RoomNotificationState;
/**
* Track the current call for this room to manager listeners
*/
private currentCall: Call | null = null;
public constructor(props: RoomItemProps) {
// Get notification state first so we can generate a complete initial snapshot
const notifState = RoomNotificationStateStore.instance.getRoomState(props.room);
const initialItem = RoomListItemViewModel.generateItemSync(props.room, props.client, notifState);
super(props, initialItem);
this.notifState = notifState;
// Subscribe to notification state changes for this room
this.disposables.trackListener(this.notifState, NotificationStateEvents.Update, this.onNotificationChanged);
// Subscribe to message preview changes for this specific room
this.disposables.trackListener(
MessagePreviewStore.instance,
MessagePreviewStore.getPreviewChangedEventName(props.room),
this.onMessagePreviewChanged,
);
// Subscribe to settings changes for message preview toggle
const settingsWatchRef = SettingsStore.watchSetting(
"RoomList.showMessagePreview",
null,
this.onMessagePreviewSettingChanged,
);
this.disposables.track(() => {
SettingsStore.unwatchSetting(settingsWatchRef);
});
// Subscribe to call state changes
this.disposables.trackListener(CallStore.instance, CallStoreEvent.Call, this.onCallStateChanged);
// If there is an active call for this room, listen to participant changes
this.listenToCallParticipants();
// Subscribe to room-specific events
this.disposables.trackListener(props.room, RoomEvent.Name, this.onRoomChanged);
this.disposables.trackListener(props.room, RoomEvent.Tags, this.onRoomChanged);
const orderSectionsRef = SettingsStore.watchSetting("RoomList.OrderedCustomSections", null, () =>
this.onOrderedCustomSectionsChange(),
);
this.disposables.track(() => {
SettingsStore.unwatchSetting(orderSectionsRef);
});
// Load message preview asynchronously (sync data is already complete)
void this.loadAndSetMessagePreview();
}
public dispose(): void {
super.dispose();
this.currentCall?.off(CallEvent.Participants, this.onCallParticipantsChanged);
this.currentCall?.off(CallEvent.CallTypeChanged, this.onCallTypeChanged);
}
private onNotificationChanged = (): void => {
this.updateItem();
};
private onMessagePreviewChanged = (): void => {
void this.loadAndSetMessagePreview();
};
private onMessagePreviewSettingChanged = (): void => {
void this.loadAndSetMessagePreview();
};
/**
* Handler for call participant changes. Only updates the item if the call moves between having participants and not having participants, to avoid unnecessary updates.
* @param participants The current call participants
*/
private onCallParticipantsChanged = (participants: Map<RoomMember, Set<string>>): void => {
const hasCall = Boolean(this.snapshot.current.notification.callType);
// There is already an active call, we don't need to update the item
if (hasCall && participants.size > 0) return;
this.updateItem();
};
/**
* Handler for call type changes. Only updates the item if the call type is actually present in the snapshot.
*/
private onCallTypeChanged = (): void => {
if (this.snapshot.current.notification.callType !== undefined) this.updateItem();
};
/**
* Listen to participant changes for the current call in this room (if any) to trigger updates when participants join/leave the call.
*/
private listenToCallParticipants(): void {
const call = CallStore.instance.getCall(this.props.room.roomId);
// Remove listeners from previous call (if any) and add to new call to track changes
if (call !== this.currentCall) {
this.currentCall?.off(CallEvent.Participants, this.onCallParticipantsChanged);
this.currentCall?.off(CallEvent.CallTypeChanged, this.onCallTypeChanged);
call?.on(CallEvent.Participants, this.onCallParticipantsChanged);
call?.on(CallEvent.CallTypeChanged, this.onCallTypeChanged);
}
this.currentCall = call;
}
private onCallStateChanged = (): void => {
// Only update if call state for this room actually changed
const call = CallStore.instance.getCall(this.props.room.roomId);
this.listenToCallParticipants();
const currentCallType = this.snapshot.current.notification.callType;
const newCallType =
call && call.participants.size > 0 ? (call.callType === CallType.Voice ? "voice" : "video") : undefined;
if (currentCallType !== newCallType) {
this.updateItem();
}
};
private onRoomChanged = (): void => {
this.updateItem();
};
/**
* Update the item snapshot with current sync data.
* Preserves the message preview which is managed separately.
*/
private updateItem(): void {
const newItem = RoomListItemViewModel.generateItemSync(this.props.room, this.props.client, this.notifState);
this.snapshot.merge({
...newItem,
notification: keepIfSame(this.snapshot.current.notification, newItem.notification),
sections: keepIfSame(this.snapshot.current.sections, newItem.sections),
// Preserve message preview - it's managed separately by loadAndSetMessagePreview
messagePreview: this.snapshot.current.messagePreview,
});
}
private getMessagePreviewTag(): string {
const isDm = Boolean(DMRoomMap.shared().getUserIdForRoomId(this.props.room.roomId));
return isDm ? DefaultTagID.DM : DefaultTagID.Untagged;
}
/**
* Load the message preview for this room if enabled.
* Returns undefined if previews are disabled or couldn't be loaded.
*/
private async loadMessagePreview(): Promise<string | undefined> {
const shouldShowMessagePreview = SettingsStore.getValue("RoomList.showMessagePreview");
if (!shouldShowMessagePreview) {
return undefined;
}
const messagePreviewTag = this.getMessagePreviewTag();
const preview = await MessagePreviewStore.instance.getPreviewForRoom(this.props.room, messagePreviewTag);
return preview?.text;
}
/**
* Load and set the message preview if it differs from current.
*/
private async loadAndSetMessagePreview(): Promise<void> {
const messagePreview = await this.loadMessagePreview();
this.snapshot.merge({ messagePreview });
}
/**
* Generate a complete RoomListItem with all synchronous data.
* Message preview is loaded separately to avoid blocking initial render.
*/
private static generateItemSync(
room: Room,
client: MatrixClient,
notifState: RoomNotificationState,
): RoomListItemViewSnapshot {
// Get room tags for menu state
const roomTags = room.tags;
const isDm = Boolean(DMRoomMap.shared().getUserIdForRoomId(room.roomId));
// Message preview will be loaded asynchronously and updated separately
const messagePreview = undefined;
const isFavourite = Boolean(roomTags[DefaultTagID.Favourite]);
const isLowPriority = Boolean(roomTags[DefaultTagID.LowPriority]);
const isArchived = Boolean(roomTags[DefaultTagID.Archived]);
// More options menu state
const showMoreOptionsMenu = hasAccessToOptionsMenu(room);
const showNotificationMenu = hasAccessToNotificationMenu(room, client.isGuest(), isArchived);
// Notification levels
const canMarkAsRead = notifState.level > NotificationLevel.None;
const canMarkAsUnread = !canMarkAsRead && !isArchived;
const canInvite = room.canInvite(client.getUserId()!) && !isDm && shouldShowComponent(UIComponent.InviteUsers);
const canCopyRoomLink = !isDm;
// Get the current room notification state from EchoChamber
const echoChamber = EchoChamber.forRoom(room);
const elementRoomNotifState = echoChamber.notificationVolume;
// Convert element-web RoomNotifState to shared-components RoomNotifState
let roomNotifState: RoomNotifState;
switch (elementRoomNotifState) {
case ElementRoomNotifState.AllMessages:
roomNotifState = RoomNotifState.AllMessages;
break;
case ElementRoomNotifState.AllMessagesLoud:
roomNotifState = RoomNotifState.AllMessagesLoud;
break;
case ElementRoomNotifState.MentionsOnly:
roomNotifState = RoomNotifState.MentionsOnly;
break;
case ElementRoomNotifState.Mute:
roomNotifState = RoomNotifState.Mute;
break;
default:
roomNotifState = RoomNotifState.AllMessages;
}
const isNotificationMute = elementRoomNotifState === ElementRoomNotifState.Mute;
// Video room and call state tracking
const call = CallStore.instance.getCall(room.roomId);
const participantCount = call?.participants.size ?? 0;
const hasParticipantsInCall = participantCount > 0;
const callType =
call?.callType === CallType.Voice ? "voice" : call?.callType === CallType.Video ? "video" : undefined;
const canMoveToSection = SettingsStore.getValue("feature_room_list_sections");
// Build sections list for the "Move to section" submenu
const sections: Section[] = canMoveToSection ? RoomListItemViewModel.buildSections(roomTags) : [];
return {
id: room.roomId,
room,
name: room.name,
isBold: notifState.hasAnyNotificationOrActivity,
messagePreview,
notification: {
hasAnyNotificationOrActivity: notifState.hasAnyNotificationOrActivity || hasParticipantsInCall,
isUnsentMessage: notifState.isUnsentMessage,
invited: notifState.invited,
isMention: notifState.isMention,
isActivityNotification: notifState.isActivityNotification,
isNotification: notifState.isNotification,
hasUnreadCount: notifState.hasUnreadCount,
count: notifState.count,
muted: isNotificationMute,
callType: hasParticipantsInCall ? callType : undefined,
},
showMoreOptionsMenu,
showNotificationMenu,
isFavourite,
isLowPriority,
canInvite,
canCopyRoomLink,
canMarkAsRead,
canMarkAsUnread,
roomNotifState,
canMoveToSection,
sections,
};
}
public onOpenRoom = (): void => {
dispatcher.dispatch<ViewRoomPayload>({
action: Action.ViewRoom,
room_id: this.props.room.roomId,
metricsTrigger: "RoomList",
});
};
public onMarkAsRead = async (): Promise<void> => {
await clearRoomNotification(this.props.room, this.props.client);
PosthogTrackers.trackInteraction("WebRoomListRoomTileContextMenuMarkRead");
};
public onMarkAsUnread = async (): Promise<void> => {
await setMarkedUnreadState(this.props.room, this.props.client, true);
PosthogTrackers.trackInteraction("WebRoomListRoomTileContextMenuMarkUnread");
};
public onToggleFavorite = (): void => {
tagRoom(this.props.room, DefaultTagID.Favourite);
PosthogTrackers.trackInteraction("WebRoomListRoomTileContextMenuFavouriteToggle");
};
public onToggleLowPriority = (): void => {
tagRoom(this.props.room, DefaultTagID.LowPriority);
};
public onInvite = (): void => {
dispatcher.dispatch({
action: "view_invite",
roomId: this.props.room.roomId,
});
PosthogTrackers.trackInteraction("WebRoomListRoomTileContextMenuInviteItem");
};
public onCopyRoomLink = (): void => {
dispatcher.dispatch({
action: "copy_room",
room_id: this.props.room.roomId,
});
};
public onLeaveRoom = (): void => {
const isArchived = Boolean(this.props.room.tags[DefaultTagID.Archived]);
dispatcher.dispatch({
action: isArchived ? "forget_room" : "leave_room",
room_id: this.props.room.roomId,
});
PosthogTrackers.trackInteraction("WebRoomListRoomTileContextMenuLeaveItem");
};
public onSetRoomNotifState = (notifState: RoomNotifState): void => {
// Convert shared-components RoomNotifState to element-web RoomNotifState
let elementNotifState: ElementRoomNotifState;
switch (notifState) {
case "all_messages":
elementNotifState = ElementRoomNotifState.AllMessages;
break;
case "all_messages_loud":
elementNotifState = ElementRoomNotifState.AllMessagesLoud;
break;
case "mentions_only":
elementNotifState = ElementRoomNotifState.MentionsOnly;
break;
case "mute":
elementNotifState = ElementRoomNotifState.Mute;
break;
default:
elementNotifState = ElementRoomNotifState.AllMessages;
}
// Set the notification state using EchoChamber
const echoChamber = EchoChamber.forRoom(this.props.room);
echoChamber.notificationVolume = elementNotifState;
};
public onCreateSection = (): void => {
RoomListStoreV3.instance.createSection();
};
public onToggleSection = (tag: string): void => {
tagRoom(this.props.room, tag);
};
private onOrderedCustomSectionsChange = (): void => {
// Rebuild sections list to reflect new order
const sections = RoomListItemViewModel.buildSections(this.props.room.tags);
this.snapshot.merge({ sections: keepIfSame(this.snapshot.current.sections, sections) });
};
/**
* Build the list of available sections for the "Move to section" submenu.
* Order follows the canonical section order from RoomListStoreV3.
*/
private static buildSections(roomTags: Room["tags"]): Section[] {
const customSectionData = SettingsStore.getValue("RoomList.CustomSectionData") || {};
return (
RoomListStoreV3.instance.orderedSectionTags
// Exclude the Chats section because the user toggle the other sections to move rooms in and out of the Chats section.
.filter((tag) => tag !== CHATS_TAG)
.map((tag) => ({
tag,
name: RoomListItemViewModel.getSectionName(tag, customSectionData),
isSelected: Boolean(roomTags[tag]),
}))
);
}
/**
* Get the display name for a section based on its tag.
*/
private static getSectionName(tag: string, customSectionData: Record<string, { name: string }>): string {
if (tag === DefaultTagID.Favourite) return _t("room_list|section|favourites");
if (tag === DefaultTagID.LowPriority) return _t("room_list|section|low_priority");
return customSectionData[tag]?.name || tag;
}
}