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