-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseChatMatrixEvents.ts
More file actions
106 lines (104 loc) · 3.48 KB
/
Copy pathuseChatMatrixEvents.ts
File metadata and controls
106 lines (104 loc) · 3.48 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
import {
MatrixEventEvent,
RoomEvent,
} from "matrix-js-sdk";
export function useChatMatrixEvents(options: {
client: Ref<Record<string, any> | null>;
selectedRoomId: Ref<string | null>;
selectedSpaceId: Ref<string | null>;
pinnedListVersion: Ref<number>;
scheduleThreadNavRefresh: () => void;
patchMessageReactions: (roomId: string) => void;
scheduleLoadMessages: (roomId: string) => void;
isReactionRelatedEvent: (eventType: string) => boolean;
scheduleSpaceHierarchyRefresh: () => void;
refreshRooms: () => void;
}) {
function setupMatrixEventWatchers() {
watch(
() => options.client.value,
(matrixClient, _previousClient, onCleanup) => {
if (!matrixClient) {
options.selectedSpaceId.value = null;
options.selectedRoomId.value = null;
return;
}
options.refreshRooms();
const timelineHandler = (
timelineEvent: Record<string, any> | undefined,
room: Record<string, any> | undefined,
) => {
if (room?.roomId) {
const eventType = timelineEvent?.getType?.() ?? "";
if (
eventType === "m.room.message" ||
options.isReactionRelatedEvent(eventType)
) {
options.scheduleThreadNavRefresh();
}
}
if (room?.roomId === options.selectedRoomId.value) {
const eventType = timelineEvent?.getType?.() ?? "";
if (eventType === "m.room.pinned_events") {
options.pinnedListVersion.value += 1;
}
if (eventType === "m.reaction") {
options.patchMessageReactions(room.roomId);
return;
}
if (eventType === "m.room.redaction") {
options.patchMessageReactions(room.roomId);
options.scheduleLoadMessages(room.roomId);
return;
}
options.scheduleLoadMessages(room.roomId);
}
};
const membershipHandler = () => {
options.scheduleSpaceHierarchyRefresh();
};
const decryptedHandler = (event: Record<string, any>) => {
if (
event?.getRoomId?.() === options.selectedRoomId.value &&
options.selectedRoomId.value
) {
const eventType = event?.getType?.() ?? "";
if (eventType === "m.reaction") {
options.patchMessageReactions(
options.selectedRoomId.value,
);
return;
}
if (eventType === "m.room.redaction") {
options.patchMessageReactions(
options.selectedRoomId.value,
);
options.scheduleLoadMessages(
options.selectedRoomId.value,
);
return;
}
options.scheduleLoadMessages(
options.selectedRoomId.value,
);
}
};
matrixClient.on(RoomEvent.Timeline, timelineHandler);
matrixClient.on(RoomEvent.MyMembership, membershipHandler);
matrixClient.on(MatrixEventEvent.Decrypted, decryptedHandler);
onCleanup(() => {
matrixClient.off(RoomEvent.Timeline, timelineHandler);
matrixClient.off(RoomEvent.MyMembership, membershipHandler);
matrixClient.off(
MatrixEventEvent.Decrypted,
decryptedHandler,
);
});
},
{ immediate: true },
);
}
return {
setupMatrixEventWatchers,
};
}