1 use crate::{fetcher::resolve_actor_identifier, objects::community::ApubCommunity};
2 use activitypub_federation::config::Data;
3 use actix_web::web::{Json, Query};
4 use lemmy_api_common::{
6 site::{Search, SearchResponse},
7 utils::{check_private_instance, is_admin, local_user_view_from_jwt_opt},
10 source::{community::Community, local_site::LocalSite},
11 utils::post_to_comment_sort_type,
14 use lemmy_db_views::{comment_view::CommentQuery, post_view::PostQuery};
15 use lemmy_db_views_actor::{community_view::CommunityQuery, person_view::PersonQuery};
16 use lemmy_utils::error::LemmyError;
18 #[tracing::instrument(skip(context))]
21 context: Data<LemmyContext>,
22 ) -> Result<Json<SearchResponse>, LemmyError> {
23 let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await;
24 let local_site = LocalSite::read(&mut context.pool()).await?;
26 check_private_instance(&local_user_view, &local_site)?;
28 let is_admin = local_user_view.as_ref().map(|luv| is_admin(luv).is_ok());
30 let mut posts = Vec::new();
31 let mut comments = Vec::new();
32 let mut communities = Vec::new();
33 let mut users = Vec::new();
35 // TODO no clean / non-nsfw searching rn
37 let q = data.q.clone();
39 let limit = data.limit;
41 let listing_type = data.listing_type;
42 let search_type = data.type_.unwrap_or(SearchType::All);
43 let community_id = if let Some(name) = &data.community_name {
45 resolve_actor_identifier::<ApubCommunity, Community>(name, &context, &local_user_view, false)
52 let creator_id = data.creator_id;
53 let local_user = local_user_view.as_ref().map(|l| l.local_user.clone());
55 SearchType::Posts => {
58 listing_type: (listing_type),
59 community_id: (community_id),
60 creator_id: (creator_id),
61 local_user: (local_user_view.as_ref()),
62 search_term: (Some(q)),
67 .list(&mut context.pool())
70 SearchType::Comments => {
71 comments = CommentQuery {
72 sort: (sort.map(post_to_comment_sort_type)),
73 listing_type: (listing_type),
74 search_term: (Some(q)),
75 community_id: (community_id),
76 creator_id: (creator_id),
77 local_user: (local_user_view.as_ref()),
82 .list(&mut context.pool())
85 SearchType::Communities => {
86 communities = CommunityQuery {
88 listing_type: (listing_type),
89 search_term: (Some(q)),
90 local_user: (local_user.as_ref()),
91 is_mod_or_admin: (is_admin),
96 .list(&mut context.pool())
99 SearchType::Users => {
100 users = PersonQuery {
102 search_term: (Some(q)),
106 .list(&mut context.pool())
110 // If the community or creator is included, dont search communities or users
111 let community_or_creator_included =
112 data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
114 let q = data.q.clone();
118 listing_type: (listing_type),
119 community_id: (community_id),
120 creator_id: (creator_id),
121 local_user: (local_user_view.as_ref()),
122 search_term: (Some(q)),
127 .list(&mut context.pool())
130 let q = data.q.clone();
132 comments = CommentQuery {
133 sort: (sort.map(post_to_comment_sort_type)),
134 listing_type: (listing_type),
135 search_term: (Some(q)),
136 community_id: (community_id),
137 creator_id: (creator_id),
138 local_user: (local_user_view.as_ref()),
143 .list(&mut context.pool())
146 let q = data.q.clone();
148 communities = if community_or_creator_included {
153 listing_type: (listing_type),
154 search_term: (Some(q)),
155 local_user: (local_user.as_ref()),
156 is_mod_or_admin: (is_admin),
161 .list(&mut context.pool())
165 let q = data.q.clone();
167 users = if community_or_creator_included {
172 search_term: (Some(q)),
176 .list(&mut context.pool())
183 listing_type: (listing_type),
184 community_id: (community_id),
185 creator_id: (creator_id),
186 url_search: (Some(q)),
191 .list(&mut context.pool())
197 Ok(Json(SearchResponse {