summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/common/read-notification.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/backend/src/server/api/common/read-notification.ts')
-rw-r--r--packages/backend/src/server/api/common/read-notification.ts26
1 files changed, 14 insertions, 12 deletions
diff --git a/packages/backend/src/server/api/common/read-notification.ts b/packages/backend/src/server/api/common/read-notification.ts
index 1f575042a0..0dad35bcc2 100644
--- a/packages/backend/src/server/api/common/read-notification.ts
+++ b/packages/backend/src/server/api/common/read-notification.ts
@@ -1,4 +1,5 @@
import { publishMainStream } from '@/services/stream.js';
+import { pushNotification } from '@/services/push-notification.js';
import { User } from '@/models/entities/user.js';
import { Notification } from '@/models/entities/notification.js';
import { Notifications, Users } from '@/models/index.js';
@@ -16,28 +17,29 @@ export async function readNotification(
isRead: true,
});
- post(userId);
+ if (!await Users.getHasUnreadNotification(userId)) return postReadAllNotifications(userId);
+ else return postReadNotifications(userId, notificationIds);
}
export async function readNotificationByQuery(
userId: User['id'],
query: Record<string, any>
) {
- // Update documents
- await Notifications.update({
+ const notificationIds = await Notifications.find({
...query,
notifieeId: userId,
isRead: false,
- }, {
- isRead: true,
- });
+ }).then(notifications => notifications.map(notification => notification.id));
+
+ return readNotification(userId, notificationIds);
+}
- post(userId);
+function postReadAllNotifications(userId: User['id']) {
+ publishMainStream(userId, 'readAllNotifications');
+ return pushNotification(userId, 'readAllNotifications', undefined);
}
-async function post(userId: User['id']) {
- if (!await Users.getHasUnreadNotification(userId)) {
- // 全ての(いままで未読だった)通知を(これで)読みましたよというイベントを発行
- publishMainStream(userId, 'readAllNotifications');
- }
+function postReadNotifications(userId: User['id'], notificationIds: Notification['id'][]) {
+ publishMainStream(userId, 'readNotifications', notificationIds);
+ return pushNotification(userId, 'readNotifications', { notificationIds });
}