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