]> Untitled Git - lemmy.git/blob - crates/api/src/site.rs
Merge pull request #1566 from LemmyNet/additional_search_filters
[lemmy.git] / crates / api / src / site.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use anyhow::Context;
4 use lemmy_api_common::{
5   blocking,
6   build_federated_instances,
7   get_local_user_settings_view_from_jwt,
8   get_local_user_view_from_jwt,
9   get_local_user_view_from_jwt_opt,
10   is_admin,
11   site::*,
12   user_show_bot_accounts,
13   user_show_nsfw,
14 };
15 use lemmy_apub::fetcher::search::search_by_apub_id;
16 use lemmy_db_queries::{
17   from_opt_str_to_opt_enum,
18   source::site::Site_,
19   Crud,
20   ListingType,
21   SearchType,
22   SortType,
23 };
24 use lemmy_db_schema::source::{moderator::*, site::Site};
25 use lemmy_db_views::{
26   comment_view::CommentQueryBuilder,
27   post_view::PostQueryBuilder,
28   site_view::SiteView,
29 };
30 use lemmy_db_views_actor::{
31   community_view::CommunityQueryBuilder,
32   person_view::{PersonQueryBuilder, PersonViewSafe},
33 };
34 use lemmy_db_views_moderator::{
35   mod_add_community_view::ModAddCommunityView,
36   mod_add_view::ModAddView,
37   mod_ban_from_community_view::ModBanFromCommunityView,
38   mod_ban_view::ModBanView,
39   mod_lock_post_view::ModLockPostView,
40   mod_remove_comment_view::ModRemoveCommentView,
41   mod_remove_community_view::ModRemoveCommunityView,
42   mod_remove_post_view::ModRemovePostView,
43   mod_sticky_post_view::ModStickyPostView,
44 };
45 use lemmy_utils::{
46   location_info,
47   settings::structs::Settings,
48   version,
49   ApiError,
50   ConnectionId,
51   LemmyError,
52 };
53 use lemmy_websocket::LemmyContext;
54 use log::debug;
55
56 #[async_trait::async_trait(?Send)]
57 impl Perform for GetModlog {
58   type Response = GetModlogResponse;
59
60   async fn perform(
61     &self,
62     context: &Data<LemmyContext>,
63     _websocket_id: Option<ConnectionId>,
64   ) -> Result<GetModlogResponse, LemmyError> {
65     let data: &GetModlog = &self;
66
67     let community_id = data.community_id;
68     let mod_person_id = data.mod_person_id;
69     let page = data.page;
70     let limit = data.limit;
71     let removed_posts = blocking(context.pool(), move |conn| {
72       ModRemovePostView::list(conn, community_id, mod_person_id, page, limit)
73     })
74     .await??;
75
76     let locked_posts = blocking(context.pool(), move |conn| {
77       ModLockPostView::list(conn, community_id, mod_person_id, page, limit)
78     })
79     .await??;
80
81     let stickied_posts = blocking(context.pool(), move |conn| {
82       ModStickyPostView::list(conn, community_id, mod_person_id, page, limit)
83     })
84     .await??;
85
86     let removed_comments = blocking(context.pool(), move |conn| {
87       ModRemoveCommentView::list(conn, community_id, mod_person_id, page, limit)
88     })
89     .await??;
90
91     let banned_from_community = blocking(context.pool(), move |conn| {
92       ModBanFromCommunityView::list(conn, community_id, mod_person_id, page, limit)
93     })
94     .await??;
95
96     let added_to_community = blocking(context.pool(), move |conn| {
97       ModAddCommunityView::list(conn, community_id, mod_person_id, page, limit)
98     })
99     .await??;
100
101     // These arrays are only for the full modlog, when a community isn't given
102     let (removed_communities, banned, added) = if data.community_id.is_none() {
103       blocking(context.pool(), move |conn| {
104         Ok((
105           ModRemoveCommunityView::list(conn, mod_person_id, page, limit)?,
106           ModBanView::list(conn, mod_person_id, page, limit)?,
107           ModAddView::list(conn, mod_person_id, page, limit)?,
108         )) as Result<_, LemmyError>
109       })
110       .await??
111     } else {
112       (Vec::new(), Vec::new(), Vec::new())
113     };
114
115     // Return the jwt
116     Ok(GetModlogResponse {
117       removed_posts,
118       locked_posts,
119       stickied_posts,
120       removed_comments,
121       removed_communities,
122       banned_from_community,
123       banned,
124       added_to_community,
125       added,
126     })
127   }
128 }
129
130 #[async_trait::async_trait(?Send)]
131 impl Perform for Search {
132   type Response = SearchResponse;
133
134   async fn perform(
135     &self,
136     context: &Data<LemmyContext>,
137     _websocket_id: Option<ConnectionId>,
138   ) -> Result<SearchResponse, LemmyError> {
139     let data: &Search = &self;
140
141     match search_by_apub_id(&data.q, context).await {
142       Ok(r) => return Ok(r),
143       Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e),
144     }
145
146     let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
147
148     let show_nsfw = user_show_nsfw(&local_user_view);
149     let show_bot_accounts = user_show_bot_accounts(&local_user_view);
150
151     let person_id = local_user_view.map(|u| u.person.id);
152
153     let mut posts = Vec::new();
154     let mut comments = Vec::new();
155     let mut communities = Vec::new();
156     let mut users = Vec::new();
157
158     // TODO no clean / non-nsfw searching rn
159
160     let q = data.q.to_owned();
161     let page = data.page;
162     let limit = data.limit;
163     let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
164     let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.listing_type);
165     let search_type: SearchType = from_opt_str_to_opt_enum(&data.type_).unwrap_or(SearchType::All);
166     let community_id = data.community_id;
167     let community_name = data.community_name.to_owned();
168     let creator_id = data.creator_id;
169     match search_type {
170       SearchType::Posts => {
171         posts = blocking(context.pool(), move |conn| {
172           PostQueryBuilder::create(conn)
173             .sort(sort)
174             .show_nsfw(show_nsfw)
175             .show_bot_accounts(show_bot_accounts)
176             .listing_type(listing_type)
177             .community_id(community_id)
178             .community_name(community_name)
179             .creator_id(creator_id)
180             .my_person_id(person_id)
181             .search_term(q)
182             .page(page)
183             .limit(limit)
184             .list()
185         })
186         .await??;
187       }
188       SearchType::Comments => {
189         comments = blocking(context.pool(), move |conn| {
190           CommentQueryBuilder::create(&conn)
191             .sort(sort)
192             .listing_type(listing_type)
193             .search_term(q)
194             .show_bot_accounts(show_bot_accounts)
195             .community_id(community_id)
196             .community_name(community_name)
197             .creator_id(creator_id)
198             .my_person_id(person_id)
199             .page(page)
200             .limit(limit)
201             .list()
202         })
203         .await??;
204       }
205       SearchType::Communities => {
206         communities = blocking(context.pool(), move |conn| {
207           CommunityQueryBuilder::create(conn)
208             .sort(sort)
209             .listing_type(listing_type)
210             .search_term(q)
211             .my_person_id(person_id)
212             .page(page)
213             .limit(limit)
214             .list()
215         })
216         .await??;
217       }
218       SearchType::Users => {
219         users = blocking(context.pool(), move |conn| {
220           PersonQueryBuilder::create(conn)
221             .sort(sort)
222             .search_term(q)
223             .page(page)
224             .limit(limit)
225             .list()
226         })
227         .await??;
228       }
229       SearchType::All => {
230         // If the community or creator is included, dont search communities or users
231         let community_or_creator_included =
232           data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
233
234         posts = blocking(context.pool(), move |conn| {
235           PostQueryBuilder::create(conn)
236             .sort(sort)
237             .show_nsfw(show_nsfw)
238             .show_bot_accounts(show_bot_accounts)
239             .listing_type(listing_type)
240             .community_id(community_id)
241             .community_name(community_name)
242             .creator_id(creator_id)
243             .my_person_id(person_id)
244             .search_term(q)
245             .page(page)
246             .limit(limit)
247             .list()
248         })
249         .await??;
250
251         let q = data.q.to_owned();
252         let community_name = data.community_name.to_owned();
253
254         comments = blocking(context.pool(), move |conn| {
255           CommentQueryBuilder::create(conn)
256             .sort(sort)
257             .listing_type(listing_type)
258             .search_term(q)
259             .show_bot_accounts(show_bot_accounts)
260             .community_id(community_id)
261             .community_name(community_name)
262             .creator_id(creator_id)
263             .my_person_id(person_id)
264             .page(page)
265             .limit(limit)
266             .list()
267         })
268         .await??;
269
270         let q = data.q.to_owned();
271
272         communities = if community_or_creator_included {
273           vec![]
274         } else {
275           blocking(context.pool(), move |conn| {
276             CommunityQueryBuilder::create(conn)
277               .sort(sort)
278               .listing_type(listing_type)
279               .search_term(q)
280               .my_person_id(person_id)
281               .page(page)
282               .limit(limit)
283               .list()
284           })
285           .await??
286         };
287
288         let q = data.q.to_owned();
289
290         users = if community_or_creator_included {
291           vec![]
292         } else {
293           blocking(context.pool(), move |conn| {
294             PersonQueryBuilder::create(conn)
295               .sort(sort)
296               .search_term(q)
297               .page(page)
298               .limit(limit)
299               .list()
300           })
301           .await??
302         };
303       }
304       SearchType::Url => {
305         posts = blocking(context.pool(), move |conn| {
306           PostQueryBuilder::create(conn)
307             .sort(sort)
308             .show_nsfw(show_nsfw)
309             .show_bot_accounts(show_bot_accounts)
310             .listing_type(listing_type)
311             .my_person_id(person_id)
312             .community_id(community_id)
313             .community_name(community_name)
314             .creator_id(creator_id)
315             .url_search(q)
316             .page(page)
317             .limit(limit)
318             .list()
319         })
320         .await??;
321       }
322     };
323
324     // Return the jwt
325     Ok(SearchResponse {
326       type_: search_type.to_string(),
327       comments,
328       posts,
329       communities,
330       users,
331     })
332   }
333 }
334
335 #[async_trait::async_trait(?Send)]
336 impl Perform for TransferSite {
337   type Response = GetSiteResponse;
338
339   async fn perform(
340     &self,
341     context: &Data<LemmyContext>,
342     _websocket_id: Option<ConnectionId>,
343   ) -> Result<GetSiteResponse, LemmyError> {
344     let data: &TransferSite = &self;
345     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
346
347     is_admin(&local_user_view)?;
348
349     let read_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
350
351     // Make sure user is the creator
352     if read_site.creator_id != local_user_view.person.id {
353       return Err(ApiError::err("not_an_admin").into());
354     }
355
356     let new_creator_id = data.person_id;
357     let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
358     if blocking(context.pool(), transfer_site).await?.is_err() {
359       return Err(ApiError::err("couldnt_update_site").into());
360     };
361
362     // Mod tables
363     let form = ModAddForm {
364       mod_person_id: local_user_view.person.id,
365       other_person_id: data.person_id,
366       removed: Some(false),
367     };
368
369     blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
370
371     let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
372
373     let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
374     let creator_index = admins
375       .iter()
376       .position(|r| r.person.id == site_view.creator.id)
377       .context(location_info!())?;
378     let creator_person = admins.remove(creator_index);
379     admins.insert(0, creator_person);
380
381     let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
382     let federated_instances = build_federated_instances(context.pool()).await?;
383
384     let my_user = Some(get_local_user_settings_view_from_jwt(&data.auth, context.pool()).await?);
385
386     Ok(GetSiteResponse {
387       site_view: Some(site_view),
388       admins,
389       banned,
390       online: 0,
391       version: version::VERSION.to_string(),
392       my_user,
393       federated_instances,
394     })
395   }
396 }
397
398 #[async_trait::async_trait(?Send)]
399 impl Perform for GetSiteConfig {
400   type Response = GetSiteConfigResponse;
401
402   async fn perform(
403     &self,
404     context: &Data<LemmyContext>,
405     _websocket_id: Option<ConnectionId>,
406   ) -> Result<GetSiteConfigResponse, LemmyError> {
407     let data: &GetSiteConfig = &self;
408     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
409
410     // Only let admins read this
411     is_admin(&local_user_view)?;
412
413     let config_hjson = Settings::read_config_file()?;
414
415     Ok(GetSiteConfigResponse { config_hjson })
416   }
417 }
418
419 #[async_trait::async_trait(?Send)]
420 impl Perform for SaveSiteConfig {
421   type Response = GetSiteConfigResponse;
422
423   async fn perform(
424     &self,
425     context: &Data<LemmyContext>,
426     _websocket_id: Option<ConnectionId>,
427   ) -> Result<GetSiteConfigResponse, LemmyError> {
428     let data: &SaveSiteConfig = &self;
429     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
430
431     // Only let admins read this
432     is_admin(&local_user_view)?;
433
434     // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
435     let config_hjson = Settings::save_config_file(&data.config_hjson)
436       .map_err(|_| ApiError::err("couldnt_update_site"))?;
437
438     Ok(GetSiteConfigResponse { config_hjson })
439   }
440 }