From ffef12ce1d5dc97c1f3c72435a94d8acdb87f55e Mon Sep 17 00:00:00 2001 From: tamaina Date: Sat, 25 Dec 2021 01:37:15 +0900 Subject: [PATCH] Truncate push notification message --- .../backend/src/services/push-notification.ts | 27 ++++++++++++++++++- .../client/src/sw/compose-notification.ts | 13 +++++---- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/packages/backend/src/services/push-notification.ts b/packages/backend/src/services/push-notification.ts index 616bc74411..2133768a96 100644 --- a/packages/backend/src/services/push-notification.ts +++ b/packages/backend/src/services/push-notification.ts @@ -3,10 +3,33 @@ import config from '@/config/index'; import { SwSubscriptions } from '@/models/index'; import { fetchMeta } from '@/misc/fetch-meta'; import { Packed } from '@/misc/schema'; +import { getNoteSummary } from '@/misc/get-note-summary'; type notificationType = 'notification' | 'unreadMessagingMessage'; type notificationBody = Packed<'Notification'> | Packed<'MessagingMessage'>; +// プッシュメッセージサーバーには文字数制限があるため、内容を削減します +function truncateNotification(notification: Packed<'Notification'>): any { + if (notification.note) { + return { + ...notification, + note: { + ...notification.note, + // textをgetNoteSummaryしたものに置き換える + text: getNoteSummary(notification.type === 'renote' ? notification.note.renote as Packed<'Note'> : notification.note), + ...{ + cw: undefined, + reply: undefined, + renote: undefined, + user: undefined as any, // 通知を受け取ったユーザーである場合が多いのでこれも捨てる + } + } + }; + } + + return notification; +} + export default async function(userId: string, type: notificationType, body: notificationBody) { const meta = await fetchMeta(); @@ -32,7 +55,9 @@ export default async function(userId: string, type: notificationType, body: noti }; push.sendNotification(pushSubscription, JSON.stringify({ - type, body, + type, + body: type === 'notification' ? truncateNotification(body as Packed<'Notification'>) : body, + userId, }), { proxy: config.proxy, }).catch((err: any) => { diff --git a/packages/client/src/sw/compose-notification.ts b/packages/client/src/sw/compose-notification.ts index 0aed9610ea..bdf261fccf 100644 --- a/packages/client/src/sw/compose-notification.ts +++ b/packages/client/src/sw/compose-notification.ts @@ -3,7 +3,6 @@ */ declare var self: ServiceWorkerGlobalScope; -import { getNoteSummary } from '@/scripts/get-note-summary'; import * as misskey from 'misskey-js'; function getUserName(user: misskey.entities.User): string { @@ -26,37 +25,37 @@ export default async function(type, data, i18n): Promise<[string, NotificationOp switch (data.type) { case 'mention': return [i18n.t('_notification.youGotMention', { name: getUserName(data.user) }), { - body: getNoteSummary(data.note, i18n.locale), + body: data.note, icon: data.user.avatarUrl }]; case 'reply': return [i18n.t('_notification.youGotReply', { name: getUserName(data.user) }), { - body: getNoteSummary(data.note, i18n.locale), + body: data.note, icon: data.user.avatarUrl }]; case 'renote': return [i18n.t('_notification.youRenoted', { name: getUserName(data.user) }), { - body: getNoteSummary(data.note, i18n.locale), + body: data.note, icon: data.user.avatarUrl }]; case 'quote': return [i18n.t('_notification.youGotQuote', { name: getUserName(data.user) }), { - body: getNoteSummary(data.note, i18n.locale), + body: data.note, icon: data.user.avatarUrl }]; case 'reaction': return [`${data.reaction} ${getUserName(data.user)}`, { - body: getNoteSummary(data.note, i18n.locale), + body: data.note, icon: data.user.avatarUrl }]; case 'pollVote': return [i18n.t('_notification.youGotPoll', { name: getUserName(data.user) }), { - body: getNoteSummary(data.note, i18n.locale), + body: data.note, icon: data.user.avatarUrl }];