feat: timeline cleanup

This commit is contained in:
nullishamy 2025-05-05 15:54:23 +01:00
parent 8cf7834cfe
commit 90be7d570e
Signed by: amy
SSH key fingerprint: SHA256:WmV0uk6WgAQvDJlM8Ld4mFPHZo02CLXXP5VkwQ5xtyk
12 changed files with 372 additions and 326 deletions

View file

@ -87,9 +87,9 @@ impl<'a> HttpWrapper<'a> {
let http_result = self
.client
.post(inbox)
.sign(self.key_id)
.json(activity)
.activity()
.json(activity)
.sign(self.key_id)
.send()
.await;

View file

@ -40,6 +40,14 @@ pub async fn handle_inbox_request(
let follower = http.get_person(&activity.actor).await.unwrap();
let actor = db::Actor {
id: follower.obj.id.clone(),
inbox: follower.inbox.clone(),
outbox: follower.outbox.clone()
};
make::new_actor(actor, &mut conn).await.unwrap();
let follow = db::Follow {
id: ObjectUri(
format!("https://ferri.amy.mov/activities/{}", crate::new_id())
@ -117,16 +125,29 @@ pub async fn handle_inbox_request(
.await
.unwrap();
let attachments = post.attachment
.into_iter()
.map(|at| {
db::Attachment {
id: ObjectUuid(crate::new_id()),
post_id: ObjectUuid(post_id.clone()),
url: at.url,
media_type: Some(at.media_type),
sensitive: at.sensitive,
alt: at.summary
}
})
.collect::<Vec<_>>();
let post = db::Post {
id: ObjectUuid(post_id),
uri: post.obj.id,
user,
content: post.content,
created_at,
attachments: vec![],
attachments,
boosted_post: None
};
make::new_post(post, &mut conn)
.await
@ -288,8 +309,7 @@ pub async fn handle_inbox_request(
attachments: vec![],
content: String::new(),
created_at,
boosted_post: Some(boosted_post.id.clone())
boosted_post: Some(Box::new(boosted_post.clone()))
}
};

View file

@ -1,13 +1,14 @@
use serde::{Deserialize, Serialize};
use tracing::info;
use std::fmt::Debug;
use crate::{ap::http::HttpClient, federation::http::HttpWrapper, types::{ap, ObjectContext}};
use crate::{ap::http::HttpClient, federation::http::HttpWrapper, types::{ap::{self, ActivityType}, as_context, db, Object, ObjectContext, ObjectUri}};
#[derive(Debug)]
pub enum OutboxRequest {
// FIXME: Make the String (key_id) nicer
// Probably store it in the DB and pass a db::User here
Accept(ap::AcceptActivity, String, ap::Person)
Accept(ap::AcceptActivity, String, ap::Person),
Status(db::Post, String)
}
#[derive(Serialize, Deserialize, Debug)]
@ -49,5 +50,45 @@ pub async fn handle_outbox_request(
info!("accept res {}", res);
},
OutboxRequest::Status(post, key_id) => {
// FIXME: Take a list of who we should send to
// for now we only propogate to my main instance
let http = HttpWrapper::new(http, &key_id);
let activity = PreparedActivity {
context: as_context(),
id: format!("https://ferri.amy.mov/activities/{}", crate::new_id()),
ty: ActivityType::Create,
actor: post.user.actor.id.0.clone(),
object: ap::Post {
obj: Object {
id: ObjectUri(
format!(
"https://ferri.amy.mov/users/{}/posts/{}",
post.user.id.0,
post.id.0
)
),
context: as_context()
},
ty: ActivityType::Note,
ts: post.created_at.to_rfc3339(),
content: post.content,
to: vec![format!("https://ferri.amy.mov/users/{}/followers", post.user.id.0)],
cc: vec!["https://www.w3.org/ns/activitystreams#Public".to_string()],
attachment: vec![],
attributed_to: Some(post.user.actor.id.0)
},
published: crate::ap::new_ts()
};
let res = http
.post_activity("https://fedi.amy.mov/users/9zkygethkdw60001/inbox", activity)
.await
.unwrap();
info!("status res {}", res);
}
}
}

View file

@ -4,6 +4,8 @@ use crate::types::db;
use crate::types::{Object, ObjectUri, as_context};
use super::ap::ActivityType;
impl From<db::Actor> for ap::Actor {
fn from(val: db::Actor) -> ap::Actor {
ap::Actor {
@ -67,6 +69,7 @@ impl From<db::User> for ap::Person {
context: as_context(),
id: ObjectUri(format!("https://ferri.amy.mov/users/{}", val.id.0)),
},
ty: ActivityType::Person,
following: format!("https://ferri.amy.mov/users/{}/following", val.id.0),
followers: format!("https://ferri.amy.mov/users/{}/followers", val.id.0),
summary: format!("ferri {}", val.username),
@ -105,10 +108,23 @@ impl From<db::Post> for api::Status {
muted: false,
bookmarked: false,
content: value.content,
reblog: None,
reblog: value.boosted_post.map(|p| {
// Probably a better way to do this without reboxing but whatever...
let p: db::Post = *p;
let p: api::Status = p.into();
Box::new(p)
}),
application: None,
account: value.user.into(),
media_attachments: vec![],
media_attachments: value.attachments
.into_iter()
.map(|at| api::StatusAttachment {
id: at.id,
ty: "image".to_string(),
url: at.url,
description: at.alt.unwrap_or(String::new())
})
.collect(),
mentions: vec![],
tags: vec![],
emojis: vec![],

View file

@ -187,6 +187,34 @@ pub async fn user_by_actor_uri(uri: ObjectUri, conn: &mut SqliteConnection) -> R
})
}
pub async fn attachments_for_post(
post_id: ObjectUuid,
conn: &mut SqliteConnection
)-> Result<Vec<db::Attachment>, DbError> {
let attachments = sqlx::query!(
"SELECT * FROM attachment WHERE post_id = ?",
post_id.0
)
.fetch_all(&mut *conn)
.await
.unwrap();
let attachments = attachments.into_iter()
.map(|at| {
db::Attachment {
id: ObjectUuid(at.id),
post_id: ObjectUuid(at.post_id),
url: at.url,
media_type: Some(at.media_type),
sensitive: at.marked_sensitive,
alt: at.alt
}
})
.collect::<Vec<_>>();
Ok(attachments)
}
pub async fn posts_for_user_id(
id: ObjectUuid,
conn: &mut SqliteConnection
@ -209,26 +237,9 @@ pub async fn posts_for_user_id(
.unwrap();
for record in posts {
let attachments = sqlx::query!(
"SELECT * FROM attachment WHERE post_id = ?",
record.post_id
)
.fetch_all(&mut *conn)
let attachments = attachments_for_post(ObjectUuid(record.post_id.clone()), conn)
.await
.unwrap();
let attachments = attachments.into_iter()
.map(|at| {
db::Attachment {
id: ObjectUuid(at.id),
post_id: ObjectUuid(at.post_id),
url: at.url,
media_type: Some(at.media_type),
sensitive: at.marked_sensitive,
alt: at.alt
}
})
.collect::<Vec<_>>();
let user_created = parse_ts(record.user_created)
.expect("no db corruption");
@ -263,3 +274,120 @@ pub async fn posts_for_user_id(
Ok(out)
}
pub async fn home_timeline(
actor: ObjectUri,
conn: &mut SqliteConnection
) -> Result<Vec<db::Post>, DbError> {
#[derive(sqlx::FromRow, Debug, Clone)]
struct Post {
is_boost_source: bool,
post_id: String,
user_id: String,
post_uri: String,
content: String,
post_created: String,
user_created: String,
actor_id: String,
acct: String,
remote: bool,
boosted_post_id: Option<String>,
display_name: String,
username: String,
icon_url: String,
user_url: String,
inbox: String,
outbox: String
}
fn make_into_db(p: Post, attachments: Vec<db::Attachment>) -> db::Post {
db::Post {
id: ObjectUuid(p.post_id),
uri: ObjectUri(p.post_uri),
user: db::User {
id: ObjectUuid(p.user_id),
actor: db::Actor {
id: ObjectUri(p.actor_id),
inbox: p.inbox,
outbox: p.outbox
},
username: p.username,
display_name: p.display_name,
acct: p.acct,
remote: p.remote,
url: p.user_url,
created_at: parse_ts(p.user_created).unwrap(),
icon_url: p.icon_url,
posts: db::UserPosts {
last_post_at: None
}
},
content: p.content,
created_at: parse_ts(p.post_created).unwrap(),
boosted_post: None,
attachments
}
}
// 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 as "post_created", p.boosted_post_id, u.icon_url, u.url as "user_url",
a.inbox, a.outbox, u.acct, u.remote, u.created_at as "user_created"
FROM get_home_timeline_with_boosts
JOIN post p ON p.id = get_home_timeline_with_boosts.id
JOIN actor a ON u.actor_id = a.id
JOIN user u ON u.id = p.user_id;
"#,
)
.bind(actor.0)
.fetch_all(&mut *conn)
.await
.unwrap();
let mut out = vec![];
for post in posts.iter() {
let boost_id = post.boosted_post_id.clone();
let is_boost_base = post.is_boost_source;
let attachments = attachments_for_post(ObjectUuid(post.post_id.clone()), &mut *conn)
.await
.unwrap();
let mut base = make_into_db(post.clone(), attachments);
if let Some(boost_id) = boost_id {
let attachments = attachments_for_post(ObjectUuid(boost_id.clone()), &mut *conn)
.await
.unwrap();
let boost = posts.iter().find(|p| p.post_id == boost_id).unwrap();
let boost = make_into_db(boost.clone(), attachments);
base.boosted_post = Some(Box::new(boost));
}
if !is_boost_base {
out.push(base);
}
}
Ok(out)
}

View file

@ -96,7 +96,7 @@ pub async fn new_post(
conn: &mut SqliteConnection,
) -> Result<db::Post, DbError> {
let ts = post.created_at.to_rfc3339();
let boosted = post.boosted_post.as_ref().map(|b| &b.0);
let boosted = post.boosted_post.as_ref().map(|b| &b.id.0);
sqlx::query!(
r#"

View file

@ -1,4 +1,4 @@
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Deserializer, Serialize};
use std::fmt::Debug;
use thiserror::Error;
use uuid::Uuid;
@ -7,6 +7,15 @@ pub mod convert;
pub mod get;
pub mod make;
fn deserialize_null_default<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
T: Default + Deserialize<'de>,
D: Deserializer<'de>,
{
let opt = Option::deserialize(deserializer)?;
Ok(opt.unwrap_or_default())
}
#[derive(Debug, Error)]
pub enum DbError {
#[error("an unknown error occured when creating: {0}")]
@ -115,7 +124,7 @@ pub mod db {
pub user: User,
pub content: String,
pub created_at: DateTime<Utc>,
pub boosted_post: Option<ObjectUuid>,
pub boosted_post: Option<Box<Post>>,
pub attachments: Vec<Attachment>
}
}
@ -127,12 +136,14 @@ pub mod ap {
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
pub enum ActivityType {
Reject,
Create,
Note,
Delete,
Undo,
Accept,
Announce,
Person,
Like,
Follow,
}
@ -227,7 +238,9 @@ pub mod ap {
pub media_type: String,
pub url: String,
#[serde(deserialize_with = "deserialize_null_default")]
pub name: String,
pub summary: Option<String>,
#[serde(default)]
pub sensitive: bool
@ -287,6 +300,9 @@ pub mod ap {
#[serde(flatten)]
pub obj: Object,
#[serde(rename = "type")]
pub ty: ActivityType,
pub following: String,
pub followers: String,
@ -371,6 +387,16 @@ pub mod api {
pub links: Vec<WebfingerLink>,
}
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
pub struct StatusAttachment {
pub id: ObjectUuid,
#[serde(rename = "type")]
pub ty: String,
pub url: String,
pub description: String
}
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
pub struct Status {
pub id: ObjectUuid,
@ -394,7 +420,7 @@ pub mod api {
pub reblog: Option<Box<Status>>,
pub application: Option<()>,
pub account: Account,
pub media_attachments: Vec<Option<()>>,
pub media_attachments: Vec<StatusAttachment>,
pub mentions: Vec<Option<()>>,
pub tags: Vec<Option<()>>,
pub emojis: Vec<Option<()>>,