diff --git a/packages/backend-rs/index.d.ts b/packages/backend-rs/index.d.ts index 37392c2796..c959593c00 100644 --- a/packages/backend-rs/index.d.ts +++ b/packages/backend-rs/index.d.ts @@ -1274,7 +1274,8 @@ export enum PushNotificationKind { ReadAllChats = 'readAllChats', ReadAllChatsInTheRoom = 'readAllChatsInTheRoom', ReadNotifications = 'readNotifications', - ReadAllNotifications = 'readAllNotifications' + ReadAllNotifications = 'readAllNotifications', + Mastodon = 'mastodon' } export function sendPushNotification(receiverUserId: string, kind: PushNotificationKind, content: any): Promise export function publishToChannelStream(channelId: string, userId: string): Promise diff --git a/packages/backend-rs/src/service/push_notification.rs b/packages/backend-rs/src/service/push_notification.rs index c33567138e..5ee86f77f0 100644 --- a/packages/backend-rs/src/service/push_notification.rs +++ b/packages/backend-rs/src/service/push_notification.rs @@ -47,6 +47,7 @@ pub enum PushNotificationKind { ReadNotifications, #[strum(serialize = "readAllNotifications")] ReadAllNotifications, + Mastodon, } fn compact_content( @@ -158,13 +159,21 @@ pub async fn send_push_notification( .all(db) .await?; - let payload = format!( - "{{\"type\":\"{}\",\"userId\":\"{}\",\"dateTime\":{},\"body\":{}}}", - kind, - receiver_user_id, - chrono::Utc::now().timestamp_millis(), - serde_json::to_string(&compact_content(&kind, content.clone())?)? - ); + // TODO: refactoring + let payload = if kind == PushNotificationKind::Mastodon { + // Leave the `content` as it is + serde_json::to_string(content)? + } else { + // Format the `content` passed from the TypeScript backend + // for Firefish push notifications + format!( + "{{\"type\":\"{}\",\"userId\":\"{}\",\"dateTime\":{},\"body\":{}}}", + kind, + receiver_user_id, + chrono::Utc::now().timestamp_millis(), + serde_json::to_string(&compact_content(&kind, content.clone())?)? + ) + }; tracing::trace!("payload: {:#?}", payload); for subscription in subscriptions.iter() { @@ -209,9 +218,15 @@ pub async fn send_push_notification( continue; } + let encoding = if kind == PushNotificationKind::Mastodon { + ContentEncoding::AesGcm + } else { + ContentEncoding::Aes128Gcm + }; + let mut message_builder = WebPushMessageBuilder::new(&subscription_info); message_builder.set_ttl(1000); - message_builder.set_payload(ContentEncoding::Aes128Gcm, payload.as_bytes()); + message_builder.set_payload(encoding, payload.as_bytes()); message_builder.set_vapid_signature(signature.unwrap()); let message = message_builder.build();