1
0
Fork 0
forked from alemi/upub

feat: first mastodon route yay, mastodon default on

This commit is contained in:
əlemi 2024-04-11 00:52:44 +02:00
parent 86e84d88aa
commit 0261f87306
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 49 additions and 3 deletions

View file

@ -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"]

View file

@ -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<Context>,
Path(id): Path<String>
) -> Result<Json<Account>, 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,
})),
}
}

View file

@ -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<Context> {
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<Context> {
.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))