From 867e31c9ffefb57d01add5ad8568151fce39614c Mon Sep 17 00:00:00 2001 From: tamaina Date: Sun, 18 Dec 2022 19:50:02 +0900 Subject: [PATCH] enhance: Push notification of Antenna note (#9338) * wip * wip * wip * fix * fix * :art: --- locales/ja-JP.yml | 1 + .../assets/notification-badges/satellite.png | Bin 0 -> 1743 bytes packages/backend/src/core/AntennaService.ts | 10 ++++ packages/backend/src/core/NoteReadService.ts | 4 ++ .../src/core/PushNotificationService.ts | 44 +++++++++++++--- .../sw/src/scripts/create-notification.ts | 9 ++++ packages/sw/src/scripts/operations.ts | 5 ++ packages/sw/src/sw.ts | 47 ++++++++++++------ packages/sw/src/types.ts | 8 ++- 9 files changed, 104 insertions(+), 24 deletions(-) create mode 100644 packages/backend/assets/notification-badges/satellite.png diff --git a/locales/ja-JP.yml b/locales/ja-JP.yml index 3a53f470e3..bd6cee0272 100644 --- a/locales/ja-JP.yml +++ b/locales/ja-JP.yml @@ -1770,6 +1770,7 @@ _notification: yourFollowRequestAccepted: "フォローリクエストが承認されました" youWereInvitedToGroup: "{userName}があなたをグループに招待しました" pollEnded: "アンケートの結果が出ました" + unreadAntennaNote: "アンテナ {name}" emptyPushNotificationMessage: "プッシュ通知の更新をしました" _types: diff --git a/packages/backend/assets/notification-badges/satellite.png b/packages/backend/assets/notification-badges/satellite.png new file mode 100644 index 0000000000000000000000000000000000000000..0e1831e8a04a1c92fbcba2d48ee62a94c4ad0a3b GIT binary patch literal 1743 zcmeAS@N?(olHy`uVBq!ia0y~yU`PRB4mJh`hJr^^Ll_v?rg^$JhE&A8z1tgl#X*3* zV5P&F0}+iujqa>TtWIE@#X3*4LY706L$o4HA#B0t_UN8U&$9HgG-K&W-@iRA{%`YF+_$vEXXVY-YDfF)Q}+GfpD+GUPQUu&9sL&__ca)o=I>qwT+cYNq_~pkZ=#?qpnYvqI;U_VTNk5KNX-O|t=~%MKSpvijYV#HI(iHGa zO_rFYG3iH;!42;ZLJE^kElbK+U=niThHjnT;dK7^YYM6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c;?@eKP`6uI(M#WV3C8<;lv8Q(-k(_6Tj?GvF77AC-yG-lia=EllUS%c~{KsWNte7 zN87j|;-_krmkwLn)SS)5O;7z^%=i)%mUK;Hj@7MgWlk^eBp7zu`f7+r9?PpQ@800# zX{#x;I%R$@_av*Fg^CvzGtAf(xu76#?vi7fv--K460S{2`k&cdlDfF}Q)klAxzDrA zI61W1=QZ8Fb@w56x{4HoxAV(ookm|I!lh>atP7eTAknt8P3YRw+p(88qPSJRHFzn9 zT{S9l3~7ETGOau4ZcE>4`L z!s+G1T7!=l!i_n!_AY9Pw)nA6zxTG;qqZxzKI&gK5@YnTIk%jFfq}u()z4*}Q$iB} D{ah8H literal 0 HcmV?d00001 diff --git a/packages/backend/src/core/AntennaService.ts b/packages/backend/src/core/AntennaService.ts index 9c120c993d..d6f326f616 100644 --- a/packages/backend/src/core/AntennaService.ts +++ b/packages/backend/src/core/AntennaService.ts @@ -3,9 +3,12 @@ import Redis from 'ioredis'; import type { Antenna } from '@/models/entities/Antenna.js'; import type { Note } from '@/models/entities/Note.js'; import type { User } from '@/models/entities/User.js'; +import { NoteEntityService } from '@/core/entities/NoteEntityService.js'; +import { AntennaEntityService } from '@/core/entities/AntennaEntityService.js'; import { IdService } from '@/core/IdService.js'; import { isUserRelated } from '@/misc/is-user-related.js'; import { GlobalEventService } from '@/core/GlobalEventService.js'; +import { PushNotificationService } from '@/core/PushNotificationService.js'; import * as Acct from '@/misc/acct.js'; import { Cache } from '@/misc/cache.js'; import type { Packed } from '@/misc/schema.js'; @@ -49,6 +52,9 @@ export class AntennaService implements OnApplicationShutdown { private utilityService: UtilityService, private idService: IdService, private globalEventServie: GlobalEventService, + private pushNotificationService: PushNotificationService, + private noteEntityService: NoteEntityService, + private antennaEntityService: AntennaEntityService, ) { this.antennasFetched = false; this.antennas = []; @@ -127,6 +133,10 @@ export class AntennaService implements OnApplicationShutdown { const unread = await this.antennaNotesRepository.findOneBy({ antennaId: antenna.id, read: false }); if (unread) { this.globalEventServie.publishMainStream(antenna.userId, 'unreadAntenna', antenna); + this.pushNotificationService.pushNotification(antenna.userId, 'unreadAntennaNote', { + antenna: { id: antenna.id, name: antenna.name }, + note: await this.noteEntityService.pack(note) + }); } }, 2000); } diff --git a/packages/backend/src/core/NoteReadService.ts b/packages/backend/src/core/NoteReadService.ts index f70495ff30..82825b8b15 100644 --- a/packages/backend/src/core/NoteReadService.ts +++ b/packages/backend/src/core/NoteReadService.ts @@ -12,6 +12,7 @@ import { UserEntityService } from '@/core/entities/UserEntityService.js'; import { NotificationService } from './NotificationService.js'; import { AntennaService } from './AntennaService.js'; import { bindThis } from '@/decorators.js'; +import { PushNotificationService } from './PushNotificationService.js'; @Injectable() export class NoteReadService { @@ -42,6 +43,7 @@ export class NoteReadService { private globalEventServie: GlobalEventService, private notificationService: NotificationService, private antennaService: AntennaService, + private pushNotificationService: PushNotificationService, ) { } @@ -205,12 +207,14 @@ export class NoteReadService { if (count === 0) { this.globalEventServie.publishMainStream(userId, 'readAntenna', antenna); + this.pushNotificationService.pushNotification(userId, 'readAntenna', { antennaId: antenna.id }); } } this.userEntityService.getHasUnreadAntenna(userId).then(unread => { if (!unread) { this.globalEventServie.publishMainStream(userId, 'readAllAntennas'); + this.pushNotificationService.pushNotification(userId, 'readAllAntennas', undefined); } }); } diff --git a/packages/backend/src/core/PushNotificationService.ts b/packages/backend/src/core/PushNotificationService.ts index 842cd1a9f8..667dc9c1fa 100644 --- a/packages/backend/src/core/PushNotificationService.ts +++ b/packages/backend/src/core/PushNotificationService.ts @@ -8,30 +8,58 @@ import type { SwSubscriptionsRepository } from '@/models/index.js'; import { MetaService } from '@/core/MetaService.js'; import { bindThis } from '@/decorators.js'; -// Defined also packages/sw/types.ts#L14-L21 +// Defined also packages/sw/types.ts#L13 type pushNotificationsTypes = { 'notification': Packed<'Notification'>; 'unreadMessagingMessage': Packed<'MessagingMessage'>; + 'unreadAntennaNote': { + antenna: { id: string, name: string }; + note: Packed<'Note'>; + }; 'readNotifications': { notificationIds: string[] }; 'readAllNotifications': undefined; 'readAllMessagingMessages': undefined; 'readAllMessagingMessagesOfARoom': { userId: string } | { groupId: string }; + 'readAntenna': { antennaId: string }; + 'readAllAntennas': undefined; }; -// プッシュメッセージサーバーには文字数制限があるため、内容を削減します -function truncateNotification(notification: Packed<'Notification'>): any { +// Reduce length because push message servers have character limits +function truncateBody(type: T, body: pushNotificationsTypes[T]): pushNotificationsTypes[T] { + if (body === undefined) return body; + + return { + ...body, + ...(('note' in body && body.note) ? { + note: { + ...body.note, + // textをgetNoteSummaryしたものに置き換える + text: getNoteSummary(('type' in body && body.type === 'renote') ? body.note.renote as Packed<'Note'> : body.note), + + cw: undefined, + reply: undefined, + renote: undefined, + user: type === 'notification' ? undefined as any : body.note.user, + } + } : {}), + }; + + return body; +} + +function truncateUnreadAntennaNote(notification: pushNotificationsTypes['unreadAntennaNote']): pushNotificationsTypes['unreadAntennaNote'] { if (notification.note) { return { ...notification, note: { ...notification.note, // textをgetNoteSummaryしたものに置き換える - text: getNoteSummary(notification.type === 'renote' ? notification.note.renote as Packed<'Note'> : notification.note), + text: getNoteSummary(('type' in notification && notification.type === 'renote') ? notification.note.renote as Packed<'Note'> : notification.note), cw: undefined, reply: undefined, renote: undefined, - user: undefined as any, // 通知を受け取ったユーザーである場合が多いのでこれも捨てる + user: undefined as any, // 通知を受け取ったユーザーである場合が多いのでこれも捨てる アンテナの場合も不要なのでいらない }, }; } @@ -75,6 +103,8 @@ export class PushNotificationService { 'readAllNotifications', 'readAllMessagingMessages', 'readAllMessagingMessagesOfARoom', + 'readAntenna', + 'readAllAntennas', ].includes(type) && !subscription.sendReadMessage) continue; const pushSubscription = { @@ -84,10 +114,10 @@ export class PushNotificationService { p256dh: subscription.publickey, }, }; - + push.sendNotification(pushSubscription, JSON.stringify({ type, - body: type === 'notification' ? truncateNotification(body as Packed<'Notification'>) : body, + body: (type === 'notification' || type === 'unreadAntennaNote') ? truncateBody(type, body) : body, userId, dateTime: (new Date()).getTime(), }), { diff --git a/packages/sw/src/scripts/create-notification.ts b/packages/sw/src/scripts/create-notification.ts index c27e8c709c..fd5b0fa312 100644 --- a/packages/sw/src/scripts/create-notification.ts +++ b/packages/sw/src/scripts/create-notification.ts @@ -252,6 +252,15 @@ async function composeNotification(data data, renotify: true, }]; + case 'unreadAntennaNote': + return [t('_notification.unreadAntennaNote', { name: data.body.antenna.name }), { + body: `${getUserName(data.body.note.user)}: ${data.body.note.text || ''}`, + icon: data.body.note.user.avatarUrl, + badge: iconUrl('satellite'), + tag: `antenna:${data.body.antenna.id}`, + data, + renotify: true, + }]; default: return null; } diff --git a/packages/sw/src/scripts/operations.ts b/packages/sw/src/scripts/operations.ts index fd27418073..719e3ea9fa 100644 --- a/packages/sw/src/scripts/operations.ts +++ b/packages/sw/src/scripts/operations.ts @@ -27,6 +27,11 @@ export function openNote(noteId: string, loginId: string) { return openClient('push', `/notes/${noteId}`, loginId, { noteId }); } +// noteIdからノートを開く +export function openAntenna(antennaId: string, loginId: string) { + return openClient('push', `/timeline/antenna/${antennaId}`, loginId, { antennaId }); +} + export async function openChat(body: any, loginId: string) { if (body.groupId === null) { return openClient('push', `/my/messaging/${getAcct(body.user)}`, loginId, { body }); diff --git a/packages/sw/src/sw.ts b/packages/sw/src/sw.ts index 04372a0708..d47563939a 100644 --- a/packages/sw/src/sw.ts +++ b/packages/sw/src/sw.ts @@ -50,6 +50,7 @@ self.addEventListener('push', ev => { // case 'driveFileCreated': case 'notification': case 'unreadMessagingMessage': + case 'unreadAntennaNote': // 1日以上経過している場合は無視 if ((new Date()).getTime() - data.dateTime > 1000 * 60 * 60 * 24) break; @@ -67,6 +68,11 @@ self.addEventListener('push', ev => { if (n?.data?.type === 'unreadMessagingMessage') n.close(); } break; + case 'readAllAntennas': + for (const n of await self.registration.getNotifications()) { + if (n?.data?.type === 'unreadAntennaNote') n.close(); + } + break; case 'readNotifications': for (const n of await self.registration.getNotifications()) { if (data.body?.notificationIds?.includes(n.data.body.id)) { @@ -85,6 +91,13 @@ self.addEventListener('push', ev => { } } break; + case 'readAntenna': + for (const n of await self.registration.getNotifications()) { + if (n?.data?.type === 'unreadAntennaNote' && data.body?.antennaId === n.data.body.antenna.id) { + n.close(); + } + } + break; } return createEmptyNotification(); @@ -99,71 +112,73 @@ self.addEventListener('notificationclick', = {