diff options
| author | tamaina <tamaina@hotmail.co.jp> | 2022-12-18 19:50:02 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-18 19:50:02 +0900 |
| commit | 867e31c9ffefb57d01add5ad8568151fce39614c (patch) | |
| tree | 133bec354505986b0db3bf95353b24e2d8277792 /packages/backend | |
| parent | chore(client): :art: about button (#9339) (diff) | |
| download | sharkey-867e31c9ffefb57d01add5ad8568151fce39614c.tar.gz sharkey-867e31c9ffefb57d01add5ad8568151fce39614c.tar.bz2 sharkey-867e31c9ffefb57d01add5ad8568151fce39614c.zip | |
enhance: Push notification of Antenna note (#9338)
* wip
* wip
* wip
* fix
* fix
* :art:
Diffstat (limited to 'packages/backend')
| -rw-r--r-- | packages/backend/assets/notification-badges/satellite.png | bin | 0 -> 1743 bytes | |||
| -rw-r--r-- | packages/backend/src/core/AntennaService.ts | 10 | ||||
| -rw-r--r-- | packages/backend/src/core/NoteReadService.ts | 4 | ||||
| -rw-r--r-- | packages/backend/src/core/PushNotificationService.ts | 44 |
4 files changed, 51 insertions, 7 deletions
diff --git a/packages/backend/assets/notification-badges/satellite.png b/packages/backend/assets/notification-badges/satellite.png Binary files differnew file mode 100644 index 0000000000..0e1831e8a0 --- /dev/null +++ b/packages/backend/assets/notification-badges/satellite.png 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<T extends keyof pushNotificationsTypes>(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(), }), { |