Skip to content

Commit

Permalink
i wrote this a week ago and forgot (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
qimiko authored Aug 2, 2024
1 parent 90c01ed commit 8c64f69
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
18 changes: 16 additions & 2 deletions src/endpoints/mod_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub async fn get_version_index(
path: web::Path<IndexPath>,
data: web::Data<AppData>,
query: web::Query<IndexQuery>,
auth: Auth,
) -> Result<impl Responder, ApiError> {
let platforms = VerPlatform::parse_query_string(&query.platforms.clone().unwrap_or_default());
let compare = query.compare.as_ref().map(|c| split_version_and_compare(c));
Expand All @@ -90,6 +91,12 @@ pub async fn get_version_index(

let mut pool = data.db.acquire().await.or(Err(ApiError::DbAcquireError))?;

let has_extended_permissions = match auth.developer() {
Ok(dev) => dev.admin ||
Developer::has_access_to_mod(dev.id, &path.id, &mut pool).await?,
_ => false
};

let mut result = ModVersion::get_index(
mod_version::IndexQuery {
mod_id: path.id.clone(),
Expand All @@ -104,7 +111,7 @@ pub async fn get_version_index(
)
.await?;
for i in &mut result.data {
i.modify_download_link(&data.app_url);
i.modify_metadata(&data.app_url, has_extended_permissions);
}

Ok(web::Json(ApiResponse {
Expand All @@ -118,9 +125,16 @@ pub async fn get_one(
path: web::Path<GetOnePath>,
data: web::Data<AppData>,
query: web::Query<GetOneQuery>,
auth: Auth,
) -> Result<impl Responder, ApiError> {
let mut pool = data.db.acquire().await.or(Err(ApiError::DbAcquireError))?;

let has_extended_permissions = match auth.developer() {
Ok(dev) => dev.admin ||
Developer::has_access_to_mod(dev.id, &path.id, &mut pool).await?,
_ => false
};

let mut version = {
if path.version == "latest" {
let gd: Option<GDVersionEnum> = match query.gd {
Expand All @@ -140,7 +154,7 @@ pub async fn get_one(
}
};

version.modify_download_link(&data.app_url);
version.modify_metadata(&data.app_url, has_extended_permissions);
Ok(web::Json(ApiResponse {
error: "".to_string(),
payload: version,
Expand Down
12 changes: 10 additions & 2 deletions src/endpoints/mods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub async fn index(
let mut result = Mod::get_index(&mut pool, query.0).await?;
for i in &mut result.data {
for j in &mut i.versions {
j.modify_download_link(&data.app_url);
j.modify_metadata(&data.app_url, false);
}
}
Ok(web::Json(ApiResponse {
Expand All @@ -80,13 +80,21 @@ pub async fn index(
pub async fn get(
data: web::Data<AppData>,
id: web::Path<String>,
auth: Auth,
) -> Result<impl Responder, ApiError> {
let mut pool = data.db.acquire().await.or(Err(ApiError::DbAcquireError))?;

let has_extended_permissions = match auth.developer() {
Ok(dev) => dev.admin ||
Developer::has_access_to_mod(dev.id, &id, &mut pool).await?,
_ => false
};

let found = Mod::get_one(&id, false, &mut pool).await?;
match found {
Some(mut m) => {
for i in &mut m.versions {
i.modify_download_link(&data.app_url);
i.modify_metadata(&data.app_url, has_extended_permissions);
}
Ok(web::Json(ApiResponse {
error: "".into(),
Expand Down
5 changes: 4 additions & 1 deletion src/types/models/mod_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ struct ModRecordGetOne {
changelog: Option<String>,
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
info: Option<String>,
}

pub enum CheckExistingResult {
Expand Down Expand Up @@ -573,7 +574,7 @@ impl Mod {
r#"SELECT
m.id, m.repository, m.about, m.changelog, m.featured, m.download_count as mod_download_count, m.created_at, m.updated_at,
mv.id as version_id, mv.name, mv.description, mv.version, mv.download_link, mv.download_count as mod_version_download_count,
mv.hash, mv.geode, mv.early_load, mv.api, mv.mod_id, mvs.status as "status: _"
mv.hash, mv.geode, mv.early_load, mv.api, mv.mod_id, mvs.status as "status: _", mvs.info
FROM mods m
INNER JOIN mod_versions mv ON m.id = mv.mod_id
INNER JOIN mod_version_statuses mvs ON mvs.mod_version_id = mv.id
Expand Down Expand Up @@ -617,6 +618,8 @@ impl Mod {
tags: None,
dependencies: None,
incompatibilities: None,
direct_download_link: Some(x.download_link.clone()),
info: Some(x.info.clone()),
})
.collect();
let ids = versions.iter().map(|x| x.id).collect();
Expand Down
25 changes: 23 additions & 2 deletions src/types/models/mod_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ pub struct ModVersion {
pub incompatibilities: Option<Vec<ResponseIncompatibility>>,
pub developers: Option<Vec<Developer>>,
pub tags: Option<Vec<String>>,

#[serde(default, skip_serializing_if = "Option::is_none")]
/// Admin/developer only - Reason given to status
pub info: Option<Option<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
/// Admin/developer only - Direct download to mod
pub direct_download_link: Option<String>,
}

#[derive(sqlx::FromRow)]
Expand All @@ -56,6 +63,7 @@ struct ModVersionGetOne {
api: bool,
mod_id: String,
status: ModVersionStatusEnum,
info: Option<String>,
}

pub struct IndexQuery {
Expand Down Expand Up @@ -97,15 +105,28 @@ impl ModVersionGetOne {
tags: None,
dependencies: None,
incompatibilities: None,
info: Some(self.info),
direct_download_link: None
}
}
}

impl ModVersion {
pub fn modify_download_link(&mut self, app_url: &str) {
fn modify_download_link(&mut self, app_url: &str) {
self.download_link = create_download_link(app_url, &self.mod_id, &self.version)
}

pub fn modify_metadata(&mut self, app_url: &str, keep_information: bool) {
if keep_information {
self.direct_download_link = Some(self.download_link.clone());
} else {
self.direct_download_link = None;
self.info = None;
}

self.modify_download_link(app_url)
}

pub async fn get_index(
query: IndexQuery,
pool: &mut PgConnection,
Expand Down Expand Up @@ -605,7 +626,7 @@ impl ModVersion {
r#"SELECT mv.id, mv.name, mv.description, mv.version,
mv.download_link, mv.download_count,
mv.hash, mv.geode, mv.early_load, mv.api,
mv.mod_id, mvs.status as "status: _"
mv.mod_id, mvs.status as "status: _", mvs.info
FROM mod_versions mv
INNER JOIN mods m ON m.id = mv.mod_id
INNER JOIN mod_version_statuses mvs ON mvs.mod_version_id = mv.id
Expand Down

0 comments on commit 8c64f69

Please sign in to comment.