Skip to content

Commit 6d19953

Browse files
committed
refactor: use packet event for message notifications
1 parent 92decd4 commit 6d19953

File tree

2 files changed

+73
-63
lines changed

2 files changed

+73
-63
lines changed

src/MainView.tsx

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from 'react';
77
import {StatusBar, StyleSheet, View} from 'react-native';
88

9-
import type {Channel, Server} from 'revolt.js';
9+
import type {API, Channel, Server} from 'revolt.js';
1010

1111
import {app, randomizeRemark, setFunction} from '@clerotri/Generic';
1212
import {client} from '@clerotri/lib/client';
@@ -162,6 +162,60 @@ function AppViews({state}: {state: any}) {
162162
);
163163
}
164164

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+
}
165219
export class MainView extends ReactComponent {
166220
constructor(props) {
167221
super(props);
@@ -250,56 +304,10 @@ export class MainView extends ReactComponent {
250304
this.setState({network: 'dropped'});
251305
});
252306

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-
302307
client.on('packet', async p => {
308+
if (p.type === 'Message') {
309+
await handleMessageNotification(this, p, defaultNotif);
310+
}
303311
if (p.type === 'UserSettingsUpdate') {
304312
console.log('[WEBSOCKET] Synced settings updated');
305313
try {

src/lib/notifications/notifee.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import notifee, {EventType} from '@notifee/react-native';
22

3-
import type {Client, Message} from 'revolt.js';
3+
import type {API, Client} from 'revolt.js';
44

55
export async function createChannel() {
66
const channel = (await notifee.getChannel('clerotri'))
@@ -32,30 +32,33 @@ export function setUpNotifeeListener(client: Client, setState: any) {
3232
}
3333

3434
export async function sendNotifeeNotification(
35-
msg: Message,
35+
msg: API.Message,
3636
client: Client,
3737
defaultNotif: string,
3838
) {
39+
const channel = client.channels.get(msg.channel);
40+
const author = client.users.get(msg.author);
41+
3942
let notifs = (await notifee.getDisplayedNotifications()).filter(
40-
n => n.id === msg.channel?._id,
43+
n => n.id === msg.channel,
4144
);
4245
const title = `${
43-
msg.channel?.server?.name
44-
? `#${msg.channel.name} (${msg.channel.server.name})`
45-
: msg.channel?.channel_type === 'Group'
46-
? `${msg.channel.name}`
47-
: `@${msg.channel?.recipient?.username}`
46+
channel?.server?.name
47+
? `#${channel.name} (${channel.server.name})`
48+
: channel?.channel_type === 'Group'
49+
? `${channel.name}`
50+
: `@${channel?.recipient?.username}`
4851
}`;
4952

5053
try {
5154
notifee.displayNotification({
5255
title: title,
5356
data: {
54-
channel: msg.channel?._id ?? 'UNKNOWN',
57+
channel: channel?._id ?? 'UNKNOWN',
5558
messageID: msg._id,
5659
},
5760
body:
58-
`<b>${msg.author?.username}</b>: ` +
61+
`<b>${author?.username}</b>: ` +
5962
msg.content
6063
?.replaceAll(
6164
'<@' + client.user?._id + '>',
@@ -87,15 +90,14 @@ export async function sendNotifeeNotification(
8790
color: '#1AD4B2',
8891
smallIcon: 'ic_launcher_monochrome',
8992
largeIcon:
90-
msg.channel?.server?.generateIconURL() ||
91-
msg.author?.generateAvatarURL(),
93+
channel?.server?.generateIconURL() || author?.generateAvatarURL(),
9294
pressAction: {
9395
id: 'default',
9496
launchActivity: 'app.upryzing.clerotri.MainActivity',
9597
},
9698
channelId: defaultNotif,
9799
},
98-
id: msg.channel?._id,
100+
id: channel?._id,
99101
});
100102
} catch (notifErr) {
101103
console.log(`[NOTIFEE] Error sending notification: ${notifErr}`);

0 commit comments

Comments
 (0)