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.map(|l| l.local_user);
55 SearchType::Posts => {
58 listing_type: (listing_type),
59 community_id: (community_id),
60 creator_id: (creator_id),
61 local_user: (local_user.as_ref()),
62 search_term: (Some(q)),
63 is_mod_or_admin: (is_admin),
68 .list(&mut context.pool())
71 SearchType::Comments => {
72 comments = CommentQuery {
73 sort: (sort.map(post_to_comment_sort_type)),
74 listing_type: (listing_type),
75 search_term: (Some(q)),
76 community_id: (community_id),
77 creator_id: (creator_id),
78 local_user: (local_user.as_ref()),
83 .list(&mut context.pool())
86 SearchType::Communities => {
87 communities = CommunityQuery {
89 listing_type: (listing_type),
90 search_term: (Some(q)),
91 local_user: (local_user.as_ref()),
92 is_mod_or_admin: (is_admin),
97 .list(&mut context.pool())
100 SearchType::Users => {
101 users = PersonQuery {
103 search_term: (Some(q)),
107 .list(&mut context.pool())
111 // If the community or creator is included, dont search communities or users
112 let community_or_creator_included =
113 data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
115 let local_user_ = local_user.clone();
118 listing_type: (listing_type),
119 community_id: (community_id),
120 creator_id: (creator_id),
121 local_user: (local_user_.as_ref()),
122 search_term: (Some(q)),
123 is_mod_or_admin: (is_admin),
128 .list(&mut context.pool())
131 let q = data.q.clone();
133 let local_user_ = local_user.clone();
134 comments = CommentQuery {
135 sort: (sort.map(post_to_comment_sort_type)),
136 listing_type: (listing_type),
137 search_term: (Some(q)),
138 community_id: (community_id),
139 creator_id: (creator_id),
140 local_user: (local_user_.as_ref()),
145 .list(&mut context.pool())
148 let q = data.q.clone();
150 communities = if community_or_creator_included {
155 listing_type: (listing_type),
156 search_term: (Some(q)),
157 local_user: (local_user.as_ref()),
158 is_mod_or_admin: (is_admin),
163 .list(&mut context.pool())
167 let q = data.q.clone();
169 users = if community_or_creator_included {
174 search_term: (Some(q)),
178 .list(&mut context.pool())
185 listing_type: (listing_type),
186 community_id: (community_id),
187 creator_id: (creator_id),
188 url_search: (Some(q)),
189 is_mod_or_admin: (is_admin),
194 .list(&mut context.pool())
200 Ok(Json(SearchResponse {