From 9bc6c12392e4bb61e1fa6dbe14d110ffd1cbc2f7 Mon Sep 17 00:00:00 2001 From: nullishamy Date: Sat, 26 Apr 2025 13:08:49 +0100 Subject: [PATCH] fix: cleanup warnings; centralise url creation into config --- ferri-main/src/ap/request_queue.rs | 2 +- ferri-main/src/config/mod.rs | 38 ++++++++++++++++++++++ ferri-server/src/endpoints/api/instance.rs | 8 +++-- ferri-server/src/endpoints/api/status.rs | 8 ++--- ferri-server/src/endpoints/api/timeline.rs | 14 ++++---- ferri-server/src/endpoints/api/user.rs | 8 ++--- ferri-server/src/endpoints/custom.rs | 2 ++ ferri-server/src/endpoints/oauth.rs | 14 ++++---- ferri-server/src/endpoints/user.rs | 36 +++++++++++--------- ferri-server/src/endpoints/well_known.rs | 13 ++++---- ferri-server/src/lib.rs | 14 +++----- 11 files changed, 102 insertions(+), 55 deletions(-) diff --git a/ferri-main/src/ap/request_queue.rs b/ferri-main/src/ap/request_queue.rs index 65c7f07..9088266 100644 --- a/ferri-main/src/ap/request_queue.rs +++ b/ferri-main/src/ap/request_queue.rs @@ -1,6 +1,6 @@ use std::sync::mpsc; use std::thread; -use tracing::{debug, info, span, Level}; +use tracing::{info, span, Level}; #[derive(Debug)] pub enum QueueMessage { diff --git a/ferri-main/src/config/mod.rs b/ferri-main/src/config/mod.rs index 5af1eef..79305d2 100644 --- a/ferri-main/src/config/mod.rs +++ b/ferri-main/src/config/mod.rs @@ -9,3 +9,41 @@ pub struct ServerConfig { pub struct Config { pub server: ServerConfig, } + +impl Config { + pub fn host(&self) -> &str { + &self.server.host + } + + pub fn user_url(&self, user_uuid: &str) -> String { + format!("{}/users/{}", self.host(), user_uuid) + } + + pub fn user_web_url(&self, user_name: &str) -> String { + format!("{}/{}", self.host(), user_name) + } + + pub fn followers_url(&self, user_uuid: &str) -> String { + format!("{}/followers", self.user_url(user_uuid)) + } + + pub fn following_url(&self, user_uuid: &str) -> String { + format!("{}/following", self.user_url(user_uuid)) + } + + pub fn inbox_url(&self, user_uuid: &str) -> String { + format!("{}/inbox", self.user_url(user_uuid)) + } + + pub fn outbox_url(&self, user_uuid: &str) -> String { + format!("{}/outbox", self.user_url(user_uuid)) + } + + pub fn post_url(&self, poster_uuid: &str, post_uuid: &str) -> String { + format!("{}/{}", self.user_url(poster_uuid), post_uuid) + } + + pub fn activity_url(&self, activity_uuid: &str) -> String { + format!("{}/activities/{}", self.host(), activity_uuid) + } +} diff --git a/ferri-server/src/endpoints/api/instance.rs b/ferri-server/src/endpoints/api/instance.rs index 0a76ba6..9fab90f 100644 --- a/ferri-server/src/endpoints/api/instance.rs +++ b/ferri-server/src/endpoints/api/instance.rs @@ -1,4 +1,6 @@ -use rocket::{get, serde::json::Json}; +use rocket::{get, serde::json::Json, State}; + +use crate::Config; use crate::types::instance::{ Accounts, Configuration, Contact, Instance, MediaAttachments, Polls, Registrations, Statuses, @@ -6,9 +8,9 @@ use crate::types::instance::{ }; #[get("/instance")] -pub async fn instance() -> Json { +pub async fn instance(config: &State) -> Json { Json(Instance { - domain: "ferri.amy.mov".to_string(), + domain: config.host().to_string(), title: "Ferri".to_string(), version: "0.0.1".to_string(), source_url: "https://forge.amy.mov/amy/ferri".to_string(), diff --git a/ferri-server/src/endpoints/api/status.rs b/ferri-server/src/endpoints/api/status.rs index 3cf1eb2..e716de4 100644 --- a/ferri-server/src/endpoints/api/status.rs +++ b/ferri-server/src/endpoints/api/status.rs @@ -26,11 +26,11 @@ pub struct StatusContext { descendants: Vec } -#[get("/statuses//context")] +#[get("/statuses/<_status>/context")] pub async fn status_context( - status: &str, - user: AuthenticatedUser, - mut db: Connection + _status: &str, + _user: AuthenticatedUser, + _db: Connection ) -> Json { Json(StatusContext { ancestors: vec![], diff --git a/ferri-server/src/endpoints/api/timeline.rs b/ferri-server/src/endpoints/api/timeline.rs index c81e387..884b82e 100644 --- a/ferri-server/src/endpoints/api/timeline.rs +++ b/ferri-server/src/endpoints/api/timeline.rs @@ -1,5 +1,6 @@ -use crate::{AuthenticatedUser, Db, endpoints::api::user::CredentialAcount}; +use crate::{AuthenticatedUser, Db, endpoints::api::user::CredentialAcount, Config}; use rocket::{ + State, get, serde::{Deserialize, Serialize, json::Json}, }; @@ -32,11 +33,12 @@ pub struct TimelineStatus { pub account: TimelineAccount, } -#[get("/timelines/home?")] +#[get("/timelines/home?<_limit>")] pub async fn home( mut db: Connection, - limit: i64, - user: AuthenticatedUser, + config: &State, + _limit: i64, + _user: AuthenticatedUser, ) -> Json> { let posts = sqlx::query!( r#" @@ -66,7 +68,7 @@ pub async fn home( .await .unwrap(); - let user_uri = format!("https://ferri.amy.mov/users/{}", record.user_id); + let user_uri = config.user_url(&record.user_id); boost = Some(Box::new(TimelineStatus { id: record.post_id.clone(), created_at: record.created_at.clone(), @@ -110,7 +112,7 @@ pub async fn home( })) } - let user_uri = format!("https://ferri.amy.mov/users/{}", record.username); + let user_uri = config.user_web_url(&record.username); out.push(TimelineStatus { id: record.post_id.clone(), created_at: record.created_at.clone(), diff --git a/ferri-server/src/endpoints/api/user.rs b/ferri-server/src/endpoints/api/user.rs index 2a686d4..38428e2 100644 --- a/ferri-server/src/endpoints/api/user.rs +++ b/ferri-server/src/endpoints/api/user.rs @@ -95,7 +95,7 @@ pub async fn new_follow( pub async fn account( mut db: Connection, uuid: &str, - user: AuthenticatedUser, + _user: AuthenticatedUser, ) -> Result, NotFound> { let user = ap::User::from_id(uuid, &mut **db) .await @@ -123,12 +123,12 @@ pub async fn account( })) } -#[get("/accounts//statuses?")] +#[get("/accounts//statuses?<_limit>")] pub async fn statuses( mut db: Connection, uuid: &str, - limit: Option, - user: AuthenticatedUser, + _limit: Option, + _user: AuthenticatedUser, ) -> Result>, NotFound> { let user = ap::User::from_id(uuid, &mut **db) .await diff --git a/ferri-server/src/endpoints/custom.rs b/ferri-server/src/endpoints/custom.rs index 8256121..8d7c57f 100644 --- a/ferri-server/src/endpoints/custom.rs +++ b/ferri-server/src/endpoints/custom.rs @@ -123,5 +123,7 @@ pub async fn test(http: &State, outbound: &State) -> .await .unwrap(); + dbg!(follow); + "Hello, world!" } diff --git a/ferri-server/src/endpoints/oauth.rs b/ferri-server/src/endpoints/oauth.rs index 81c8d55..ae897fa 100644 --- a/ferri-server/src/endpoints/oauth.rs +++ b/ferri-server/src/endpoints/oauth.rs @@ -8,12 +8,12 @@ use rocket::{ }; use rocket_db_pools::Connection; -#[get("/oauth/authorize?&&&")] +#[get("/oauth/authorize?&&&<_response_type>")] pub async fn authorize( client_id: &str, scope: &str, redirect_uri: &str, - response_type: &str, + _response_type: &str, mut db: Connection, ) -> Redirect { // For now, we will always authorize the request and assign it to an admin user @@ -68,11 +68,11 @@ pub struct Token { #[derive(Deserialize, Debug, FromForm)] #[serde(crate = "rocket::serde")] pub struct NewTokenRequest { - client_id: String, - redirect_uri: String, - grant_type: String, - code: String, - client_secret: String, + // pub client_id: String, + // pub redirect_uri: String, + // pub grant_type: String, + pub code: String, + // pub client_secret: String, } #[post("/oauth/token", data = "")] diff --git a/ferri-server/src/endpoints/user.rs b/ferri-server/src/endpoints/user.rs index a205e60..0464123 100644 --- a/ferri-server/src/endpoints/user.rs +++ b/ferri-server/src/endpoints/user.rs @@ -1,17 +1,18 @@ use main::ap; -use rocket::{get, http::ContentType, serde::json::Json}; +use rocket::{get, http::ContentType, serde::json::Json, State}; use rocket_db_pools::Connection; use rocket::response::status::NotFound; use crate::{ + Config, Db, types::{OrderedCollection, Person, UserKey, content}, }; use super::activity_type; -#[get("/users//inbox")] -pub async fn inbox(user: String) -> Json { +#[get("/users/<_user>/inbox")] +pub async fn inbox(_user: String) -> Json { Json(OrderedCollection { ty: "OrderedCollection".to_string(), total_items: 0, @@ -19,8 +20,8 @@ pub async fn inbox(user: String) -> Json { }) } -#[get("/users//outbox")] -pub async fn outbox(user: String) -> Json { +#[get("/users/<_user>/outbox")] +pub async fn outbox(_user: String) -> Json { Json(OrderedCollection { ty: "OrderedCollection".to_string(), total_items: 0, @@ -89,6 +90,7 @@ pub async fn following(mut db: Connection, uuid: &str) -> Result/posts/")] pub async fn post( mut db: Connection, + config: &State, uuid: &str, post: String, ) -> (ContentType, Json) { @@ -106,19 +108,23 @@ pub async fn post( activity_type(), Json(content::Post { context: "https://www.w3.org/ns/activitystreams".to_string(), - id: format!("https://ferri.amy.mov/users/{}/posts/{}", uuid, post.id), - attributed_to: Some(format!("https://ferri.amy.mov/users/{}/posts/{}", uuid, post.id)), + id: config.post_url(uuid, &post.id), + attributed_to: Some(config.user_url(uuid)), ty: "Note".to_string(), content: post.content, ts: post.created_at, - to: vec!["https://ferri.amy.mov/users/amy/followers".to_string()], + to: vec![config.followers_url(uuid)], cc: vec!["https://www.w3.org/ns/activitystreams#Public".to_string()], }), ) } #[get("/users/")] -pub async fn user(mut db: Connection, uuid: &str) -> Result<(ContentType, Json), NotFound> { +pub async fn user( + mut db: Connection, + config: &State, + uuid: &str +) -> Result<(ContentType, Json), NotFound> { let user = ap::User::from_id(uuid, &mut **db) .await .map_err(|e| NotFound(e.to_string()))?; @@ -128,17 +134,17 @@ pub async fn user(mut db: Connection, uuid: &str) -> Result<(ContentType, Js Json(Person { context: "https://www.w3.org/ns/activitystreams".to_string(), ty: "Person".to_string(), - id: format!("https://ferri.amy.mov/users/{}", user.id()), + id: config.user_url(user.id()), name: user.username().to_string(), preferred_username: user.display_name().to_string(), - followers: format!("https://ferri.amy.mov/users/{}/followers", uuid), - following: format!("https://ferri.amy.mov/users/{}/following", uuid), + followers: config.followers_url(user.id()), + following: config.following_url(user.id()), summary: format!("ferri {}", user.username()), - inbox: format!("https://ferri.amy.mov/users/{}/inbox", uuid), - outbox: format!("https://ferri.amy.mov/users/{}/outbox", uuid), + inbox: config.inbox_url(user.id()), + outbox: config.outbox_url(user.id()), public_key: Some(UserKey { id: format!("https://ferri.amy.mov/users/{}#main-key", uuid), - owner: format!("https://ferri.amy.mov/users/{}", uuid), + owner: config.user_url(user.id()), public_key: include_str!("../../../public.pem").to_string(), }), }), diff --git a/ferri-server/src/endpoints/well_known.rs b/ferri-server/src/endpoints/well_known.rs index 9692b61..fe03322 100644 --- a/ferri-server/src/endpoints/well_known.rs +++ b/ferri-server/src/endpoints/well_known.rs @@ -1,9 +1,10 @@ use main::ap; -use rocket::{get, serde::json::Json}; +use rocket::{get, serde::json::Json, State}; use rocket_db_pools::Connection; use tracing::info; use crate::{ + Config, Db, types::webfinger::{Link, WebfingerResponse}, }; @@ -20,7 +21,7 @@ pub async fn host_meta() -> &'static str { // https://mastodon.social/.well-known/webfinger?resource=acct:gargron@mastodon.social #[get("/.well-known/webfinger?")] -pub async fn webfinger(mut db: Connection, resource: &str) -> Json { +pub async fn webfinger(mut db: Connection, config: &State, resource: &str) -> Json { info!(?resource, "incoming webfinger request"); let acct = resource.strip_prefix("acct:").unwrap(); @@ -30,19 +31,19 @@ pub async fn webfinger(mut db: Connection, resource: &str) -> Json) -> (ContentType, &'static str) { +async fn user_profile() -> (ContentType, &'static str) { (ContentType::HTML, "

hello

") } -#[get("/activities/")] -async fn activity_endpoint(activity: String) { +#[get("/activities/<_activity>")] +async fn activity_endpoint(_activity: String) { } #[derive(Debug)] -struct AuthenticatedUser { +pub struct AuthenticatedUser { pub username: String, pub id: String, pub token: String, @@ -45,10 +44,7 @@ struct AuthenticatedUser { } #[derive(Debug)] -enum LoginError { - InvalidData, - UsernameDoesNotExist, - WrongPassword, +pub enum LoginError { } #[rocket::async_trait]