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::{ApiError, ConnectionId, LemmyError};
17 use lemmy_websocket::LemmyContext;
19 #[async_trait::async_trait(?Send)]
20 impl PerformCrud for GetComment {
21 type Response = CommentResponse;
25 context: &Data<LemmyContext>,
26 _websocket_id: Option<ConnectionId>,
27 ) -> Result<Self::Response, LemmyError> {
30 get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
32 let person_id = local_user_view.map(|u| u.person.id);
34 let comment_view = blocking(context.pool(), move |conn| {
35 CommentView::read(conn, id, person_id)
38 .map_err(|e| ApiError::err("couldnt_find_comment", e))?;
43 recipient_ids: Vec::new(),
48 #[async_trait::async_trait(?Send)]
49 impl PerformCrud for GetComments {
50 type Response = GetCommentsResponse;
54 context: &Data<LemmyContext>,
55 _websocket_id: Option<ConnectionId>,
56 ) -> Result<GetCommentsResponse, LemmyError> {
57 let data: &GetComments = self;
59 get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
61 let show_bot_accounts = local_user_view
63 .map(|t| t.local_user.show_bot_accounts);
64 let person_id = local_user_view.map(|u| u.person.id);
66 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
67 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
69 let community_id = data.community_id;
70 let community_actor_id = if let Some(name) = &data.community_name {
71 webfinger_resolve::<ApubCommunity>(name, EndpointType::Community, context, &mut 0)
77 let saved_only = data.saved_only;
79 let limit = data.limit;
80 let mut comments = blocking(context.pool(), move |conn| {
81 CommentQueryBuilder::create(conn)
82 .listing_type(listing_type)
84 .saved_only(saved_only)
85 .community_id(community_id)
86 .community_actor_id(community_actor_id)
87 .my_person_id(person_id)
88 .show_bot_accounts(show_bot_accounts)
94 .map_err(|e| ApiError::err("couldnt_get_comments", e))?;
96 // Blank out deleted or removed info
99 .filter(|cv| cv.comment.deleted || cv.comment.removed)
101 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
104 Ok(GetCommentsResponse { comments })