]> Untitled Git - lemmy.git/blob - crates/apub/src/api/list_comments.rs
Dont return error in case optional auth is invalid (#2879)
[lemmy.git] / crates / apub / src / api / list_comments.rs
1 use crate::{
2   api::{listing_type_with_default, PerformApub},
3   fetcher::resolve_actor_identifier,
4   objects::community::ApubCommunity,
5 };
6 use activitypub_federation::config::Data;
7 use lemmy_api_common::{
8   comment::{GetComments, GetCommentsResponse},
9   context::LemmyContext,
10   utils::{check_private_instance, local_user_view_from_jwt_opt},
11 };
12 use lemmy_db_schema::{
13   source::{comment::Comment, community::Community, local_site::LocalSite},
14   traits::Crud,
15 };
16 use lemmy_db_views::comment_view::CommentQuery;
17 use lemmy_utils::{error::LemmyError, ConnectionId};
18
19 #[async_trait::async_trait]
20 impl PerformApub for GetComments {
21   type Response = GetCommentsResponse;
22
23   #[tracing::instrument(skip(context, _websocket_id))]
24   async fn perform(
25     &self,
26     context: &Data<LemmyContext>,
27     _websocket_id: Option<ConnectionId>,
28   ) -> Result<GetCommentsResponse, LemmyError> {
29     let data: &GetComments = self;
30     let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await;
31     let local_site = LocalSite::read(context.pool()).await?;
32     check_private_instance(&local_user_view, &local_site)?;
33
34     let community_id = if let Some(name) = &data.community_name {
35       resolve_actor_identifier::<ApubCommunity, Community>(name, context, &None, true)
36         .await
37         .ok()
38         .map(|c| c.id)
39     } else {
40       data.community_id
41     };
42     let sort = data.sort;
43     let max_depth = data.max_depth;
44     let saved_only = data.saved_only;
45     let page = data.page;
46     let limit = data.limit;
47     let parent_id = data.parent_id;
48
49     let listing_type = listing_type_with_default(data.type_, &local_site, community_id)?;
50
51     // If a parent_id is given, fetch the comment to get the path
52     let parent_path = if let Some(parent_id) = parent_id {
53       Some(Comment::read(context.pool(), parent_id).await?.path)
54     } else {
55       None
56     };
57
58     let parent_path_cloned = parent_path.clone();
59     let post_id = data.post_id;
60     let local_user = local_user_view.map(|l| l.local_user);
61     let comments = CommentQuery::builder()
62       .pool(context.pool())
63       .listing_type(Some(listing_type))
64       .sort(sort)
65       .max_depth(max_depth)
66       .saved_only(saved_only)
67       .community_id(community_id)
68       .parent_path(parent_path_cloned)
69       .post_id(post_id)
70       .local_user(local_user.as_ref())
71       .page(page)
72       .limit(limit)
73       .build()
74       .list()
75       .await
76       .map_err(|e| LemmyError::from_error_message(e, "couldnt_get_comments"))?;
77
78     Ok(GetCommentsResponse { comments })
79   }
80 }