2022-05-30 00:21:42 +00:00
|
|
|
import webpush, { type PushSubscription, WebPushError } from "web-push";
|
2022-06-12 21:17:22 +00:00
|
|
|
import type { MessageInstance } from "twilio/lib/rest/api/v2010/account/message";
|
|
|
|
import { parsePhoneNumber } from "awesome-phonenumber";
|
|
|
|
import { type NotificationSubscription, Direction } from "@prisma/client";
|
2022-05-30 00:21:42 +00:00
|
|
|
|
|
|
|
import serverConfig from "~/config/config.server";
|
|
|
|
import db from "~/utils/db.server";
|
|
|
|
import logger from "~/utils/logger.server";
|
2022-06-12 21:17:22 +00:00
|
|
|
import { translateMessageDirection } from "~/utils/twilio.server";
|
2022-05-30 00:21:42 +00:00
|
|
|
|
2022-06-12 21:17:22 +00:00
|
|
|
export type NotificationPayload = Omit<NotificationOptions, "data"> & {
|
2022-06-01 20:45:49 +00:00
|
|
|
title: string; // max 50 characters
|
|
|
|
body: string; // max 150 characters
|
2022-06-12 21:17:22 +00:00
|
|
|
data: {
|
|
|
|
recipient: string;
|
|
|
|
type: "message" | "call";
|
|
|
|
};
|
2022-05-30 00:21:42 +00:00
|
|
|
};
|
|
|
|
|
2022-06-01 20:45:49 +00:00
|
|
|
export async function notify(subscriptions: NotificationSubscription[], payload: NotificationPayload) {
|
2022-05-30 00:21:42 +00:00
|
|
|
webpush.setVapidDetails("mailto:mokht@rmi.al", serverConfig.webPush.publicKey, serverConfig.webPush.privateKey);
|
2022-06-01 20:45:49 +00:00
|
|
|
const title = truncate(payload.title, 50);
|
|
|
|
const body = truncate(payload.body, 150);
|
|
|
|
const _payload = JSON.stringify({
|
|
|
|
...payload,
|
|
|
|
title,
|
|
|
|
body,
|
2022-05-30 00:21:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
subscriptions.map(async (subscription) => {
|
|
|
|
const webPushSubscription: PushSubscription = {
|
|
|
|
endpoint: subscription.endpoint,
|
|
|
|
keys: {
|
|
|
|
p256dh: subscription.keys_p256dh,
|
|
|
|
auth: subscription.keys_auth,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2022-06-01 20:45:49 +00:00
|
|
|
await webpush.sendNotification(webPushSubscription, _payload);
|
2022-05-30 00:21:42 +00:00
|
|
|
} catch (error: any) {
|
|
|
|
logger.error(error);
|
|
|
|
if (error instanceof WebPushError) {
|
|
|
|
// subscription most likely expired or has been revoked
|
|
|
|
await db.notificationSubscription.delete({ where: { id: subscription.id } });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2022-06-01 20:45:49 +00:00
|
|
|
|
|
|
|
function truncate(str: string, maxLength: number) {
|
|
|
|
if (str.length <= maxLength) {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str.substring(0, maxLength - 1) + "\u2026";
|
|
|
|
}
|
2022-06-12 21:17:22 +00:00
|
|
|
|
|
|
|
export function buildMessageNotificationPayload(message: MessageInstance): NotificationPayload {
|
|
|
|
const direction = translateMessageDirection(message.direction);
|
|
|
|
const recipient = direction === Direction.Outbound ? message.to : message.from;
|
|
|
|
return {
|
|
|
|
title: parsePhoneNumber(recipient).getNumber("international"),
|
|
|
|
body: message.body,
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
action: "reply",
|
|
|
|
title: "Reply",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
data: { recipient, type: "message" },
|
|
|
|
};
|
|
|
|
}
|