Add include_reposts parameter to get_posts_by_author() func

This commit is contained in:
silverpill 2022-04-13 20:27:38 +00:00
parent 78d66f8a06
commit 56d1ebf9e5
3 changed files with 38 additions and 18 deletions

View file

@ -148,29 +148,28 @@ async fn outbox(
};
let db_client = &**get_database_client(&db_pool).await?;
let user = get_user_by_name(db_client, &username).await?;
// Post are ordered by creation date
// Posts are ordered by creation date
// TODO: include reposts
let posts = get_posts_by_author(
db_client,
&user.id,
None, // include only public posts
false,
false, // exclude replies
false, // exclude reposts
None, COLLECTION_PAGE_SIZE,
).await?;
// TODO: include reposts
let activities: Vec<_> = posts.iter().filter_map(|post| {
match post.repost_of_id {
Some(_) => None,
None => {
// Replies are not included so post.in_reply_to
// does not need to be populated
let activity = create_activity_note(
&instance.host(),
&instance.url(),
post,
);
Some(activity)
},
}
if post.in_reply_to_id.is_some() || post.repost_of_id.is_some() {
return None;
};
// Replies are not included so post.in_reply_to
// does not need to be populated
let activity = create_activity_note(
&instance.host(),
&instance.url(),
post,
);
Some(activity)
}).collect();
let collection_page = OrderedCollectionPage::new(
first_page_id,

View file

@ -368,11 +368,13 @@ async fn get_account_statuses(
return Ok(HttpResponse::Ok().json(statuses));
};
let profile = get_profile_by_id(db_client, &account_id).await?;
// Include reposts but not replies
let mut posts = get_posts_by_author(
db_client,
&profile.id,
maybe_current_user.as_ref().map(|user| &user.id),
false,
true,
query_params.max_id,
query_params.limit,
).await?;

View file

@ -378,6 +378,7 @@ pub async fn get_posts_by_author(
profile_id: &Uuid,
current_user_id: Option<&Uuid>,
include_replies: bool,
include_reposts: bool,
max_post_id: Option<Uuid>,
limit: i64,
) -> Result<Vec<Post>, DatabaseError> {
@ -390,6 +391,9 @@ pub async fn get_posts_by_author(
if !include_replies {
condition.push_str(" AND post.in_reply_to_id IS NULL");
};
if !include_reposts {
condition.push_str(" AND post.repost_of_id IS NULL");
};
let statement = format!(
"
SELECT
@ -1135,15 +1139,30 @@ mod tests {
..Default::default()
};
let post_4 = create_post(db_client, &user.id, post_data_4).await.unwrap();
// Reply
let reply_data = PostCreateData {
content: "my reply".to_string(),
in_reply_to_id: Some(post_1.id.clone()),
..Default::default()
};
let reply = create_post(db_client, &user.id, reply_data).await.unwrap();
// Repost
let repost_data = PostCreateData {
repost_of_id: Some(reply.id.clone()),
..Default::default()
};
let repost = create_post(db_client, &user.id, repost_data).await.unwrap();
// Anonymous viewer
let timeline = get_posts_by_author(
db_client, &user.id, None, false, None, 10
db_client, &user.id, None, false, true, None, 10
).await.unwrap();
assert_eq!(timeline.len(), 1);
assert_eq!(timeline.len(), 2);
assert_eq!(timeline.iter().any(|post| post.id == post_1.id), true);
assert_eq!(timeline.iter().any(|post| post.id == post_2.id), false);
assert_eq!(timeline.iter().any(|post| post.id == post_3.id), false);
assert_eq!(timeline.iter().any(|post| post.id == post_4.id), false);
assert_eq!(timeline.iter().any(|post| post.id == reply.id), false);
assert_eq!(timeline.iter().any(|post| post.id == repost.id), true);
}
}