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