2024-09-19 08:03:58 +00:00
|
|
|
use super::comment_sort_type_with_default;
|
2022-11-28 14:29:33 +00:00
|
|
|
use crate::{
|
2023-07-03 09:01:41 +00:00
|
|
|
api::listing_type_with_default,
|
2022-11-28 14:29:33 +00:00
|
|
|
fetcher::resolve_actor_identifier,
|
|
|
|
objects::community::ApubCommunity,
|
|
|
|
};
|
2023-03-21 15:03:05 +00:00
|
|
|
use activitypub_federation::config::Data;
|
2023-07-03 09:01:41 +00:00
|
|
|
use actix_web::web::{Json, Query};
|
2022-04-13 18:12:25 +00:00
|
|
|
use lemmy_api_common::{
|
2022-05-03 17:44:13 +00:00
|
|
|
comment::{GetComments, GetCommentsResponse},
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2023-09-21 10:42:28 +00:00
|
|
|
utils::check_private_instance,
|
2022-04-13 18:12:25 +00:00
|
|
|
};
|
2022-07-30 03:55:59 +00:00
|
|
|
use lemmy_db_schema::{
|
2024-09-24 08:33:53 +00:00
|
|
|
source::{comment::Comment, community::Community},
|
2023-03-01 03:46:15 +00:00
|
|
|
traits::Crud,
|
2022-07-30 03:55:59 +00:00
|
|
|
};
|
2024-09-24 08:33:53 +00:00
|
|
|
use lemmy_db_views::{
|
|
|
|
comment_view::CommentQuery,
|
|
|
|
structs::{LocalUserView, SiteView},
|
|
|
|
};
|
2024-04-10 14:14:11 +00:00
|
|
|
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType, LemmyResult};
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-07-03 09:01:41 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn list_comments(
|
|
|
|
data: Query<GetComments>,
|
|
|
|
context: Data<LemmyContext>,
|
2023-09-21 10:42:28 +00:00
|
|
|
local_user_view: Option<LocalUserView>,
|
2024-04-10 14:14:11 +00:00
|
|
|
) -> LemmyResult<Json<GetCommentsResponse>> {
|
2024-09-24 08:33:53 +00:00
|
|
|
let site_view = SiteView::read_local(&mut context.pool()).await?;
|
|
|
|
check_private_instance(&local_user_view, &site_view.local_site)?;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-07-03 09:01:41 +00:00
|
|
|
let community_id = if let Some(name) = &data.community_name {
|
2024-03-14 20:57:56 +00:00
|
|
|
Some(
|
|
|
|
resolve_actor_identifier::<ApubCommunity, Community>(name, &context, &local_user_view, true)
|
|
|
|
.await?,
|
|
|
|
)
|
|
|
|
.map(|c| c.id)
|
2023-07-03 09:01:41 +00:00
|
|
|
} else {
|
|
|
|
data.community_id
|
|
|
|
};
|
2024-09-19 08:03:58 +00:00
|
|
|
let local_user_ref = local_user_view.as_ref().map(|u| &u.local_user);
|
|
|
|
let sort = Some(comment_sort_type_with_default(
|
|
|
|
data.sort,
|
|
|
|
local_user_ref,
|
2024-09-24 08:33:53 +00:00
|
|
|
&site_view.local_site,
|
2024-09-19 08:03:58 +00:00
|
|
|
));
|
2023-07-03 09:01:41 +00:00
|
|
|
let max_depth = data.max_depth;
|
2024-06-21 21:39:40 +00:00
|
|
|
let saved_only = data.saved_only;
|
2023-08-08 09:40:28 +00:00
|
|
|
|
2024-06-21 21:39:40 +00:00
|
|
|
let liked_only = data.liked_only;
|
|
|
|
let disliked_only = data.disliked_only;
|
|
|
|
if liked_only.unwrap_or_default() && disliked_only.unwrap_or_default() {
|
2023-08-08 09:40:28 +00:00
|
|
|
return Err(LemmyError::from(LemmyErrorType::ContradictingFilters));
|
|
|
|
}
|
|
|
|
|
2023-07-03 09:01:41 +00:00
|
|
|
let page = data.page;
|
|
|
|
let limit = data.limit;
|
|
|
|
let parent_id = data.parent_id;
|
2022-07-30 03:55:59 +00:00
|
|
|
|
2023-07-17 10:20:25 +00:00
|
|
|
let listing_type = Some(listing_type_with_default(
|
|
|
|
data.type_,
|
2024-02-16 14:36:46 +00:00
|
|
|
local_user_view.as_ref().map(|u| &u.local_user),
|
2024-09-24 08:33:53 +00:00
|
|
|
&site_view.local_site,
|
2023-07-17 10:20:25 +00:00
|
|
|
community_id,
|
2024-02-16 14:36:46 +00:00
|
|
|
));
|
2023-04-12 14:40:59 +00:00
|
|
|
|
2023-07-03 09:01:41 +00:00
|
|
|
// If a parent_id is given, fetch the comment to get the path
|
|
|
|
let parent_path = if let Some(parent_id) = parent_id {
|
2024-09-23 15:26:50 +00:00
|
|
|
Some(Comment::read(&mut context.pool(), parent_id).await?.path)
|
2023-07-03 09:01:41 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2022-07-30 03:55:59 +00:00
|
|
|
|
2023-07-03 09:01:41 +00:00
|
|
|
let parent_path_cloned = parent_path.clone();
|
|
|
|
let post_id = data.post_id;
|
2024-06-15 01:51:24 +00:00
|
|
|
let local_user = local_user_view.as_ref().map(|l| &l.local_user);
|
|
|
|
|
2023-07-17 10:20:25 +00:00
|
|
|
let comments = CommentQuery {
|
|
|
|
listing_type,
|
|
|
|
sort,
|
|
|
|
max_depth,
|
|
|
|
saved_only,
|
2023-08-08 09:40:28 +00:00
|
|
|
liked_only,
|
|
|
|
disliked_only,
|
2023-07-17 10:20:25 +00:00
|
|
|
community_id,
|
|
|
|
parent_path: parent_path_cloned,
|
|
|
|
post_id,
|
2024-06-15 01:51:24 +00:00
|
|
|
local_user,
|
2023-07-17 10:20:25 +00:00
|
|
|
page,
|
|
|
|
limit,
|
|
|
|
..Default::default()
|
|
|
|
}
|
2024-09-24 08:33:53 +00:00
|
|
|
.list(&site_view.site, &mut context.pool())
|
2023-07-17 10:20:25 +00:00
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntGetComments)?;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-07-03 09:01:41 +00:00
|
|
|
Ok(Json(GetCommentsResponse { comments }))
|
2022-04-13 18:12:25 +00:00
|
|
|
}
|