mirror of
https://github.com/modrinth/code.git
synced 2026-08-31 12:05:53 +00:00
* wip * ui edits * better logic for syncing tech review state in a single centralized place * never automatically finish a tech review report * fix css * fix clippy * prepr
156 lines
4.1 KiB
Rust
156 lines
4.1 KiB
Rust
use crate::database::models::delphi_report_item::DelphiVerdict;
|
|
use crate::models::ids::{
|
|
ImageId, ProjectId, ReportId, ThreadId, ThreadMessageId,
|
|
};
|
|
use crate::models::projects::ProjectStatus;
|
|
use crate::models::users::User;
|
|
use ariadne::ids::UserId;
|
|
use chrono::{DateTime, Utc};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema)]
|
|
pub struct Thread {
|
|
pub id: ThreadId,
|
|
#[serde(rename = "type")]
|
|
pub type_: ThreadType,
|
|
pub project_id: Option<ProjectId>,
|
|
pub report_id: Option<ReportId>,
|
|
pub messages: Vec<ThreadMessage>,
|
|
pub members: Vec<User>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema)]
|
|
pub struct ThreadMessage {
|
|
pub id: ThreadMessageId,
|
|
pub author_id: Option<UserId>,
|
|
pub body: MessageBody,
|
|
pub created: DateTime<Utc>,
|
|
pub hide_identity: bool,
|
|
}
|
|
|
|
#[derive(
|
|
Debug, Clone, Serialize, Deserialize, utoipa::ToSchema, strum::AsRefStr,
|
|
)]
|
|
#[serde(tag = "type", rename_all = "snake_case")]
|
|
#[strum(serialize_all = "snake_case")]
|
|
pub enum MessageBody {
|
|
Text {
|
|
body: String,
|
|
#[serde(default)]
|
|
private: bool,
|
|
replying_to: Option<ThreadMessageId>,
|
|
#[serde(default)]
|
|
associated_images: Vec<ImageId>,
|
|
},
|
|
StatusChange {
|
|
new_status: ProjectStatus,
|
|
old_status: ProjectStatus,
|
|
},
|
|
TechReview {
|
|
verdict: DelphiVerdict,
|
|
},
|
|
TechReviewEntered,
|
|
TechReviewExited,
|
|
TechReviewExitFileDeleted,
|
|
ThreadClosure,
|
|
ThreadReopen,
|
|
Deleted {
|
|
#[serde(default)]
|
|
private: bool,
|
|
},
|
|
}
|
|
|
|
impl MessageBody {
|
|
pub fn is_private(&self) -> bool {
|
|
match self {
|
|
Self::Text { private, .. } | Self::Deleted { private } => *private,
|
|
Self::TechReview { .. }
|
|
| Self::TechReviewEntered
|
|
| Self::TechReviewExited
|
|
| Self::TechReviewExitFileDeleted => true,
|
|
Self::StatusChange { .. }
|
|
| Self::ThreadClosure
|
|
| Self::ThreadReopen => false,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(
|
|
Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, utoipa::ToSchema,
|
|
)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum ThreadType {
|
|
Report,
|
|
Project,
|
|
DirectMessage,
|
|
}
|
|
|
|
impl std::fmt::Display for ThreadType {
|
|
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
write!(fmt, "{}", self.as_str())
|
|
}
|
|
}
|
|
|
|
impl ThreadType {
|
|
// These are constant, so this can remove unnecessary allocations (`to_string`)
|
|
pub fn as_str(&self) -> &'static str {
|
|
match self {
|
|
ThreadType::Report => "report",
|
|
ThreadType::Project => "project",
|
|
ThreadType::DirectMessage => "direct_message",
|
|
}
|
|
}
|
|
|
|
pub fn from_string(string: &str) -> ThreadType {
|
|
match string {
|
|
"report" => ThreadType::Report,
|
|
"project" => ThreadType::Project,
|
|
"direct_message" => ThreadType::DirectMessage,
|
|
_ => ThreadType::DirectMessage,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Thread {
|
|
pub fn from(
|
|
data: crate::database::models::DBThread,
|
|
users: Vec<User>,
|
|
user: &User,
|
|
) -> Self {
|
|
let thread_type = data.type_;
|
|
|
|
Thread {
|
|
id: data.id.into(),
|
|
type_: thread_type,
|
|
project_id: data.project_id.map(|x| x.into()),
|
|
report_id: data.report_id.map(|x| x.into()),
|
|
messages: data
|
|
.messages
|
|
.into_iter()
|
|
.filter(|x| user.role.is_mod() || !x.body.is_private())
|
|
.map(|x| ThreadMessage::from(x, user))
|
|
.collect(),
|
|
members: users,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl ThreadMessage {
|
|
pub fn from(
|
|
data: crate::database::models::DBThreadMessage,
|
|
user: &User,
|
|
) -> Self {
|
|
Self {
|
|
id: data.id.into(),
|
|
author_id: if data.hide_identity && !user.role.is_mod() {
|
|
None
|
|
} else {
|
|
data.author_id.map(|x| x.into())
|
|
},
|
|
body: data.body,
|
|
created: data.created,
|
|
hide_identity: data.hide_identity,
|
|
}
|
|
}
|
|
}
|