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 resolve_actor_identifier,
11 use lemmy_db_schema::{
12 from_opt_str_to_opt_enum,
13 source::community::Community,
14 traits::DeleteableOrRemoveable,
19 comment_view::CommentQueryBuilder,
20 post_view::{PostQueryBuilder, PostView},
22 use lemmy_db_views_actor::{
23 community_moderator_view::CommunityModeratorView,
24 community_view::CommunityView,
26 use lemmy_utils::{ConnectionId, LemmyError};
27 use lemmy_websocket::{messages::GetPostUsersOnline, LemmyContext};
29 #[async_trait::async_trait(?Send)]
30 impl PerformCrud for GetPost {
31 type Response = GetPostResponse;
33 #[tracing::instrument(skip(context, _websocket_id))]
36 context: &Data<LemmyContext>,
37 _websocket_id: Option<ConnectionId>,
38 ) -> Result<GetPostResponse, LemmyError> {
39 let data: &GetPost = self;
41 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
44 check_private_instance(&local_user_view, context.pool()).await?;
46 let show_bot_accounts = local_user_view
48 .map(|t| t.local_user.show_bot_accounts);
49 let person_id = local_user_view.map(|u| u.person.id);
52 let mut post_view = blocking(context.pool(), move |conn| {
53 PostView::read(conn, id, person_id)
56 .map_err(LemmyError::from)
57 .map_err(|e| e.with_message("couldnt_find_post"))?;
59 // Mark the post as read
60 if let Some(person_id) = person_id {
61 mark_post_as_read(person_id, id, context.pool()).await?;
65 let mut comments = blocking(context.pool(), move |conn| {
66 CommentQueryBuilder::create(conn)
67 .my_person_id(person_id)
68 .show_bot_accounts(show_bot_accounts)
75 // Necessary for the sidebar
76 let community_id = post_view.community.id;
77 let mut community_view = blocking(context.pool(), move |conn| {
78 CommunityView::read(conn, community_id, person_id)
81 .map_err(LemmyError::from)
82 .map_err(|e| e.with_message("couldnt_find_community"))?;
84 // Blank out deleted or removed info for non-logged in users
85 if person_id.is_none() {
86 if post_view.post.deleted || post_view.post.removed {
87 post_view.post = post_view.post.blank_out_deleted_or_removed_info();
92 .filter(|cv| cv.comment.deleted || cv.comment.removed)
94 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
96 if community_view.community.deleted || community_view.community.removed {
97 community_view.community = community_view.community.blank_out_deleted_or_removed_info();
101 let moderators = blocking(context.pool(), move |conn| {
102 CommunityModeratorView::for_community(conn, community_id)
108 .send(GetPostUsersOnline { post_id: data.id })
123 #[async_trait::async_trait(?Send)]
124 impl PerformCrud for GetPosts {
125 type Response = GetPostsResponse;
127 #[tracing::instrument(skip(context, _websocket_id))]
130 context: &Data<LemmyContext>,
131 _websocket_id: Option<ConnectionId>,
132 ) -> Result<GetPostsResponse, LemmyError> {
133 let data: &GetPosts = self;
134 let local_user_view =
135 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
138 check_private_instance(&local_user_view, context.pool()).await?;
140 let person_id = local_user_view.to_owned().map(|l| l.person.id);
142 let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
143 let show_bot_accounts = local_user_view
145 .map(|t| t.local_user.show_bot_accounts);
146 let show_read_posts = local_user_view
148 .map(|t| t.local_user.show_read_posts);
150 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
151 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
153 let page = data.page;
154 let limit = data.limit;
155 let community_id = data.community_id;
156 let community_actor_id = if let Some(name) = &data.community_name {
157 resolve_actor_identifier::<Community>(name, context.pool())
164 let saved_only = data.saved_only;
166 let mut posts = blocking(context.pool(), move |conn| {
167 PostQueryBuilder::create(conn)
168 .listing_type(listing_type)
170 .show_nsfw(show_nsfw)
171 .show_bot_accounts(show_bot_accounts)
172 .show_read_posts(show_read_posts)
173 .community_id(community_id)
174 .community_actor_id(community_actor_id)
175 .saved_only(saved_only)
176 .my_person_id(person_id)
182 .map_err(LemmyError::from)
183 .map_err(|e| e.with_message("couldnt_get_posts"))?;
185 // Blank out deleted or removed info for non-logged in users
186 if person_id.is_none() {
189 .filter(|p| p.post.deleted || p.post.removed)
191 pv.post = pv.to_owned().post.blank_out_deleted_or_removed_info();
196 .filter(|p| p.community.deleted || p.community.removed)
198 pv.community = pv.to_owned().community.blank_out_deleted_or_removed_info();
202 Ok(GetPostsResponse { posts })