Truncate push notification message
This commit is contained in:
parent
8e7744a695
commit
ffef12ce1d
2 changed files with 32 additions and 8 deletions
|
@ -3,10 +3,33 @@ import config from '@/config/index';
|
||||||
import { SwSubscriptions } from '@/models/index';
|
import { SwSubscriptions } from '@/models/index';
|
||||||
import { fetchMeta } from '@/misc/fetch-meta';
|
import { fetchMeta } from '@/misc/fetch-meta';
|
||||||
import { Packed } from '@/misc/schema';
|
import { Packed } from '@/misc/schema';
|
||||||
|
import { getNoteSummary } from '@/misc/get-note-summary';
|
||||||
|
|
||||||
type notificationType = 'notification' | 'unreadMessagingMessage';
|
type notificationType = 'notification' | 'unreadMessagingMessage';
|
||||||
type notificationBody = Packed<'Notification'> | Packed<'MessagingMessage'>;
|
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) {
|
export default async function(userId: string, type: notificationType, body: notificationBody) {
|
||||||
const meta = await fetchMeta();
|
const meta = await fetchMeta();
|
||||||
|
|
||||||
|
@ -32,7 +55,9 @@ export default async function(userId: string, type: notificationType, body: noti
|
||||||
};
|
};
|
||||||
|
|
||||||
push.sendNotification(pushSubscription, JSON.stringify({
|
push.sendNotification(pushSubscription, JSON.stringify({
|
||||||
type, body,
|
type,
|
||||||
|
body: type === 'notification' ? truncateNotification(body as Packed<'Notification'>) : body,
|
||||||
|
userId,
|
||||||
}), {
|
}), {
|
||||||
proxy: config.proxy,
|
proxy: config.proxy,
|
||||||
}).catch((err: any) => {
|
}).catch((err: any) => {
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
*/
|
*/
|
||||||
declare var self: ServiceWorkerGlobalScope;
|
declare var self: ServiceWorkerGlobalScope;
|
||||||
|
|
||||||
import { getNoteSummary } from '@/scripts/get-note-summary';
|
|
||||||
import * as misskey from 'misskey-js';
|
import * as misskey from 'misskey-js';
|
||||||
|
|
||||||
function getUserName(user: misskey.entities.User): string {
|
function getUserName(user: misskey.entities.User): string {
|
||||||
|
@ -26,37 +25,37 @@ export default async function(type, data, i18n): Promise<[string, NotificationOp
|
||||||
switch (data.type) {
|
switch (data.type) {
|
||||||
case 'mention':
|
case 'mention':
|
||||||
return [i18n.t('_notification.youGotMention', { name: getUserName(data.user) }), {
|
return [i18n.t('_notification.youGotMention', { name: getUserName(data.user) }), {
|
||||||
body: getNoteSummary(data.note, i18n.locale),
|
body: data.note,
|
||||||
icon: data.user.avatarUrl
|
icon: data.user.avatarUrl
|
||||||
}];
|
}];
|
||||||
|
|
||||||
case 'reply':
|
case 'reply':
|
||||||
return [i18n.t('_notification.youGotReply', { name: getUserName(data.user) }), {
|
return [i18n.t('_notification.youGotReply', { name: getUserName(data.user) }), {
|
||||||
body: getNoteSummary(data.note, i18n.locale),
|
body: data.note,
|
||||||
icon: data.user.avatarUrl
|
icon: data.user.avatarUrl
|
||||||
}];
|
}];
|
||||||
|
|
||||||
case 'renote':
|
case 'renote':
|
||||||
return [i18n.t('_notification.youRenoted', { name: getUserName(data.user) }), {
|
return [i18n.t('_notification.youRenoted', { name: getUserName(data.user) }), {
|
||||||
body: getNoteSummary(data.note, i18n.locale),
|
body: data.note,
|
||||||
icon: data.user.avatarUrl
|
icon: data.user.avatarUrl
|
||||||
}];
|
}];
|
||||||
|
|
||||||
case 'quote':
|
case 'quote':
|
||||||
return [i18n.t('_notification.youGotQuote', { name: getUserName(data.user) }), {
|
return [i18n.t('_notification.youGotQuote', { name: getUserName(data.user) }), {
|
||||||
body: getNoteSummary(data.note, i18n.locale),
|
body: data.note,
|
||||||
icon: data.user.avatarUrl
|
icon: data.user.avatarUrl
|
||||||
}];
|
}];
|
||||||
|
|
||||||
case 'reaction':
|
case 'reaction':
|
||||||
return [`${data.reaction} ${getUserName(data.user)}`, {
|
return [`${data.reaction} ${getUserName(data.user)}`, {
|
||||||
body: getNoteSummary(data.note, i18n.locale),
|
body: data.note,
|
||||||
icon: data.user.avatarUrl
|
icon: data.user.avatarUrl
|
||||||
}];
|
}];
|
||||||
|
|
||||||
case 'pollVote':
|
case 'pollVote':
|
||||||
return [i18n.t('_notification.youGotPoll', { name: getUserName(data.user) }), {
|
return [i18n.t('_notification.youGotPoll', { name: getUserName(data.user) }), {
|
||||||
body: getNoteSummary(data.note, i18n.locale),
|
body: data.note,
|
||||||
icon: data.user.avatarUrl
|
icon: data.user.avatarUrl
|
||||||
}];
|
}];
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue