Overhaul notifs + threads fixes (#573)

* Overhaul notifs + threads fixes

* fix lang
This commit is contained in:
Geometrically
2023-04-15 19:48:21 -07:00
committed by GitHub
parent 969eb67217
commit 95ae981698
12 changed files with 719 additions and 619 deletions

View File

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

View File

@@ -7,6 +7,8 @@ use serde::Deserialize;
pub struct ThreadBuilder {
pub type_: ThreadType,
pub members: Vec<UserId>,
pub project_id: Option<ProjectId>,
pub report_id: Option<ReportId>,
}
#[derive(Clone)]
@@ -15,13 +17,15 @@ pub struct Thread {
pub type_: ThreadType,
pub messages: Vec<ThreadMessage>,
pub members: Vec<UserId>,
pub show_in_mod_inbox: bool,
pub project_id: Option<ProjectId>,
pub report_id: Option<ReportId>,
}
pub struct ThreadMessageBuilder {
pub author_id: Option<UserId>,
pub body: MessageBody,
pub thread_id: ThreadId,
pub show_in_mod_inbox: bool,
}
#[derive(Deserialize, Clone)]
@@ -31,7 +35,6 @@ pub struct ThreadMessage {
pub author_id: Option<UserId>,
pub body: MessageBody,
pub created: DateTime<Utc>,
pub show_in_mod_inbox: bool,
}
impl ThreadMessageBuilder {
@@ -45,17 +48,16 @@ impl ThreadMessageBuilder {
sqlx::query!(
"
INSERT INTO threads_messages (
id, author_id, body, thread_id, show_in_mod_inbox
id, author_id, body, thread_id
)
VALUES (
$1, $2, $3, $4, $5
$1, $2, $3, $4
)
",
thread_message_id as ThreadMessageId,
self.author_id.map(|x| x.0),
serde_json::value::to_value(self.body.clone())?,
self.thread_id as ThreadId,
self.show_in_mod_inbox,
)
.execute(&mut *transaction)
.await?;
@@ -133,9 +135,9 @@ impl Thread {
thread_ids.iter().map(|x| x.0).collect();
let threads = sqlx::query!(
"
SELECT t.id, t.thread_type,
SELECT t.id, t.thread_type, t.show_in_mod_inbox, t.project_id, t.report_id,
ARRAY_AGG(DISTINCT tm.user_id) filter (where tm.user_id is not null) members,
JSONB_AGG(DISTINCT jsonb_build_object('id', tmsg.id, 'author_id', tmsg.author_id, 'thread_id', tmsg.thread_id, 'body', tmsg.body, 'created', tmsg.created, 'show_in_mod_inbox', tmsg.show_in_mod_inbox)) filter (where tmsg.id is not null) messages
JSONB_AGG(DISTINCT jsonb_build_object('id', tmsg.id, 'author_id', tmsg.author_id, 'thread_id', tmsg.thread_id, 'body', tmsg.body, 'created', tmsg.created)) filter (where tmsg.id is not null) messages
FROM threads t
LEFT OUTER JOIN threads_messages tmsg ON tmsg.thread_id = t.id
LEFT OUTER JOIN threads_members tm ON tm.thread_id = t.id
@@ -159,6 +161,9 @@ impl Thread {
messages
},
members: x.members.unwrap_or_default().into_iter().map(UserId).collect(),
show_in_mod_inbox: x.show_in_mod_inbox,
project_id: x.project_id.map(ProjectId),
report_id: x.report_id.map(ReportId),
}))
})
.try_collect::<Vec<Thread>>()
@@ -229,7 +234,7 @@ impl ThreadMessage {
message_ids.iter().map(|x| x.0).collect();
let messages = sqlx::query!(
"
SELECT tm.id, tm.author_id, tm.thread_id, tm.body, tm.created, tm.show_in_mod_inbox
SELECT tm.id, tm.author_id, tm.thread_id, tm.body, tm.created
FROM threads_messages tm
WHERE tm.id = ANY($1)
",
@@ -244,7 +249,6 @@ impl ThreadMessage {
body: serde_json::from_value(x.body)
.unwrap_or(MessageBody::Deleted),
created: x.created,
show_in_mod_inbox: x.show_in_mod_inbox,
}))
})
.try_collect::<Vec<ThreadMessage>>()
@@ -259,10 +263,13 @@ impl ThreadMessage {
) -> Result<Option<()>, sqlx::error::Error> {
sqlx::query!(
"
DELETE FROM threads_messages
UPDATE threads_messages
SET body = $2
WHERE id = $1
",
id as ThreadMessageId,
serde_json::to_value(MessageBody::Deleted)
.unwrap_or(serde_json::json!({}))
)
.execute(&mut *transaction)
.await?;