|
6 | 6 | } from 'react'; |
7 | 7 | import {StatusBar, StyleSheet, View} from 'react-native'; |
8 | 8 |
|
9 | | -import type {Channel, Server} from 'revolt.js'; |
| 9 | +import type {API, Channel, Server} from 'revolt.js'; |
10 | 10 |
|
11 | 11 | import {app, randomizeRemark, setFunction} from '@clerotri/Generic'; |
12 | 12 | import {client} from '@clerotri/lib/client'; |
@@ -162,6 +162,60 @@ function AppViews({state}: {state: any}) { |
162 | 162 | ); |
163 | 163 | } |
164 | 164 |
|
| 165 | +async function handleMessageNotification( |
| 166 | + state: any, |
| 167 | + msg: API.Message, |
| 168 | + notifeeChannel: string, |
| 169 | +) { |
| 170 | + console.log(`[APP] Handling message ${msg._id}`); |
| 171 | + |
| 172 | + const channel = client.channels.get(msg.channel); |
| 173 | + |
| 174 | + let channelNotif = |
| 175 | + state.state.channelNotifications && channel |
| 176 | + ? state.state.channelNotifications[channel?._id] |
| 177 | + : undefined; |
| 178 | + let serverNotif = |
| 179 | + state.state.serverNotifications && channel?.server |
| 180 | + ? state.state.serverNotifications[channel?.server?._id] |
| 181 | + : undefined; |
| 182 | + |
| 183 | + const isMuted = |
| 184 | + (channelNotif && channelNotif === 'none') || |
| 185 | + channelNotif === 'muted' || |
| 186 | + (serverNotif && serverNotif === 'none') || |
| 187 | + serverNotif === 'muted'; |
| 188 | + |
| 189 | + const alwaysNotif = |
| 190 | + channelNotif === 'all' || (!isMuted && serverNotif === 'all'); |
| 191 | + |
| 192 | + const mentionsUser = |
| 193 | + (msg.mentions?.includes(client.user?._id!) && |
| 194 | + (app.settings.get('app.notifications.notifyOnSelfPing') || |
| 195 | + msg.author !== client.user?._id)) || |
| 196 | + channel?.channel_type === 'DirectMessage'; |
| 197 | + |
| 198 | + const shouldNotif = |
| 199 | + (alwaysNotif && |
| 200 | + (app.settings.get('app.notifications.notifyOnSelfPing') || |
| 201 | + msg.author !== client.user?._id)) || |
| 202 | + (!isMuted && mentionsUser); |
| 203 | + |
| 204 | + console.log( |
| 205 | + `[NOTIFICATIONS] Should notify for ${msg._id}: ${shouldNotif} (channel/server muted? ${isMuted}, notifications for all messages enabled? ${alwaysNotif}, message mentions the user? ${mentionsUser})`, |
| 206 | + ); |
| 207 | + |
| 208 | + if (app.settings.get('app.notifications.enabled') && shouldNotif) { |
| 209 | + console.log(`[NOTIFICATIONS] Pushing notification for message ${msg._id}`); |
| 210 | + if (state.state.currentChannel !== msg.channel) { |
| 211 | + state.setState({notificationMessage: msg}); |
| 212 | + await sleep(5000); |
| 213 | + state.setState({notificationMessage: null}); |
| 214 | + } |
| 215 | + |
| 216 | + await sendNotifeeNotification(msg, client, notifeeChannel); |
| 217 | + } |
| 218 | +} |
165 | 219 | export class MainView extends ReactComponent { |
166 | 220 | constructor(props) { |
167 | 221 | super(props); |
@@ -250,56 +304,10 @@ export class MainView extends ReactComponent { |
250 | 304 | this.setState({network: 'dropped'}); |
251 | 305 | }); |
252 | 306 |
|
253 | | - client.on('message', async msg => { |
254 | | - console.log(`[APP] Handling message ${msg._id}`); |
255 | | - |
256 | | - let channelNotif = this.state.channelNotifications |
257 | | - ? this.state.channelNotifications[msg.channel?._id] |
258 | | - : undefined; |
259 | | - let serverNotif = this.state.serverNotifications |
260 | | - ? this.state.serverNotifications[msg.channel?.server?._id] |
261 | | - : undefined; |
262 | | - |
263 | | - const isMuted = |
264 | | - (channelNotif && channelNotif === 'none') || |
265 | | - channelNotif === 'muted' || |
266 | | - (serverNotif && serverNotif === 'none') || |
267 | | - serverNotif === 'muted'; |
268 | | - |
269 | | - const alwaysNotif = |
270 | | - channelNotif === 'all' || (!isMuted && serverNotif === 'all'); |
271 | | - |
272 | | - const mentionsUser = |
273 | | - (msg.mention_ids?.includes(client.user?._id!) && |
274 | | - (app.settings.get('app.notifications.notifyOnSelfPing') || |
275 | | - msg.author?._id !== client.user?._id)) || |
276 | | - msg.channel?.channel_type === 'DirectMessage'; |
277 | | - |
278 | | - const shouldNotif = |
279 | | - (alwaysNotif && |
280 | | - (app.settings.get('app.notifications.notifyOnSelfPing') || |
281 | | - msg.author?._id !== client.user?._id)) || |
282 | | - (!isMuted && mentionsUser); |
283 | | - |
284 | | - console.log( |
285 | | - `[NOTIFICATIONS] Should notify for ${msg._id}: ${shouldNotif} (channel/server muted? ${isMuted}, notifications for all messages enabled? ${alwaysNotif}, message mentions the user? ${mentionsUser})`, |
286 | | - ); |
287 | | - |
288 | | - if (app.settings.get('app.notifications.enabled') && shouldNotif) { |
289 | | - console.log( |
290 | | - `[NOTIFICATIONS] Pushing notification for message ${msg._id}`, |
291 | | - ); |
292 | | - if (this.state.currentChannel !== msg.channel) { |
293 | | - this.setState({notificationMessage: msg}); |
294 | | - await sleep(5000); |
295 | | - this.setState({notificationMessage: null}); |
296 | | - } |
297 | | - |
298 | | - await sendNotifeeNotification(msg, client, defaultNotif); |
299 | | - } |
300 | | - }); |
301 | | - |
302 | 307 | client.on('packet', async p => { |
| 308 | + if (p.type === 'Message') { |
| 309 | + await handleMessageNotification(this, p, defaultNotif); |
| 310 | + } |
303 | 311 | if (p.type === 'UserSettingsUpdate') { |
304 | 312 | console.log('[WEBSOCKET] Synced settings updated'); |
305 | 313 | try { |
|
0 commit comments