1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{blocking, get_local_user_view_from_jwt_opt, mark_post_as_read, post::*};
4 use lemmy_apub::get_actor_id_from_name;
5 use lemmy_apub_lib::webfinger::WebfingerType;
7 from_opt_str_to_opt_enum,
8 traits::DeleteableOrRemoveable,
13 comment_view::CommentQueryBuilder,
14 post_view::{PostQueryBuilder, PostView},
16 use lemmy_db_views_actor::{
17 community_moderator_view::CommunityModeratorView,
18 community_view::CommunityView,
20 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
21 use lemmy_websocket::{messages::GetPostUsersOnline, LemmyContext};
23 #[async_trait::async_trait(?Send)]
24 impl PerformCrud for GetPost {
25 type Response = GetPostResponse;
29 context: &Data<LemmyContext>,
30 _websocket_id: Option<ConnectionId>,
31 ) -> Result<GetPostResponse, LemmyError> {
32 let data: &GetPost = self;
34 get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
36 let show_bot_accounts = local_user_view
38 .map(|t| t.local_user.show_bot_accounts);
39 let person_id = local_user_view.map(|u| u.person.id);
42 let mut post_view = blocking(context.pool(), move |conn| {
43 PostView::read(conn, id, person_id)
46 .map_err(|e| ApiError::err("couldnt_find_post", e))?;
48 // Mark the post as read
49 if let Some(person_id) = person_id {
50 mark_post_as_read(person_id, id, context.pool()).await?;
54 let mut comments = blocking(context.pool(), move |conn| {
55 CommentQueryBuilder::create(conn)
56 .my_person_id(person_id)
57 .show_bot_accounts(show_bot_accounts)
64 // Necessary for the sidebar
65 let community_id = post_view.community.id;
66 let mut community_view = blocking(context.pool(), move |conn| {
67 CommunityView::read(conn, community_id, person_id)
70 .map_err(|e| ApiError::err("couldnt_find_community", e))?;
72 // Blank out deleted or removed info for non-logged in users
73 if person_id.is_none() {
74 if post_view.post.deleted || post_view.post.removed {
75 post_view.post = post_view.post.blank_out_deleted_or_removed_info();
80 .filter(|cv| cv.comment.deleted || cv.comment.removed)
82 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
84 if community_view.community.deleted || community_view.community.removed {
85 community_view.community = community_view.community.blank_out_deleted_or_removed_info();
89 let moderators = blocking(context.pool(), move |conn| {
90 CommunityModeratorView::for_community(conn, community_id)
96 .send(GetPostUsersOnline { post_id: data.id })
111 #[async_trait::async_trait(?Send)]
112 impl PerformCrud for GetPosts {
113 type Response = GetPostsResponse;
117 context: &Data<LemmyContext>,
118 _websocket_id: Option<ConnectionId>,
119 ) -> Result<GetPostsResponse, LemmyError> {
120 let data: &GetPosts = self;
121 let local_user_view =
122 get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
124 let person_id = local_user_view.to_owned().map(|l| l.person.id);
126 let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
127 let show_bot_accounts = local_user_view
129 .map(|t| t.local_user.show_bot_accounts);
130 let show_read_posts = local_user_view
132 .map(|t| t.local_user.show_read_posts);
134 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
135 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
137 let page = data.page;
138 let limit = data.limit;
139 let community_id = data.community_id;
140 let community_actor_id = if let Some(name) = &data.community_name {
141 get_actor_id_from_name(WebfingerType::Group, name, context)
147 let saved_only = data.saved_only;
149 let mut posts = blocking(context.pool(), move |conn| {
150 PostQueryBuilder::create(conn)
151 .listing_type(listing_type)
153 .show_nsfw(show_nsfw)
154 .show_bot_accounts(show_bot_accounts)
155 .show_read_posts(show_read_posts)
156 .community_id(community_id)
157 .community_actor_id(community_actor_id)
158 .saved_only(saved_only)
159 .my_person_id(person_id)
165 .map_err(|e| ApiError::err("couldnt_get_posts", e))?;
167 // Blank out deleted or removed info for non-logged in users
168 if person_id.is_none() {
171 .filter(|p| p.post.deleted || p.post.removed)
173 pv.post = pv.to_owned().post.blank_out_deleted_or_removed_info();
178 .filter(|p| p.community.deleted || p.community.removed)
180 pv.community = pv.to_owned().community.blank_out_deleted_or_removed_info();
184 Ok(GetPostsResponse { posts })