]> Untitled Git - lemmy.git/blob - crates/apub/src/api/search.rs
2c65f302d51e9c67947d43cef183ceaf403c9b1f
[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,
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.map(|l| l.local_user);
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.as_ref()),
62         search_term: (Some(q)),
63         is_mod_or_admin: (is_admin),
64         page: (page),
65         limit: (limit),
66         ..Default::default()
67       }
68       .list(&mut context.pool())
69       .await?;
70     }
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()),
79         page: (page),
80         limit: (limit),
81         ..Default::default()
82       }
83       .list(&mut context.pool())
84       .await?;
85     }
86     SearchType::Communities => {
87       communities = CommunityQuery {
88         sort: (sort),
89         listing_type: (listing_type),
90         search_term: (Some(q)),
91         local_user: (local_user.as_ref()),
92         is_mod_or_admin: (is_admin),
93         page: (page),
94         limit: (limit),
95         ..Default::default()
96       }
97       .list(&mut context.pool())
98       .await?;
99     }
100     SearchType::Users => {
101       users = PersonQuery {
102         sort: (sort),
103         search_term: (Some(q)),
104         page: (page),
105         limit: (limit),
106       }
107       .list(&mut context.pool())
108       .await?;
109     }
110     SearchType::All => {
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();
114
115       let local_user_ = local_user.clone();
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_.as_ref()),
122         search_term: (Some(q)),
123         is_mod_or_admin: (is_admin),
124         page: (page),
125         limit: (limit),
126         ..Default::default()
127       }
128       .list(&mut context.pool())
129       .await?;
130
131       let q = data.q.clone();
132
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()),
141         page: (page),
142         limit: (limit),
143         ..Default::default()
144       }
145       .list(&mut context.pool())
146       .await?;
147
148       let q = data.q.clone();
149
150       communities = if community_or_creator_included {
151         vec![]
152       } else {
153         CommunityQuery {
154           sort: (sort),
155           listing_type: (listing_type),
156           search_term: (Some(q)),
157           local_user: (local_user.as_ref()),
158           is_mod_or_admin: (is_admin),
159           page: (page),
160           limit: (limit),
161           ..Default::default()
162         }
163         .list(&mut context.pool())
164         .await?
165       };
166
167       let q = data.q.clone();
168
169       users = if community_or_creator_included {
170         vec![]
171       } else {
172         PersonQuery {
173           sort: (sort),
174           search_term: (Some(q)),
175           page: (page),
176           limit: (limit),
177         }
178         .list(&mut context.pool())
179         .await?
180       };
181     }
182     SearchType::Url => {
183       posts = PostQuery {
184         sort: (sort),
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),
190         page: (page),
191         limit: (limit),
192         ..Default::default()
193       }
194       .list(&mut context.pool())
195       .await?;
196     }
197   };
198
199   // Return the jwt
200   Ok(Json(SearchResponse {
201     type_: search_type,
202     comments,
203     posts,
204     communities,
205     users,
206   }))
207 }