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