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, post_to_person_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
30 .map(|luv| is_admin(luv).is_ok())
33 let mut posts = Vec::new();
34 let mut comments = Vec::new();
35 let mut communities = Vec::new();
36 let mut users = Vec::new();
38 // TODO no clean / non-nsfw searching rn
40 let q = data.q.clone();
42 let limit = data.limit;
44 let listing_type = data.listing_type;
45 let search_type = data.type_.unwrap_or(SearchType::All);
46 let community_id = if let Some(name) = &data.community_name {
48 resolve_actor_identifier::<ApubCommunity, Community>(name, &context, &local_user_view, false)
55 let creator_id = data.creator_id;
56 let local_user = local_user_view.as_ref().map(|l| l.local_user.clone());
58 SearchType::Posts => {
61 listing_type: (listing_type),
62 community_id: (community_id),
63 creator_id: (creator_id),
64 local_user: (local_user_view.as_ref()),
65 search_term: (Some(q)),
70 .list(&mut context.pool())
73 SearchType::Comments => {
74 comments = CommentQuery {
75 sort: (sort.map(post_to_comment_sort_type)),
76 listing_type: (listing_type),
77 search_term: (Some(q)),
78 community_id: (community_id),
79 creator_id: (creator_id),
80 local_user: (local_user_view.as_ref()),
85 .list(&mut context.pool())
88 SearchType::Communities => {
89 communities = CommunityQuery {
91 listing_type: (listing_type),
92 search_term: (Some(q)),
93 local_user: (local_user.as_ref()),
94 is_mod_or_admin: (is_admin),
99 .list(&mut context.pool())
102 SearchType::Users => {
103 users = PersonQuery {
104 sort: (sort.map(post_to_person_sort_type)),
105 search_term: (Some(q)),
109 .list(&mut context.pool())
113 // If the community or creator is included, dont search communities or users
114 let community_or_creator_included =
115 data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
117 let q = data.q.clone();
121 listing_type: (listing_type),
122 community_id: (community_id),
123 creator_id: (creator_id),
124 local_user: (local_user_view.as_ref()),
125 search_term: (Some(q)),
130 .list(&mut context.pool())
133 let q = data.q.clone();
135 comments = CommentQuery {
136 sort: (sort.map(post_to_comment_sort_type)),
137 listing_type: (listing_type),
138 search_term: (Some(q)),
139 community_id: (community_id),
140 creator_id: (creator_id),
141 local_user: (local_user_view.as_ref()),
146 .list(&mut context.pool())
149 let q = data.q.clone();
151 communities = if community_or_creator_included {
156 listing_type: (listing_type),
157 search_term: (Some(q)),
158 local_user: (local_user.as_ref()),
159 is_mod_or_admin: (is_admin),
164 .list(&mut context.pool())
168 let q = data.q.clone();
170 users = if community_or_creator_included {
174 sort: (sort.map(post_to_person_sort_type)),
175 search_term: (Some(q)),
179 .list(&mut context.pool())
186 listing_type: (listing_type),
187 community_id: (community_id),
188 creator_id: (creator_id),
189 url_search: (Some(q)),
194 .list(&mut context.pool())
200 Ok(Json(SearchResponse {