1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 comment::{GetComments, GetCommentsResponse},
5 utils::{blocking, check_private_instance, get_local_user_view_from_jwt_opt},
7 use lemmy_apub::{fetcher::resolve_actor_identifier, objects::community::ApubCommunity};
8 use lemmy_db_schema::{source::community::Community, traits::DeleteableOrRemoveable};
9 use lemmy_db_views::comment_view::CommentQueryBuilder;
10 use lemmy_utils::{ConnectionId, LemmyError};
11 use lemmy_websocket::LemmyContext;
13 #[async_trait::async_trait(?Send)]
14 impl PerformCrud for GetComments {
15 type Response = GetCommentsResponse;
17 #[tracing::instrument(skip(context, _websocket_id))]
20 context: &Data<LemmyContext>,
21 _websocket_id: Option<ConnectionId>,
22 ) -> Result<GetCommentsResponse, LemmyError> {
23 let data: &GetComments = self;
25 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
28 check_private_instance(&local_user_view, context.pool()).await?;
30 let show_bot_accounts = local_user_view
32 .map(|t| t.local_user.show_bot_accounts);
33 let person_id = local_user_view.map(|u| u.person.id);
35 let community_id = data.community_id;
36 let community_actor_id = if let Some(name) = &data.community_name {
37 resolve_actor_identifier::<ApubCommunity, Community>(name, context)
45 let listing_type = data.type_;
46 let saved_only = data.saved_only;
48 let limit = data.limit;
49 let mut comments = blocking(context.pool(), move |conn| {
50 CommentQueryBuilder::create(conn)
51 .listing_type(listing_type)
53 .saved_only(saved_only)
54 .community_id(community_id)
55 .community_actor_id(community_actor_id)
56 .my_person_id(person_id)
57 .show_bot_accounts(show_bot_accounts)
63 .map_err(|e| LemmyError::from_error_message(e, "couldnt_get_comments"))?;
65 // Blank out deleted or removed info
68 .filter(|cv| cv.comment.deleted || cv.comment.removed)
70 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
73 Ok(GetCommentsResponse { comments })