diff --git a/src/database/models/thread_item.rs b/src/database/models/thread_item.rs index bfb6d7549..aea93e3ed 100644 --- a/src/database/models/thread_item.rs +++ b/src/database/models/thread_item.rs @@ -20,7 +20,7 @@ pub struct ThreadMessageBuilder { pub author_id: Option, pub body: MessageBody, pub thread_id: ThreadId, - pub show_in_mod_inbox: Option, + pub show_in_mod_inbox: bool, } #[derive(Deserialize)] @@ -148,11 +148,15 @@ impl Thread { Ok(e.right().map(|x| Thread { id: ThreadId(x.id), type_: ThreadType::from_str(&x.thread_type), - messages: serde_json::from_value( - x.messages.unwrap_or_default(), - ) - .ok() - .unwrap_or_default(), + messages: { + let mut messages: Vec = serde_json::from_value( + x.messages.unwrap_or_default(), + ) + .ok() + .unwrap_or_default(); + messages.sort_by(|a, b| a.created.cmp(&b.created)); + messages + }, members: x.members.unwrap_or_default().into_iter().map(UserId).collect(), })) }) diff --git a/src/routes/v2/projects.rs b/src/routes/v2/projects.rs index d37c1a73a..d5c8383c9 100644 --- a/src/routes/v2/projects.rs +++ b/src/routes/v2/projects.rs @@ -645,7 +645,7 @@ pub async fn project_edit( old_status: project_item.inner.status, }, thread_id: thread, - show_in_mod_inbox: None, + show_in_mod_inbox: false, } .insert(&mut transaction) .await?; diff --git a/src/routes/v2/reports.rs b/src/routes/v2/reports.rs index fb0c11738..cc947a433 100644 --- a/src/routes/v2/reports.rs +++ b/src/routes/v2/reports.rs @@ -325,7 +325,7 @@ pub async fn report_edit( author_id: Some(user.id.into()), body: MessageBody::ThreadClosure, thread_id: thread, - show_in_mod_inbox: None, + show_in_mod_inbox: false, } .insert(&mut transaction) .await?; diff --git a/src/routes/v2/threads.rs b/src/routes/v2/threads.rs index 83fc7d63e..b712d1cc2 100644 --- a/src/routes/v2/threads.rs +++ b/src/routes/v2/threads.rs @@ -100,12 +100,11 @@ pub async fn thread_get( .into_iter() .map(|x| ThreadMessage { id: x.id.into(), - author_id: if thread_type == ThreadType::Report - && users - .iter() - .find(|y| x.author_id == Some(y.id.into())) - .map(|x| x.role.is_mod()) - .unwrap_or(false) + author_id: if users + .iter() + .find(|y| x.author_id == Some(y.id.into())) + .map(|x| x.role.is_mod()) + .unwrap_or(false) { None } else { @@ -152,6 +151,16 @@ pub async fn thread_send_message( return Ok(HttpResponse::NotFound().body("")); } + match &new_message.body { + MessageBody::Text { .. } => {} + _ => { + return Err(ApiError::InvalidInput( + "You may only send text messages through this route!" + .to_string(), + )) + } + } + let mod_notif = if thread.type_ == ThreadType::Project { let status = sqlx::query!( "SELECT m.status FROM mods m WHERE thread_id = $1", @@ -172,7 +181,7 @@ pub async fn thread_send_message( author_id: Some(user.id.into()), body: new_message.body.clone(), thread_id: thread.id, - show_in_mod_inbox: Some(mod_notif), + show_in_mod_inbox: mod_notif, } .insert(&mut transaction) .await?;