summaryrefslogtreecommitdiff
path: root/packages/backend/src/core/PushNotificationService.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/backend/src/core/PushNotificationService.ts')
-rw-r--r--packages/backend/src/core/PushNotificationService.ts44
1 files changed, 37 insertions, 7 deletions
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(),
}), {