|
|
|
@ -9,6 +9,13 @@ use crate::schema;
|
|
|
|
|
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl}; |
|
|
|
|
use rocket::form::Form; |
|
|
|
|
use rocket::serde::json::json; |
|
|
|
|
use rocket::serde::json::serde_json; |
|
|
|
|
use rocket::serde::Serialize; |
|
|
|
|
use std::fs::File; |
|
|
|
|
use url::Url; |
|
|
|
|
use web_push::{ |
|
|
|
|
ContentEncoding, SubscriptionInfo, VapidSignatureBuilder, WebPushClient, WebPushMessageBuilder, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
#[derive(FromForm)] |
|
|
|
|
pub struct AttentionInput { |
|
|
|
@ -76,3 +83,57 @@ pub async fn get_attention(user: CurrentUser, db: Db, rconn: RdsConn) -> JsonApi
|
|
|
|
|
|
|
|
|
|
code0!(ps_data) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(FromForm)] |
|
|
|
|
pub struct NotificatinInput { |
|
|
|
|
enable: bool, |
|
|
|
|
endpoint: String, |
|
|
|
|
auth: String, |
|
|
|
|
p256dh: String, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(Serialize)] |
|
|
|
|
#[serde(crate = "rocket::serde")] |
|
|
|
|
struct PushData { |
|
|
|
|
title: String, |
|
|
|
|
pid: i32, |
|
|
|
|
text: String, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[post("/post/<pid>/notification", data = "<ni>")] |
|
|
|
|
pub async fn set_notification(pid: i32, ni: Form<NotificatinInput>, _user: CurrentUser) -> JsonApi { |
|
|
|
|
let url_host = Url::parse(&ni.endpoint) |
|
|
|
|
.map_err(|_| UnknownPushEndpoint)? |
|
|
|
|
.host() |
|
|
|
|
.ok_or(UnknownPushEndpoint)? |
|
|
|
|
.to_string(); |
|
|
|
|
(url_host.ends_with("googleapis.com") || url_host.ends_with("mozilla.com")) |
|
|
|
|
.then(|| ()) |
|
|
|
|
.ok_or(UnknownPushEndpoint)?; |
|
|
|
|
|
|
|
|
|
if ni.enable { |
|
|
|
|
let subscription_info = SubscriptionInfo::new(&ni.endpoint, &ni.p256dh, &ni.auth); |
|
|
|
|
|
|
|
|
|
let file = File::open("keys/private.pem").unwrap(); |
|
|
|
|
let sig_builder = VapidSignatureBuilder::from_pem(file, &subscription_info) |
|
|
|
|
.unwrap() |
|
|
|
|
.build() |
|
|
|
|
.unwrap(); |
|
|
|
|
|
|
|
|
|
let mut builder = WebPushMessageBuilder::new(&subscription_info).unwrap(); |
|
|
|
|
let data = PushData { |
|
|
|
|
title: "测试".to_owned(), |
|
|
|
|
pid, |
|
|
|
|
text: format!("#{} 开启提醒测试成功,消息提醒功能即将正式上线", &pid), |
|
|
|
|
}; |
|
|
|
|
let content = serde_json::to_string(&data).unwrap(); |
|
|
|
|
builder.set_payload(ContentEncoding::Aes128Gcm, content.as_bytes()); |
|
|
|
|
builder.set_vapid_signature(sig_builder); |
|
|
|
|
|
|
|
|
|
let client = WebPushClient::new()?; |
|
|
|
|
|
|
|
|
|
client.send(builder.build()?).await?; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
code0!() |
|
|
|
|
} |
|
|
|
|