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

@@ -759,6 +759,8 @@ async fn project_create_inner(
let thread_id = ThreadBuilder {
type_: ThreadType::Project,
members: vec![],
project_id: Some(project_id.into()),
report_id: None,
}
.insert(&mut *transaction)
.await?;

View File

@@ -4,6 +4,7 @@ use crate::database::models::thread_item::ThreadMessageBuilder;
use crate::file_hosting::FileHost;
use crate::models;
use crate::models::ids::base62_impl::parse_base62;
use crate::models::notifications::NotificationBody;
use crate::models::projects::{
DonationLink, Project, ProjectId, ProjectStatus, SearchRequest, SideType,
};
@@ -615,23 +616,11 @@ pub async fn project_edit(
.await?;
NotificationBuilder {
notification_type: Some("status_update".to_string()),
title: format!(
"**{}**'s status has changed!",
project_item.inner.title
),
text: format!(
"The project {}'s status has changed from {} to {}",
project_item.inner.title,
project_item.inner.status.as_friendly_str(),
status.as_friendly_str()
),
link: format!(
"/{}/{}",
project_item.project_type,
ProjectId::from(id)
),
actions: vec![],
body: NotificationBody::StatusChange {
project_id: project_item.inner.id.into(),
old_status: project_item.inner.status,
new_status: *status,
},
}
.insert_many(notified_members, &mut transaction)
.await?;
@@ -645,7 +634,6 @@ pub async fn project_edit(
old_status: project_item.inner.status,
},
thread_id: thread,
show_in_mod_inbox: false,
}
.insert(&mut transaction)
.await?;

View File

@@ -71,6 +71,8 @@ pub async fn report_create(
let thread_id = ThreadBuilder {
type_: ThreadType::Report,
members: vec![],
project_id: None,
report_id: Some(id),
}
.insert(&mut transaction)
.await?;
@@ -314,7 +316,7 @@ pub async fn report_edit(
}
if let Some(edit_closed) = edit_report.closed {
if report.closed && !edit_closed && !user.role.is_mod() {
if !user.role.is_mod() {
return Err(ApiError::InvalidInput(
"You cannot reopen a report!".to_string(),
));
@@ -323,9 +325,12 @@ pub async fn report_edit(
if let Some(thread) = report.thread_id {
ThreadMessageBuilder {
author_id: Some(user.id.into()),
body: MessageBody::ThreadClosure,
body: if !edit_closed && report.closed {
MessageBody::ThreadReopen
} else {
MessageBody::ThreadClosure
},
thread_id: thread,
show_in_mod_inbox: false,
}
.insert(&mut transaction)
.await?;

View File

@@ -1,8 +1,7 @@
use crate::database::models::notification_item::{
NotificationActionBuilder, NotificationBuilder,
};
use crate::database::models::notification_item::NotificationBuilder;
use crate::database::models::TeamMember;
use crate::models::ids::ProjectId;
use crate::models::notifications::NotificationBody;
use crate::models::teams::{Permissions, TeamId};
use crate::models::users::UserId;
use crate::routes::ApiError;
@@ -333,9 +332,8 @@ pub async fn add_team_member(
let result = sqlx::query!(
"
SELECT m.title title, m.id id, pt.name project_type
SELECT m.id
FROM mods m
INNER JOIN project_types pt ON pt.id = m.project_type
WHERE m.team_id = $1
",
team_id as crate::database::models::ids::TeamId
@@ -343,32 +341,13 @@ pub async fn add_team_member(
.fetch_one(&**pool)
.await?;
let team: TeamId = team_id.into();
NotificationBuilder {
notification_type: Some("team_invite".to_string()),
title: "You have been invited to join a team!".to_string(),
text: format!(
"Team invite from {} to join the team for project {}",
current_user.username, result.title
),
link: format!(
"/{}/{}",
result.project_type,
ProjectId(result.id as u64)
),
actions: vec![
NotificationActionBuilder {
title: "Accept".to_string(),
action_route: ("POST".to_string(), format!("team/{team}/join")),
},
NotificationActionBuilder {
title: "Deny".to_string(),
action_route: (
"DELETE".to_string(),
format!("team/{team}/members/{}", new_member.user_id),
),
},
],
body: NotificationBody::TeamInvite {
project_id: ProjectId(result.id as u64),
team_id: team_id.into(),
invited_by: current_user.id,
role: new_member.role.clone(),
},
}
.insert(new_member.user_id.into(), &mut transaction)
.await?;

View File

@@ -1,7 +1,9 @@
use crate::database;
use crate::database::models::notification_item::NotificationBuilder;
use crate::database::models::thread_item::ThreadMessageBuilder;
use crate::models::ids::ThreadMessageId;
use crate::models::projects::ProjectStatus;
use crate::models::ids::{ReportId, ThreadMessageId};
use crate::models::notifications::NotificationBody;
use crate::models::projects::{ProjectId, ProjectStatus};
use crate::models::threads::{
MessageBody, Thread, ThreadId, ThreadMessage, ThreadType,
};
@@ -11,6 +13,7 @@ use crate::util::auth::{
check_is_moderator_from_headers, get_user_from_headers,
};
use actix_web::{delete, get, post, web, HttpRequest, HttpResponse};
use futures::TryStreamExt;
use serde::Deserialize;
use sqlx::PgPool;
@@ -19,7 +22,8 @@ pub fn config(cfg: &mut web::ServiceConfig) {
web::scope("thread")
.service(moderation_inbox)
.service(thread_get)
.service(thread_send_message),
.service(thread_send_message)
.service(thread_read),
);
cfg.service(web::scope("message").service(message_delete));
cfg.service(threads_get);
@@ -86,8 +90,6 @@ pub async fn filter_authorized_threads(
}
if !check_threads.is_empty() {
use futures::TryStreamExt;
let project_thread_ids = check_threads
.iter()
.filter(|x| x.type_ == ThreadType::Project)
@@ -248,6 +250,8 @@ fn convert_thread(
.into_iter()
.filter(|x| !x.role.is_mod() || user.role.is_mod())
.collect(),
project_id: data.project_id.map(ProjectId::from),
report_id: data.report_id.map(ReportId::from),
}
}
@@ -384,30 +388,93 @@ pub async fn thread_send_message(
return Ok(HttpResponse::NotFound().body(""));
}
let mod_notif = if thread.type_ == ThreadType::Project {
let status = sqlx::query!(
"SELECT m.status FROM mods m WHERE thread_id = $1",
let report = if let Some(report) = thread.report_id {
database::models::report_item::Report::get(report, &**pool).await?
} else {
None
};
if report.as_ref().map(|x| x.closed).unwrap_or(false)
&& !user.role.is_mod()
{
return Err(ApiError::InvalidInput(
"You may not reply to a closed report".to_string(),
));
}
let (mod_notif, (user_notif, team_id)) = if thread.type_
== ThreadType::Project
{
let record = sqlx::query!(
"SELECT m.status, m.team_id FROM mods m WHERE thread_id = $1",
thread.id as database::models::ids::ThreadId,
)
.fetch_one(&**pool)
.await?;
let status = ProjectStatus::from_str(&status.status);
let status = ProjectStatus::from_str(&record.status);
status == ProjectStatus::Processing && !user.role.is_mod()
(
status == ProjectStatus::Processing && !user.role.is_mod(),
(
status != ProjectStatus::Processing && user.role.is_mod(),
Some(record.team_id),
),
)
} else {
false
(false, (thread.type_ == ThreadType::Report, None))
};
let mut transaction = pool.begin().await?;
ThreadMessageBuilder {
sqlx::query!(
"
UPDATE threads
SET show_in_mod_inbox = $1
WHERE id = $2
",
mod_notif,
thread.id.0,
)
.execute(&mut *transaction)
.await?;
let id = ThreadMessageBuilder {
author_id: Some(user.id.into()),
body: new_message.body.clone(),
thread_id: thread.id,
show_in_mod_inbox: mod_notif,
}
.insert(&mut transaction)
.await?;
if user_notif {
let users_to_notify = if let Some(team_id) = team_id {
let members = database::models::TeamMember::get_from_team_full(
database::models::TeamId(team_id),
&**pool,
)
.await?;
members.into_iter().map(|x| x.user.id).collect()
} else if let Some(report) = report {
vec![report.reporter]
} else {
vec![]
};
if !users_to_notify.is_empty() {
NotificationBuilder {
body: NotificationBody::ModeratorMessage {
thread_id: thread.id.into(),
message_id: id.into(),
project_id: thread.project_id.map(ProjectId::from),
report_id: thread.report_id.map(ReportId::from),
},
}
.insert_many(users_to_notify, &mut transaction)
.await?;
}
}
transaction.commit().await?;
Ok(HttpResponse::NoContent().body(""))
@@ -421,36 +488,33 @@ pub async fn moderation_inbox(
req: HttpRequest,
pool: web::Data<PgPool>,
) -> Result<HttpResponse, ApiError> {
check_is_moderator_from_headers(req.headers(), &**pool).await?;
let user = check_is_moderator_from_headers(req.headers(), &**pool).await?;
let messages = sqlx::query!(
let ids = sqlx::query!(
"
SELECT tm.id, tm.thread_id, tm.author_id, tm.body, tm.created, m.id project_id FROM threads_messages tm
INNER JOIN mods m ON m.thread_id = tm.thread_id
WHERE tm.show_in_mod_inbox = TRUE
SELECT id
FROM threads
WHERE show_in_mod_inbox = TRUE
"
)
.fetch_all(&**pool)
.await?
.into_iter()
.map(|x| serde_json::json! ({
"message": ThreadMessage {
id: ThreadMessageId(x.id as u64),
author_id: x.author_id.map(|x| crate::models::users::UserId(x as u64)),
body: serde_json::from_value(x.body).unwrap_or(MessageBody::Deleted),
created: x.created
},
"project_id": crate::models::projects::ProjectId(x.project_id as u64),
}))
.collect::<Vec<_>>();
.fetch_many(&**pool)
.try_filter_map(|e| async {
Ok(e.right().map(|m| database::models::ThreadId(m.id)))
})
.try_collect::<Vec<database::models::ThreadId>>()
.await?;
Ok(HttpResponse::Ok().json(messages))
let threads_data =
database::models::Thread::get_many(&ids, &**pool).await?;
let threads = filter_authorized_threads(threads_data, &user, &pool).await?;
Ok(HttpResponse::Ok().json(threads))
}
#[post("{id}/read")]
pub async fn read_message(
pub async fn thread_read(
req: HttpRequest,
info: web::Path<(ThreadMessageId,)>,
info: web::Path<(ThreadId,)>,
pool: web::Data<PgPool>,
) -> Result<HttpResponse, ApiError> {
check_is_moderator_from_headers(req.headers(), &**pool).await?;
@@ -460,7 +524,7 @@ pub async fn read_message(
sqlx::query!(
"
UPDATE threads_messages
UPDATE threads
SET show_in_mod_inbox = FALSE
WHERE id = $1
",

View File

@@ -5,6 +5,7 @@ use crate::database::models::version_item::{
DependencyBuilder, VersionBuilder, VersionFileBuilder,
};
use crate::file_hosting::FileHost;
use crate::models::notifications::NotificationBody;
use crate::models::pack::PackFileHash;
use crate::models::projects::{
Dependency, DependencyType, FileType, GameVersion, Loader, ProjectId,
@@ -377,18 +378,6 @@ async fn version_create_inner(
));
}
let result = sqlx::query!(
"
SELECT m.title title, pt.name project_type
FROM mods m
INNER JOIN project_types pt ON pt.id = m.project_type
WHERE m.id = $1
",
builder.project_id as crate::database::models::ids::ProjectId
)
.fetch_one(&mut *transaction)
.await?;
use futures::stream::TryStreamExt;
let users = sqlx::query!(
@@ -409,18 +398,10 @@ async fn version_create_inner(
let version_id: VersionId = builder.version_id.into();
NotificationBuilder {
notification_type: Some("project_update".to_string()),
title: format!("**{}** has been updated!", result.title),
text: format!(
"The project {} has released a new version: {}",
result.title,
version_data.version_number.clone()
),
link: format!(
"/{}/{}/version/{}",
result.project_type, project_id, version_id
),
actions: vec![],
body: NotificationBody::ProjectUpdate {
project_id,
version_id,
},
}
.insert_many(users, &mut *transaction)
.await?;