]> Untitled Git - lemmy.git/blob - crates/api/src/site.rs
Merge pull request #1666 from LemmyNet/resolve-endpoint
[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     let res = search_by_apub_id(&self.q, local_user_view, context)
380       .await
381       .map_err(|_| ApiError::err("couldnt_find_object"))?;
382     Ok(res)
383   }
384 }
385
386 #[async_trait::async_trait(?Send)]
387 impl Perform for TransferSite {
388   type Response = GetSiteResponse;
389
390   async fn perform(
391     &self,
392     context: &Data<LemmyContext>,
393     _websocket_id: Option<ConnectionId>,
394   ) -> Result<GetSiteResponse, LemmyError> {
395     let data: &TransferSite = self;
396     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
397
398     is_admin(&local_user_view)?;
399
400     let read_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
401
402     // Make sure user is the creator
403     if read_site.creator_id != local_user_view.person.id {
404       return Err(ApiError::err("not_an_admin").into());
405     }
406
407     let new_creator_id = data.person_id;
408     let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
409     if blocking(context.pool(), transfer_site).await?.is_err() {
410       return Err(ApiError::err("couldnt_update_site").into());
411     };
412
413     // Mod tables
414     let form = ModAddForm {
415       mod_person_id: local_user_view.person.id,
416       other_person_id: data.person_id,
417       removed: Some(false),
418     };
419
420     blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
421
422     let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
423
424     let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
425     let creator_index = admins
426       .iter()
427       .position(|r| r.person.id == site_view.creator.id)
428       .context(location_info!())?;
429     let creator_person = admins.remove(creator_index);
430     admins.insert(0, creator_person);
431
432     let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
433     let federated_instances = build_federated_instances(context.pool()).await?;
434
435     Ok(GetSiteResponse {
436       site_view: Some(site_view),
437       admins,
438       banned,
439       online: 0,
440       version: version::VERSION.to_string(),
441       my_user: None,
442       federated_instances,
443     })
444   }
445 }
446
447 #[async_trait::async_trait(?Send)]
448 impl Perform for GetSiteConfig {
449   type Response = GetSiteConfigResponse;
450
451   async fn perform(
452     &self,
453     context: &Data<LemmyContext>,
454     _websocket_id: Option<ConnectionId>,
455   ) -> Result<GetSiteConfigResponse, LemmyError> {
456     let data: &GetSiteConfig = self;
457     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
458
459     // Only let admins read this
460     is_admin(&local_user_view)?;
461
462     let config_hjson = Settings::read_config_file()?;
463
464     Ok(GetSiteConfigResponse { config_hjson })
465   }
466 }
467
468 #[async_trait::async_trait(?Send)]
469 impl Perform for SaveSiteConfig {
470   type Response = GetSiteConfigResponse;
471
472   async fn perform(
473     &self,
474     context: &Data<LemmyContext>,
475     _websocket_id: Option<ConnectionId>,
476   ) -> Result<GetSiteConfigResponse, LemmyError> {
477     let data: &SaveSiteConfig = self;
478     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
479
480     // Only let admins read this
481     is_admin(&local_user_view)?;
482
483     // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
484     let config_hjson = Settings::save_config_file(&data.config_hjson)
485       .map_err(|_| ApiError::err("couldnt_update_site"))?;
486
487     Ok(GetSiteConfigResponse { config_hjson })
488   }
489 }