mirror of
https://github.com/nullishamy/ferri.git
synced 2025-06-28 00:54:17 +00:00
feat: timeline cleanup
This commit is contained in:
parent
8cf7834cfe
commit
90be7d570e
12 changed files with 372 additions and 326 deletions
|
@ -1,5 +1,4 @@
|
|||
use crate::timeline::TimelineStatus;
|
||||
use main::ap::{self, http::HttpClient};
|
||||
use main::{config::Config, federation::{outbox::OutboxRequest, QueueMessage}, types::{api, make, ObjectUri, ObjectUuid}};
|
||||
use rocket::{
|
||||
FromForm, State,
|
||||
form::Form,
|
||||
|
@ -7,22 +6,15 @@ use rocket::{
|
|||
serde::{Deserialize, Serialize, json::Json},
|
||||
};
|
||||
use rocket_db_pools::Connection;
|
||||
use uuid::Uuid;
|
||||
use main::types::db;
|
||||
|
||||
use crate::api::user::CredentialAcount;
|
||||
use crate::{AuthenticatedUser, Db};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, FromForm)]
|
||||
#[serde(crate = "rocket::serde")]
|
||||
pub struct Status {
|
||||
status: String,
|
||||
}
|
||||
use crate::{AuthenticatedUser, Db, OutboundQueue};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, FromForm)]
|
||||
#[serde(crate = "rocket::serde")]
|
||||
pub struct StatusContext {
|
||||
ancestors: Vec<Status>,
|
||||
descendants: Vec<Status>,
|
||||
ancestors: Vec<CreateStatus>,
|
||||
descendants: Vec<CreateStatus>,
|
||||
}
|
||||
|
||||
#[get("/statuses/<_status>/context")]
|
||||
|
@ -37,114 +29,64 @@ pub async fn status_context(
|
|||
})
|
||||
}
|
||||
|
||||
async fn create_status(
|
||||
user: AuthenticatedUser,
|
||||
mut db: Connection<Db>,
|
||||
http: &HttpClient,
|
||||
status: &Status,
|
||||
) -> TimelineStatus {
|
||||
let user = ap::User::from_id(&user.id.0, &mut **db).await.unwrap();
|
||||
let outbox = ap::Outbox::for_user(user.clone(), http);
|
||||
#[derive(Serialize, Deserialize, Debug, FromForm)]
|
||||
#[serde(crate = "rocket::serde")]
|
||||
pub struct CreateStatus {
|
||||
status: String,
|
||||
}
|
||||
|
||||
let post_id = ap::new_id();
|
||||
let now = ap::now();
|
||||
|
||||
let post = ap::Post::from_parts(post_id, status.status.clone(), user.clone())
|
||||
.to(format!("{}/followers", user.uri()))
|
||||
.cc("https://www.w3.org/ns/activitystreams#Public".to_string());
|
||||
|
||||
post.save(&mut **db).await;
|
||||
|
||||
let actor = sqlx::query!(
|
||||
"SELECT * FROM actor WHERE id = ?1",
|
||||
"https://fedi.amy.mov/users/9zkygethkdw60001"
|
||||
)
|
||||
.fetch_one(&mut **db)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let create_id = format!("https://ferri.amy.mov/activities/{}", Uuid::new_v4());
|
||||
|
||||
let activity = ap::Activity {
|
||||
id: create_id,
|
||||
ty: ap::ActivityType::Create,
|
||||
object: post.clone().to_ap(),
|
||||
to: vec![format!("{}/followers", user.uri())],
|
||||
published: now,
|
||||
cc: vec!["https://www.w3.org/ns/activitystreams#Public".to_string()],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let actor = ap::Actor::from_raw(actor.id.clone(), actor.inbox.clone(), actor.outbox.clone());
|
||||
|
||||
let req = ap::OutgoingActivity {
|
||||
req: activity,
|
||||
signed_by: format!("{}#main-key", user.uri()),
|
||||
to: actor,
|
||||
};
|
||||
|
||||
req.save(&mut **db).await;
|
||||
outbox.post(req).await;
|
||||
|
||||
TimelineStatus {
|
||||
id: post.id().to_string(),
|
||||
created_at: post.created_at(),
|
||||
in_reply_to_id: None,
|
||||
in_reply_to_account_id: None,
|
||||
content: post.content().to_string(),
|
||||
visibility: "public".to_string(),
|
||||
spoiler_text: "".to_string(),
|
||||
sensitive: false,
|
||||
uri: post.uri(),
|
||||
url: post.uri(),
|
||||
replies_count: 0,
|
||||
reblogs_count: 0,
|
||||
favourites_count: 0,
|
||||
favourited: false,
|
||||
reblogged: false,
|
||||
reblog: None,
|
||||
muted: false,
|
||||
bookmarked: false,
|
||||
media_attachments: vec![],
|
||||
account: CredentialAcount {
|
||||
id: user.id().to_string(),
|
||||
username: user.username().to_string(),
|
||||
acct: user.username().to_string(),
|
||||
display_name: user.display_name().to_string(),
|
||||
locked: false,
|
||||
bot: false,
|
||||
created_at: "2025-04-10T22:12:09Z".to_string(),
|
||||
attribution_domains: vec![],
|
||||
note: "".to_string(),
|
||||
url: user.uri(),
|
||||
avatar: "https://ferri.amy.mov/assets/pfp.png".to_string(),
|
||||
avatar_static: "https://ferri.amy.mov/assets/pfp.png".to_string(),
|
||||
header: "https://ferri.amy.mov/assets/pfp.png".to_string(),
|
||||
header_static: "https://ferri.amy.mov/assets/pfp.png".to_string(),
|
||||
followers_count: 1,
|
||||
following_count: 1,
|
||||
statuses_count: 1,
|
||||
last_status_at: "2025-04-10T22:14:34Z".to_string(),
|
||||
},
|
||||
fn to_db_post(req: &CreateStatus, user: &AuthenticatedUser, config: &Config) -> db::Post {
|
||||
let post_id = main::new_id();
|
||||
|
||||
db::Post {
|
||||
id: ObjectUuid(post_id.clone()),
|
||||
uri: ObjectUri(config.post_url(&user.id.0, &post_id)),
|
||||
user: user.user.clone(),
|
||||
content: req.status.clone(),
|
||||
created_at: main::ap::now(),
|
||||
boosted_post: None,
|
||||
attachments: vec![]
|
||||
}
|
||||
}
|
||||
|
||||
#[post("/statuses", data = "<status>")]
|
||||
pub async fn new_status(
|
||||
db: Connection<Db>,
|
||||
mut db: Connection<Db>,
|
||||
helpers: &State<crate::Helpers>,
|
||||
status: Form<Status>,
|
||||
status: Form<CreateStatus>,
|
||||
user: AuthenticatedUser,
|
||||
) -> Json<TimelineStatus> {
|
||||
Json(create_status(user, db, &helpers.http, &status).await)
|
||||
}
|
||||
) -> Json<api::Status> {
|
||||
let post = make::new_post(
|
||||
to_db_post(&status, &user, &helpers.config),
|
||||
&mut **db
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Json(post.into())
|
||||
}
|
||||
|
||||
#[post("/statuses", data = "<status>", rank = 2)]
|
||||
pub async fn new_status_json(
|
||||
db: Connection<Db>,
|
||||
mut db: Connection<Db>,
|
||||
helpers: &State<crate::Helpers>,
|
||||
status: Json<Status>,
|
||||
outbound: &State<OutboundQueue>,
|
||||
status: Json<CreateStatus>,
|
||||
user: AuthenticatedUser,
|
||||
) -> Json<TimelineStatus> {
|
||||
Json(create_status(user, db, &helpers.http, &status).await)
|
||||
) -> Json<api::Status> {
|
||||
let post = make::new_post(
|
||||
to_db_post(&status, &user, &helpers.config),
|
||||
&mut **db
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
|
||||
let key_id = "https://ferri.amy.mov/users/9b9d497b-2731-435f-a929-e609ca69dac9#main-key";
|
||||
outbound.0.send(QueueMessage::Outbound(
|
||||
OutboxRequest::Status(post.clone(), key_id.to_string()))
|
||||
)
|
||||
.await;
|
||||
|
||||
Json(post.into())
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use crate::{AuthenticatedUser, Db, endpoints::api::user::CredentialAcount};
|
||||
use main::types::ObjectUuid;
|
||||
use main::types::{api, get, ObjectUuid};
|
||||
use rocket::{
|
||||
get,
|
||||
serde::{Deserialize, Serialize, json::Json},
|
||||
|
@ -47,166 +47,10 @@ pub struct TimelineStatus {
|
|||
pub async fn home(
|
||||
mut db: Connection<Db>,
|
||||
user: AuthenticatedUser,
|
||||
) -> Json<Vec<TimelineStatus>> {
|
||||
#[derive(sqlx::FromRow, Debug)]
|
||||
struct Post {
|
||||
is_boost_source: bool,
|
||||
post_id: String,
|
||||
user_id: String,
|
||||
post_uri: String,
|
||||
content: String,
|
||||
created_at: String,
|
||||
boosted_post_id: Option<String>,
|
||||
display_name: String,
|
||||
username: String,
|
||||
icon_url: String,
|
||||
user_url: String
|
||||
}
|
||||
) -> Json<Vec<api::Status>> {
|
||||
let posts = get::home_timeline(user.actor_id, &mut **db)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// FIXME: query! can't cope with this. returns a type error
|
||||
let posts = sqlx::query_as::<_, Post>(
|
||||
r#"
|
||||
WITH RECURSIVE get_home_timeline_with_boosts(
|
||||
id, boosted_post_id, is_boost_source
|
||||
) AS
|
||||
(
|
||||
SELECT p.id, p.boosted_post_id, 0 as is_boost_source
|
||||
FROM post p
|
||||
WHERE p.user_id IN (
|
||||
SELECT u.id
|
||||
FROM follow f
|
||||
INNER JOIN user u ON u.actor_id = f.followed_id
|
||||
WHERE f.follower_id = $1
|
||||
)
|
||||
UNION
|
||||
SELECT p.id, p.boosted_post_id, 1 as is_boost_source
|
||||
FROM post p
|
||||
JOIN get_home_timeline_with_boosts tl ON tl.boosted_post_id = p.id
|
||||
)
|
||||
SELECT is_boost_source, p.id as "post_id", u.id as "user_id",
|
||||
p.content, p.uri as "post_uri", u.username, u.display_name,
|
||||
u.actor_id, p.created_at, p.boosted_post_id, u.icon_url, u.url as "user_url"
|
||||
FROM get_home_timeline_with_boosts
|
||||
JOIN post p ON p.id = get_home_timeline_with_boosts.id
|
||||
JOIN user u ON u.id = p.user_id;
|
||||
"#,
|
||||
)
|
||||
.bind(user.actor_id.0)
|
||||
.fetch_all(&mut **db)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let mut out = Vec::<TimelineStatus>::new();
|
||||
for record in posts.iter() {
|
||||
let mut boost: Option<Box<TimelineStatus>> = None;
|
||||
if let Some(ref boosted_id) = record.boosted_post_id {
|
||||
let record = posts.iter().find(|p| &p.post_id == boosted_id).unwrap();
|
||||
let attachments = sqlx::query!(
|
||||
"SELECT * FROM attachment WHERE post_id = ?1",
|
||||
boosted_id
|
||||
)
|
||||
.fetch_all(&mut **db)
|
||||
.await
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|at| {
|
||||
TimelineStatusAttachment {
|
||||
id: ObjectUuid(at.id),
|
||||
url: at.url,
|
||||
ty: "image".to_string(),
|
||||
description: at.alt.unwrap_or(String::new())
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
boost = Some(Box::new(TimelineStatus {
|
||||
id: record.post_id.clone(),
|
||||
created_at: record.created_at.clone(),
|
||||
in_reply_to_id: None,
|
||||
in_reply_to_account_id: None,
|
||||
content: record.content.clone(),
|
||||
visibility: "public".to_string(),
|
||||
spoiler_text: "".to_string(),
|
||||
sensitive: false,
|
||||
uri: record.post_uri.clone(),
|
||||
url: record.post_uri.clone(),
|
||||
replies_count: 0,
|
||||
reblogs_count: 0,
|
||||
favourites_count: 0,
|
||||
favourited: false,
|
||||
reblogged: false,
|
||||
reblog: boost,
|
||||
muted: false,
|
||||
bookmarked: false,
|
||||
media_attachments: attachments,
|
||||
account: CredentialAcount {
|
||||
id: record.user_id.clone(),
|
||||
username: record.username.clone(),
|
||||
acct: record.username.clone(),
|
||||
display_name: record.display_name.clone(),
|
||||
locked: false,
|
||||
bot: false,
|
||||
created_at: "2025-04-10T22:12:09Z".to_string(),
|
||||
attribution_domains: vec![],
|
||||
note: "".to_string(),
|
||||
url: record.user_url.clone(),
|
||||
avatar: record.icon_url.clone(),
|
||||
avatar_static: record.icon_url.clone(),
|
||||
header: record.icon_url.clone(),
|
||||
header_static: record.icon_url.clone(),
|
||||
followers_count: 1,
|
||||
following_count: 1,
|
||||
statuses_count: 1,
|
||||
last_status_at: "2025-04-10T22:14:34Z".to_string(),
|
||||
},
|
||||
}))
|
||||
}
|
||||
|
||||
// Don't send the empty boost source posts
|
||||
if !record.is_boost_source {
|
||||
out.push(TimelineStatus {
|
||||
id: record.post_id.clone(),
|
||||
created_at: record.created_at.clone(),
|
||||
in_reply_to_id: None,
|
||||
in_reply_to_account_id: None,
|
||||
content: record.content.clone(),
|
||||
visibility: "public".to_string(),
|
||||
spoiler_text: "".to_string(),
|
||||
sensitive: false,
|
||||
uri: record.post_uri.clone(),
|
||||
url: record.post_uri.clone(),
|
||||
replies_count: 0,
|
||||
reblogs_count: 0,
|
||||
favourites_count: 0,
|
||||
favourited: false,
|
||||
reblogged: false,
|
||||
reblog: boost,
|
||||
muted: false,
|
||||
bookmarked: false,
|
||||
media_attachments: vec![],
|
||||
account: CredentialAcount {
|
||||
id: record.user_id.clone(),
|
||||
username: record.username.clone(),
|
||||
acct: record.username.clone(),
|
||||
display_name: record.display_name.clone(),
|
||||
locked: false,
|
||||
bot: false,
|
||||
created_at: "2025-04-10T22:12:09Z".to_string(),
|
||||
attribution_domains: vec![],
|
||||
note: "".to_string(),
|
||||
url: record.user_url.clone(),
|
||||
avatar: record.icon_url.clone(),
|
||||
avatar_static: record.icon_url.clone(),
|
||||
header: record.icon_url.clone(),
|
||||
header_static: record.icon_url.clone(),
|
||||
followers_count: 1,
|
||||
following_count: 1,
|
||||
statuses_count: 1,
|
||||
last_status_at: "2025-04-10T22:14:34Z".to_string(),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Json(out)
|
||||
Json(posts.into_iter().map(Into::into).collect())
|
||||
}
|
||||
|
|
|
@ -5,10 +5,10 @@ use main::{
|
|||
},
|
||||
types::{ap, get, ObjectUuid}
|
||||
};
|
||||
use rocket::{State, post, serde::json::serde_json};
|
||||
use rocket::{post, response::Redirect, serde::json::serde_json, State};
|
||||
use rocket_db_pools::Connection;
|
||||
use serde::de::DeserializeOwned;
|
||||
use tracing::{debug, event, span, warn, Instrument, Level};
|
||||
use tracing::{debug, event, info, span, warn, Instrument, Level};
|
||||
|
||||
use crate::{Db, InboundQueue, OutboundQueue};
|
||||
|
||||
|
@ -23,7 +23,13 @@ pub async fn inbox(
|
|||
outbound: &State<OutboundQueue>,
|
||||
user_uuid: &str,
|
||||
body: String
|
||||
) {
|
||||
) -> Result<(), Redirect> {
|
||||
if user_uuid == "amy" {
|
||||
return Err(Redirect::permanent(
|
||||
"https://ferri.amy.mov/users/9b9d497b-2731-435f-a929-e609ca69dac9/inbox",
|
||||
));
|
||||
}
|
||||
|
||||
let user = get::user_by_id(
|
||||
ObjectUuid(user_uuid.to_string()),
|
||||
&mut db
|
||||
|
@ -65,6 +71,7 @@ pub async fn inbox(
|
|||
queue.0.send(msg).await;
|
||||
}
|
||||
ap::ActivityType::Create => {
|
||||
info!("{}", body);
|
||||
let activity = deser::<ap::CreateActivity>(&body);
|
||||
let msg = QueueMessage::Inbound(
|
||||
InboxRequest::Create(activity, user, conn)
|
||||
|
@ -94,5 +101,7 @@ pub async fn inbox(
|
|||
}
|
||||
}
|
||||
.instrument(span)
|
||||
.await;
|
||||
.await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -7,31 +7,39 @@ use rocket::{
|
|||
use rocket_db_pools::Connection;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use main::types::{Object, ObjectUri, ObjectUuid, ap, as_context, get};
|
||||
use main::types::{ap, as_context, get, Object, ObjectContext, ObjectUri, ObjectUuid};
|
||||
|
||||
use super::activity_type;
|
||||
use crate::Db;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct OrderedCollection {
|
||||
#[serde(rename = "@context")]
|
||||
context: ObjectContext,
|
||||
#[serde(rename = "type")]
|
||||
ty: String,
|
||||
id: String,
|
||||
total_items: i64,
|
||||
ordered_items: Vec<String>,
|
||||
}
|
||||
|
||||
#[get("/users/<_user>/inbox")]
|
||||
pub async fn inbox(_user: String) -> Json<OrderedCollection> {
|
||||
#[get("/users/<user>/inbox")]
|
||||
pub async fn inbox(user: String) -> Json<OrderedCollection> {
|
||||
Json(OrderedCollection {
|
||||
context: as_context(),
|
||||
ty: "OrderedCollection".to_string(),
|
||||
id: format!("https://ferri.amy.mov/users/{}/inbox", user),
|
||||
total_items: 0,
|
||||
ordered_items: vec![],
|
||||
})
|
||||
}
|
||||
|
||||
#[get("/users/<_user>/outbox")]
|
||||
pub async fn outbox(_user: String) -> Json<OrderedCollection> {
|
||||
#[get("/users/<user>/outbox")]
|
||||
pub async fn outbox(user: String) -> Json<OrderedCollection> {
|
||||
Json(OrderedCollection {
|
||||
context: as_context(),
|
||||
ty: "OrderedCollection".to_string(),
|
||||
id: format!("https://ferri.amy.mov/users/{}/outbox", user),
|
||||
total_items: 0,
|
||||
ordered_items: vec![],
|
||||
})
|
||||
|
@ -41,7 +49,7 @@ pub async fn outbox(_user: String) -> Json<OrderedCollection> {
|
|||
pub async fn followers(
|
||||
mut db: Connection<Db>,
|
||||
uuid: &str,
|
||||
) -> Result<Json<OrderedCollection>, NotFound<String>> {
|
||||
) -> Result<ActivityResponse<Json<OrderedCollection>>, NotFound<String>> {
|
||||
let target = main::ap::User::from_id(uuid, &mut **db)
|
||||
.await
|
||||
.map_err(|e| NotFound(e.to_string()))?;
|
||||
|
@ -59,9 +67,11 @@ pub async fn followers(
|
|||
.await
|
||||
.unwrap();
|
||||
|
||||
Ok(Json(OrderedCollection {
|
||||
ap_ok(Json(OrderedCollection {
|
||||
context: as_context(),
|
||||
ty: "OrderedCollection".to_string(),
|
||||
total_items: 1,
|
||||
id: format!("https://ferri.amy.mov/users/{}/followers", uuid),
|
||||
ordered_items: followers
|
||||
.into_iter()
|
||||
.map(|f| f.follower_id)
|
||||
|
@ -73,7 +83,7 @@ pub async fn followers(
|
|||
pub async fn following(
|
||||
mut db: Connection<Db>,
|
||||
uuid: &str,
|
||||
) -> Result<Json<OrderedCollection>, NotFound<String>> {
|
||||
) -> Result<ActivityResponse<Json<OrderedCollection>>, NotFound<String>> {
|
||||
let target = main::ap::User::from_id(uuid, &mut **db)
|
||||
.await
|
||||
.map_err(|e| NotFound(e.to_string()))?;
|
||||
|
@ -91,9 +101,11 @@ pub async fn following(
|
|||
.await
|
||||
.unwrap();
|
||||
|
||||
Ok(Json(OrderedCollection {
|
||||
ap_ok(Json(OrderedCollection {
|
||||
context: as_context(),
|
||||
ty: "OrderedCollection".to_string(),
|
||||
total_items: 1,
|
||||
id: format!("https://ferri.amy.mov/users/{}/following", uuid),
|
||||
ordered_items: following
|
||||
.into_iter()
|
||||
.map(|f| f.followed_id)
|
||||
|
|
|
@ -5,7 +5,7 @@ use endpoints::{
|
|||
|
||||
use tracing_subscriber::fmt;
|
||||
|
||||
use main::{federation, types::{ObjectUri, ObjectUuid}};
|
||||
use main::{federation, types::{db, get, ObjectUri, ObjectUuid}};
|
||||
|
||||
use main::ap::http;
|
||||
use main::config::Config;
|
||||
|
@ -35,10 +35,12 @@ async fn activity_endpoint(_activity: String) {}
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct AuthenticatedUser {
|
||||
pub username: String,
|
||||
pub id: ObjectUuid,
|
||||
pub token: String,
|
||||
pub actor_id: ObjectUri,
|
||||
pub user: db::User,
|
||||
pub username: String,
|
||||
pub token: String,
|
||||
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -70,12 +72,18 @@ impl<'a> FromRequest<'a> for AuthenticatedUser {
|
|||
.await;
|
||||
|
||||
if let Ok(auth) = auth {
|
||||
return Outcome::Success(AuthenticatedUser {
|
||||
token: auth.token,
|
||||
id: ObjectUuid(auth.id),
|
||||
username: auth.display_name,
|
||||
actor_id: ObjectUri(auth.actor_id),
|
||||
});
|
||||
let uid = ObjectUuid(auth.id);
|
||||
let user = get::user_by_id(uid.clone(), &mut **conn).await;
|
||||
|
||||
if let Ok(user) = user {
|
||||
return Outcome::Success(AuthenticatedUser {
|
||||
id: uid,
|
||||
actor_id: ObjectUri(auth.actor_id),
|
||||
user,
|
||||
token: auth.token,
|
||||
username: auth.display_name,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue