You've already forked AstralRinth
forked from didirus/AstralRinth
Fix update route (#364)
* Fix version updates route * Run formatter, fix clippy, run prepare
This commit is contained in:
5
Cargo.lock
generated
5
Cargo.lock
generated
@@ -1432,6 +1432,7 @@ dependencies = [
|
|||||||
"sqlx",
|
"sqlx",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"time 0.2.27",
|
"time 0.2.27",
|
||||||
|
"tokio",
|
||||||
"tokio-stream",
|
"tokio-stream",
|
||||||
"url",
|
"url",
|
||||||
"urlencoding",
|
"urlencoding",
|
||||||
@@ -2761,9 +2762,9 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio"
|
name = "tokio"
|
||||||
version = "1.18.2"
|
version = "1.19.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4903bf0427cf68dddd5aa6a93220756f8be0c34fcfa9f5e6191e103e15a31395"
|
checksum = "95eec79ea28c00a365f539f1961e9278fbcaf81c0ff6aaf0e93c181352446948"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bytes",
|
"bytes",
|
||||||
"libc",
|
"libc",
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ path = "src/main.rs"
|
|||||||
actix = "0.13.0"
|
actix = "0.13.0"
|
||||||
actix-web = { git = "https://github.com/modrinth/actix-web", rev = "88c7c18" }
|
actix-web = { git = "https://github.com/modrinth/actix-web", rev = "88c7c18" }
|
||||||
actix-rt = "2.7.0"
|
actix-rt = "2.7.0"
|
||||||
|
tokio = { version = "1.19.0", features = ["sync"] }
|
||||||
tokio-stream = "0.1.8"
|
tokio-stream = "0.1.8"
|
||||||
actix-multipart = { git = "https://github.com/modrinth/actix-web", rev = "88c7c18" }
|
actix-multipart = { git = "https://github.com/modrinth/actix-web", rev = "88c7c18" }
|
||||||
actix-cors = { git = "https://github.com/modrinth/actix-extras.git", rev = "34d301f" }
|
actix-cors = { git = "https://github.com/modrinth/actix-extras.git", rev = "34d301f" }
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ pub struct LicenseId(pub i32);
|
|||||||
#[sqlx(transparent)]
|
#[sqlx(transparent)]
|
||||||
pub struct DonationPlatformId(pub i32);
|
pub struct DonationPlatformId(pub i32);
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, Type, PartialEq)]
|
#[derive(Copy, Clone, Debug, Type, PartialEq, Eq, Hash)]
|
||||||
#[sqlx(transparent)]
|
#[sqlx(transparent)]
|
||||||
pub struct VersionId(pub i64);
|
pub struct VersionId(pub i64);
|
||||||
#[derive(Copy, Clone, Debug, Type)]
|
#[derive(Copy, Clone, Debug, Type)]
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct Algorithm {
|
pub struct Algorithm {
|
||||||
@@ -428,9 +429,10 @@ pub async fn update_files(
|
|||||||
.fetch_all(&mut *transaction)
|
.fetch_all(&mut *transaction)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let mut version_ids = Vec::new();
|
let version_ids: RwLock<HashMap<database::models::VersionId, Vec<u8>>> =
|
||||||
|
RwLock::new(HashMap::new());
|
||||||
|
|
||||||
for row in &result {
|
futures::future::try_join_all(result.into_iter().map(|row| async {
|
||||||
let updated_versions = database::models::Version::get_project_versions(
|
let updated_versions = database::models::Version::get_project_versions(
|
||||||
database::models::ProjectId(row.project_id),
|
database::models::ProjectId(row.project_id),
|
||||||
Some(
|
Some(
|
||||||
@@ -454,28 +456,40 @@ pub async fn update_files(
|
|||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
if let Some(latest_version) = updated_versions.last() {
|
if let Some(latest_version) = updated_versions.last() {
|
||||||
version_ids.push(*latest_version);
|
let mut version_ids = version_ids.write().await;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let versions =
|
version_ids.insert(*latest_version, row.hash);
|
||||||
database::models::Version::get_many_full(version_ids, &**pool).await?;
|
}
|
||||||
|
|
||||||
|
Ok::<(), ApiError>(())
|
||||||
|
}))
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let version_ids = version_ids.into_inner();
|
||||||
|
|
||||||
|
let versions = database::models::Version::get_many_full(
|
||||||
|
version_ids.keys().copied().collect(),
|
||||||
|
&**pool,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
let mut response = HashMap::new();
|
let mut response = HashMap::new();
|
||||||
|
|
||||||
for row in &result {
|
for version in versions {
|
||||||
if let Some(version) =
|
let hash = version_ids.get(&version.id);
|
||||||
versions.iter().find(|x| x.id.0 == row.version_id)
|
|
||||||
{
|
if let Some(hash) = hash {
|
||||||
if let Ok(parsed_hash) = String::from_utf8(row.hash.clone()) {
|
if let Ok(parsed_hash) = String::from_utf8(hash.clone()) {
|
||||||
response.insert(
|
response.insert(
|
||||||
parsed_hash,
|
parsed_hash,
|
||||||
models::projects::Version::from(version.clone()),
|
models::projects::Version::from(version),
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
let version_id: models::projects::VersionId = version.id.into();
|
||||||
|
|
||||||
return Err(ApiError::Database(DatabaseError::Other(format!(
|
return Err(ApiError::Database(DatabaseError::Other(format!(
|
||||||
"Could not parse hash for version {}",
|
"Could not parse hash for version {}",
|
||||||
row.version_id
|
version_id
|
||||||
))));
|
))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user