From 3ce28b706ad138f3f5faccbc131d278038869206 Mon Sep 17 00:00:00 2001 From: tamaina Date: Sat, 30 Jan 2021 19:56:36 +0900 Subject: [PATCH] wip --- src/client/account.ts | 1 - src/server/api/common/read-notification.ts | 8 +++++++- src/services/push-notification.ts | 10 +++++++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/client/account.ts b/src/client/account.ts index f6f72a9699..b509b53fca 100644 --- a/src/client/account.ts +++ b/src/client/account.ts @@ -50,7 +50,6 @@ export async function getAccounts(): Promise<{ id: Account['id'], token: Account export async function addAccount(id: Account['id'], token: Account['token']) { const accounts = await getAccounts(); - console.log(accounts); if (!accounts.some(x => x.id === id)) { return set('accounts', accounts.concat([{ id, token }])); } diff --git a/src/server/api/common/read-notification.ts b/src/server/api/common/read-notification.ts index f686446c5c..4cc50468e0 100644 --- a/src/server/api/common/read-notification.ts +++ b/src/server/api/common/read-notification.ts @@ -12,7 +12,7 @@ export async function readNotification( notificationIds: Notification['id'][] ) { // Update documents - await Notifications.update({ + const updatedNotificatons = await Notifications.update({ id: In(notificationIds), isRead: false }, { @@ -20,7 +20,13 @@ export async function readNotification( }); if (!await Users.getHasUnreadNotification(userId)) { + // ユーザーのすべての通知が既読だったら、 // 全ての(いままで未読だった)通知を(これで)読みましたよというイベントを発行 publishMainStream(userId, 'readAllNotifications'); + } else { + // まだすべて既読になっていなければ、 + // アップデートしたという + const updatedNotificationsIds = updatedNotificatons.generatedMaps.map(e => e.id); + publishMainStream(userId, 'readNotifications', updatedNotificationsIds); } } diff --git a/src/services/push-notification.ts b/src/services/push-notification.ts index aad9964061..939a8b635c 100644 --- a/src/services/push-notification.ts +++ b/src/services/push-notification.ts @@ -5,10 +5,14 @@ import { fetchMeta } from '../misc/fetch-meta'; import { PackedNotification } from '../models/repositories/notification'; import { PackedMessagingMessage } from '../models/repositories/messaging-message'; -type notificationType = 'notification' | 'unreadMessagingMessage'; -type notificationBody = PackedNotification | PackedMessagingMessage; +type pushNotificationsTypes = { + 'notification': PackedNotification; + 'unreadMessagingMessage': PackedMessagingMessage; + 'readNotifications': { notificationIds: string[] }; + 'readAllNotifications': undefined; +}; -export default async function(userId: string, type: notificationType, body: notificationBody) { +export default async function(userId: string, type: T, body: pushNotificationsTypes[T]) { const meta = await fetchMeta(); if (!meta.enableServiceWorker || meta.swPublicKey == null || meta.swPrivateKey == null) return;