1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2025-01-30 23:31:35 +00:00
ibis/src/backend/api/mod.rs

90 lines
3 KiB
Rust
Raw Normal View History

use crate::backend::api::article::{
create_article, list_articles, resolve_article, search_article,
};
2024-01-03 12:29:25 +00:00
use crate::backend::api::article::{edit_article, fork_article, get_article};
use crate::backend::api::instance::get_local_instance;
2024-01-17 15:40:01 +00:00
use crate::backend::api::instance::{follow_instance, resolve_instance};
2024-01-03 12:29:25 +00:00
use crate::backend::api::user::register_user;
use crate::backend::api::user::validate;
2024-01-16 15:07:01 +00:00
use crate::backend::api::user::{login_user, logout_user};
2024-01-17 15:40:01 +00:00
use crate::backend::api::user::{my_profile, AUTH_COOKIE};
2024-01-18 11:38:31 +00:00
use crate::backend::database::conflict::DbConflict;
2024-01-03 12:29:25 +00:00
use crate::backend::database::MyDataHandle;
use crate::backend::error::MyResult;
2024-01-29 16:04:22 +00:00
use crate::common::ApiConflict;
2024-01-16 15:07:01 +00:00
use crate::common::LocalUserView;
2023-12-13 12:32:44 +00:00
use activitypub_federation::config::Data;
use axum::routing::{get, post};
use axum::{
http::Request,
http::StatusCode,
middleware::{self, Next},
response::Response,
2023-12-19 14:32:14 +00:00
Extension,
};
2023-12-13 12:32:44 +00:00
use axum::{Json, Router};
2024-01-17 15:40:01 +00:00
use axum_extra::extract::CookieJar;
2023-12-13 12:32:44 +00:00
use axum_macros::debug_handler;
use futures::future::try_join_all;
2024-01-03 12:29:25 +00:00
use log::warn;
2023-12-13 12:32:44 +00:00
pub mod article;
pub mod instance;
pub mod user;
pub fn api_routes() -> Router {
Router::new()
.route(
"/article",
get(get_article).post(create_article).patch(edit_article),
)
.route("/article/list", get(list_articles))
2023-12-13 12:32:44 +00:00
.route("/article/fork", post(fork_article))
2024-01-17 15:40:01 +00:00
.route("/article/resolve", get(resolve_article))
2023-12-13 12:32:44 +00:00
.route("/edit_conflicts", get(edit_conflicts))
.route("/instance", get(get_local_instance))
.route("/instance/follow", post(follow_instance))
2024-01-17 15:40:01 +00:00
.route("/instance/resolve", get(resolve_instance))
2023-12-13 12:32:44 +00:00
.route("/search", get(search_article))
2024-01-16 15:07:01 +00:00
.route("/account/register", post(register_user))
.route("/account/login", post(login_user))
.route("/account/my_profile", get(my_profile))
.route("/account/logout", get(logout_user))
.route_layer(middleware::from_fn(auth))
}
async fn auth<B>(
data: Data<MyDataHandle>,
2024-01-17 15:40:01 +00:00
jar: CookieJar,
mut request: Request<B>,
next: Next<B>,
) -> Result<Response, StatusCode> {
2024-01-17 15:40:01 +00:00
if let Some(auth) = jar.get(AUTH_COOKIE) {
let user = validate(auth.value(), &data).await.map_err(|e| {
warn!("Failed to validate auth token: {e}");
StatusCode::UNAUTHORIZED
})?;
request.extensions_mut().insert(user);
}
let response = next.run(request).await;
Ok(response)
2023-12-13 12:32:44 +00:00
}
/// Get a list of all unresolved edit conflicts.
#[debug_handler]
2023-12-19 14:32:14 +00:00
async fn edit_conflicts(
Extension(user): Extension<LocalUserView>,
data: Data<MyDataHandle>,
) -> MyResult<Json<Vec<ApiConflict>>> {
let conflicts = DbConflict::list(&user.local_user, &data.db_connection)?;
2023-12-13 12:32:44 +00:00
let conflicts: Vec<ApiConflict> = try_join_all(conflicts.into_iter().map(|c| {
let data = data.reset_request_count();
async move { c.to_api_conflict(&data).await }
}))
.await?
.into_iter()
.flatten()
.collect();
Ok(Json(conflicts))
}