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::{build_actor_id_from_shortname, EndpointType};
5 use lemmy_db_queries::{from_opt_str_to_opt_enum, DeleteableOrRemoveable, ListingType, SortType};
7 comment_view::CommentQueryBuilder,
8 post_view::{PostQueryBuilder, PostView},
10 use lemmy_db_views_actor::{
11 community_moderator_view::CommunityModeratorView,
12 community_view::CommunityView,
14 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
15 use lemmy_websocket::{messages::GetPostUsersOnline, LemmyContext};
17 #[async_trait::async_trait(?Send)]
18 impl PerformCrud for GetPost {
19 type Response = GetPostResponse;
23 context: &Data<LemmyContext>,
24 _websocket_id: Option<ConnectionId>,
25 ) -> Result<GetPostResponse, LemmyError> {
26 let data: &GetPost = self;
27 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
29 let show_bot_accounts = local_user_view
31 .map(|t| t.local_user.show_bot_accounts);
32 let person_id = local_user_view.map(|u| u.person.id);
35 let mut post_view = blocking(context.pool(), move |conn| {
36 PostView::read(conn, id, person_id)
39 .map_err(|_| ApiError::err("couldnt_find_post"))?;
41 // Blank out deleted info
42 if post_view.post.deleted || post_view.post.removed {
43 post_view.post = post_view.post.blank_out_deleted_or_removed_info();
46 // Mark the post as read
47 if let Some(person_id) = person_id {
48 mark_post_as_read(person_id, id, context.pool()).await?;
52 let mut comments = blocking(context.pool(), move |conn| {
53 CommentQueryBuilder::create(conn)
54 .my_person_id(person_id)
55 .show_bot_accounts(show_bot_accounts)
62 // Blank out deleted or removed info
65 .filter(|cv| cv.comment.deleted || cv.comment.removed)
67 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
70 let community_id = post_view.community.id;
71 let moderators = blocking(context.pool(), move |conn| {
72 CommunityModeratorView::for_community(conn, community_id)
76 // Necessary for the sidebar
77 let mut community_view = blocking(context.pool(), move |conn| {
78 CommunityView::read(conn, community_id, person_id)
81 .map_err(|_| ApiError::err("couldnt_find_community"))?;
83 // 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();
90 .send(GetPostUsersOnline { post_id: data.id })
105 #[async_trait::async_trait(?Send)]
106 impl PerformCrud for GetPosts {
107 type Response = GetPostsResponse;
111 context: &Data<LemmyContext>,
112 _websocket_id: Option<ConnectionId>,
113 ) -> Result<GetPostsResponse, LemmyError> {
114 let data: &GetPosts = self;
115 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
117 let person_id = local_user_view.to_owned().map(|l| l.person.id);
119 let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
120 let show_bot_accounts = local_user_view
122 .map(|t| t.local_user.show_bot_accounts);
123 let show_read_posts = local_user_view
125 .map(|t| t.local_user.show_read_posts);
127 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
128 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
130 let page = data.page;
131 let limit = data.limit;
132 let community_id = data.community_id;
133 let community_actor_id = data
136 .map(|t| build_actor_id_from_shortname(EndpointType::Community, t).ok())
138 let saved_only = data.saved_only;
140 let mut posts = blocking(context.pool(), move |conn| {
141 PostQueryBuilder::create(conn)
142 .listing_type(listing_type)
144 .show_nsfw(show_nsfw)
145 .show_bot_accounts(show_bot_accounts)
146 .show_read_posts(show_read_posts)
147 .community_id(community_id)
148 .community_actor_id(community_actor_id)
149 .saved_only(saved_only)
150 .my_person_id(person_id)
156 .map_err(|_| ApiError::err("couldnt_get_posts"))?;
158 // Blank out deleted or removed info
161 .filter(|p| p.post.deleted || p.post.removed)
163 pv.post = pv.to_owned().post.blank_out_deleted_or_removed_info();
166 Ok(GetPostsResponse { posts })