2024-03-05 15:38:07 +00:00
|
|
|
use crate::{
|
|
|
|
backend::{
|
|
|
|
api::{
|
|
|
|
article::{
|
|
|
|
create_article,
|
|
|
|
edit_article,
|
|
|
|
fork_article,
|
|
|
|
get_article,
|
|
|
|
list_articles,
|
|
|
|
protect_article,
|
|
|
|
resolve_article,
|
|
|
|
search_article,
|
|
|
|
},
|
|
|
|
instance::{follow_instance, get_local_instance, resolve_instance},
|
|
|
|
user::{
|
|
|
|
get_user,
|
|
|
|
login_user,
|
|
|
|
logout_user,
|
|
|
|
my_profile,
|
|
|
|
register_user,
|
|
|
|
validate,
|
|
|
|
AUTH_COOKIE,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
database::{conflict::DbConflict, IbisData},
|
|
|
|
error::MyResult,
|
|
|
|
},
|
|
|
|
common::{ApiConflict, LocalUserView},
|
2024-02-01 10:34:09 +00:00
|
|
|
};
|
2023-12-13 12:32:44 +00:00
|
|
|
use activitypub_federation::config::Data;
|
2023-12-13 15:40:20 +00:00
|
|
|
use axum::{
|
2024-03-05 15:38:07 +00:00
|
|
|
http::{Request, StatusCode},
|
2023-12-13 15:40:20 +00:00
|
|
|
middleware::{self, Next},
|
|
|
|
response::Response,
|
2024-03-05 15:38:07 +00:00
|
|
|
routing::{get, post},
|
2023-12-19 14:32:14 +00:00
|
|
|
Extension,
|
2024-03-05 15:38:07 +00:00
|
|
|
Json,
|
|
|
|
Router,
|
2023-12-13 15:40:20 +00:00
|
|
|
};
|
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;
|
|
|
|
|
|
|
|
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),
|
|
|
|
)
|
2024-02-01 10:34:09 +00:00
|
|
|
.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))
|
2024-03-05 15:06:27 +00:00
|
|
|
.route("/article/protect", post(protect_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-02-13 12:09:45 +00:00
|
|
|
.route("/user", get(get_user))
|
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))
|
2023-12-13 15:40:20 +00:00
|
|
|
.route_layer(middleware::from_fn(auth))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn auth<B>(
|
2024-02-08 10:40:27 +00:00
|
|
|
data: Data<IbisData>,
|
2024-01-17 15:40:01 +00:00
|
|
|
jar: CookieJar,
|
2023-12-13 15:40:20 +00:00
|
|
|
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) {
|
2024-02-09 09:58:39 +00:00
|
|
|
if let Ok(user) = validate(auth.value(), &data).await {
|
|
|
|
request.extensions_mut().insert(user);
|
|
|
|
}
|
2023-12-13 15:40:20 +00:00
|
|
|
}
|
|
|
|
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>,
|
2024-02-08 10:40:27 +00:00
|
|
|
data: Data<IbisData>,
|
2023-12-19 14:32:14 +00:00
|
|
|
) -> MyResult<Json<Vec<ApiConflict>>> {
|
2024-02-27 16:49:36 +00:00
|
|
|
let conflicts = DbConflict::list(&user.local_user, &data)?;
|
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))
|
|
|
|
}
|