mirror of
https://github.com/modrinth/code.git
synced 2026-08-27 10:04:52 +00:00
feat(labrinth): project disclosures (#6945)
* feat(labrinth): project disclosures model * feat(labrinth): project disclosures database model * feat(labrinth): project disclosures get endpoint * feat(labrinth): censor user ids if set by moderator * feat(labrinth): wrap disclosures in struct * feat(labrinth): edit project disclosures endpoint * style(labrinth): cargo fmt * style(labrinth): fix typo * feat(labrinth): add fields for ai content disclosure * fix(labrinth): field typo * chore(labrinth): update query cache * feat(labrinth): index disclosures in search * refactor(labrinth): use enum instead of bools * feat(labrinth): trigger incremental index on disclosure change * feat(labrinth): change archived status to disclosure * feat(labrinth): use disclosure for archival status * fix(labrinth): error type for deserialization * fix(labrinth): migration timestamp * fix(labrinth): add disclosures to elasticsearch schema * fix(labrinth): don't remove archival disclosure when changing status via v3 api * feat(labrinth): derive str for ai usages and telemtry consent * feat(labrinth): include ai usages and telemetry consent in disclosure types * refactor(labrinth): move disclosure parsing to document creation * fix(labrinth): prevent removing moderator added archival disclosures * fix(labrinth): dedupe ai usages * fix(labrinth): auth logic on archived disclosure removal * feat(labrinth): add interactions field * style(labrinth): cargo fmt * feat(labrinth): soft delete disclosures * feat(labrinth): return soft-deleted disclosures * fix(labrinth): update disclosure on removal --------- Co-authored-by: Prospector <6166773+Prospector@users.noreply.github.com>
This commit is contained in:
@@ -30,6 +30,7 @@ pub mod payout_item;
|
||||
pub mod payouts_values_notifications;
|
||||
pub mod product_item;
|
||||
pub mod products_tax_identifier_item;
|
||||
pub mod project_disclosure_item;
|
||||
pub mod project_item;
|
||||
pub mod report_item;
|
||||
pub mod session_item;
|
||||
@@ -53,6 +54,7 @@ pub use image_item::DBImage;
|
||||
pub use oauth_client_item::DBOAuthClient;
|
||||
pub use organization_item::DBOrganization;
|
||||
pub use passkey_item::DBPasskey;
|
||||
pub use project_disclosure_item::DBProjectDisclosure;
|
||||
pub use project_item::DBProject;
|
||||
pub use team_item::DBTeam;
|
||||
pub use team_item::DBTeamMember;
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
database::models::{DBProjectId, DBUserId, DatabaseError},
|
||||
models::v3::disclosures::{ProjectDisclosure, ProjectDisclosureType},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct DBProjectDisclosure {
|
||||
pub project_id: DBProjectId,
|
||||
pub disclosure: ProjectDisclosure,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
pub updated_by: DBUserId,
|
||||
pub set_by_moderator: bool,
|
||||
pub deleted_at: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
impl DBProjectDisclosure {
|
||||
pub async fn upsert(
|
||||
&self,
|
||||
exec: impl crate::database::Executor<'_, Database = sqlx::Postgres>,
|
||||
) -> Result<(), DatabaseError> {
|
||||
let (disclosure_type, metadata) =
|
||||
self.disclosure.to_parts().map_err(|e| {
|
||||
DatabaseError::Internal(eyre::Report::new(e).wrap_err(
|
||||
"failed to serialize project disclosure metadata",
|
||||
))
|
||||
})?;
|
||||
|
||||
sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO project_disclosures (project_id, type, metadata, updated_by, set_by_moderator)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT (project_id, type) DO UPDATE SET
|
||||
metadata = $3,
|
||||
updated_at = now(),
|
||||
updated_by = $4,
|
||||
set_by_moderator = $5,
|
||||
deleted_at = NULL
|
||||
"#,
|
||||
self.project_id as DBProjectId,
|
||||
disclosure_type,
|
||||
metadata,
|
||||
self.updated_by as DBUserId,
|
||||
self.set_by_moderator,
|
||||
)
|
||||
.execute(exec)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_many_for_project(
|
||||
project_id: DBProjectId,
|
||||
include_deleted: bool,
|
||||
exec: impl crate::database::Executor<'_, Database = sqlx::Postgres>,
|
||||
) -> Result<Vec<DBProjectDisclosure>, DatabaseError> {
|
||||
let rows = sqlx::query!(
|
||||
r#"
|
||||
SELECT project_id, type AS "disclosure_type!", metadata, updated_at, updated_by, set_by_moderator, deleted_at
|
||||
FROM project_disclosures
|
||||
WHERE project_id = $1 AND ($2 OR deleted_at IS NULL)
|
||||
ORDER BY updated_at DESC
|
||||
"#,
|
||||
project_id as DBProjectId,
|
||||
include_deleted,
|
||||
)
|
||||
.fetch_all(exec)
|
||||
.await?;
|
||||
|
||||
rows.into_iter()
|
||||
.map(|row| {
|
||||
Ok(DBProjectDisclosure {
|
||||
project_id: DBProjectId(row.project_id),
|
||||
disclosure: ProjectDisclosure::from_parts(
|
||||
&row.disclosure_type,
|
||||
row.metadata,
|
||||
)
|
||||
.map_err(|e| {
|
||||
DatabaseError::Internal(eyre::Report::new(e).wrap_err(
|
||||
"failed to deserialize project disclosure metadata",
|
||||
))
|
||||
})?,
|
||||
updated_at: row.updated_at,
|
||||
updated_by: DBUserId(row.updated_by),
|
||||
set_by_moderator: row.set_by_moderator,
|
||||
deleted_at: row.deleted_at,
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Returns the subset of `project_ids` that carry a disclosure of the given type.
|
||||
pub async fn projects_with_type(
|
||||
disclosure_type: ProjectDisclosureType,
|
||||
project_ids: &[DBProjectId],
|
||||
exec: impl crate::database::Executor<'_, Database = sqlx::Postgres>,
|
||||
) -> Result<HashSet<DBProjectId>, DatabaseError> {
|
||||
let ids = project_ids.iter().map(|id| id.0).collect::<Vec<_>>();
|
||||
let rows = sqlx::query_scalar!(
|
||||
r#"
|
||||
SELECT project_id
|
||||
FROM project_disclosures
|
||||
WHERE type = $1 AND project_id = ANY($2) AND deleted_at IS NULL
|
||||
"#,
|
||||
<&'static str>::from(disclosure_type),
|
||||
&ids,
|
||||
)
|
||||
.fetch_all(exec)
|
||||
.await?;
|
||||
|
||||
Ok(rows.into_iter().map(DBProjectId).collect())
|
||||
}
|
||||
|
||||
pub async fn any_set_by_moderator(
|
||||
project_id: DBProjectId,
|
||||
types: &[String],
|
||||
exec: impl crate::database::Executor<'_, Database = sqlx::Postgres>,
|
||||
) -> Result<bool, DatabaseError> {
|
||||
let existing = sqlx::query_scalar!(
|
||||
r#"
|
||||
SELECT 1 FROM project_disclosures
|
||||
WHERE project_id = $1 AND type = ANY($2) AND set_by_moderator AND deleted_at IS NULL
|
||||
LIMIT 1
|
||||
"#,
|
||||
project_id as DBProjectId,
|
||||
types,
|
||||
)
|
||||
.fetch_optional(exec)
|
||||
.await?;
|
||||
|
||||
Ok(existing.is_some())
|
||||
}
|
||||
|
||||
pub async fn remove(
|
||||
project_id: DBProjectId,
|
||||
disclosure_type: ProjectDisclosureType,
|
||||
updated_by: DBUserId,
|
||||
set_by_moderator: bool,
|
||||
exec: impl crate::database::Executor<'_, Database = sqlx::Postgres>,
|
||||
) -> Result<bool, DatabaseError> {
|
||||
let result = sqlx::query!(
|
||||
r#"
|
||||
UPDATE project_disclosures
|
||||
SET deleted_at = now(), updated_at = now(), updated_by = $3, set_by_moderator = $4
|
||||
WHERE project_id = $1 AND type = $2 AND deleted_at IS NULL
|
||||
"#,
|
||||
project_id as DBProjectId,
|
||||
<&'static str>::from(disclosure_type),
|
||||
updated_by as DBUserId,
|
||||
set_by_moderator,
|
||||
)
|
||||
.execute(exec)
|
||||
.await?;
|
||||
|
||||
Ok(result.rows_affected() > 0)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user