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,
8 resolve_actor_identifier,
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(LemmyError::from)
45 .map_err(|e| e.with_message("couldnt_find_comment"))?;
50 recipient_ids: Vec::new(),
55 #[async_trait::async_trait(?Send)]
56 impl PerformCrud for GetComments {
57 type Response = GetCommentsResponse;
59 #[tracing::instrument(skip(context, _websocket_id))]
62 context: &Data<LemmyContext>,
63 _websocket_id: Option<ConnectionId>,
64 ) -> Result<GetCommentsResponse, LemmyError> {
65 let data: &GetComments = self;
67 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
70 check_private_instance(&local_user_view, context.pool()).await?;
72 let show_bot_accounts = local_user_view
74 .map(|t| t.local_user.show_bot_accounts);
75 let person_id = local_user_view.map(|u| u.person.id);
77 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
78 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
80 let community_id = data.community_id;
81 let community_actor_id = if let Some(name) = &data.community_name {
82 resolve_actor_identifier::<Community>(name, context.pool())
89 let saved_only = data.saved_only;
91 let limit = data.limit;
92 let mut comments = blocking(context.pool(), move |conn| {
93 CommentQueryBuilder::create(conn)
94 .listing_type(listing_type)
96 .saved_only(saved_only)
97 .community_id(community_id)
98 .community_actor_id(community_actor_id)
99 .my_person_id(person_id)
100 .show_bot_accounts(show_bot_accounts)
106 .map_err(LemmyError::from)
107 .map_err(|e| e.with_message("couldnt_get_comments"))?;
109 // Blank out deleted or removed info
112 .filter(|cv| cv.comment.deleted || cv.comment.removed)
114 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
117 Ok(GetCommentsResponse { comments })