mirror of
https://github.com/modrinth/code.git
synced 2026-08-31 12:05:53 +00:00
feat: add ability to report organizations
This commit is contained in:
@@ -0,0 +1,2 @@
|
|||||||
|
ALTER TABLE reports
|
||||||
|
ADD COLUMN organization_id bigint
|
||||||
@@ -9,6 +9,7 @@ pub struct DBReport {
|
|||||||
pub project_id: Option<DBProjectId>,
|
pub project_id: Option<DBProjectId>,
|
||||||
pub version_id: Option<DBVersionId>,
|
pub version_id: Option<DBVersionId>,
|
||||||
pub user_id: Option<DBUserId>,
|
pub user_id: Option<DBUserId>,
|
||||||
|
pub organization_id: Option<DBOrganizationId>,
|
||||||
pub shared_instance_id: Option<SharedInstanceId>,
|
pub shared_instance_id: Option<SharedInstanceId>,
|
||||||
pub shared_instance_version_id: Option<i32>,
|
pub shared_instance_version_id: Option<i32>,
|
||||||
pub body: String,
|
pub body: String,
|
||||||
@@ -23,6 +24,7 @@ pub struct ReportQueryResult {
|
|||||||
pub project_id: Option<DBProjectId>,
|
pub project_id: Option<DBProjectId>,
|
||||||
pub version_id: Option<DBVersionId>,
|
pub version_id: Option<DBVersionId>,
|
||||||
pub user_id: Option<DBUserId>,
|
pub user_id: Option<DBUserId>,
|
||||||
|
pub organization_id: Option<DBOrganizationId>,
|
||||||
pub shared_instance_id: Option<SharedInstanceId>,
|
pub shared_instance_id: Option<SharedInstanceId>,
|
||||||
pub shared_instance_version_id: Option<i32>,
|
pub shared_instance_version_id: Option<i32>,
|
||||||
pub body: String,
|
pub body: String,
|
||||||
@@ -40,12 +42,12 @@ impl DBReport {
|
|||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
"
|
"
|
||||||
INSERT INTO reports (
|
INSERT INTO reports (
|
||||||
id, report_type_id, mod_id, version_id, user_id,
|
id, report_type_id, mod_id, version_id, user_id, organization_id,
|
||||||
shared_instance_id, shared_instance_version_id, body, reporter
|
shared_instance_id, shared_instance_version_id, body, reporter
|
||||||
)
|
)
|
||||||
VALUES (
|
VALUES (
|
||||||
$1, $2, $3, $4, $5,
|
$1, $2, $3, $4, $5,
|
||||||
$6, $7, $8, $9
|
$6, $7, $8, $9, $10
|
||||||
)
|
)
|
||||||
",
|
",
|
||||||
self.id as DBReportId,
|
self.id as DBReportId,
|
||||||
@@ -53,6 +55,7 @@ impl DBReport {
|
|||||||
self.project_id.map(|x| x.0 as i64),
|
self.project_id.map(|x| x.0 as i64),
|
||||||
self.version_id.map(|x| x.0 as i64),
|
self.version_id.map(|x| x.0 as i64),
|
||||||
self.user_id.map(|x| x.0 as i64),
|
self.user_id.map(|x| x.0 as i64),
|
||||||
|
self.organization_id.map(|x| x.0 as i64),
|
||||||
self.shared_instance_id.map(|x| x.0 as i64),
|
self.shared_instance_id.map(|x| x.0 as i64),
|
||||||
self.shared_instance_version_id,
|
self.shared_instance_version_id,
|
||||||
self.body,
|
self.body,
|
||||||
@@ -89,7 +92,7 @@ impl DBReport {
|
|||||||
report_ids.iter().map(|x| x.0).collect();
|
report_ids.iter().map(|x| x.0).collect();
|
||||||
let reports = sqlx::query!(
|
let reports = sqlx::query!(
|
||||||
"
|
"
|
||||||
SELECT r.id, rt.name, r.mod_id, r.version_id, r.user_id, r.shared_instance_id, r.shared_instance_version_id, r.body, r.reporter, r.created, t.id thread_id, r.closed
|
SELECT r.id, rt.name, r.mod_id, r.version_id, r.user_id, r.organization_id, r.shared_instance_id, r.shared_instance_version_id, r.body, r.reporter, r.created, t.id thread_id, r.closed
|
||||||
FROM reports r
|
FROM reports r
|
||||||
INNER JOIN report_types rt ON rt.id = r.report_type_id
|
INNER JOIN report_types rt ON rt.id = r.report_type_id
|
||||||
INNER JOIN threads t ON t.report_id = r.id
|
INNER JOIN threads t ON t.report_id = r.id
|
||||||
@@ -105,6 +108,7 @@ impl DBReport {
|
|||||||
project_id: x.mod_id.map(DBProjectId),
|
project_id: x.mod_id.map(DBProjectId),
|
||||||
version_id: x.version_id.map(DBVersionId),
|
version_id: x.version_id.map(DBVersionId),
|
||||||
user_id: x.user_id.map(DBUserId),
|
user_id: x.user_id.map(DBUserId),
|
||||||
|
organization_id: x.organization_id.map(DBOrganizationId),
|
||||||
shared_instance_id: x.shared_instance_id.map(SharedInstanceId),
|
shared_instance_id: x.shared_instance_id.map(SharedInstanceId),
|
||||||
shared_instance_version_id: x.shared_instance_version_id,
|
shared_instance_version_id: x.shared_instance_version_id,
|
||||||
body: x.body,
|
body: x.body,
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
use crate::database::models::report_item::ReportQueryResult as DBReport;
|
use crate::database::models::report_item::ReportQueryResult as DBReport;
|
||||||
use crate::models::ids::{ProjectId, ReportId, ThreadId, VersionId};
|
use crate::models::ids::{
|
||||||
|
OrganizationId, ProjectId, ReportId, ThreadId, VersionId,
|
||||||
|
};
|
||||||
use ariadne::ids::UserId;
|
use ariadne::ids::UserId;
|
||||||
use ariadne::ids::base62_impl::to_base62;
|
use ariadne::ids::base62_impl::to_base62;
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
@@ -26,6 +28,7 @@ pub enum ItemType {
|
|||||||
Project,
|
Project,
|
||||||
Version,
|
Version,
|
||||||
User,
|
User,
|
||||||
|
Organization,
|
||||||
SharedInstance,
|
SharedInstance,
|
||||||
Unknown,
|
Unknown,
|
||||||
}
|
}
|
||||||
@@ -36,6 +39,7 @@ impl ItemType {
|
|||||||
ItemType::Project => "project",
|
ItemType::Project => "project",
|
||||||
ItemType::Version => "version",
|
ItemType::Version => "version",
|
||||||
ItemType::User => "user",
|
ItemType::User => "user",
|
||||||
|
ItemType::Organization => "organization",
|
||||||
ItemType::SharedInstance => "shared-instance",
|
ItemType::SharedInstance => "shared-instance",
|
||||||
ItemType::Unknown => "unknown",
|
ItemType::Unknown => "unknown",
|
||||||
}
|
}
|
||||||
@@ -57,6 +61,9 @@ impl From<DBReport> for Report {
|
|||||||
} else if let Some(user_id) = x.user_id {
|
} else if let Some(user_id) = x.user_id {
|
||||||
item_id = UserId::from(user_id).to_string();
|
item_id = UserId::from(user_id).to_string();
|
||||||
item_type = ItemType::User;
|
item_type = ItemType::User;
|
||||||
|
} else if let Some(organization_id) = x.organization_id {
|
||||||
|
item_id = OrganizationId::from(organization_id).to_string();
|
||||||
|
item_type = ItemType::Organization;
|
||||||
} else if let Some(shared_instance_id) = x.shared_instance_id {
|
} else if let Some(shared_instance_id) = x.shared_instance_id {
|
||||||
item_id = to_base62(shared_instance_id.0 as u64);
|
item_id = to_base62(shared_instance_id.0 as u64);
|
||||||
item_type = ItemType::SharedInstance;
|
item_type = ItemType::SharedInstance;
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use crate::database::models::thread_item::{
|
|||||||
ThreadBuilder, ThreadMessageBuilder,
|
ThreadBuilder, ThreadMessageBuilder,
|
||||||
};
|
};
|
||||||
use crate::env::ENV;
|
use crate::env::ENV;
|
||||||
use crate::models::ids::ImageId;
|
use crate::models::ids::{ImageId, OrganizationId};
|
||||||
use crate::models::ids::{ProjectId, VersionId};
|
use crate::models::ids::{ProjectId, VersionId};
|
||||||
use crate::models::images::{Image, ImageContext};
|
use crate::models::images::{Image, ImageContext};
|
||||||
use crate::models::notifications::NotificationBody;
|
use crate::models::notifications::NotificationBody;
|
||||||
@@ -113,6 +113,7 @@ pub async fn report_create(
|
|||||||
project_id: None,
|
project_id: None,
|
||||||
version_id: None,
|
version_id: None,
|
||||||
user_id: None,
|
user_id: None,
|
||||||
|
organization_id: None,
|
||||||
shared_instance_id: None,
|
shared_instance_id: None,
|
||||||
shared_instance_version_id: None,
|
shared_instance_version_id: None,
|
||||||
body: new_report.body.clone(),
|
body: new_report.body.clone(),
|
||||||
@@ -191,6 +192,29 @@ pub async fn report_create(
|
|||||||
|
|
||||||
report.user_id = Some(user_id.into())
|
report.user_id = Some(user_id.into())
|
||||||
}
|
}
|
||||||
|
ItemType::Organization => {
|
||||||
|
let organization_id = OrganizationId(
|
||||||
|
parse_base62(new_report.item_id.as_str())
|
||||||
|
.wrap_request_err("parsing reported organization ID")?,
|
||||||
|
);
|
||||||
|
|
||||||
|
let result = sqlx::query!(
|
||||||
|
"SELECT EXISTS(SELECT 1 FROM organizations WHERE id = $1)",
|
||||||
|
organization_id.0 as i64
|
||||||
|
)
|
||||||
|
.fetch_one(&mut transaction)
|
||||||
|
.await
|
||||||
|
.wrap_internal_err("querying database for `report_create`")?;
|
||||||
|
|
||||||
|
if !result.exists.unwrap_or(false) {
|
||||||
|
return Err(ApiError::Request(eyre::eyre!(format!(
|
||||||
|
"Organization could not be found: {}",
|
||||||
|
new_report.item_id
|
||||||
|
))));
|
||||||
|
}
|
||||||
|
|
||||||
|
report.organization_id = Some(organization_id.into())
|
||||||
|
}
|
||||||
ItemType::SharedInstance => {
|
ItemType::SharedInstance => {
|
||||||
// parsing
|
// parsing
|
||||||
let (instance_part, version_part) = new_report
|
let (instance_part, version_part) = new_report
|
||||||
|
|||||||
Reference in New Issue
Block a user