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,
9 use lemmy_apub::{fetcher::resolve_actor_identifier, objects::community::ApubCommunity};
10 use lemmy_db_schema::{
11 from_opt_str_to_opt_enum,
12 source::community::Community,
13 traits::DeleteableOrRemoveable,
17 use lemmy_db_views::comment_view::{CommentQueryBuilder, CommentView};
18 use lemmy_utils::{ConnectionId, LemmyError};
19 use lemmy_websocket::LemmyContext;
21 #[async_trait::async_trait(?Send)]
22 impl PerformCrud for GetComment {
23 type Response = CommentResponse;
25 #[tracing::instrument(skip(context, _websocket_id))]
28 context: &Data<LemmyContext>,
29 _websocket_id: Option<ConnectionId>,
30 ) -> Result<Self::Response, LemmyError> {
33 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
36 check_private_instance(&local_user_view, context.pool()).await?;
38 let person_id = local_user_view.map(|u| u.person.id);
40 let comment_view = blocking(context.pool(), move |conn| {
41 CommentView::read(conn, id, person_id)
44 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_comment"))?;
49 recipient_ids: Vec::new(),
54 #[async_trait::async_trait(?Send)]
55 impl PerformCrud for GetComments {
56 type Response = GetCommentsResponse;
58 #[tracing::instrument(skip(context, _websocket_id))]
61 context: &Data<LemmyContext>,
62 _websocket_id: Option<ConnectionId>,
63 ) -> Result<GetCommentsResponse, LemmyError> {
64 let data: &GetComments = self;
66 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
69 check_private_instance(&local_user_view, context.pool()).await?;
71 let show_bot_accounts = local_user_view
73 .map(|t| t.local_user.show_bot_accounts);
74 let person_id = local_user_view.map(|u| u.person.id);
76 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
77 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
79 let community_id = data.community_id;
80 let community_actor_id = if let Some(name) = &data.community_name {
81 resolve_actor_identifier::<ApubCommunity, Community>(name, context)
88 let saved_only = data.saved_only;
90 let limit = data.limit;
91 let mut comments = blocking(context.pool(), move |conn| {
92 CommentQueryBuilder::create(conn)
93 .listing_type(listing_type)
95 .saved_only(saved_only)
96 .community_id(community_id)
97 .community_actor_id(community_actor_id)
98 .my_person_id(person_id)
99 .show_bot_accounts(show_bot_accounts)
105 .map_err(|e| LemmyError::from_error_message(e, "couldnt_get_comments"))?;
107 // Blank out deleted or removed info
110 .filter(|cv| cv.comment.deleted || cv.comment.removed)
112 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
115 Ok(GetCommentsResponse { comments })