2023-01-13 04:40:33 +00:00
|
|
|
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 type { Packed } from "@/misc/schema.js";
|
|
|
|
import { getNoteSummary } from "@/misc/get-note-summary.js";
|
2017-11-20 18:40:09 +00:00
|
|
|
|
2022-04-30 12:52:07 +00:00
|
|
|
// Defined also packages/sw/types.ts#L14-L21
|
|
|
|
type pushNotificationsTypes = {
|
2023-01-13 04:40:33 +00:00
|
|
|
notification: Packed<"Notification">;
|
|
|
|
unreadMessagingMessage: Packed<"MessagingMessage">;
|
|
|
|
readNotifications: { notificationIds: string[] };
|
|
|
|
readAllNotifications: undefined;
|
|
|
|
readAllMessagingMessages: undefined;
|
|
|
|
readAllMessagingMessagesOfARoom: { userId: string } | { groupId: string };
|
2022-04-30 12:52:07 +00:00
|
|
|
};
|
2020-05-23 04:19:31 +00:00
|
|
|
|
2021-12-24 17:01:35 +00:00
|
|
|
// プッシュメッセージサーバーには文字数制限があるため、内容を削減します
|
2023-01-13 04:40:33 +00:00
|
|
|
function truncateNotification(notification: Packed<"Notification">): any {
|
2021-12-24 17:01:35 +00:00
|
|
|
if (notification.note) {
|
|
|
|
return {
|
|
|
|
...notification,
|
|
|
|
note: {
|
|
|
|
...notification.note,
|
|
|
|
// textをgetNoteSummaryしたものに置き換える
|
2023-01-13 04:40:33 +00:00
|
|
|
text: getNoteSummary(
|
|
|
|
notification.type === "renote"
|
|
|
|
? (notification.note.renote as Packed<"Note">)
|
|
|
|
: notification.note,
|
|
|
|
),
|
2022-04-30 12:52:07 +00:00
|
|
|
|
|
|
|
cw: undefined,
|
|
|
|
reply: undefined,
|
|
|
|
renote: undefined,
|
|
|
|
user: undefined as any, // 通知を受け取ったユーザーである場合が多いのでこれも捨てる
|
2023-01-13 04:40:33 +00:00
|
|
|
},
|
2021-12-24 17:01:35 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return notification;
|
|
|
|
}
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
export async function pushNotification<T extends keyof pushNotificationsTypes>(
|
|
|
|
userId: string,
|
|
|
|
type: T,
|
|
|
|
body: pushNotificationsTypes[T],
|
|
|
|
) {
|
2019-04-12 16:43:22 +00:00
|
|
|
const meta = await fetchMeta();
|
2018-12-19 19:08:13 +00:00
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
if (
|
|
|
|
!meta.enableServiceWorker ||
|
|
|
|
meta.swPublicKey == null ||
|
|
|
|
meta.swPrivateKey == null
|
|
|
|
)
|
|
|
|
return;
|
2017-11-20 18:40:09 +00:00
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
// アプリケーションの連絡先と、サーバーサイドの鍵ペアの情報を登録
|
2023-01-13 04:40:33 +00:00
|
|
|
push.setVapidDetails(config.url, meta.swPublicKey, meta.swPrivateKey);
|
2017-11-20 22:19:02 +00:00
|
|
|
|
2017-11-20 18:40:09 +00:00
|
|
|
// Fetch
|
2022-03-26 06:34:00 +00:00
|
|
|
const subscriptions = await SwSubscriptions.findBy({
|
2021-12-09 14:58:30 +00:00
|
|
|
userId: userId,
|
2017-11-20 18:40:09 +00:00
|
|
|
});
|
|
|
|
|
2018-12-11 11:36:55 +00:00
|
|
|
for (const subscription of subscriptions) {
|
2023-04-07 01:56:46 +00:00
|
|
|
if (
|
|
|
|
[
|
|
|
|
"readNotifications",
|
|
|
|
"readAllNotifications",
|
|
|
|
"readAllMessagingMessages",
|
|
|
|
"readAllMessagingMessagesOfARoom",
|
|
|
|
].includes(type) &&
|
|
|
|
!subscription.sendReadMessage
|
|
|
|
)
|
|
|
|
continue;
|
2023-04-04 03:36:59 +00:00
|
|
|
|
2017-11-20 18:40:09 +00:00
|
|
|
const pushSubscription = {
|
|
|
|
endpoint: subscription.endpoint,
|
|
|
|
keys: {
|
|
|
|
auth: subscription.auth,
|
2021-12-09 14:58:30 +00:00
|
|
|
p256dh: subscription.publickey,
|
|
|
|
},
|
2017-11-20 18:40:09 +00:00
|
|
|
};
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
push
|
|
|
|
.sendNotification(
|
|
|
|
pushSubscription,
|
|
|
|
JSON.stringify({
|
|
|
|
type,
|
|
|
|
body:
|
|
|
|
type === "notification"
|
|
|
|
? truncateNotification(body as Packed<"Notification">)
|
|
|
|
: body,
|
|
|
|
userId,
|
|
|
|
dateTime: new Date().getTime(),
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
proxy: config.proxy,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.catch((err: any) => {
|
|
|
|
//swLogger.info(err.statusCode);
|
|
|
|
//swLogger.info(err.headers);
|
|
|
|
//swLogger.info(err.body);
|
2017-11-20 18:40:09 +00:00
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
if (err.statusCode === 410) {
|
|
|
|
SwSubscriptions.delete({
|
|
|
|
userId: userId,
|
|
|
|
endpoint: subscription.endpoint,
|
|
|
|
auth: subscription.auth,
|
|
|
|
publickey: subscription.publickey,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-12-11 11:36:55 +00:00
|
|
|
}
|
2017-11-20 18:40:09 +00:00
|
|
|
}
|