1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 check_private_instance,
7 get_local_user_view_from_jwt_opt,
10 fetcher::webfinger::webfinger_resolve,
11 objects::community::ApubCommunity,
14 use lemmy_db_schema::{
15 from_opt_str_to_opt_enum,
16 traits::DeleteableOrRemoveable,
20 use lemmy_db_views::comment_view::{CommentQueryBuilder, CommentView};
21 use lemmy_utils::{ConnectionId, LemmyError};
22 use lemmy_websocket::LemmyContext;
24 #[async_trait::async_trait(?Send)]
25 impl PerformCrud for GetComment {
26 type Response = CommentResponse;
28 #[tracing::instrument(skip(context, _websocket_id))]
31 context: &Data<LemmyContext>,
32 _websocket_id: Option<ConnectionId>,
33 ) -> Result<Self::Response, LemmyError> {
36 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
39 check_private_instance(&local_user_view, context.pool()).await?;
41 let person_id = local_user_view.map(|u| u.person.id);
43 let comment_view = blocking(context.pool(), move |conn| {
44 CommentView::read(conn, id, person_id)
47 .map_err(LemmyError::from)
48 .map_err(|e| e.with_message("couldnt_find_comment"))?;
53 recipient_ids: Vec::new(),
58 #[async_trait::async_trait(?Send)]
59 impl PerformCrud for GetComments {
60 type Response = GetCommentsResponse;
62 #[tracing::instrument(skip(context, _websocket_id))]
65 context: &Data<LemmyContext>,
66 _websocket_id: Option<ConnectionId>,
67 ) -> Result<GetCommentsResponse, LemmyError> {
68 let data: &GetComments = self;
70 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
73 check_private_instance(&local_user_view, context.pool()).await?;
75 let show_bot_accounts = local_user_view
77 .map(|t| t.local_user.show_bot_accounts);
78 let person_id = local_user_view.map(|u| u.person.id);
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_);
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)
91 let saved_only = data.saved_only;
93 let limit = data.limit;
94 let mut comments = blocking(context.pool(), move |conn| {
95 CommentQueryBuilder::create(conn)
96 .listing_type(listing_type)
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)
108 .map_err(LemmyError::from)
109 .map_err(|e| e.with_message("couldnt_get_comments"))?;
111 // Blank out deleted or removed info
114 .filter(|cv| cv.comment.deleted || cv.comment.removed)
116 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
119 Ok(GetCommentsResponse { comments })