ferri/ferri-server/src/endpoints/custom.rs

102 lines
2.4 KiB
Rust
Raw Normal View History

2025-04-12 15:16:40 +01:00
use rocket::{State, get, response::status};
2025-04-11 12:29:29 +01:00
use rocket_db_pools::Connection;
2025-04-26 12:44:45 +01:00
use main::ap;
use crate::OutboundQueue;
2025-04-11 12:29:29 +01:00
use uuid::Uuid;
2025-04-12 15:16:40 +01:00
use crate::{
Db,
types::{self, webfinger},
2025-04-12 15:16:40 +01:00
};
2025-04-11 12:29:29 +01:00
#[get("/finger/<account>")]
pub async fn finger_account(mut db: Connection<Db>, account: &str) -> status::Accepted<String> {
// user@host.com
let (name, host) = account.split_once("@").unwrap();
let user = resolve_user(name, host).await;
// Make actor
sqlx::query!(
r#"
INSERT INTO actor (id, inbox, outbox)
VALUES (?1, ?2, ?3)
ON CONFLICT(id) DO NOTHING
"#,
user.id,
user.inbox,
user.outbox
)
.execute(&mut **db)
.await
.unwrap();
let uuid = Uuid::new_v4().to_string();
let username = format!("{}@{}", user.name, host);
sqlx::query!(
r#"
INSERT INTO user (id, username, actor_id, display_name)
VALUES (?1, ?2, ?3, ?4)
ON CONFLICT(actor_id) DO NOTHING
2025-04-11 12:29:29 +01:00
"#,
uuid,
username,
user.id,
user.preferred_username
)
.execute(&mut **db)
.await
.unwrap();
status::Accepted(format!("https://ferri.amy.mov/users/{}", uuid))
2025-04-11 12:29:29 +01:00
}
pub async fn resolve_user(acct: &str, host: &str) -> types::Person {
let client = reqwest::Client::new();
let url = format!(
"https://{}/.well-known/webfinger?resource=acct:{}",
host, acct
);
let wf = client
.get(url)
.send()
.await
.unwrap()
.json::<webfinger::WebfingerResponse>()
.await
.unwrap();
let actor_link = wf
.links
.iter()
.find(|l| l.ty == Some("application/activity+json".to_string()))
.unwrap();
let href = actor_link.href.as_ref().unwrap();
client
.get(href)
.header("Accept", "application/activity+json")
.send()
.await
.unwrap()
.json::<types::Person>()
.await
.unwrap()
}
#[get("/test")]
pub async fn test(
outbound: &State<OutboundQueue>,
mut db: Connection<Db>
) -> &'static str {
use main::types_rewrite::{ObjectUuid, fetch, api};
2025-04-26 12:44:45 +01:00
outbound.0.send(ap::QueueMessage::Heartbeat);
2025-04-11 12:29:29 +01:00
let id = ObjectUuid("9b9d497b-2731-435f-a929-e609ca69dac9".to_string());
let user= dbg!(fetch::user_by_id(id, &mut **db).await.unwrap());
let apu: api::Account = user.into();
dbg!(apu);
2025-04-11 12:29:29 +01:00
"Hello, world!"
2025-04-12 15:16:40 +01:00
}