@@ -1,58 +0,0 @@
|
||||
import { get } from 'idb-keyval';
|
||||
import { pushNotificationDataMap } from '@/types';
|
||||
import { api } from '@/scripts/operations';
|
||||
|
||||
type Accounts = {
|
||||
[x: string]: {
|
||||
queue: string[],
|
||||
timeout: number | null
|
||||
}
|
||||
};
|
||||
|
||||
class SwNotificationReadManager {
|
||||
private accounts: Accounts = {};
|
||||
|
||||
public async construct() {
|
||||
const accounts = await get('accounts');
|
||||
if (!accounts) Error('Accounts are not recorded');
|
||||
|
||||
this.accounts = accounts.reduce((acc, e) => {
|
||||
acc[e.id] = {
|
||||
queue: [],
|
||||
timeout: null
|
||||
};
|
||||
return acc;
|
||||
}, {} as Accounts);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
// プッシュ通知の既読をサーバーに送信
|
||||
public async read(data: pushNotificationDataMap[keyof pushNotificationDataMap]) {
|
||||
if (data.type !== 'notification' || !(data.userId in this.accounts)) return;
|
||||
|
||||
const account = this.accounts[data.userId];
|
||||
|
||||
account.queue.push(data.body.id as string);
|
||||
|
||||
if (account.queue.length >= 20) {
|
||||
if (account.timeout) clearTimeout(account.timeout);
|
||||
const notificationIds = account.queue;
|
||||
account.queue = [];
|
||||
await api('notifications/read', data.userId, { notificationIds });
|
||||
return;
|
||||
}
|
||||
|
||||
// 最後の呼び出しから200ms待ってまとめて処理する
|
||||
if (account.timeout) clearTimeout(account.timeout);
|
||||
account.timeout = setTimeout(() => {
|
||||
account.timeout = null;
|
||||
|
||||
const notificationIds = account.queue;
|
||||
account.queue = [];
|
||||
api('notifications/read', data.userId, { notificationIds });
|
||||
}, 200);
|
||||
}
|
||||
}
|
||||
|
||||
export const swNotificationRead = (new SwNotificationReadManager()).construct();
|
@@ -1,6 +1,6 @@
|
||||
import { createEmptyNotification, createNotification } from '@/scripts/create-notification';
|
||||
import { swLang } from '@/scripts/lang';
|
||||
import { swNotificationRead } from '@/scripts/notification-read';
|
||||
import { api } from '@/scripts/operations';
|
||||
import { pushNotificationDataMap } from '@/types';
|
||||
import * as swos from '@/scripts/operations';
|
||||
import { acct as getAcct } from '@/filters/user';
|
||||
@@ -54,30 +54,6 @@ globalThis.addEventListener('push', ev => {
|
||||
if ((new Date()).getTime() - data.dateTime > 1000 * 60 * 60 * 24) break;
|
||||
|
||||
return createNotification(data);
|
||||
case 'readAllNotifications':
|
||||
for (const n of await globalThis.registration.getNotifications()) {
|
||||
if (n?.data?.type === 'notification') n.close();
|
||||
}
|
||||
break;
|
||||
case 'readAllAntennas':
|
||||
for (const n of await globalThis.registration.getNotifications()) {
|
||||
if (n?.data?.type === 'unreadAntennaNote') n.close();
|
||||
}
|
||||
break;
|
||||
case 'readNotifications':
|
||||
for (const n of await globalThis.registration.getNotifications()) {
|
||||
if (data.body.notificationIds.includes(n.data.body.id)) {
|
||||
n.close();
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'readAntenna':
|
||||
for (const n of await globalThis.registration.getNotifications()) {
|
||||
if (n?.data?.type === 'unreadAntennaNote' && data.body.antennaId === n.data.body.antenna.id) {
|
||||
n.close();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
await createEmptyNotification();
|
||||
@@ -154,7 +130,7 @@ globalThis.addEventListener('notificationclick', (ev: ServiceWorkerGlobalScopeEv
|
||||
client.focus();
|
||||
}
|
||||
if (data.type === 'notification') {
|
||||
swNotificationRead.then(that => that.read(data));
|
||||
api('notifications/mark-all-as-read', data.userId);
|
||||
}
|
||||
|
||||
notification.close();
|
||||
@@ -165,7 +141,7 @@ globalThis.addEventListener('notificationclose', (ev: ServiceWorkerGlobalScopeEv
|
||||
const data: pushNotificationDataMap[keyof pushNotificationDataMap] = ev.notification.data;
|
||||
|
||||
if (data.type === 'notification') {
|
||||
swNotificationRead.then(that => that.read(data));
|
||||
api('notifications/mark-all-as-read', data.userId);
|
||||
}
|
||||
});
|
||||
|
||||
|
@@ -17,10 +17,6 @@ type pushNotificationDataSourceMap = {
|
||||
antenna: { id: string, name: string };
|
||||
note: Misskey.entities.Note;
|
||||
};
|
||||
readNotifications: { notificationIds: string[] };
|
||||
readAllNotifications: undefined;
|
||||
readAntenna: { antennaId: string };
|
||||
readAllAntennas: undefined;
|
||||
};
|
||||
|
||||
export type pushNotificationData<K extends keyof pushNotificationDataSourceMap> = {
|
||||
|
Reference in New Issue
Block a user