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_db_queries::{from_opt_str_to_opt_enum, ListingType, SortType};
6 comment_view::CommentQueryBuilder,
7 post_view::{PostQueryBuilder, PostView},
9 use lemmy_db_views_actor::{
10 community_moderator_view::CommunityModeratorView,
11 community_view::CommunityView,
13 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
14 use lemmy_websocket::{messages::GetPostUsersOnline, LemmyContext};
16 #[async_trait::async_trait(?Send)]
17 impl PerformCrud for GetPost {
18 type Response = GetPostResponse;
22 context: &Data<LemmyContext>,
23 _websocket_id: Option<ConnectionId>,
24 ) -> Result<GetPostResponse, LemmyError> {
25 let data: &GetPost = self;
26 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
28 let show_bot_accounts = local_user_view
30 .map(|t| t.local_user.show_bot_accounts);
31 let person_id = local_user_view.map(|u| u.person.id);
34 let post_view = blocking(context.pool(), move |conn| {
35 PostView::read(conn, id, person_id)
38 .map_err(|_| ApiError::err("couldnt_find_post"))?;
40 // Mark the post as read
41 if let Some(person_id) = person_id {
42 mark_post_as_read(person_id, id, context.pool()).await?;
46 let comments = blocking(context.pool(), move |conn| {
47 CommentQueryBuilder::create(conn)
48 .my_person_id(person_id)
49 .show_bot_accounts(show_bot_accounts)
56 let community_id = post_view.community.id;
57 let moderators = blocking(context.pool(), move |conn| {
58 CommunityModeratorView::for_community(conn, community_id)
62 // Necessary for the sidebar
63 let community_view = blocking(context.pool(), move |conn| {
64 CommunityView::read(conn, community_id, person_id)
67 .map_err(|_| ApiError::err("couldnt_find_community"))?;
71 .send(GetPostUsersOnline { post_id: data.id })
86 #[async_trait::async_trait(?Send)]
87 impl PerformCrud for GetPosts {
88 type Response = GetPostsResponse;
92 context: &Data<LemmyContext>,
93 _websocket_id: Option<ConnectionId>,
94 ) -> Result<GetPostsResponse, LemmyError> {
95 let data: &GetPosts = self;
96 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
98 let person_id = local_user_view.to_owned().map(|l| l.person.id);
100 let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
101 let show_bot_accounts = local_user_view
103 .map(|t| t.local_user.show_bot_accounts);
104 let show_read_posts = local_user_view
106 .map(|t| t.local_user.show_read_posts);
108 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
109 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
111 let page = data.page;
112 let limit = data.limit;
113 let community_id = data.community_id;
114 let community_name = data.community_name.to_owned();
115 let saved_only = data.saved_only;
117 let posts = blocking(context.pool(), move |conn| {
118 PostQueryBuilder::create(conn)
119 .listing_type(listing_type)
121 .show_nsfw(show_nsfw)
122 .show_bot_accounts(show_bot_accounts)
123 .show_read_posts(show_read_posts)
124 .community_id(community_id)
125 .community_name(community_name)
126 .saved_only(saved_only)
127 .my_person_id(person_id)
133 .map_err(|_| ApiError::err("couldnt_get_posts"))?;
135 Ok(GetPostsResponse { posts })