]> Untitled Git - lemmy.git/blob - crates/apub/src/api/search.rs
aaea69e07944fd343988989a40749ea76a119854
[lemmy.git] / crates / apub / src / api / search.rs
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::{
5   context::LemmyContext,
6   site::{Search, SearchResponse},
7   utils::{check_private_instance, is_admin, local_user_view_from_jwt_opt},
8 };
9 use lemmy_db_schema::{
10   source::{community::Community, local_site::LocalSite},
11   utils::{post_to_comment_sort_type, post_to_person_sort_type},
12   SearchType,
13 };
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;
17
18 #[tracing::instrument(skip(context))]
19 pub async fn search(
20   data: Query<Search>,
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?;
25
26   check_private_instance(&local_user_view, &local_site)?;
27
28   let is_admin = local_user_view.as_ref().map(|luv| is_admin(luv).is_ok());
29
30   let mut posts = Vec::new();
31   let mut comments = Vec::new();
32   let mut communities = Vec::new();
33   let mut users = Vec::new();
34
35   // TODO no clean / non-nsfw searching rn
36
37   let q = data.q.clone();
38   let page = data.page;
39   let limit = data.limit;
40   let sort = data.sort;
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 {
44     Some(
45       resolve_actor_identifier::<ApubCommunity, Community>(name, &context, &local_user_view, false)
46         .await?,
47     )
48     .map(|c| c.id)
49   } else {
50     data.community_id
51   };
52   let creator_id = data.creator_id;
53   let local_user = local_user_view.as_ref().map(|l| l.local_user.clone());
54   match search_type {
55     SearchType::Posts => {
56       posts = PostQuery {
57         sort: (sort),
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)),
63         page: (page),
64         limit: (limit),
65         ..Default::default()
66       }
67       .list(&mut context.pool())
68       .await?;
69     }
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()),
78         page: (page),
79         limit: (limit),
80         ..Default::default()
81       }
82       .list(&mut context.pool())
83       .await?;
84     }
85     SearchType::Communities => {
86       communities = CommunityQuery {
87         sort: (sort),
88         listing_type: (listing_type),
89         search_term: (Some(q)),
90         local_user: (local_user.as_ref()),
91         is_mod_or_admin: (is_admin),
92         page: (page),
93         limit: (limit),
94         ..Default::default()
95       }
96       .list(&mut context.pool())
97       .await?;
98     }
99     SearchType::Users => {
100       users = PersonQuery {
101         sort: (sort.map(post_to_person_sort_type)),
102         search_term: (Some(q)),
103         page: (page),
104         limit: (limit),
105       }
106       .list(&mut context.pool())
107       .await?;
108     }
109     SearchType::All => {
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();
113
114       let q = data.q.clone();
115
116       posts = PostQuery {
117         sort: (sort),
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)),
123         page: (page),
124         limit: (limit),
125         ..Default::default()
126       }
127       .list(&mut context.pool())
128       .await?;
129
130       let q = data.q.clone();
131
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()),
139         page: (page),
140         limit: (limit),
141         ..Default::default()
142       }
143       .list(&mut context.pool())
144       .await?;
145
146       let q = data.q.clone();
147
148       communities = if community_or_creator_included {
149         vec![]
150       } else {
151         CommunityQuery {
152           sort: (sort),
153           listing_type: (listing_type),
154           search_term: (Some(q)),
155           local_user: (local_user.as_ref()),
156           is_mod_or_admin: (is_admin),
157           page: (page),
158           limit: (limit),
159           ..Default::default()
160         }
161         .list(&mut context.pool())
162         .await?
163       };
164
165       let q = data.q.clone();
166
167       users = if community_or_creator_included {
168         vec![]
169       } else {
170         PersonQuery {
171           sort: (sort.map(post_to_person_sort_type)),
172           search_term: (Some(q)),
173           page: (page),
174           limit: (limit),
175         }
176         .list(&mut context.pool())
177         .await?
178       };
179     }
180     SearchType::Url => {
181       posts = PostQuery {
182         sort: (sort),
183         listing_type: (listing_type),
184         community_id: (community_id),
185         creator_id: (creator_id),
186         url_search: (Some(q)),
187         page: (page),
188         limit: (limit),
189         ..Default::default()
190       }
191       .list(&mut context.pool())
192       .await?;
193     }
194   };
195
196   // Return the jwt
197   Ok(Json(SearchResponse {
198     type_: search_type,
199     comments,
200     posts,
201     communities,
202     users,
203   }))
204 }