]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/read.rs
56584fc37bf0d2b1040df58432ffa7fe6631c82a
[lemmy.git] / crates / api_crud / src / comment / read.rs
1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   blocking,
5   check_private_instance,
6   comment::*,
7   get_local_user_view_from_jwt_opt,
8   resolve_actor_identifier,
9 };
10 use lemmy_db_schema::{
11   from_opt_str_to_opt_enum,
12   source::community::Community,
13   traits::DeleteableOrRemoveable,
14   ListingType,
15   SortType,
16 };
17 use lemmy_db_views::comment_view::{CommentQueryBuilder, CommentView};
18 use lemmy_utils::{ConnectionId, LemmyError};
19 use lemmy_websocket::LemmyContext;
20
21 #[async_trait::async_trait(?Send)]
22 impl PerformCrud for GetComment {
23   type Response = CommentResponse;
24
25   #[tracing::instrument(skip(context, _websocket_id))]
26   async fn perform(
27     &self,
28     context: &Data<LemmyContext>,
29     _websocket_id: Option<ConnectionId>,
30   ) -> Result<Self::Response, LemmyError> {
31     let data = self;
32     let local_user_view =
33       get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
34         .await?;
35
36     check_private_instance(&local_user_view, context.pool()).await?;
37
38     let person_id = local_user_view.map(|u| u.person.id);
39     let id = data.id;
40     let comment_view = blocking(context.pool(), move |conn| {
41       CommentView::read(conn, id, person_id)
42     })
43     .await?
44     .map_err(LemmyError::from)
45     .map_err(|e| e.with_message("couldnt_find_comment"))?;
46
47     Ok(Self::Response {
48       comment_view,
49       form_id: None,
50       recipient_ids: Vec::new(),
51     })
52   }
53 }
54
55 #[async_trait::async_trait(?Send)]
56 impl PerformCrud for GetComments {
57   type Response = GetCommentsResponse;
58
59   #[tracing::instrument(skip(context, _websocket_id))]
60   async fn perform(
61     &self,
62     context: &Data<LemmyContext>,
63     _websocket_id: Option<ConnectionId>,
64   ) -> Result<GetCommentsResponse, LemmyError> {
65     let data: &GetComments = self;
66     let local_user_view =
67       get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
68         .await?;
69
70     check_private_instance(&local_user_view, context.pool()).await?;
71
72     let show_bot_accounts = local_user_view
73       .as_ref()
74       .map(|t| t.local_user.show_bot_accounts);
75     let person_id = local_user_view.map(|u| u.person.id);
76
77     let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
78     let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
79
80     let community_id = data.community_id;
81     let community_actor_id = if let Some(name) = &data.community_name {
82       resolve_actor_identifier::<Community>(name, context.pool())
83         .await
84         .ok()
85         .map(|c| c.actor_id)
86     } else {
87       None
88     };
89     let saved_only = data.saved_only;
90     let page = data.page;
91     let limit = data.limit;
92     let mut comments = blocking(context.pool(), move |conn| {
93       CommentQueryBuilder::create(conn)
94         .listing_type(listing_type)
95         .sort(sort)
96         .saved_only(saved_only)
97         .community_id(community_id)
98         .community_actor_id(community_actor_id)
99         .my_person_id(person_id)
100         .show_bot_accounts(show_bot_accounts)
101         .page(page)
102         .limit(limit)
103         .list()
104     })
105     .await?
106     .map_err(LemmyError::from)
107     .map_err(|e| e.with_message("couldnt_get_comments"))?;
108
109     // Blank out deleted or removed info
110     for cv in comments
111       .iter_mut()
112       .filter(|cv| cv.comment.deleted || cv.comment.removed)
113     {
114       cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
115     }
116
117     Ok(GetCommentsResponse { comments })
118   }
119 }