mirror of
https://github.com/nullishamy/ferri.git
synced 2025-04-30 04:39:20 +00:00
feat: ! minor crimes ! timeline query
This commit is contained in:
parent
62931ee20b
commit
d252131e0d
2 changed files with 94 additions and 68 deletions
|
@ -39,36 +39,60 @@ pub async fn home(
|
||||||
config: &State<Config>,
|
config: &State<Config>,
|
||||||
_user: AuthenticatedUser,
|
_user: AuthenticatedUser,
|
||||||
) -> Json<Vec<TimelineStatus>> {
|
) -> Json<Vec<TimelineStatus>> {
|
||||||
let posts = sqlx::query!(
|
#[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
|
||||||
|
}
|
||||||
|
|
||||||
|
let posts = sqlx::query_as::<_, Post>(
|
||||||
r#"
|
r#"
|
||||||
SELECT p.id as "post_id", u.id as "user_id", p.content, p.uri as "post_uri",
|
WITH RECURSIVE get_home_timeline_with_boosts(
|
||||||
u.username, u.display_name, u.actor_id, p.created_at, p.boosted_post_id
|
id, boosted_post_id, is_boost_source
|
||||||
|
) AS
|
||||||
|
(
|
||||||
|
SELECT p.id, p.boosted_post_id, 0 as is_boost_source
|
||||||
FROM post p
|
FROM post p
|
||||||
INNER JOIN user u on p.user_id = u.id
|
WHERE p.user_id IN (
|
||||||
ORDER BY datetime(p.created_at) DESC
|
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
|
||||||
|
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("https://ferri.amy.mov/users/9b9d497b-2731-435f-a929-e609ca69dac9")
|
||||||
.fetch_all(&mut **db)
|
.fetch_all(&mut **db)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let mut out = Vec::<TimelineStatus>::new();
|
dbg!(&posts);
|
||||||
for record in posts {
|
|
||||||
let mut boost: Option<Box<TimelineStatus>> = None;
|
|
||||||
if let Some(boosted_id) = record.boosted_post_id {
|
|
||||||
let record = sqlx::query!(
|
|
||||||
r#"
|
|
||||||
SELECT 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
|
|
||||||
FROM post p
|
|
||||||
INNER JOIN user u on p.user_id = u.id
|
|
||||||
WHERE p.id = ?1
|
|
||||||
"#, boosted_id)
|
|
||||||
.fetch_one(&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 user_uri = config.user_url(&record.user_id);
|
let user_uri = config.user_url(&record.user_id);
|
||||||
|
let record = posts.iter().find(|p| &p.post_id == boosted_id).unwrap();
|
||||||
|
|
||||||
boost = Some(Box::new(TimelineStatus {
|
boost = Some(Box::new(TimelineStatus {
|
||||||
id: record.post_id.clone(),
|
id: record.post_id.clone(),
|
||||||
created_at: record.created_at.clone(),
|
created_at: record.created_at.clone(),
|
||||||
|
@ -112,6 +136,7 @@ pub async fn home(
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !record.is_boost_source {
|
||||||
let user_uri = config.user_web_url(&record.username);
|
let user_uri = config.user_web_url(&record.username);
|
||||||
out.push(TimelineStatus {
|
out.push(TimelineStatus {
|
||||||
id: record.post_id.clone(),
|
id: record.post_id.clone(),
|
||||||
|
@ -155,6 +180,7 @@ pub async fn home(
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Json(out)
|
Json(out)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
CREATE TABLE IF NOT EXISTS post
|
CREATE TABLE IF NOT EXISTS post
|
||||||
(
|
(
|
||||||
id TEXT PRIMARY KEY NOT NULL,
|
id TEXT PRIMARY KEY NOT NULL,
|
||||||
uri TEXT NOT NULL,
|
uri TEXT NOT NULL UNIQUE,
|
||||||
user_id TEXT NOT NULL,
|
user_id TEXT NOT NULL,
|
||||||
content TEXT NOT NULL,
|
content TEXT NOT NULL,
|
||||||
created_at TEXT NOT NULL,
|
created_at TEXT NOT NULL,
|
||||||
|
|
Loading…
Add table
Reference in a new issue