Miscellaneous improvements and removals (#502)

This commit is contained in:
triphora
2022-12-23 15:19:15 -05:00
committed by GitHub
parent 16d5a70c08
commit 983e2df065
17 changed files with 1164 additions and 1792 deletions

64
Cargo.lock generated
View File

@@ -69,7 +69,7 @@ dependencies = [
"actix-service",
"actix-utils",
"ahash",
"base64",
"base64 0.13.1",
"bitflags",
"brotli",
"bytes",
@@ -443,6 +443,12 @@ version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
[[package]]
name = "base64"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ea22880d78093b0cbe17c89f64a7d457941e65759157ec6cb31a31d652b05e5"
[[package]]
name = "base64ct"
version = "1.5.3"
@@ -574,9 +580,9 @@ dependencies = [
[[package]]
name = "censor"
version = "0.2.0"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5563d2728feef9a6186acdd148bccbe850dad63c5ba55a3b3355abc9137cb3eb"
checksum = "d41e3b9fdbb9b3edc10dc66a06dc255822f699c432e19403fb966e6d60e0dec4"
dependencies = [
"once_cell",
]
@@ -976,9 +982,9 @@ dependencies = [
[[package]]
name = "env_logger"
version = "0.9.1"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c90bf5f19754d10198ccb95b70664fc925bd1fc090a0fd9a6ebc54acc8cd6272"
checksum = "a12e6657c4c97ebab115a42dcee77225f7f482cdd841cf7088c657a42e9e00e7"
dependencies = [
"atty",
"humantime",
@@ -1538,7 +1544,7 @@ dependencies = [
"actix-rt",
"actix-web",
"async-trait",
"base64",
"base64 0.20.0",
"bitflags",
"bytes",
"censor",
@@ -2278,7 +2284,7 @@ version = "0.11.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "431949c384f4e2ae07605ccaa56d1d9d2ecdb5cadd4f9577ccfab29f2e5149fc"
dependencies = [
"base64",
"base64 0.13.1",
"bytes",
"encoding_rs",
"futures-core",
@@ -2345,7 +2351,7 @@ dependencies = [
"async-trait",
"aws-creds",
"aws-region",
"base64",
"base64 0.13.1",
"cfg-if",
"hex",
"hmac 0.12.1",
@@ -2411,7 +2417,7 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0864aeff53f8c05aa08d86e5ef839d3dfcf07aeba2db32f12db0ef716e87bd55"
dependencies = [
"base64",
"base64 0.13.1",
]
[[package]]
@@ -2502,9 +2508,9 @@ checksum = "e25dfac463d778e353db5be2449d1cce89bd6fd23c9f1ea21310ce6e5a1b29c4"
[[package]]
name = "sentry"
version = "0.28.0"
version = "0.29.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a120fb5e8b7975736bf1fc57de380531e617a6a8f5a55d037bcea25a7f5e8371"
checksum = "17ad137b9df78294b98cab1a650bef237cc6c950e82e5ce164655e674d07c5cc"
dependencies = [
"httpdate",
"native-tls",
@@ -2519,9 +2525,9 @@ dependencies = [
[[package]]
name = "sentry-actix"
version = "0.28.0"
version = "0.29.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b889eb376e04a7f3b61aee3ed158c90dac12671042e07b6f54452be099ba5db7"
checksum = "8c832f99bbda819c89ce700fba806ff4def97d6bdff59d15b5c898bfaf54a956"
dependencies = [
"actix-web",
"futures-util",
@@ -2530,9 +2536,9 @@ dependencies = [
[[package]]
name = "sentry-backtrace"
version = "0.28.0"
version = "0.29.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ac56ff9aae25b024a5aad4f0242808dfde29161c82d183adce778338c6822ef"
checksum = "afe4800806552aab314129761d5d3b3d422284eca3de2ab59e9fd133636cbd3d"
dependencies = [
"backtrace",
"once_cell",
@@ -2542,9 +2548,9 @@ dependencies = [
[[package]]
name = "sentry-contexts"
version = "0.28.0"
version = "0.29.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "188506b08b5e64004c71b7a5edb34959083e6e1288fada3b8d18d0bc7449ce1e"
checksum = "a42938426670f6e7974989cd1417837a96dd8bbb01567094f567d6acb360bf88"
dependencies = [
"hostname",
"libc",
@@ -2556,9 +2562,9 @@ dependencies = [
[[package]]
name = "sentry-core"
version = "0.28.0"
version = "0.29.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff58433a7ad557b586a09c42c4298d5f3ddb0c777e1a79d950e510d7b93fce0e"
checksum = "4df9b9d8de2658a1ecd4e45f7b06c80c5dd97b891bfbc7c501186189b7e9bbdf"
dependencies = [
"once_cell",
"rand",
@@ -2569,9 +2575,9 @@ dependencies = [
[[package]]
name = "sentry-panic"
version = "0.28.0"
version = "0.29.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4145005d9b5c117132765c34e2cb33e9d24d16e73d7f3a357122b77fe3a3b815"
checksum = "0af37b8500f273e511ebd6eb0d342ff7937d64ce3f134764b2b4653112d48cb4"
dependencies = [
"sentry-backtrace",
"sentry-core",
@@ -2579,11 +2585,10 @@ dependencies = [
[[package]]
name = "sentry-types"
version = "0.28.0"
version = "0.29.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb30d75498a041005a774ec1b6b7d9589c5906d17ebaca338cb685dc92170f9b"
checksum = "ccc95faa4078768a6bf8df45e2b894bbf372b3dbbfb364e9429c1c58ab7545c6"
dependencies = [
"chrono",
"debugid",
"getrandom",
"hex",
@@ -2656,7 +2661,7 @@ version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "368f2d60d049ea019a84dcd6687b0d1e0030fe663ae105039bdf967ed5e6a9a7"
dependencies = [
"base64",
"base64 0.13.1",
"chrono",
"hex",
"indexmap",
@@ -2826,7 +2831,7 @@ checksum = "dcbc16ddba161afc99e14d1713a453747a2b07fc097d2009f4c300ec99286105"
dependencies = [
"ahash",
"atoi",
"base64",
"base64 0.13.1",
"bitflags",
"byteorder",
"bytes",
@@ -3255,7 +3260,7 @@ version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b97acb4c28a254fd7a4aeec976c46a7fa404eac4d7c134b30c75144846d7cb8f"
dependencies = [
"base64",
"base64 0.13.1",
"chunked_transfer",
"log",
"native-tls",
@@ -3667,8 +3672,9 @@ dependencies = [
[[package]]
name = "zip"
version = "0.6.2"
source = "git+https://github.com/zip-rs/zip?rev=bb230ef56adc13436d1fcdfaa489249d119c498f#bb230ef56adc13436d1fcdfaa489249d119c498f"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "537ce7411d25e54e8ae21a7ce0b15840e7bfcff15b51d697ec3266cc76bdf080"
dependencies = [
"aes",
"byteorder",

View File

@@ -5,12 +5,11 @@ authors = ["geometrically <jai@modrinth.com>"]
edition = "2018"
license = "AGPL-3.0"
# This seems redundant, but it's necessary for Docker to work
[[bin]]
name = "labrinth"
path = "src/main.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix = "0.13.0"
actix-web = "4.2.1"
@@ -41,7 +40,7 @@ xml-rs = "0.8.4"
rand = "0.8.5"
bytes = "1.2.1"
base64 = "0.13.0"
base64 = "0.20.0"
sha1 = { version = "0.6.1", features = ["std"] }
sha2 = "0.9.9"
hmac = "0.11.0"
@@ -51,14 +50,13 @@ hex = "0.4.3"
url = "2.3.1"
urlencoding = "2.1.2"
# Temporary - to fix zstd conflict
zip = { git = "https://github.com/zip-rs/zip", rev = "bb230ef56adc13436d1fcdfaa489249d119c498f" }
zip = "0.6.3"
itertools = "0.10.5"
validator = { version = "0.16.0", features = ["derive", "phone"] }
regex = "1.6.0"
censor = "0.2.0"
censor = "0.3.0"
spdx = { version = "0.9.0", features = ["text"] }
dotenvy = "0.15.6"
@@ -69,5 +67,5 @@ thiserror = "1.0.37"
sqlx = { version = "0.6.2", features = ["runtime-actix-rustls", "postgres", "chrono", "offline", "macros", "migrate", "decimal", "json"] }
rust_decimal = { version = "1.26", features = ["serde-with-float", "serde-with-str"] }
sentry = "0.28.0"
sentry-actix = "0.28.0"
sentry = "0.29.1"
sentry-actix = "0.29.1"

View File

@@ -20,17 +20,6 @@ services:
- meilisearch-data:/data.ms
environment:
MEILI_MASTER_KEY: modrinth
pgadmin:
image: dpage/pgadmin4:latest
environment:
PGADMIN_DEFAULT_EMAIL: admin@modrinth.com
PGADMIN_DEFAULT_PASSWORD: secret
PGADMIN_CONFIG_SERVER_MODE: "False"
PGADMIN_CONFIG_MASTER_PASSWORD_REQUIRED: "False"
ports:
- "8070:80"
volumes:
- ./pgadmin_default_servers.json:/pgadmin4/servers.json
volumes:
meilisearch-data:
db-data:

View File

@@ -0,0 +1,2 @@
ALTER TABLE mods DROP COLUMN body_url;
ALTER TABLE versions DROP COLUMN changelog_url;

View File

@@ -1,18 +0,0 @@
{
"Servers": {
"1": {
"Name": "Labrinth",
"Group": "Servers",
"Host": "postgres_db",
"Port": 5432,
"MaintenanceDB": "postgres",
"Username": "labrinth",
"SSLMode": "prefer",
"SSLCompression": 0,
"Timeout": 10,
"UseSSHTunnel": 0,
"TunnelPort": "22",
"TunnelAuthentication": 0
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -274,7 +274,7 @@ impl Project {
let result = sqlx::query!(
"
SELECT project_type, title, description, downloads, follows,
icon_url, body, body_url, published,
icon_url, body, published,
updated, approved, status, requested_status,
issues_url, source_url, wiki_url, discord_url, license_url,
team_id, client_side, server_side, license, slug,
@@ -296,7 +296,7 @@ impl Project {
title: row.title,
description: row.description,
downloads: row.downloads,
body_url: row.body_url,
body_url: None,
icon_url: row.icon_url,
published: row.published,
updated: row.updated,
@@ -341,7 +341,7 @@ impl Project {
let projects = sqlx::query!(
"
SELECT id, project_type, title, description, downloads, follows,
icon_url, body, body_url, published,
icon_url, body, published,
updated, approved, status, requested_status,
issues_url, source_url, wiki_url, discord_url, license_url,
team_id, client_side, server_side, license, slug,
@@ -361,7 +361,7 @@ impl Project {
title: m.title,
description: m.description,
downloads: m.downloads,
body_url: m.body_url,
body_url: None,
icon_url: m.icon_url,
published: m.published,
updated: m.updated,
@@ -662,7 +662,7 @@ impl Project {
let result = sqlx::query!(
"
SELECT m.id id, m.project_type project_type, m.title title, m.description description, m.downloads downloads, m.follows follows,
m.icon_url icon_url, m.body body, m.body_url body_url, m.published published,
m.icon_url icon_url, m.body body, m.published published,
m.updated updated, m.approved approved, m.status status, m.requested_status requested_status,
m.issues_url issues_url, m.source_url source_url, m.wiki_url wiki_url, m.discord_url discord_url, m.license_url license_url,
m.team_id team_id, m.client_side client_side, m.server_side server_side, m.license license, m.slug slug, m.moderation_message moderation_message, m.moderation_message_body moderation_message_body,
@@ -700,7 +700,7 @@ impl Project {
title: m.title.clone(),
description: m.description.clone(),
downloads: m.downloads,
body_url: m.body_url.clone(),
body_url: None,
icon_url: m.icon_url.clone(),
published: m.published,
updated: m.updated,
@@ -790,7 +790,7 @@ impl Project {
sqlx::query!(
"
SELECT m.id id, m.project_type project_type, m.title title, m.description description, m.downloads downloads, m.follows follows,
m.icon_url icon_url, m.body body, m.body_url body_url, m.published published,
m.icon_url icon_url, m.body body, m.published published,
m.updated updated, m.approved approved, m.status status, m.requested_status requested_status,
m.issues_url issues_url, m.source_url source_url, m.wiki_url wiki_url, m.discord_url discord_url, m.license_url license_url,
m.team_id team_id, m.client_side client_side, m.server_side server_side, m.license license, m.slug slug, m.moderation_message moderation_message, m.moderation_message_body moderation_message_body,
@@ -829,7 +829,7 @@ impl Project {
title: m.title.clone(),
description: m.description.clone(),
downloads: m.downloads,
body_url: m.body_url.clone(),
body_url: None,
icon_url: m.icon_url.clone(),
published: m.published,
updated: m.updated,

View File

@@ -268,14 +268,13 @@ impl Version {
"
INSERT INTO versions (
id, mod_id, author_id, name, version_number,
changelog, changelog_url, date_published,
downloads, version_type, featured, status
changelog, date_published, downloads,
version_type, featured, status
)
VALUES (
$1, $2, $3, $4, $5,
$6, $7,
$8, $9,
$10, $11, $12
$6, $7, $8,
$9, $10, $11
)
",
self.id as VersionId,
@@ -284,7 +283,6 @@ impl Version {
&self.name,
&self.version_number,
self.changelog,
self.changelog_url.as_ref(),
self.date_published,
self.downloads,
&self.version_type,
@@ -508,7 +506,7 @@ impl Version {
let result = sqlx::query!(
"
SELECT v.mod_id, v.author_id, v.name, v.version_number,
v.changelog, v.changelog_url, v.date_published, v.downloads,
v.changelog, v.date_published, v.downloads,
v.version_type, v.featured, v.status, v.requested_status
FROM versions v
WHERE v.id = $1
@@ -526,7 +524,7 @@ impl Version {
name: row.name,
version_number: row.version_number,
changelog: row.changelog,
changelog_url: row.changelog_url,
changelog_url: None,
date_published: row.date_published,
downloads: row.downloads,
version_type: row.version_type,
@@ -555,7 +553,7 @@ impl Version {
let versions = sqlx::query!(
"
SELECT v.id, v.mod_id, v.author_id, v.name, v.version_number,
v.changelog, v.changelog_url, v.date_published, v.downloads,
v.changelog, v.date_published, v.downloads,
v.version_type, v.featured, v.status, v.requested_status
FROM versions v
WHERE v.id = ANY($1)
@@ -572,7 +570,7 @@ impl Version {
name: v.name,
version_number: v.version_number,
changelog: v.changelog,
changelog_url: v.changelog_url,
changelog_url: None,
date_published: v.date_published,
downloads: v.downloads,
featured: v.featured,
@@ -599,7 +597,7 @@ impl Version {
let result = sqlx::query!(
"
SELECT v.id id, v.mod_id mod_id, v.author_id author_id, v.name version_name, v.version_number version_number,
v.changelog changelog, v.changelog_url changelog_url, v.date_published date_published, v.downloads downloads,
v.changelog changelog, v.date_published date_published, v.downloads downloads,
v.version_type version_type, v.featured featured, v.status status, v.requested_status requested_status,
JSONB_AGG(DISTINCT jsonb_build_object('version', gv.version, 'created', gv.created)) filter (where gv.version is not null) game_versions,
ARRAY_AGG(DISTINCT l.loader) filter (where l.loader is not null) loaders,
@@ -631,7 +629,7 @@ impl Version {
name: v.version_name,
version_number: v.version_number,
changelog: v.changelog,
changelog_url: v.changelog_url,
changelog_url: None,
date_published: v.date_published,
downloads: v.downloads,
version_type: v.version_type,
@@ -749,7 +747,7 @@ impl Version {
sqlx::query!(
"
SELECT v.id id, v.mod_id mod_id, v.author_id author_id, v.name version_name, v.version_number version_number,
v.changelog changelog, v.changelog_url changelog_url, v.date_published date_published, v.downloads downloads,
v.changelog changelog, v.date_published date_published, v.downloads downloads,
v.version_type version_type, v.featured featured, v.status status, v.requested_status requested_status,
JSONB_AGG(DISTINCT jsonb_build_object('version', gv.version, 'created', gv.created)) filter (where gv.version is not null) game_versions,
ARRAY_AGG(DISTINCT l.loader) filter (where l.loader is not null) loaders,
@@ -781,7 +779,7 @@ impl Version {
name: v.version_name,
version_number: v.version_number,
changelog: v.changelog,
changelog_url: v.changelog_url,
changelog_url: None,
date_published: v.date_published,
downloads: v.downloads,
version_type: v.version_type,

View File

@@ -36,7 +36,7 @@ pub struct Project {
pub description: String,
/// A long form description of the project.
pub body: String,
/// The link to the long description of the project. (Deprecated), being replaced by `body`
/// The link to the long description of the project. Deprecated, always None
pub body_url: Option<String>,
/// The date at which the project was first published.
@@ -110,7 +110,7 @@ impl From<QueryProject> for Project {
title: m.title,
description: m.description,
body: m.body,
body_url: m.body_url,
body_url: None,
published: m.published,
updated: m.updated,
approved: m.approved,
@@ -402,7 +402,7 @@ pub struct Version {
pub version_number: String,
/// The changelog for this version of the project.
pub changelog: String,
/// A link to the changelog for this version of the project. (Deprecated), being replaced by `changelog`
/// A link to the changelog for this version of the project. Deprecated, always None
pub changelog_url: Option<String>,
/// The date that this version was published.
@@ -439,7 +439,7 @@ impl From<QueryVersion> for Version {
name: v.name,
version_number: v.version_number,
changelog: v.changelog,
changelog_url: v.changelog_url,
changelog_url: None,
date_published: v.date_published,
downloads: v.downloads as u32,
version_type: match v.version_type.as_str() {

View File

@@ -23,6 +23,8 @@ pub struct Report {
#[serde(rename_all = "kebab-case")]
pub enum ItemType {
Project,
// TODO remove when API v1 POST routes are removed
Mod,
Version,
User,
Unknown,
@@ -32,6 +34,7 @@ impl ItemType {
pub fn as_str(&self) -> &'static str {
match self {
ItemType::Project => "project",
ItemType::Mod => "mod",
ItemType::Version => "version",
ItemType::User => "user",
ItemType::Unknown => "unknown",

View File

@@ -64,7 +64,7 @@ pub async fn report_create(
};
match new_report.item_type {
ItemType::Project => {
ItemType::Project | ItemType::Mod => {
report.project_id = Some(
serde_json::from_str::<ProjectId>(&format!(
"\"{}\"",

View File

@@ -1,8 +1,6 @@
use actix_web::web;
mod moderation;
mod mods;
mod reports;
mod tags;
mod teams;
mod users;
@@ -11,13 +9,11 @@ mod versions;
pub fn v1_config(cfg: &mut web::ServiceConfig) {
cfg.service(
web::scope("api/v1")
.configure(super::auth_config)
.configure(tags_config)
.configure(mods_config)
.configure(versions_config)
.configure(teams_config)
.configure(users_config)
.configure(moderation_config)
.configure(reports_config)
.configure(notifications_config),
);
@@ -27,20 +23,9 @@ pub fn tags_config(cfg: &mut web::ServiceConfig) {
cfg.service(
web::scope("tag")
.service(tags::category_list)
.service(tags::category_create)
.service(super::tags::category_delete)
.service(tags::loader_list)
.service(tags::loader_create)
.service(super::tags::loader_delete)
.service(tags::game_version_list)
.service(super::tags::game_version_create)
.service(super::tags::game_version_delete)
.service(super::tags::license_list)
.service(super::tags::donation_platform_create)
.service(super::tags::donation_platform_list)
.service(super::tags::donation_platform_delete)
.service(super::tags::report_type_create)
.service(super::tags::report_type_delete)
.service(super::tags::report_type_list),
);
}
@@ -74,9 +59,9 @@ pub fn versions_config(cfg: &mut web::ServiceConfig) {
);
cfg.service(
web::scope("version_file")
.service(versions::delete_file)
.service(versions::get_version_from_hash)
.service(versions::download_version),
.service(super::version_file::delete_file)
.service(super::version_file::get_version_from_hash)
.service(super::version_file::download_version),
);
}
@@ -117,12 +102,6 @@ pub fn notifications_config(cfg: &mut web::ServiceConfig) {
);
}
pub fn moderation_config(cfg: &mut web::ServiceConfig) {
cfg.service(web::scope("moderation").service(moderation::get_mods));
}
pub fn reports_config(cfg: &mut web::ServiceConfig) {
cfg.service(reports::reports);
cfg.service(reports::report_create);
cfg.service(super::reports::delete_report);
cfg.service(super::reports::report_create);
}

View File

@@ -1,45 +0,0 @@
use crate::database;
use crate::models::projects::{Project, ProjectStatus};
use crate::routes::moderation::ResultCount;
use crate::routes::ApiError;
use crate::util::auth::check_is_moderator_from_headers;
use actix_web::web;
use actix_web::{get, HttpRequest, HttpResponse};
use sqlx::PgPool;
#[get("mods")]
pub async fn get_mods(
req: HttpRequest,
pool: web::Data<PgPool>,
count: web::Query<ResultCount>,
) -> Result<HttpResponse, ApiError> {
check_is_moderator_from_headers(req.headers(), &**pool).await?;
use futures::stream::TryStreamExt;
let project_ids = sqlx::query!(
"
SELECT id FROM mods
WHERE status = $1
ORDER BY updated ASC
LIMIT $2;
",
ProjectStatus::Processing.as_str(),
count.count as i64
)
.fetch_many(&**pool)
.try_filter_map(|e| async {
Ok(e.right().map(|m| database::models::ProjectId(m.id)))
})
.try_collect::<Vec<database::models::ProjectId>>()
.await?;
let projects: Vec<_> =
database::Project::get_many_full(project_ids, &**pool)
.await?
.into_iter()
.map(Project::from)
.collect();
Ok(HttpResponse::Ok().json(projects))
}

View File

@@ -1,218 +0,0 @@
use crate::models::ids::ReportId;
use crate::models::projects::{ProjectId, VersionId};
use crate::models::users::UserId;
use crate::routes::ApiError;
use crate::util::auth::{
check_is_moderator_from_headers, get_user_from_headers,
};
use actix_web::web;
use actix_web::{get, post, HttpRequest, HttpResponse};
use chrono::{DateTime, Utc};
use futures::StreamExt;
use serde::{Deserialize, Serialize};
use sqlx::PgPool;
#[derive(Serialize, Deserialize)]
pub struct Report {
pub id: ReportId,
pub report_type: String,
pub item_id: String,
pub item_type: ItemType,
pub reporter: UserId,
pub body: String,
pub created: DateTime<Utc>,
}
#[derive(Serialize, Deserialize, Clone)]
#[serde(rename_all = "kebab-case")]
pub enum ItemType {
Mod,
Version,
User,
Unknown,
}
impl ItemType {
pub fn as_str(&self) -> &'static str {
match self {
ItemType::Mod => "mod",
ItemType::Version => "version",
ItemType::User => "user",
ItemType::Unknown => "unknown",
}
}
}
#[derive(Deserialize)]
pub struct CreateReport {
pub report_type: String,
pub item_id: String,
pub item_type: ItemType,
pub body: String,
}
#[post("report")]
pub async fn report_create(
req: HttpRequest,
pool: web::Data<PgPool>,
mut body: web::Payload,
) -> Result<HttpResponse, ApiError> {
let mut transaction = pool.begin().await?;
let current_user =
get_user_from_headers(req.headers(), &mut *transaction).await?;
let mut bytes = web::BytesMut::new();
while let Some(item) = body.next().await {
bytes.extend_from_slice(&item.map_err(|_| {
ApiError::InvalidInput(
"Error while parsing request payload!".to_string(),
)
})?);
}
let new_report: CreateReport = serde_json::from_slice(bytes.as_ref())?;
let id =
crate::database::models::generate_report_id(&mut transaction).await?;
let report_type = crate::database::models::categories::ReportType::get_id(
&new_report.report_type,
&mut *transaction,
)
.await?
.ok_or_else(|| {
ApiError::InvalidInput(format!(
"Invalid report type: {}",
new_report.report_type
))
})?;
let mut report = crate::database::models::report_item::Report {
id,
report_type_id: report_type,
project_id: None,
version_id: None,
user_id: None,
body: new_report.body.clone(),
reporter: current_user.id.into(),
created: Utc::now(),
};
match new_report.item_type {
ItemType::Mod => {
report.project_id = Some(
serde_json::from_str::<ProjectId>(&format!(
"\"{}\"",
new_report.item_id
))?
.into(),
)
}
ItemType::Version => {
report.version_id = Some(
serde_json::from_str::<VersionId>(&format!(
"\"{}\"",
new_report.item_id
))?
.into(),
)
}
ItemType::User => {
report.user_id = Some(
serde_json::from_str::<UserId>(&format!(
"\"{}\"",
new_report.item_id
))?
.into(),
)
}
ItemType::Unknown => {
return Err(ApiError::InvalidInput(format!(
"Invalid report item type: {}",
new_report.item_type.as_str()
)))
}
}
report.insert(&mut transaction).await?;
transaction.commit().await?;
Ok(HttpResponse::Ok().json(Report {
id: id.into(),
report_type: new_report.report_type.clone(),
item_id: new_report.item_id.clone(),
item_type: new_report.item_type.clone(),
reporter: current_user.id,
body: new_report.body.clone(),
created: Utc::now(),
}))
}
#[derive(Deserialize)]
pub struct ResultCount {
#[serde(default = "default_count")]
count: i16,
}
fn default_count() -> i16 {
100
}
#[get("report")]
pub async fn reports(
req: HttpRequest,
pool: web::Data<PgPool>,
count: web::Query<ResultCount>,
) -> Result<HttpResponse, ApiError> {
check_is_moderator_from_headers(req.headers(), &**pool).await?;
use futures::stream::TryStreamExt;
let report_ids = sqlx::query!(
"
SELECT id FROM reports
ORDER BY created ASC
LIMIT $1;
",
count.count as i64
)
.fetch_many(&**pool)
.try_filter_map(|e| async {
Ok(e.right()
.map(|m| crate::database::models::ids::ReportId(m.id)))
})
.try_collect::<Vec<crate::database::models::ids::ReportId>>()
.await?;
let query_reports = crate::database::models::report_item::Report::get_many(
report_ids, &**pool,
)
.await?;
let mut reports = Vec::new();
for x in query_reports {
let mut item_id = "".to_string();
let mut item_type = ItemType::Unknown;
if let Some(project_id) = x.project_id {
item_id = serde_json::to_string::<ProjectId>(&project_id.into())?;
item_type = ItemType::Mod;
} else if let Some(version_id) = x.version_id {
item_id = serde_json::to_string::<VersionId>(&version_id.into())?;
item_type = ItemType::Version;
} else if let Some(user_id) = x.user_id {
item_id = serde_json::to_string::<UserId>(&user_id.into())?;
item_type = ItemType::User;
}
reports.push(Report {
id: x.id.into(),
report_type: x.report_type,
item_id,
item_type,
reporter: x.reporter.into(),
body: x.body,
created: x.created,
})
}
Ok(HttpResponse::Ok().json(reports))
}

View File

@@ -1,14 +1,8 @@
use crate::database::models::categories::{
Category, GameVersion, Loader, ProjectType,
};
use crate::database::models::categories::{Category, GameVersion, Loader};
use crate::routes::ApiError;
use crate::util::auth::check_is_admin_from_headers;
use actix_web::{get, put, web};
use actix_web::{HttpRequest, HttpResponse};
use actix_web::{get, web, HttpResponse};
use sqlx::PgPool;
const DEFAULT_ICON: &str = r#"<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"></circle><path d="M9.09 9a3 3 0 0 1 5.83 1c0 2-3 3-3 3"></path><line x1="12" y1="17" x2="12.01" y2="17"></line></svg>"#;
#[get("category")]
pub async fn category_list(
pool: web::Data<PgPool>,
@@ -22,37 +16,6 @@ pub async fn category_list(
Ok(HttpResponse::Ok().json(results))
}
#[put("category/{name}")]
pub async fn category_create(
req: HttpRequest,
pool: web::Data<PgPool>,
category: web::Path<(String,)>,
) -> Result<HttpResponse, ApiError> {
check_is_admin_from_headers(req.headers(), &**pool).await?;
let name = category.into_inner().0;
let project_type = crate::database::models::ProjectTypeId::get_id(
"mod".to_string(),
&**pool,
)
.await?
.ok_or_else(|| {
ApiError::InvalidInput(
"Specified project type does not exist!".to_string(),
)
})?;
let _id = Category::builder()
.name(&name)?
.icon(DEFAULT_ICON)?
.project_type(&project_type)?
.insert(&**pool)
.await?;
Ok(HttpResponse::NoContent().body(""))
}
#[get("loader")]
pub async fn loader_list(
pool: web::Data<PgPool>,
@@ -67,33 +30,6 @@ pub async fn loader_list(
Ok(HttpResponse::Ok().json(results))
}
#[put("loader/{name}")]
pub async fn loader_create(
req: HttpRequest,
pool: web::Data<PgPool>,
loader: web::Path<(String,)>,
) -> Result<HttpResponse, ApiError> {
check_is_admin_from_headers(req.headers(), &**pool).await?;
let name = loader.into_inner().0;
let mut transaction = pool.begin().await?;
let project_types =
ProjectType::get_many_id(&["mod".to_string()], &mut *transaction)
.await?;
let _id = Loader::builder()
.name(&name)?
.icon(DEFAULT_ICON)?
.supported_project_types(
&project_types.into_iter().map(|x| x.id).collect::<Vec<_>>(),
)?
.insert(&mut transaction)
.await?;
Ok(HttpResponse::NoContent().body(""))
}
#[derive(serde::Deserialize)]
pub struct GameVersionQueryData {
#[serde(rename = "type")]

View File

@@ -4,6 +4,7 @@ use crate::models::projects::{
Dependency, GameVersion, Loader, Version, VersionFile, VersionType,
};
use crate::models::teams::Permissions;
use crate::routes::version_file::Algorithm;
use crate::routes::versions::{VersionIds, VersionListFilters};
use crate::routes::ApiError;
use crate::util::auth::get_user_from_headers;
@@ -41,7 +42,7 @@ fn convert_to_legacy(version: Version) -> LegacyVersion {
name: version.name,
version_number: version.version_number,
changelog: version.changelog,
changelog_url: version.changelog_url,
changelog_url: None,
date_published: version.date_published,
downloads: version.downloads,
version_type: version.version_type,
@@ -192,174 +193,3 @@ pub async fn version_get(
Ok(HttpResponse::NotFound().body(""))
}
}
#[derive(Deserialize)]
pub struct Algorithm {
#[serde(default = "default_algorithm")]
algorithm: String,
}
fn default_algorithm() -> String {
"sha1".into()
}
// under /api/v1/version_file/{hash}
#[get("{version_id}")]
pub async fn get_version_from_hash(
info: web::Path<(String,)>,
pool: web::Data<PgPool>,
algorithm: web::Query<Algorithm>,
) -> Result<HttpResponse, ApiError> {
let hash = info.into_inner().0.to_lowercase();
let result = sqlx::query!(
"
SELECT f.version_id version_id FROM hashes h
INNER JOIN files f ON h.file_id = f.id
WHERE h.algorithm = $2 AND h.hash = $1
",
hash.as_bytes(),
algorithm.algorithm
)
.fetch_optional(&**pool)
.await?;
if let Some(id) = result {
let version_data = database::models::Version::get_full(
database::models::VersionId(id.version_id),
&**pool,
)
.await?;
if let Some(data) = version_data {
Ok(HttpResponse::Ok()
.json(crate::models::projects::Version::from(data)))
} else {
Ok(HttpResponse::NotFound().body(""))
}
} else {
Ok(HttpResponse::NotFound().body(""))
}
}
#[derive(Serialize, Deserialize)]
pub struct DownloadRedirect {
pub url: String,
}
// under /api/v1/version_file/{hash}/download
#[allow(clippy::await_holding_refcell_ref)]
#[get("{version_id}/download")]
pub async fn download_version(
info: web::Path<(String,)>,
pool: web::Data<PgPool>,
algorithm: web::Query<Algorithm>,
) -> Result<HttpResponse, ApiError> {
let hash = info.into_inner().0;
let result = sqlx::query!(
"
SELECT f.url url, f.id id, f.version_id version_id, v.mod_id mod_id FROM hashes h
INNER JOIN files f ON h.file_id = f.id
INNER JOIN versions v ON v.id = f.version_id
WHERE h.algorithm = $2 AND h.hash = $1
",
hash.as_bytes(),
algorithm.algorithm
)
.fetch_optional(&**pool)
.await
.map_err(|e| ApiError::Database(e.into()))?;
if let Some(id) = result {
Ok(HttpResponse::TemporaryRedirect()
.append_header(("Location", &*id.url))
.json(DownloadRedirect { url: id.url }))
} else {
Ok(HttpResponse::NotFound().body(""))
}
}
// under /api/v1/version_file/{hash}
#[delete("{version_id}")]
pub async fn delete_file(
req: HttpRequest,
info: web::Path<(String,)>,
pool: web::Data<PgPool>,
algorithm: web::Query<Algorithm>,
) -> Result<HttpResponse, ApiError> {
let user = get_user_from_headers(req.headers(), &**pool).await?;
let hash = info.into_inner().0.to_lowercase();
let result = sqlx::query!(
"
SELECT f.id id, f.version_id version_id, f.filename filename, v.version_number version_number, v.mod_id project_id FROM hashes h
INNER JOIN files f ON h.file_id = f.id
INNER JOIN versions v ON v.id = f.version_id
WHERE h.algorithm = $2 AND h.hash = $1
",
hash.as_bytes(),
algorithm.algorithm
)
.fetch_optional(&**pool)
.await
?;
if let Some(row) = result {
if !user.role.is_admin() {
let team_member =
database::models::TeamMember::get_from_user_id_version(
database::models::ids::VersionId(row.version_id),
user.id.into(),
&**pool,
)
.await
.map_err(ApiError::Database)?
.ok_or_else(|| {
ApiError::CustomAuthentication(
"You don't have permission to delete this file!"
.to_string(),
)
})?;
if !team_member
.permissions
.contains(Permissions::DELETE_VERSION)
{
return Err(ApiError::CustomAuthentication(
"You don't have permission to delete this file!"
.to_string(),
));
}
}
let mut transaction = pool.begin().await?;
sqlx::query!(
"
DELETE FROM hashes
WHERE file_id = $1
",
row.id
)
.execute(&mut *transaction)
.await?;
sqlx::query!(
"
DELETE FROM files
WHERE files.id = $1
",
row.id,
)
.execute(&mut *transaction)
.await?;
transaction.commit().await?;
Ok(HttpResponse::NoContent().body(""))
} else {
Ok(HttpResponse::NotFound().body(""))
}
}

View File

@@ -14,7 +14,7 @@ use tokio::sync::RwLock;
#[derive(Deserialize)]
pub struct Algorithm {
#[serde(default = "default_algorithm")]
algorithm: String,
pub algorithm: String,
}
fn default_algorithm() -> String {