2021-03-25 19:19:40 +00:00
|
|
|
use crate::PerformCrud;
|
|
|
|
use actix_web::web::Data;
|
2021-12-15 19:49:59 +00:00
|
|
|
use lemmy_api_common::{
|
|
|
|
blocking,
|
|
|
|
check_private_instance,
|
|
|
|
comment::*,
|
|
|
|
get_local_user_view_from_jwt_opt,
|
2021-11-16 17:03:09 +00:00
|
|
|
};
|
2022-03-23 21:27:51 +00:00
|
|
|
use lemmy_apub::{fetcher::resolve_actor_identifier, objects::community::ApubCommunity};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
from_opt_str_to_opt_enum,
|
2022-01-27 16:39:22 +00:00
|
|
|
source::community::Community,
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::DeleteableOrRemoveable,
|
|
|
|
ListingType,
|
|
|
|
SortType,
|
|
|
|
};
|
2021-11-23 15:53:48 +00:00
|
|
|
use lemmy_db_views::comment_view::{CommentQueryBuilder, CommentView};
|
2021-12-06 14:54:47 +00:00
|
|
|
use lemmy_utils::{ConnectionId, LemmyError};
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_websocket::LemmyContext;
|
|
|
|
|
2021-11-23 15:53:48 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for GetComment {
|
|
|
|
type Response = CommentResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, _websocket_id))]
|
2021-11-23 15:53:48 +00:00
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
_websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<Self::Response, LemmyError> {
|
|
|
|
let data = self;
|
|
|
|
let local_user_view =
|
2021-12-06 14:54:47 +00:00
|
|
|
get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
|
|
|
|
.await?;
|
2021-11-23 15:53:48 +00:00
|
|
|
|
2021-12-15 19:49:59 +00:00
|
|
|
check_private_instance(&local_user_view, context.pool()).await?;
|
|
|
|
|
2021-11-23 15:53:48 +00:00
|
|
|
let person_id = local_user_view.map(|u| u.person.id);
|
|
|
|
let id = data.id;
|
|
|
|
let comment_view = blocking(context.pool(), move |conn| {
|
|
|
|
CommentView::read(conn, id, person_id)
|
|
|
|
})
|
|
|
|
.await?
|
2022-03-16 20:11:49 +00:00
|
|
|
.map_err(|e| LemmyError::from_error_message(e, "couldnt_find_comment"))?;
|
2021-11-23 15:53:48 +00:00
|
|
|
|
|
|
|
Ok(Self::Response {
|
|
|
|
comment_view,
|
|
|
|
form_id: None,
|
|
|
|
recipient_ids: Vec::new(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 19:19:40 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for GetComments {
|
|
|
|
type Response = GetCommentsResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, _websocket_id))]
|
2021-03-25 19:19:40 +00:00
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
_websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<GetCommentsResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &GetComments = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
2021-12-06 14:54:47 +00:00
|
|
|
get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
|
|
|
|
.await?;
|
2021-04-21 21:41:14 +00:00
|
|
|
|
2021-12-15 19:49:59 +00:00
|
|
|
check_private_instance(&local_user_view, context.pool()).await?;
|
|
|
|
|
2021-04-26 14:44:19 +00:00
|
|
|
let show_bot_accounts = local_user_view
|
|
|
|
.as_ref()
|
|
|
|
.map(|t| t.local_user.show_bot_accounts);
|
2021-03-25 19:19:40 +00:00
|
|
|
let person_id = local_user_view.map(|u| u.person.id);
|
|
|
|
|
2021-04-15 03:37:51 +00:00
|
|
|
let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
|
|
|
|
let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
let community_id = data.community_id;
|
2021-10-25 16:09:21 +00:00
|
|
|
let community_actor_id = if let Some(name) = &data.community_name {
|
2022-03-23 21:27:51 +00:00
|
|
|
resolve_actor_identifier::<ApubCommunity, Community>(name, context)
|
2021-10-25 16:09:21 +00:00
|
|
|
.await
|
|
|
|
.ok()
|
2022-01-27 16:39:22 +00:00
|
|
|
.map(|c| c.actor_id)
|
2021-10-25 16:09:21 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2021-03-25 19:19:40 +00:00
|
|
|
let saved_only = data.saved_only;
|
|
|
|
let page = data.page;
|
|
|
|
let limit = data.limit;
|
2021-07-30 18:44:15 +00:00
|
|
|
let mut comments = blocking(context.pool(), move |conn| {
|
2021-03-25 19:19:40 +00:00
|
|
|
CommentQueryBuilder::create(conn)
|
2021-04-15 03:37:51 +00:00
|
|
|
.listing_type(listing_type)
|
|
|
|
.sort(sort)
|
2021-03-25 19:19:40 +00:00
|
|
|
.saved_only(saved_only)
|
|
|
|
.community_id(community_id)
|
2021-07-20 04:29:50 +00:00
|
|
|
.community_actor_id(community_actor_id)
|
2021-03-25 19:19:40 +00:00
|
|
|
.my_person_id(person_id)
|
2021-04-21 21:41:14 +00:00
|
|
|
.show_bot_accounts(show_bot_accounts)
|
2021-03-25 19:19:40 +00:00
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.list()
|
|
|
|
})
|
2021-04-16 13:10:43 +00:00
|
|
|
.await?
|
2022-03-16 20:11:49 +00:00
|
|
|
.map_err(|e| LemmyError::from_error_message(e, "couldnt_get_comments"))?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-07-30 18:44:15 +00:00
|
|
|
// Blank out deleted or removed info
|
|
|
|
for cv in comments
|
|
|
|
.iter_mut()
|
|
|
|
.filter(|cv| cv.comment.deleted || cv.comment.removed)
|
|
|
|
{
|
|
|
|
cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
|
|
|
|
}
|
|
|
|
|
2021-03-25 19:19:40 +00:00
|
|
|
Ok(GetCommentsResponse { comments })
|
|
|
|
}
|
|
|
|
}
|