feat (backend-rs): add Mastodon push notification type

This commit is contained in:
naskya 2024-05-19 12:19:43 +09:00
parent 945465cc5e
commit 797c768f24
No known key found for this signature in database
GPG key ID: 712D413B3A9FED5C
2 changed files with 25 additions and 9 deletions

View file

@ -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<void>
export function publishToChannelStream(channelId: string, userId: string): Promise<void>

View file

@ -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();