]> Untitled Git - lemmy.git/blob - crates/api/src/site.rs
Blank out extra info for deleted or removed content. Fixes #1679 (#1680)
[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 };
13 use lemmy_apub::{build_actor_id_from_shortname, fetcher::search::search_by_apub_id, EndpointType};
14 use lemmy_db_queries::{
15   from_opt_str_to_opt_enum,
16   source::site::Site_,
17   Crud,
18   DeleteableOrRemoveable,
19   ListingType,
20   SearchType,
21   SortType,
22 };
23 use lemmy_db_schema::source::{moderator::*, site::Site};
24 use lemmy_db_views::{
25   comment_view::CommentQueryBuilder,
26   post_view::PostQueryBuilder,
27   site_view::SiteView,
28 };
29 use lemmy_db_views_actor::{
30   community_view::CommunityQueryBuilder,
31   person_view::{PersonQueryBuilder, PersonViewSafe},
32 };
33 use lemmy_db_views_moderator::{
34   mod_add_community_view::ModAddCommunityView,
35   mod_add_view::ModAddView,
36   mod_ban_from_community_view::ModBanFromCommunityView,
37   mod_ban_view::ModBanView,
38   mod_lock_post_view::ModLockPostView,
39   mod_remove_comment_view::ModRemoveCommentView,
40   mod_remove_community_view::ModRemoveCommunityView,
41   mod_remove_post_view::ModRemovePostView,
42   mod_sticky_post_view::ModStickyPostView,
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     // These arrays are only for the full modlog, when a community isn't given
101     let (removed_communities, banned, added) = if data.community_id.is_none() {
102       blocking(context.pool(), move |conn| {
103         Ok((
104           ModRemoveCommunityView::list(conn, mod_person_id, page, limit)?,
105           ModBanView::list(conn, mod_person_id, page, limit)?,
106           ModAddView::list(conn, mod_person_id, page, limit)?,
107         )) as Result<_, LemmyError>
108       })
109       .await??
110     } else {
111       (Vec::new(), Vec::new(), Vec::new())
112     };
113
114     // Return the jwt
115     Ok(GetModlogResponse {
116       removed_posts,
117       locked_posts,
118       stickied_posts,
119       removed_comments,
120       removed_communities,
121       banned_from_community,
122       banned,
123       added_to_community,
124       added,
125     })
126   }
127 }
128
129 #[async_trait::async_trait(?Send)]
130 impl Perform for Search {
131   type Response = SearchResponse;
132
133   async fn perform(
134     &self,
135     context: &Data<LemmyContext>,
136     _websocket_id: Option<ConnectionId>,
137   ) -> Result<SearchResponse, LemmyError> {
138     let data: &Search = self;
139
140     match search_by_apub_id(&data.q, context).await {
141       Ok(r) => return Ok(r),
142       Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e),
143     }
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 TransferSite {
371   type Response = GetSiteResponse;
372
373   async fn perform(
374     &self,
375     context: &Data<LemmyContext>,
376     _websocket_id: Option<ConnectionId>,
377   ) -> Result<GetSiteResponse, LemmyError> {
378     let data: &TransferSite = self;
379     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
380
381     is_admin(&local_user_view)?;
382
383     let read_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
384
385     // Make sure user is the creator
386     if read_site.creator_id != local_user_view.person.id {
387       return Err(ApiError::err("not_an_admin").into());
388     }
389
390     let new_creator_id = data.person_id;
391     let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
392     if blocking(context.pool(), transfer_site).await?.is_err() {
393       return Err(ApiError::err("couldnt_update_site").into());
394     };
395
396     // Mod tables
397     let form = ModAddForm {
398       mod_person_id: local_user_view.person.id,
399       other_person_id: data.person_id,
400       removed: Some(false),
401     };
402
403     blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
404
405     let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
406
407     let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
408     let creator_index = admins
409       .iter()
410       .position(|r| r.person.id == site_view.creator.id)
411       .context(location_info!())?;
412     let creator_person = admins.remove(creator_index);
413     admins.insert(0, creator_person);
414
415     let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
416     let federated_instances = build_federated_instances(context.pool()).await?;
417
418     let my_user = Some(get_local_user_settings_view_from_jwt(&data.auth, context.pool()).await?);
419
420     Ok(GetSiteResponse {
421       site_view: Some(site_view),
422       admins,
423       banned,
424       online: 0,
425       version: version::VERSION.to_string(),
426       my_user,
427       federated_instances,
428     })
429   }
430 }
431
432 #[async_trait::async_trait(?Send)]
433 impl Perform for GetSiteConfig {
434   type Response = GetSiteConfigResponse;
435
436   async fn perform(
437     &self,
438     context: &Data<LemmyContext>,
439     _websocket_id: Option<ConnectionId>,
440   ) -> Result<GetSiteConfigResponse, LemmyError> {
441     let data: &GetSiteConfig = self;
442     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
443
444     // Only let admins read this
445     is_admin(&local_user_view)?;
446
447     let config_hjson = Settings::read_config_file()?;
448
449     Ok(GetSiteConfigResponse { config_hjson })
450   }
451 }
452
453 #[async_trait::async_trait(?Send)]
454 impl Perform for SaveSiteConfig {
455   type Response = GetSiteConfigResponse;
456
457   async fn perform(
458     &self,
459     context: &Data<LemmyContext>,
460     _websocket_id: Option<ConnectionId>,
461   ) -> Result<GetSiteConfigResponse, LemmyError> {
462     let data: &SaveSiteConfig = self;
463     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
464
465     // Only let admins read this
466     is_admin(&local_user_view)?;
467
468     // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
469     let config_hjson = Settings::save_config_file(&data.config_hjson)
470       .map_err(|_| ApiError::err("couldnt_update_site"))?;
471
472     Ok(GetSiteConfigResponse { config_hjson })
473   }
474 }