From 0261f87306dc2a54900da4a8778c5260643998df Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 11 Apr 2024 00:52:44 +0200 Subject: [PATCH] feat: first mastodon route yay, mastodon default on --- Cargo.toml | 5 ++-- src/routes/mastodon/accounts.rs | 42 +++++++++++++++++++++++++++++++++ src/routes/mastodon/mod.rs | 5 +++- 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 src/routes/mastodon/accounts.rs diff --git a/Cargo.toml b/Cargo.toml index 7d9b8efd..79822fbc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,9 +40,10 @@ http-signature-normalization = "0.7.0" sea-orm-migration = { version = "0.12", optional = true } # mastodon mastodon-async-entities = { version = "1.1.0", optional = true } +time = { version = "0.3", features = ["serde"], optional = true } [features] -default = ["faker", "migrations"] +default = ["faker", "migrations", "mastodon"] faker = [] migrations = ["dep:sea-orm-migration"] -mastodon = ["dep:mastodon-async-entities"] +mastodon = ["dep:mastodon-async-entities", "dep:time"] diff --git a/src/routes/mastodon/accounts.rs b/src/routes/mastodon/accounts.rs new file mode 100644 index 00000000..9fcda30d --- /dev/null +++ b/src/routes/mastodon/accounts.rs @@ -0,0 +1,42 @@ +use axum::{extract::{Path, State}, http::StatusCode, Json}; +use mastodon_async_entities::account::{Account, AccountId}; +use sea_orm::EntityTrait; + +use crate::{model, server::Context}; + +pub async fn view( + State(ctx): State, + Path(id): Path +) -> Result, StatusCode> { + match model::user::Entity::find_by_id(ctx.uid(id)) + .find_also_related(model::config::Entity) + .one(ctx.db()) + .await + { + Err(e) => Err(StatusCode::INTERNAL_SERVER_ERROR), + Ok(None) => Err(StatusCode::NOT_FOUND), + Ok(Some((x, None))) => Err(StatusCode::NOT_IMPLEMENTED), // TODO remote user + Ok(Some((x, Some(cfg)))) => Ok(Json(Account { + acct: x.preferred_username, + avatar: x.icon.unwrap_or_default(), + avatar_static: x.icon.unwrap_or_default(), + created_at: time::OffsetDateTime::from_unix_timestamp(x.created.timestamp()), + display_name: x.name.unwrap_or_default(), + // TODO hide these maybe + followers_count: x.followers_count as u64, + following_count: x.following_count as u64, + header: x.image.unwrap_or_default(), + header_static: x.image.unwrap_or_default(), + id: AccountId::new(x.id), + locked: !cfg.accept_follow_requests, + note: x.summary.unwrap_or_default(), + statuses_count: 0, + url: x.id, + username: x.preferred_username, + source: None, + moved: None, + fields: None, + bot: None, + })), + } +} diff --git a/src/routes/mastodon/mod.rs b/src/routes/mastodon/mod.rs index 1965191e..faef270e 100644 --- a/src/routes/mastodon/mod.rs +++ b/src/routes/mastodon/mod.rs @@ -1,3 +1,5 @@ +pub mod accounts; + use axum::{http::StatusCode, routing::{delete, get, patch, post}, Router}; use crate::server::Context; @@ -9,6 +11,7 @@ pub trait MastodonRouter { impl MastodonRouter for Router { fn mastodon_routes(self) -> Self { + use crate::routes::mastodon as mas; self.nest( // TODO Oauth is just under /oauth "/api/v1", Router::new() @@ -18,7 +21,7 @@ impl MastodonRouter for Router { .route("/accounts", post(todo)) .route("/accounts/verify_credentials", get(todo)) .route("/accounts/update_credentials", patch(todo)) - .route("/accounts/:id", get(todo)) + .route("/accounts/:id", get(mas::accounts::view)) .route("/accounts/:id/statuses", get(todo)) .route("/accounts/:id/followers", get(todo)) .route("/accounts/:id/following", get(todo))