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