Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api endpoint for listing read posts #4934 #5264

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions crates/api_common/src/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ pub struct GetPosts {
#[cfg_attr(feature = "full", ts(optional))]
pub saved_only: Option<bool>,
#[cfg_attr(feature = "full", ts(optional))]
pub read_only: Option<bool>,
#[cfg_attr(feature = "full", ts(optional))]
pub liked_only: Option<bool>,
#[cfg_attr(feature = "full", ts(optional))]
pub disliked_only: Option<bool>,
Expand Down
2 changes: 2 additions & 0 deletions crates/apub/src/api/list_posts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub async fn list_posts(
data.community_id
};
let saved_only = data.saved_only;
let read_only = data.read_only;
let show_hidden = data.show_hidden;
let show_read = data.show_read;
let show_nsfw = data.show_nsfw;
Expand Down Expand Up @@ -78,6 +79,7 @@ pub async fn list_posts(
sort,
community_id,
saved_only,
read_only,
liked_only,
disliked_only,
page,
Expand Down
35 changes: 35 additions & 0 deletions crates/db_views/src/post_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,12 @@ fn queries<'a>() -> Queries<
.filter(post_actions::saved.is_not_null())
.then_order_by(post_actions::saved.desc());
}

if options.read_only.unwrap_or_default() {
query = query
.filter(post_actions::read.is_not_null())
.then_order_by(post_actions::read.desc())
}
// Only hide the read posts, if the saved_only is false. Otherwise ppl with the hide_read
// setting wont be able to see saved posts.
else if !options
Expand Down Expand Up @@ -519,6 +525,7 @@ pub struct PostQuery<'a> {
pub search_term: Option<String>,
pub url_only: Option<bool>,
pub saved_only: Option<bool>,
pub read_only: Option<bool>,
pub liked_only: Option<bool>,
pub disliked_only: Option<bool>,
pub title_only: Option<bool>,
Expand Down Expand Up @@ -1245,6 +1252,34 @@ mod tests {
Ok(())
}

#[test_context(Data)]
#[tokio::test]
#[serial]
async fn post_listing_read_only(data: &mut Data) -> LemmyResult<()> {
let pool = &data.pool();
let pool = &mut pool.into();

// Only mark the bot post as read
// The read_only should only show the bot post
let post_read_form =
PostReadForm::new(data.inserted_bot_post.id, data.local_user_view.person.id);
PostRead::mark_as_read(pool, &post_read_form).await?;

// Only read the post marked as read
let read_read_post_listing = PostQuery {
community_id: Some(data.inserted_community.id),
read_only: Some(true),
..data.default_post_query()
}
.list(&data.site, pool)
.await?;

// This should only include the bot post, not the one you created
assert_eq!(vec![POST_BY_BOT], names(&read_read_post_listing));

Ok(())
}

#[test_context(Data)]
#[tokio::test]
#[serial]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP INDEX idx_post_actions_on_read_read_not_null;

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE INDEX idx_post_actions_on_read_read_not_null ON post_actions (person_id, read, post_id)
WHERE
read IS NOT NULL;