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