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,19 +148,20 @@ async fn outbox(
}; };
let db_client = &**get_database_client(&db_pool).await?; let db_client = &**get_database_client(&db_pool).await?;
let user = get_user_by_name(db_client, &username).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( let posts = get_posts_by_author(
db_client, db_client,
&user.id, &user.id,
None, // include only public posts None, // include only public posts
false, false, // exclude replies
false, // exclude reposts
None, COLLECTION_PAGE_SIZE, None, COLLECTION_PAGE_SIZE,
).await?; ).await?;
// TODO: include reposts
let activities: Vec<_> = posts.iter().filter_map(|post| { let activities: Vec<_> = posts.iter().filter_map(|post| {
match post.repost_of_id { if post.in_reply_to_id.is_some() || post.repost_of_id.is_some() {
Some(_) => None, return None;
None => { };
// Replies are not included so post.in_reply_to // Replies are not included so post.in_reply_to
// does not need to be populated // does not need to be populated
let activity = create_activity_note( let activity = create_activity_note(
@ -169,8 +170,6 @@ async fn outbox(
post, post,
); );
Some(activity) Some(activity)
},
}
}).collect(); }).collect();
let collection_page = OrderedCollectionPage::new( let collection_page = OrderedCollectionPage::new(
first_page_id, first_page_id,

View file

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

View file

@ -378,6 +378,7 @@ pub async fn get_posts_by_author(
profile_id: &Uuid, profile_id: &Uuid,
current_user_id: Option<&Uuid>, current_user_id: Option<&Uuid>,
include_replies: bool, include_replies: bool,
include_reposts: bool,
max_post_id: Option<Uuid>, max_post_id: Option<Uuid>,
limit: i64, limit: i64,
) -> Result<Vec<Post>, DatabaseError> { ) -> Result<Vec<Post>, DatabaseError> {
@ -390,6 +391,9 @@ pub async fn get_posts_by_author(
if !include_replies { if !include_replies {
condition.push_str(" AND post.in_reply_to_id IS NULL"); 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!( let statement = format!(
" "
SELECT SELECT
@ -1135,15 +1139,30 @@ mod tests {
..Default::default() ..Default::default()
}; };
let post_4 = create_post(db_client, &user.id, post_data_4).await.unwrap(); 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 // Anonymous viewer
let timeline = get_posts_by_author( 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(); ).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_1.id), true);
assert_eq!(timeline.iter().any(|post| post.id == post_2.id), false); 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_3.id), false);
assert_eq!(timeline.iter().any(|post| post.id == post_4.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);
} }
} }