import push from "web-push"; import config from "../config/index.js"; import { SwSubscriptions } from "../models/index.js"; import { fetchMeta } from "../misc/fetch-meta.js"; import { getNoteSummary } from "../misc/get-note-summary.js"; // プッシュメッセージサーバーには文字数制限があるため、内容を削減します function truncateNotification(notification) { if (notification.note) { return { ...notification, note: { ...notification.note, // textをgetNoteSummaryしたものに置き換える text: getNoteSummary(notification.type === "renote" ? notification.note.renote : notification.note), cw: undefined, reply: undefined, renote: undefined, user: undefined } }; } return notification; } export async function pushNotification(userId, type, body) { const meta = await fetchMeta(); // アプリケーションの連絡先と、サーバーサイドの鍵ペアの情報を登録 push.setVapidDetails(config.url, meta.swPublicKey, meta.swPrivateKey); // Fetch const subscriptions = await SwSubscriptions.findBy({ userId: userId }); for (const subscription of subscriptions){ if ([ "readNotifications", "readAllNotifications", "readAllMessagingMessages", "readAllMessagingMessagesOfARoom" ].includes(type) && !subscription.sendReadMessage) continue; const pushSubscription = { endpoint: subscription.endpoint, keys: { auth: subscription.auth, p256dh: subscription.publickey } }; push.sendNotification(pushSubscription, JSON.stringify({ type, body: type === "notification" ? truncateNotification(body) : body, userId, dateTime: new Date().getTime() }), { proxy: config.proxy }).catch((err)=>{ //swLogger.info(err.statusCode); //swLogger.info(err.headers); //swLogger.info(err.body); if (err.statusCode === 410) { SwSubscriptions.delete({ userId: userId, endpoint: subscription.endpoint, auth: subscription.auth, publickey: subscription.publickey }); } }); } }