1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 check_private_instance,
6 get_local_user_view_from_jwt_opt,
9 use lemmy_apub::{fetcher::resolve_actor_identifier, objects::community::ApubCommunity};
10 use lemmy_db_schema::{
11 from_opt_str_to_opt_enum,
12 source::community::Community,
13 traits::DeleteableOrRemoveable,
17 use lemmy_db_views::post_view::PostQueryBuilder;
18 use lemmy_utils::{ConnectionId, LemmyError};
19 use lemmy_websocket::LemmyContext;
21 #[async_trait::async_trait(?Send)]
22 impl PerformCrud for GetPosts {
23 type Response = GetPostsResponse;
25 #[tracing::instrument(skip(context, _websocket_id))]
28 context: &Data<LemmyContext>,
29 _websocket_id: Option<ConnectionId>,
30 ) -> Result<GetPostsResponse, LemmyError> {
31 let data: &GetPosts = self;
33 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
36 check_private_instance(&local_user_view, context.pool()).await?;
38 let person_id = local_user_view.to_owned().map(|l| l.person.id);
40 let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
41 let show_bot_accounts = local_user_view
43 .map(|t| t.local_user.show_bot_accounts);
44 let show_read_posts = local_user_view
46 .map(|t| t.local_user.show_read_posts);
48 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
49 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
52 let limit = data.limit;
53 let community_id = data.community_id;
54 let community_actor_id = if let Some(name) = &data.community_name {
55 resolve_actor_identifier::<ApubCommunity, Community>(name, context)
62 let saved_only = data.saved_only;
64 let mut posts = blocking(context.pool(), move |conn| {
65 PostQueryBuilder::create(conn)
66 .listing_type(listing_type)
69 .show_bot_accounts(show_bot_accounts)
70 .show_read_posts(show_read_posts)
71 .community_id(community_id)
72 .community_actor_id(community_actor_id)
73 .saved_only(saved_only)
74 .my_person_id(person_id)
80 .map_err(|e| LemmyError::from_error_message(e, "couldnt_get_posts"))?;
82 // Blank out deleted or removed info for non-logged in users
83 if person_id.is_none() {
86 .filter(|p| p.post.deleted || p.post.removed)
88 pv.post = pv.to_owned().post.blank_out_deleted_or_removed_info();
93 .filter(|p| p.community.deleted || p.community.removed)
95 pv.community = pv.to_owned().community.blank_out_deleted_or_removed_info();
99 Ok(GetPostsResponse { posts })