You've already forked AstralRinth
forked from didirus/AstralRinth
Overhaul notifs + threads fixes (#573)
* Overhaul notifs + threads fixes * fix lang
This commit is contained in:
@@ -1,31 +1,19 @@
|
||||
use super::ids::*;
|
||||
use crate::database::models::DatabaseError;
|
||||
use crate::models::notifications::NotificationBody;
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::Deserialize;
|
||||
|
||||
pub struct NotificationBuilder {
|
||||
pub notification_type: Option<String>,
|
||||
pub title: String,
|
||||
pub text: String,
|
||||
pub link: String,
|
||||
pub actions: Vec<NotificationActionBuilder>,
|
||||
}
|
||||
|
||||
pub struct NotificationActionBuilder {
|
||||
pub title: String,
|
||||
pub action_route: (String, String),
|
||||
pub body: NotificationBody,
|
||||
}
|
||||
|
||||
pub struct Notification {
|
||||
pub id: NotificationId,
|
||||
pub user_id: UserId,
|
||||
pub notification_type: Option<String>,
|
||||
pub title: String,
|
||||
pub text: String,
|
||||
pub link: String,
|
||||
pub body: NotificationBody,
|
||||
pub read: bool,
|
||||
pub created: DateTime<Utc>,
|
||||
pub actions: Vec<NotificationAction>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@@ -54,28 +42,12 @@ impl NotificationBuilder {
|
||||
for user in users {
|
||||
let id = generate_notification_id(&mut *transaction).await?;
|
||||
|
||||
let mut actions = Vec::new();
|
||||
|
||||
for action in &self.actions {
|
||||
actions.push(NotificationAction {
|
||||
id: NotificationActionId(0),
|
||||
notification_id: id,
|
||||
title: action.title.clone(),
|
||||
action_route_method: action.action_route.0.clone(),
|
||||
action_route: action.action_route.1.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
Notification {
|
||||
id,
|
||||
user_id: user,
|
||||
notification_type: self.notification_type.clone(),
|
||||
title: self.title.clone(),
|
||||
text: self.text.clone(),
|
||||
link: self.link.clone(),
|
||||
body: self.body.clone(),
|
||||
read: false,
|
||||
created: Utc::now(),
|
||||
actions,
|
||||
}
|
||||
.insert(&mut *transaction)
|
||||
.await?;
|
||||
@@ -89,30 +61,23 @@ impl Notification {
|
||||
pub async fn insert(
|
||||
&self,
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
) -> Result<(), sqlx::error::Error> {
|
||||
) -> Result<(), DatabaseError> {
|
||||
sqlx::query!(
|
||||
"
|
||||
INSERT INTO notifications (
|
||||
id, user_id, title, text, link, type
|
||||
id, user_id, body
|
||||
)
|
||||
VALUES (
|
||||
$1, $2, $3, $4, $5, $6
|
||||
$1, $2, $3
|
||||
)
|
||||
",
|
||||
self.id as NotificationId,
|
||||
self.user_id as UserId,
|
||||
&self.title,
|
||||
&self.text,
|
||||
&self.link,
|
||||
self.notification_type
|
||||
serde_json::value::to_value(self.body.clone())?
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
|
||||
for action in &self.actions {
|
||||
action.insert(&mut *transaction).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -141,7 +106,7 @@ impl Notification {
|
||||
notification_ids.iter().map(|x| x.0).collect();
|
||||
sqlx::query!(
|
||||
"
|
||||
SELECT n.id, n.user_id, n.title, n.text, n.link, n.created, n.read, n.type notification_type,
|
||||
SELECT n.id, n.user_id, n.title, n.text, n.link, n.created, n.read, n.type notification_type, n.body,
|
||||
JSONB_AGG(DISTINCT jsonb_build_object('id', na.id, 'notification_id', na.notification_id, 'title', na.title, 'action_route_method', na.action_route_method, 'action_route', na.action_route)) filter (where na.id is not null) actions
|
||||
FROM notifications n
|
||||
LEFT OUTER JOIN notifications_actions na on n.id = na.notification_id
|
||||
@@ -159,17 +124,25 @@ impl Notification {
|
||||
Notification {
|
||||
id,
|
||||
user_id: UserId(row.user_id),
|
||||
notification_type: row.notification_type,
|
||||
title: row.title,
|
||||
text: row.text,
|
||||
link: row.link,
|
||||
read: row.read,
|
||||
created: row.created,
|
||||
actions: serde_json::from_value(
|
||||
row.actions.unwrap_or_default(),
|
||||
)
|
||||
.ok()
|
||||
.unwrap_or_default(),
|
||||
body: row.body.clone().and_then(|x| serde_json::from_value(x).ok()).unwrap_or_else(|| {
|
||||
if let Some(title) = row.title {
|
||||
NotificationBody::LegacyMarkdown {
|
||||
notification_type: row.notification_type,
|
||||
title,
|
||||
text: row.text.unwrap_or_default(),
|
||||
link: row.link.unwrap_or_default(),
|
||||
actions: serde_json::from_value(
|
||||
row.actions.unwrap_or_default(),
|
||||
)
|
||||
.ok()
|
||||
.unwrap_or_default(),
|
||||
}
|
||||
} else {
|
||||
NotificationBody::Unknown
|
||||
}
|
||||
}),
|
||||
}
|
||||
}))
|
||||
})
|
||||
@@ -188,7 +161,7 @@ impl Notification {
|
||||
|
||||
sqlx::query!(
|
||||
"
|
||||
SELECT n.id, n.user_id, n.title, n.text, n.link, n.created, n.read, n.type notification_type,
|
||||
SELECT n.id, n.user_id, n.title, n.text, n.link, n.created, n.read, n.type notification_type, n.body,
|
||||
JSONB_AGG(DISTINCT jsonb_build_object('id', na.id, 'notification_id', na.notification_id, 'title', na.title, 'action_route_method', na.action_route_method, 'action_route', na.action_route)) filter (where na.id is not null) actions
|
||||
FROM notifications n
|
||||
LEFT OUTER JOIN notifications_actions na on n.id = na.notification_id
|
||||
@@ -205,17 +178,25 @@ impl Notification {
|
||||
Notification {
|
||||
id,
|
||||
user_id: UserId(row.user_id),
|
||||
notification_type: row.notification_type,
|
||||
title: row.title,
|
||||
text: row.text,
|
||||
link: row.link,
|
||||
read: row.read,
|
||||
created: row.created,
|
||||
actions: serde_json::from_value(
|
||||
row.actions.unwrap_or_default(),
|
||||
)
|
||||
.ok()
|
||||
.unwrap_or_default(),
|
||||
body: row.body.clone().and_then(|x| serde_json::from_value(x).ok()).unwrap_or_else(|| {
|
||||
if let Some(title) = row.title {
|
||||
NotificationBody::LegacyMarkdown {
|
||||
notification_type: row.notification_type,
|
||||
title,
|
||||
text: row.text.unwrap_or_default(),
|
||||
link: row.link.unwrap_or_default(),
|
||||
actions: serde_json::from_value(
|
||||
row.actions.unwrap_or_default(),
|
||||
)
|
||||
.ok()
|
||||
.unwrap_or_default(),
|
||||
}
|
||||
} else {
|
||||
NotificationBody::Unknown
|
||||
}
|
||||
}),
|
||||
}
|
||||
}))
|
||||
})
|
||||
@@ -260,29 +241,3 @@ impl Notification {
|
||||
Ok(Some(()))
|
||||
}
|
||||
}
|
||||
|
||||
impl NotificationAction {
|
||||
pub async fn insert(
|
||||
&self,
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
) -> Result<(), sqlx::error::Error> {
|
||||
sqlx::query!(
|
||||
"
|
||||
INSERT INTO notifications_actions (
|
||||
notification_id, title, action_route, action_route_method
|
||||
)
|
||||
VALUES (
|
||||
$1, $2, $3, $4
|
||||
)
|
||||
",
|
||||
self.notification_id as NotificationId,
|
||||
&self.title,
|
||||
&self.action_route,
|
||||
&self.action_route_method
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user