1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{blocking, comment::*, get_local_user_view_from_jwt_opt};
5 fetcher::webfinger::webfinger_resolve,
6 objects::community::ApubCommunity,
10 from_opt_str_to_opt_enum,
11 traits::DeleteableOrRemoveable,
15 use lemmy_db_views::comment_view::{CommentQueryBuilder, CommentView};
16 use lemmy_utils::{ConnectionId, LemmyError};
17 use lemmy_websocket::LemmyContext;
19 #[async_trait::async_trait(?Send)]
20 impl PerformCrud for GetComment {
21 type Response = CommentResponse;
23 #[tracing::instrument(skip(context, _websocket_id))]
26 context: &Data<LemmyContext>,
27 _websocket_id: Option<ConnectionId>,
28 ) -> Result<Self::Response, LemmyError> {
31 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
34 let person_id = local_user_view.map(|u| u.person.id);
36 let comment_view = blocking(context.pool(), move |conn| {
37 CommentView::read(conn, id, person_id)
40 .map_err(LemmyError::from)
41 .map_err(|e| e.with_message("couldnt_find_comment"))?;
46 recipient_ids: Vec::new(),
51 #[async_trait::async_trait(?Send)]
52 impl PerformCrud for GetComments {
53 type Response = GetCommentsResponse;
55 #[tracing::instrument(skip(context, _websocket_id))]
58 context: &Data<LemmyContext>,
59 _websocket_id: Option<ConnectionId>,
60 ) -> Result<GetCommentsResponse, LemmyError> {
61 let data: &GetComments = self;
63 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
66 let show_bot_accounts = local_user_view
68 .map(|t| t.local_user.show_bot_accounts);
69 let person_id = local_user_view.map(|u| u.person.id);
71 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
72 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
74 let community_id = data.community_id;
75 let community_actor_id = if let Some(name) = &data.community_name {
76 webfinger_resolve::<ApubCommunity>(name, EndpointType::Community, context, &mut 0)
82 let saved_only = data.saved_only;
84 let limit = data.limit;
85 let mut comments = blocking(context.pool(), move |conn| {
86 CommentQueryBuilder::create(conn)
87 .listing_type(listing_type)
89 .saved_only(saved_only)
90 .community_id(community_id)
91 .community_actor_id(community_actor_id)
92 .my_person_id(person_id)
93 .show_bot_accounts(show_bot_accounts)
99 .map_err(LemmyError::from)
100 .map_err(|e| e.with_message("couldnt_get_comments"))?;
102 // Blank out deleted or removed info
105 .filter(|cv| cv.comment.deleted || cv.comment.removed)
107 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
110 Ok(GetCommentsResponse { comments })