Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
v2 functions
Browse files Browse the repository at this point in the history
  • Loading branch information
thesuzerain committed Jan 17, 2024
1 parent 0b473c1 commit bd5a573
Show file tree
Hide file tree
Showing 28 changed files with 2,419 additions and 2,242 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ tokio-stream = "0.1.14"

futures = "0.3.30"
futures-timer = "3.0.2"
futures-lite = "1.13.0"
async-trait = "0.1.70"
dashmap = "5.4.0"
lazy_static = "1.4.0"
Expand Down Expand Up @@ -113,6 +114,8 @@ derive-new = "0.6.0"
# country list
rust_iso3166 = "0.1.11"

multer = "3.0.0"

[dev-dependencies]
axum-test = "14.2.2"
json-patch = "*"
Expand Down
13 changes: 8 additions & 5 deletions src/database/models/legacy_loader_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// These fields only apply to minecraft-java, and are hardcoded to the minecraft-java game.

use chrono::{DateTime, Utc};
use futures::Future;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use serde_json::json;
Expand Down Expand Up @@ -34,15 +35,16 @@ impl MinecraftGameVersion {
MinecraftGameVersionBuilder::default()
}

pub async fn list<'a, E>(
version_type_option: Option<&str>,
pub fn list<'a, 'c, E>(
version_type_option: Option<&'a str>,
major_option: Option<bool>,
exec: E,
redis: &RedisPool,
) -> Result<Vec<MinecraftGameVersion>, DatabaseError>
redis: &'a RedisPool,
) -> impl Future<Output = Result<Vec<MinecraftGameVersion>, DatabaseError>> + Send + 'a
where
E: sqlx::Acquire<'a, Database = sqlx::Postgres>,
E: sqlx::Acquire<'c, Database = sqlx::Postgres> + Send + 'a,
{
async move {
let mut exec = exec.acquire().await?;
let game_version_enum = LoaderFieldEnum::get(Self::FIELD_NAME, &mut *exec, redis)
.await?
Expand Down Expand Up @@ -70,6 +72,7 @@ impl MinecraftGameVersion {
.collect_vec();

Ok(game_versions)
}
}

// Tries to create a MinecraftGameVersion from a VersionField
Expand Down
21 changes: 20 additions & 1 deletion src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use tower::ServiceExt;
use tower_http::services::ServeDir;

pub mod internal;
// pub mod v2;
pub mod v2;
pub mod v3;

pub mod v2_reroute;
Expand Down Expand Up @@ -202,3 +202,22 @@ impl IntoResponse for ApiError {
(status_code, Json(error_message)).into_response()
}
}


#[derive(Debug)]
pub struct ApiErrorV2(pub ApiError);

impl IntoResponse for ApiErrorV2 {
fn into_response(self) -> Response {
match self.0 {
ApiError::NotFound => (StatusCode::NOT_FOUND, "").into_response(),
err => err.into_response(),
}
}
}

impl From<ApiError> for ApiErrorV2 {
fn from(error: ApiError) -> Self {
ApiErrorV2(error)
}
}
3 changes: 2 additions & 1 deletion src/routes/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn config() -> Router {
Router::new()
.merge(super::internal::admin::config())
.merge(super::internal::session::config())
.merge(super::internal::flows::config)
.merge(super::internal::flows::config())
.merge(super::internal::pats::config())
.merge(moderation::config())
.merge(notifications::config())
Expand All @@ -39,3 +39,4 @@ pub fn config() -> Router {
.layer(default_cors()),
)
}

49 changes: 24 additions & 25 deletions src/routes/v2/moderation.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
use super::ApiError;
use crate::models::projects::Project;
use std::net::SocketAddr;
use std::sync::Arc;

use crate::models::v2::projects::LegacyProject;
use crate::queue::session::AuthQueue;
use crate::routes::v3;
use crate::{database::redis::RedisPool, routes::v2_reroute};
use actix_web::{get, web, HttpRequest, HttpResponse};
use crate::routes::{v3, ApiErrorV2};
use crate::database::redis::RedisPool;
use crate::util::extract::{ConnectInfo, Extension, Json, Query};
use axum::Router;
use axum::http::HeaderMap;
use axum::routing::get;
use serde::Deserialize;
use sqlx::PgPool;
use v3::ApiError;

pub fn config(cfg: &mut web::ServiceConfig) {
cfg.service(web::scope("moderation").service(get_projects));
pub fn config() -> Router {
Router::new()
.route("/moderation/projects", get(get_projects))
}

#[derive(Deserialize)]
Expand All @@ -22,31 +28,24 @@ fn default_count() -> i16 {
100
}

#[get("projects")]
pub async fn get_projects(
ConnectInfo(addr): ConnectInfo<SocketAddr>,
headers: HeaderMap,
Extension(pool): Extension<PgPool>,
Extension(redis): Extension<RedisPool>,
count: Query<ResultCount>,
Query(count): Query<ResultCount>,
Extension(session_queue): Extension<Arc<AuthQueue>>,
) -> Result<HttpResponse, ApiError> {
let response = v3::moderation::get_projects(
req,
pool.clone(),
redis.clone(),
) -> Result<Json<Vec<LegacyProject>>, ApiErrorV2> {
let Json(response) = v3::moderation::get_projects(
ConnectInfo(addr),
headers,
Extension(pool.clone()),
Extension(redis.clone()),
Query(v3::moderation::ResultCount { count: count.count }),
session_queue,
Extension(session_queue),
)
.await
.or_else(v2_reroute::flatten_404_error)?;
.await?;

// Convert to V2 projects
match v2_reroute::extract_ok_json::<Vec<Project>>(response).await {
Ok(project) => {
let legacy_projects = LegacyProject::from_many(project, &pool, &redis).await?;
Ok(Json(legacy_projects))
}
Err(response) => Ok(response),
}
let legacy_projects = LegacyProject::from_many(response, &pool, &redis).await.map_err(ApiError::from)?;
Ok(Json(legacy_projects))
}
Loading

0 comments on commit bd5a573

Please sign in to comment.