-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseNotificationSettings.ts
More file actions
187 lines (172 loc) · 4.73 KB
/
Copy pathuseNotificationSettings.ts
File metadata and controls
187 lines (172 loc) · 4.73 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
import {
ClientEvent,
ConditionKind,
PushRuleActionName,
PushRuleKind,
TweakName,
} from 'matrix-js-sdk'
import type { MatrixClient, MatrixEvent } from 'matrix-js-sdk'
import {
findOverrideMuteRule,
findRoomPushRule,
resolveRoomNotificationLevel,
resolveSpaceNotificationLevel,
type RoomNotificationLevel,
type SpaceNotificationLevel,
} from '~/utils/matrixNotificationRules'
const PUSH_RULES_SCOPE = 'global'
export function useNotificationSettings(options: {
client: Ref<MatrixClient | null>
}) {
const pushRulesVersion = useState('matrixPushRulesVersion', () => 0)
function bumpVersion(): void {
pushRulesVersion.value += 1
}
/** Cached push rules from the Matrix client (not the async HTTP getter). */
function readPushRules(matrixClient: MatrixClient) {
return matrixClient.pushRules ?? null
}
async function refreshPushRules(matrixClient: MatrixClient): Promise<void> {
await matrixClient.getPushRules()
bumpVersion()
}
function getRoomLevel(roomId: string): RoomNotificationLevel {
pushRulesVersion.value
const matrixClient = options.client.value
if (!matrixClient) {
return 'default'
}
return resolveRoomNotificationLevel(readPushRules(matrixClient), roomId)
}
function getSpaceLevel(roomIds: string[]): SpaceNotificationLevel {
pushRulesVersion.value
return resolveSpaceNotificationLevel(
roomIds.map((roomId) => getRoomLevel(roomId)),
)
}
async function deleteOverrideMuteRule(
matrixClient: MatrixClient,
roomId: string,
): Promise<void> {
const overrideMuteRule = findOverrideMuteRule(
readPushRules(matrixClient),
roomId,
)
if (!overrideMuteRule?.rule_id) {
return
}
await matrixClient.deletePushRule(
PUSH_RULES_SCOPE,
PushRuleKind.Override,
overrideMuteRule.rule_id,
)
}
async function deleteRoomPushRule(
matrixClient: MatrixClient,
roomId: string,
): Promise<void> {
const roomRule = findRoomPushRule(readPushRules(matrixClient), roomId)
if (!roomRule?.rule_id) {
return
}
await matrixClient.deletePushRule(
PUSH_RULES_SCOPE,
PushRuleKind.RoomSpecific,
roomRule.rule_id,
)
}
async function setRoomLevel(
roomId: string,
level: RoomNotificationLevel,
): Promise<void> {
const matrixClient = options.client.value
if (!matrixClient || !roomId) {
return
}
try {
if (level === 'mute') {
// Match Element Web: drop the room rule, then squelch via override.
await deleteRoomPushRule(matrixClient, roomId)
await deleteOverrideMuteRule(matrixClient, roomId)
await matrixClient.addPushRule(
PUSH_RULES_SCOPE,
PushRuleKind.Override,
roomId,
{
conditions: [
{
kind: ConditionKind.EventMatch,
key: 'room_id',
pattern: roomId,
},
],
actions: [PushRuleActionName.DontNotify],
},
)
await refreshPushRules(matrixClient)
return
}
await deleteOverrideMuteRule(matrixClient, roomId)
if (level === 'default') {
await deleteRoomPushRule(matrixClient, roomId)
} else if (level === 'all') {
await matrixClient.addPushRule(
PUSH_RULES_SCOPE,
PushRuleKind.RoomSpecific,
roomId,
{
actions: [
PushRuleActionName.Notify,
{ set_tweak: TweakName.Sound, value: 'default' },
],
},
)
} else if (level === 'mentions') {
await matrixClient.addPushRule(
PUSH_RULES_SCOPE,
PushRuleKind.RoomSpecific,
roomId,
{
actions: [PushRuleActionName.DontNotify],
},
)
}
await refreshPushRules(matrixClient)
} catch (thrownError) {
console.error('setRoomLevel failed', thrownError)
}
}
async function setSpaceLevel(
roomIds: string[],
level: RoomNotificationLevel,
): Promise<void> {
for (const roomId of roomIds) {
await setRoomLevel(roomId, level)
}
}
watch(
() => options.client.value,
(matrixClient, _previousClient, onCleanup) => {
if (!matrixClient) {
return
}
const accountDataHandler = (event: MatrixEvent): void => {
if (event.getType?.() === 'm.push_rules') {
bumpVersion()
}
}
matrixClient.on(ClientEvent.AccountData, accountDataHandler)
onCleanup(() => {
matrixClient.off(ClientEvent.AccountData, accountDataHandler)
})
},
{ immediate: true },
)
return {
pushRulesVersion,
getRoomLevel,
getSpaceLevel,
setRoomLevel,
setSpaceLevel,
}
}