Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE mods
ADD COLUMN approved_because JSONB;
1 change: 1 addition & 0 deletions apps/labrinth/src/models/v2/threads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ impl From<crate::models::v3::threads::MessageBody> for LegacyMessageBody {
| crate::models::v3::threads::MessageBody::AutoApproval {
new_status,
old_status,
..
} => LegacyMessageBody::StatusChange {
new_status,
old_status,
Expand Down
28 changes: 27 additions & 1 deletion apps/labrinth/src/models/v3/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::database::models::version_item::VersionQueryResult;
use crate::models::exp;
use crate::models::ids::{
FileId, GalleryImageId, OrganizationId, ProjectId, TeamId, ThreadId,
VersionId,
ThreadIssueId, VersionId,
};
use crate::models::minecraft::legacy_game_version_name;
use crate::routes::{FileHash, HashAlgorithm};
Expand All @@ -17,6 +17,32 @@ use serde::{Deserialize, Serialize};
use url::Url;
use validator::Validate;

#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema)]
pub enum ApprovalReason {
ByUser(UserId),
Auto(ApprovalProvenance),
}

/// Evidence captured when a project is automatically approved.
#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema)]
pub struct ApprovalProvenance {
pub trigger: AutoApprovalCause,
pub previous_status: ProjectStatus,
pub resolved_issue_ids: Vec<ThreadIssueId>,
}

#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum AutoApprovalCause {
Resubmission { user_id: UserId },
TechnicalReviewNoDetails,
ScanCompleted { report_id: i64, scan_id: String },
RulesChanged { revision: i64, user_id: UserId },
ModeratorTechnicalReviewPass { user_id: UserId },
LocalVerdictsUpdated { user_id: UserId },
GlobalVerdictsUpdated { user_id: UserId },
}

/// A project returned from the API
#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema)]
pub struct Project {
Expand Down
4 changes: 3 additions & 1 deletion apps/labrinth/src/models/v3/threads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::models::ids::{
ImageId, ProjectId, ReportId, ThreadId, ThreadMessageId,
};
use crate::models::pats::Scopes;
use crate::models::projects::ProjectStatus;
use crate::models::projects::{ApprovalProvenance, ProjectStatus};
use crate::models::thread_issues::ThreadIssue;
use crate::models::users::User;
use ariadne::ids::UserId;
Expand Down Expand Up @@ -53,6 +53,8 @@ pub enum MessageBody {
AutoApproval {
new_status: ProjectStatus,
old_status: ProjectStatus,
#[serde(default, skip_serializing_if = "Option::is_none")]
provenance: Option<ApprovalProvenance>,
},
TechReview {
verdict: DelphiVerdict,
Expand Down
5 changes: 5 additions & 0 deletions apps/labrinth/src/routes/internal/delphi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::models::projects::AutoApprovalCause;
use crate::util::MinecraftMeta;
use crate::util::error::ApiContext as _;
use std::{collections::HashMap, fmt::Write, time::Instant};
Expand Down Expand Up @@ -328,6 +329,10 @@ async fn ingest_report_deserialized(
let prepared =
crate::routes::v3::projects::mutation::prepare_technical_review_exits(
&exited_projects,
AutoApprovalCause::ScanCompleted {
report_id: report_id.0,
scan_id: report.identifier.to_string(),
},
&mut transaction,
&pool,
&redis,
Expand Down
19 changes: 16 additions & 3 deletions apps/labrinth/src/routes/internal/moderation/tech_review.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use serde::{Deserialize, Serialize};
use super::ownership::get_projects_ownership;
use crate::database::models::{DBOrganization, DBOrganizationId};
use crate::models::ids::OrganizationId;
use crate::models::projects::AutoApprovalCause;
use crate::routes::v3::organizations::OrganizationIds;
use crate::routes::v3::users::UserIds;
use crate::{
Expand Down Expand Up @@ -1323,7 +1324,13 @@ pub async fn submit_report(

if prepared.is_none() && removed.rows_affected() > 0 {
prepared = Some(crate::routes::v3::projects::mutation::prepare_technical_review_exits(
&[project_id], &mut txn, &pool, &redis,
&[project_id],
AutoApprovalCause::ModeratorTechnicalReviewPass {
user_id: user.user.id,
},
&mut txn,
&pool,
&redis,
).await?);
}

Expand Down Expand Up @@ -1384,7 +1391,7 @@ pub async fn update_issue_details(
session_queue: web::Data<AuthQueue>,
update_reqs: web::Json<Vec<UpdateIssue>>,
) -> Result<(), ApiError> {
get_user_from_headers(
let user = get_user_from_headers(
&req,
&**pool,
&redis,
Expand Down Expand Up @@ -1519,6 +1526,9 @@ pub async fn update_issue_details(
let prepared =
crate::routes::v3::projects::mutation::prepare_technical_review_exits(
&exited_projects,
AutoApprovalCause::LocalVerdictsUpdated {
user_id: user.user.id,
},
&mut txn,
&pool,
&redis,
Expand Down Expand Up @@ -1547,7 +1557,7 @@ pub async fn update_global_issue_details(
session_queue: web::Data<AuthQueue>,
update_reqs: web::Json<Vec<UpdateGlobalIssue>>,
) -> Result<(), ApiError> {
get_user_from_headers(
let user = get_user_from_headers(
&req,
&**pool,
&redis,
Expand Down Expand Up @@ -1660,6 +1670,9 @@ pub async fn update_global_issue_details(
let prepared =
crate::routes::v3::projects::mutation::prepare_technical_review_exits(
&exited_projects,
AutoApprovalCause::GlobalVerdictsUpdated {
user_id: user.user.id,
},
&mut txn,
&pool,
&redis,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use utoipa::{PartialSchema, ToSchema};
use xredis::RedisPool;

use super::rules::{DelphiRuleEffect, DelphiRuleOutput};
use crate::models::projects::AutoApprovalCause;
use crate::routes::internal::delphi::tech_review_queue::{
self, TechReviewRemovalReason,
};
Expand Down Expand Up @@ -353,7 +354,7 @@ pub async fn scan_rules(
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
) -> Result<HttpResponse, ApiError> {
get_user_from_headers(
let user = get_user_from_headers(
&req,
&**pool,
&redis,
Expand Down Expand Up @@ -400,8 +401,9 @@ pub async fn scan_rules(
let (sender, receiver) = mpsc::unbounded_channel();
let _ = sender.send(Bytes::from_static(b": connected\n\n"));

let user_id = user.user.id;
actix_web::rt::spawn(async move {
match run_scan(transaction, &sender, &pool, &redis).await {
match run_scan(transaction, &sender, &pool, &redis, user_id).await {
Ok(summary) => {
let event = RuleScanEvent {
phase: "complete",
Expand Down Expand Up @@ -463,6 +465,7 @@ async fn run_scan(
sender: &mpsc::UnboundedSender<Bytes>,
pool: &PgPool,
redis: &RedisPool,
user_id: ariadne::ids::UserId,
) -> Result<ScanSummary> {
let project_ids = sqlx::query!(
r#"
Expand Down Expand Up @@ -797,6 +800,7 @@ async fn run_scan(
let prepared =
crate::routes::v3::projects::mutation::prepare_technical_review_exits(
&exited_projects,
AutoApprovalCause::RulesChanged { revision, user_id },
&mut transaction,
pool,
redis,
Expand Down
26 changes: 23 additions & 3 deletions apps/labrinth/src/routes/v3/projects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ use crate::models::link_platform::LinkPlatform;
use crate::models::notifications::NotificationBody;
use crate::models::pats::Scopes;
use crate::models::projects::{
MonetizationStatus, Project, ProjectStatus, SideTypesMigrationReviewStatus,
ApprovalReason, AutoApprovalCause, MonetizationStatus, Project,
ProjectStatus, SideTypesMigrationReviewStatus,
};
use crate::models::teams::{DEFAULT_ROLE, ProjectPermissions};
use crate::models::threads::MessageBody;
Expand Down Expand Up @@ -731,13 +732,24 @@ pub async fn project_edit_internal(

if status.is_approved() && !project_item.inner.status.is_approved()
{
let reason =
serde_json::to_value(ApprovalReason::ByUser(user.user.id))
.wrap_internal_err(
"serializing project approval reason",
)?;
sqlx::query!(
"
UPDATE mods
SET approved = COALESCE(approved, NOW()), initial_queued = NULL
SET approved_because = CASE
WHEN approved IS NULL THEN $2::jsonb
ELSE approved_because
END,
approved = COALESCE(approved, NOW()),
initial_queued = NULL
WHERE id = $1
",
id as db_ids::DBProjectId,
reason,
)
.execute(&mut transaction)
.await
Expand Down Expand Up @@ -1614,7 +1626,15 @@ async fn submit_project_for_review(
state.status(),
ProjectStatus::Rejected | ProjectStatus::Withheld
)
&& state.try_auto_approve(transaction, redis).await?
&& state
.try_auto_approve(
AutoApprovalCause::Resubmission {
user_id: user.user.id,
},
transaction,
redis,
)
.await?
{
return Ok(true);
}
Expand Down
Loading
Loading