From c77fe1f2cf5da7c111e9008d869c702644075d44 Mon Sep 17 00:00:00 2001 From: tamaina Date: Sat, 25 Dec 2021 02:01:35 +0900 Subject: [PATCH] Truncate push notification message (#8089) * Truncate push notification message * fix --- .../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 616bc7441..2133768a9 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 0aed9610e..10bd27c9e 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.text, icon: data.user.avatarUrl }]; case 'reply': return [i18n.t('_notification.youGotReply', { name: getUserName(data.user) }), { - body: getNoteSummary(data.note, i18n.locale), + body: data.note.text, icon: data.user.avatarUrl }]; case 'renote': return [i18n.t('_notification.youRenoted', { name: getUserName(data.user) }), { - body: getNoteSummary(data.note, i18n.locale), + body: data.note.text, icon: data.user.avatarUrl }]; case 'quote': return [i18n.t('_notification.youGotQuote', { name: getUserName(data.user) }), { - body: getNoteSummary(data.note, i18n.locale), + body: data.note.text, icon: data.user.avatarUrl }]; case 'reaction': return [`${data.reaction} ${getUserName(data.user)}`, { - body: getNoteSummary(data.note, i18n.locale), + body: data.note.text, icon: data.user.avatarUrl }]; case 'pollVote': return [i18n.t('_notification.youGotPoll', { name: getUserName(data.user) }), { - body: getNoteSummary(data.note, i18n.locale), + body: data.note.text, icon: data.user.avatarUrl }];