This commit is contained in:
tamaina 2021-02-10 22:30:02 +09:00
parent 1fca78f0a1
commit 9e0f53a7e7
3 changed files with 28 additions and 28 deletions

View File

@ -1,47 +1,47 @@
import { get } from "idb-keyval"; import { get } from 'idb-keyval';
import { pushNotificationData } from '../../types'; import { pushNotificationData } from '../../types';
type Accounts = { type Accounts = {
[x: string]: { [x: string]: {
queue: string[], queue: string[],
timeout: number | null, timeout: number | null,
token: string, token: string,
} }
}; };
class SwNotificationRead { class SwNotificationRead {
private accounts: Accounts = {}; private accounts: Accounts = {};
public async construct() { public async construct() {
const accounts = await get('accounts') as { i: string, id: string }[]; const accounts = await get('accounts') as { i: string, id: string }[];
if (accounts) Error('Account is not recorded'); if (accounts) Error('Account is not recorded');
this.accounts = accounts.reduce((acc, e) => { this.accounts = accounts.reduce((acc, e) => {
acc[e.id] = { acc[e.id] = {
queue: [], queue: [],
timeout: null, timeout: null,
token: e.i, token: e.i,
}; };
return acc; return acc;
}, {} as Accounts); }, {} as Accounts);
return this; return this;
} }
// プッシュ通知の既読をサーバーに送信 // プッシュ通知の既読をサーバーに送信
public async read(data: pushNotificationData) { public async read(data: pushNotificationData) {
if (data.type !== 'notification' || !(data.userId in this.accounts)) return; if (data.type !== 'notification' || !(data.userId in this.accounts)) return;
const account = this.accounts[data.userId] const account = this.accounts[data.userId];
account.queue.push(data.body.id) account.queue.push(data.body.id);
// 最後の呼び出しから100ms待ってまとめて処理する // 最後の呼び出しから100ms待ってまとめて処理する
if (account.timeout) clearTimeout(account.timeout); if (account.timeout) clearTimeout(account.timeout);
account.timeout = setTimeout(() => { account.timeout = setTimeout(() => {
account.timeout = null; account.timeout = null;
console.info(account.token, account.queue) console.info(account.token, account.queue);
fetch(`${location.origin}/api/notifications/read`, { fetch(`${location.origin}/api/notifications/read`, {
method: 'POST', method: 'POST',
body: JSON.stringify({ body: JSON.stringify({

View File

@ -71,8 +71,8 @@ self.addEventListener('push', ev => {
case 'readNotifications': case 'readNotifications':
for (const notification of await self.registration.getNotifications()) { for (const notification of await self.registration.getNotifications()) {
if (data.body.notificationIds.includes(notification.data.body.id)) { if (data.body.notificationIds.includes(notification.data.body.id)) {
notification.close() notification.close();
}; }
} }
break; break;
} }

View File

@ -5,7 +5,7 @@ export const noteVisibilities = ['public', 'home', 'followers', 'specified'] as
export const mutedNoteReasons = ['word', 'manual', 'spam', 'other'] as const; export const mutedNoteReasons = ['word', 'manual', 'spam', 'other'] as const;
export type pushNotificationData = { export type pushNotificationData = {
type: 'notification' | 'unreadMessagingMessage' | 'readNotifications' | 'readAllNotifications', type: 'notification' | 'unreadMessagingMessage' | 'readNotifications' | 'readAllNotifications',
body: any, body: any,
userId: string userId: string
}; };