]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/read.rs
Merge branch 'main' into add_jerboa_link
[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 };
9 use lemmy_apub::{fetcher::resolve_actor_identifier, objects::community::ApubCommunity};
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(|e| LemmyError::from_error_message(e, "couldnt_find_comment"))?;
45
46     Ok(Self::Response {
47       comment_view,
48       form_id: None,
49       recipient_ids: Vec::new(),
50     })
51   }
52 }
53
54 #[async_trait::async_trait(?Send)]
55 impl PerformCrud for GetComments {
56   type Response = GetCommentsResponse;
57
58   #[tracing::instrument(skip(context, _websocket_id))]
59   async fn perform(
60     &self,
61     context: &Data<LemmyContext>,
62     _websocket_id: Option<ConnectionId>,
63   ) -> Result<GetCommentsResponse, LemmyError> {
64     let data: &GetComments = self;
65     let local_user_view =
66       get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
67         .await?;
68
69     check_private_instance(&local_user_view, context.pool()).await?;
70
71     let show_bot_accounts = local_user_view
72       .as_ref()
73       .map(|t| t.local_user.show_bot_accounts);
74     let person_id = local_user_view.map(|u| u.person.id);
75
76     let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
77     let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
78
79     let community_id = data.community_id;
80     let community_actor_id = if let Some(name) = &data.community_name {
81       resolve_actor_identifier::<ApubCommunity, Community>(name, context)
82         .await
83         .ok()
84         .map(|c| c.actor_id)
85     } else {
86       None
87     };
88     let saved_only = data.saved_only;
89     let page = data.page;
90     let limit = data.limit;
91     let mut comments = blocking(context.pool(), move |conn| {
92       CommentQueryBuilder::create(conn)
93         .listing_type(listing_type)
94         .sort(sort)
95         .saved_only(saved_only)
96         .community_id(community_id)
97         .community_actor_id(community_actor_id)
98         .my_person_id(person_id)
99         .show_bot_accounts(show_bot_accounts)
100         .page(page)
101         .limit(limit)
102         .list()
103     })
104     .await?
105     .map_err(|e| LemmyError::from_error_message(e, "couldnt_get_comments"))?;
106
107     // Blank out deleted or removed info
108     for cv in comments
109       .iter_mut()
110       .filter(|cv| cv.comment.deleted || cv.comment.removed)
111     {
112       cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
113     }
114
115     Ok(GetCommentsResponse { comments })
116   }
117 }