You've already forked AstralRinth
forked from didirus/AstralRinth
Collections users route (#711)
* users route * Added user route * collections
This commit is contained in:
@@ -5,7 +5,10 @@ use crate::models::pats::Scopes;
|
||||
use crate::models::projects::{ProjectId, VersionId};
|
||||
use crate::queue::session::AuthQueue;
|
||||
use crate::routes::ApiError;
|
||||
use crate::{auth::{is_authorized, is_authorized_version, get_user_from_headers}, database};
|
||||
use crate::{
|
||||
auth::{get_user_from_headers, is_authorized, is_authorized_version},
|
||||
database,
|
||||
};
|
||||
use actix_web::{get, route, web, HttpRequest, HttpResponse};
|
||||
use sqlx::PgPool;
|
||||
use std::collections::HashSet;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
mod admin;
|
||||
mod analytics_get;
|
||||
mod collections;
|
||||
mod images;
|
||||
mod analytics_get;
|
||||
mod moderation;
|
||||
mod notifications;
|
||||
pub(crate) mod project_creation;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use crate::auth::{get_user_from_headers, AuthenticationError};
|
||||
use crate::database::models::User;
|
||||
use crate::file_hosting::FileHost;
|
||||
use crate::models::collections::{Collection, CollectionStatus};
|
||||
use crate::models::notifications::Notification;
|
||||
use crate::models::pats::Scopes;
|
||||
use crate::models::projects::Project;
|
||||
@@ -30,6 +31,7 @@ pub fn config(cfg: &mut web::ServiceConfig) {
|
||||
web::scope("user")
|
||||
.service(user_get)
|
||||
.service(projects_list)
|
||||
.service(collections_list)
|
||||
.service(user_delete)
|
||||
.service(user_edit)
|
||||
.service(user_icon_edit)
|
||||
@@ -157,6 +159,50 @@ pub async fn projects_list(
|
||||
}
|
||||
}
|
||||
|
||||
#[get("{user_id}/collections")]
|
||||
pub async fn collections_list(
|
||||
req: HttpRequest,
|
||||
info: web::Path<(String,)>,
|
||||
pool: web::Data<PgPool>,
|
||||
redis: web::Data<deadpool_redis::Pool>,
|
||||
session_queue: web::Data<AuthQueue>,
|
||||
) -> Result<HttpResponse, ApiError> {
|
||||
let user = get_user_from_headers(
|
||||
&req,
|
||||
&**pool,
|
||||
&redis,
|
||||
&session_queue,
|
||||
Some(&[Scopes::COLLECTION_READ]),
|
||||
)
|
||||
.await
|
||||
.map(|x| x.1)
|
||||
.ok();
|
||||
|
||||
let id_option = User::get(&info.into_inner().0, &**pool, &redis).await?;
|
||||
|
||||
if let Some(id) = id_option.map(|x| x.id) {
|
||||
let user_id: UserId = id.into();
|
||||
|
||||
let can_view_private = user
|
||||
.map(|y| y.role.is_mod() || y.id == user_id)
|
||||
.unwrap_or(false);
|
||||
|
||||
let project_data = User::get_collections(id, &**pool).await?;
|
||||
|
||||
let response: Vec<_> =
|
||||
crate::database::models::Collection::get_many(&project_data, &**pool, &redis)
|
||||
.await?
|
||||
.into_iter()
|
||||
.filter(|x| can_view_private || matches!(x.status, CollectionStatus::Listed))
|
||||
.map(Collection::from)
|
||||
.collect();
|
||||
|
||||
Ok(HttpResponse::Ok().json(response))
|
||||
} else {
|
||||
Ok(HttpResponse::NotFound().body(""))
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref RE_URL_SAFE: Regex = Regex::new(r"^[a-zA-Z0-9_-]*$").unwrap();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user