1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 post::{GetPosts, GetPostsResponse},
5 utils::{blocking, check_private_instance, get_local_user_view_from_jwt_opt},
7 use lemmy_apub::{fetcher::resolve_actor_identifier, objects::community::ApubCommunity};
9 source::{community::Community, site::Site},
10 traits::DeleteableOrRemoveable,
13 use lemmy_db_views::post_view::PostQueryBuilder;
14 use lemmy_utils::{ConnectionId, LemmyError};
15 use lemmy_websocket::LemmyContext;
16 use std::str::FromStr;
18 #[async_trait::async_trait(?Send)]
19 impl PerformCrud for GetPosts {
20 type Response = GetPostsResponse;
22 #[tracing::instrument(skip(context, _websocket_id))]
25 context: &Data<LemmyContext>,
26 _websocket_id: Option<ConnectionId>,
27 ) -> Result<GetPostsResponse, LemmyError> {
28 let data: &GetPosts = self;
30 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
33 check_private_instance(&local_user_view, context.pool()).await?;
35 let person_id = local_user_view.to_owned().map(|l| l.person.id);
37 let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
38 let show_bot_accounts = local_user_view
40 .map(|t| t.local_user.show_bot_accounts);
41 let show_read_posts = local_user_view
43 .map(|t| t.local_user.show_read_posts);
46 let listing_type: ListingType = match data.type_ {
49 let site = blocking(context.pool(), Site::read_local_site).await??;
50 ListingType::from_str(&site.default_post_listing_type)?
54 let limit = data.limit;
55 let community_id = data.community_id;
56 let community_actor_id = if let Some(name) = &data.community_name {
57 resolve_actor_identifier::<ApubCommunity, Community>(name, context)
64 let saved_only = data.saved_only;
66 let mut posts = blocking(context.pool(), move |conn| {
67 PostQueryBuilder::create(conn)
68 .listing_type(listing_type)
71 .show_bot_accounts(show_bot_accounts)
72 .show_read_posts(show_read_posts)
73 .community_id(community_id)
74 .community_actor_id(community_actor_id)
75 .saved_only(saved_only)
76 .my_person_id(person_id)
82 .map_err(|e| LemmyError::from_error_message(e, "couldnt_get_posts"))?;
84 // Blank out deleted or removed info for non-logged in users
85 if person_id.is_none() {
88 .filter(|p| p.post.deleted || p.post.removed)
90 pv.post = pv.to_owned().post.blank_out_deleted_or_removed_info();
95 .filter(|p| p.community.deleted || p.community.removed)
97 pv.community = pv.to_owned().community.blank_out_deleted_or_removed_info();
101 Ok(GetPostsResponse { posts })