2 use actix_web::web::Data;
5 use lemmy_api_common::{
7 build_federated_instances,
8 get_local_user_view_from_jwt,
9 get_local_user_view_from_jwt_opt,
14 build_actor_id_from_shortname,
15 fetcher::search::{search_by_apub_id, SearchableObjects},
18 use lemmy_db_schema::{
19 from_opt_str_to_opt_enum,
21 source::{moderator::*, site::Site},
22 traits::{Crud, DeleteableOrRemoveable},
29 comment_view::{CommentQueryBuilder, CommentView},
30 post_view::{PostQueryBuilder, PostView},
33 use lemmy_db_views_actor::{
34 community_view::{CommunityQueryBuilder, CommunityView},
35 person_view::{PersonQueryBuilder, PersonViewSafe},
37 use lemmy_db_views_moderator::{
38 mod_add_community_view::ModAddCommunityView,
39 mod_add_view::ModAddView,
40 mod_ban_from_community_view::ModBanFromCommunityView,
41 mod_ban_view::ModBanView,
42 mod_lock_post_view::ModLockPostView,
43 mod_remove_comment_view::ModRemoveCommentView,
44 mod_remove_community_view::ModRemoveCommunityView,
45 mod_remove_post_view::ModRemovePostView,
46 mod_sticky_post_view::ModStickyPostView,
47 mod_transfer_community_view::ModTransferCommunityView,
51 settings::structs::Settings,
57 use lemmy_websocket::LemmyContext;
59 #[async_trait::async_trait(?Send)]
60 impl Perform for GetModlog {
61 type Response = GetModlogResponse;
65 context: &Data<LemmyContext>,
66 _websocket_id: Option<ConnectionId>,
67 ) -> Result<GetModlogResponse, LemmyError> {
68 let data: &GetModlog = self;
70 let community_id = data.community_id;
71 let mod_person_id = data.mod_person_id;
73 let limit = data.limit;
74 let removed_posts = blocking(context.pool(), move |conn| {
75 ModRemovePostView::list(conn, community_id, mod_person_id, page, limit)
79 let locked_posts = blocking(context.pool(), move |conn| {
80 ModLockPostView::list(conn, community_id, mod_person_id, page, limit)
84 let stickied_posts = blocking(context.pool(), move |conn| {
85 ModStickyPostView::list(conn, community_id, mod_person_id, page, limit)
89 let removed_comments = blocking(context.pool(), move |conn| {
90 ModRemoveCommentView::list(conn, community_id, mod_person_id, page, limit)
94 let banned_from_community = blocking(context.pool(), move |conn| {
95 ModBanFromCommunityView::list(conn, community_id, mod_person_id, page, limit)
99 let added_to_community = blocking(context.pool(), move |conn| {
100 ModAddCommunityView::list(conn, community_id, mod_person_id, page, limit)
104 let transferred_to_community = blocking(context.pool(), move |conn| {
105 ModTransferCommunityView::list(conn, community_id, mod_person_id, page, limit)
109 // These arrays are only for the full modlog, when a community isn't given
110 let (removed_communities, banned, added) = if data.community_id.is_none() {
111 blocking(context.pool(), move |conn| {
113 ModRemoveCommunityView::list(conn, mod_person_id, page, limit)?,
114 ModBanView::list(conn, mod_person_id, page, limit)?,
115 ModAddView::list(conn, mod_person_id, page, limit)?,
116 )) as Result<_, LemmyError>
120 (Vec::new(), Vec::new(), Vec::new())
124 Ok(GetModlogResponse {
130 banned_from_community,
134 transferred_to_community,
139 #[async_trait::async_trait(?Send)]
140 impl Perform for Search {
141 type Response = SearchResponse;
145 context: &Data<LemmyContext>,
146 _websocket_id: Option<ConnectionId>,
147 ) -> Result<SearchResponse, LemmyError> {
148 let data: &Search = self;
150 let local_user_view =
151 get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
153 let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
154 let show_bot_accounts = local_user_view
156 .map(|t| t.local_user.show_bot_accounts);
157 let show_read_posts = local_user_view
159 .map(|t| t.local_user.show_read_posts);
161 let person_id = local_user_view.map(|u| u.person.id);
163 let mut posts = Vec::new();
164 let mut comments = Vec::new();
165 let mut communities = Vec::new();
166 let mut users = Vec::new();
168 // TODO no clean / non-nsfw searching rn
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
180 .map(|t| build_actor_id_from_shortname(EndpointType::Community, t, &context.settings()).ok())
182 let creator_id = data.creator_id;
184 SearchType::Posts => {
185 posts = blocking(context.pool(), move |conn| {
186 PostQueryBuilder::create(conn)
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)
203 SearchType::Comments => {
204 comments = blocking(context.pool(), move |conn| {
205 CommentQueryBuilder::create(conn)
207 .listing_type(listing_type)
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)
220 SearchType::Communities => {
221 communities = blocking(context.pool(), move |conn| {
222 CommunityQueryBuilder::create(conn)
224 .listing_type(listing_type)
226 .my_person_id(person_id)
233 SearchType::Users => {
234 users = blocking(context.pool(), move |conn| {
235 PersonQueryBuilder::create(conn)
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();
250 posts = blocking(context.pool(), move |conn| {
251 PostQueryBuilder::create(conn)
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)
268 let q = data.q.to_owned();
269 let community_actor_id = community_actor_id.to_owned();
271 comments = blocking(context.pool(), move |conn| {
272 CommentQueryBuilder::create(conn)
274 .listing_type(listing_type)
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)
287 let q = data.q.to_owned();
289 communities = if community_or_creator_included {
292 blocking(context.pool(), move |conn| {
293 CommunityQueryBuilder::create(conn)
295 .listing_type(listing_type)
297 .my_person_id(person_id)
305 let q = data.q.to_owned();
307 users = if community_or_creator_included {
310 blocking(context.pool(), move |conn| {
311 PersonQueryBuilder::create(conn)
322 posts = blocking(context.pool(), move |conn| {
323 PostQueryBuilder::create(conn)
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)
342 // Blank out deleted or removed info for non logged in users
343 if person_id.is_none() {
344 for cv in communities
346 .filter(|cv| cv.community.deleted || cv.community.removed)
348 cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
353 .filter(|p| p.post.deleted || p.post.removed)
355 pv.post = pv.to_owned().post.blank_out_deleted_or_removed_info();
360 .filter(|cv| cv.comment.deleted || cv.comment.removed)
362 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
368 type_: search_type.to_string(),
377 #[async_trait::async_trait(?Send)]
378 impl Perform for ResolveObject {
379 type Response = ResolveObjectResponse;
383 context: &Data<LemmyContext>,
384 _websocket_id: Option<ConnectionId>,
385 ) -> Result<ResolveObjectResponse, LemmyError> {
386 let local_user_view =
387 get_local_user_view_from_jwt_opt(&self.auth, context.pool(), context.secret()).await?;
388 let res = search_by_apub_id(&self.q, context)
390 .map_err(|e| ApiError::err("couldnt_find_object", e))?;
391 convert_response(res, local_user_view.map(|l| l.person.id), context.pool())
393 .map_err(|e| ApiError::err("couldnt_find_object", e).into())
397 async fn convert_response(
398 object: SearchableObjects,
399 user_id: Option<PersonId>,
401 ) -> Result<ResolveObjectResponse, LemmyError> {
402 let removed_or_deleted;
403 let mut res = ResolveObjectResponse {
409 use SearchableObjects::*;
412 removed_or_deleted = p.deleted;
413 res.person = Some(blocking(pool, move |conn| PersonViewSafe::read(conn, p.id)).await??)
416 removed_or_deleted = c.deleted || c.removed;
418 Some(blocking(pool, move |conn| CommunityView::read(conn, c.id, user_id)).await??)
421 removed_or_deleted = p.deleted || p.removed;
422 res.post = Some(blocking(pool, move |conn| PostView::read(conn, p.id, user_id)).await??)
425 removed_or_deleted = c.deleted || c.removed;
426 res.comment = Some(blocking(pool, move |conn| CommentView::read(conn, c.id, user_id)).await??)
429 // if the object was deleted from database, dont return it
430 if removed_or_deleted {
431 return Err(NotFound {}.into());
436 #[async_trait::async_trait(?Send)]
437 impl Perform for TransferSite {
438 type Response = GetSiteResponse;
442 context: &Data<LemmyContext>,
443 _websocket_id: Option<ConnectionId>,
444 ) -> Result<GetSiteResponse, LemmyError> {
445 let data: &TransferSite = self;
446 let local_user_view =
447 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
449 is_admin(&local_user_view)?;
451 let read_site = blocking(context.pool(), Site::read_simple).await??;
453 // Make sure user is the creator
454 if read_site.creator_id != local_user_view.person.id {
455 return Err(ApiError::err_plain("not_an_admin").into());
458 let new_creator_id = data.person_id;
459 let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
460 blocking(context.pool(), transfer_site)
462 .map_err(|e| ApiError::err("couldnt_update_site", e))?;
465 let form = ModAddForm {
466 mod_person_id: local_user_view.person.id,
467 other_person_id: data.person_id,
468 removed: Some(false),
471 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
473 let site_view = blocking(context.pool(), SiteView::read).await??;
475 let mut admins = blocking(context.pool(), PersonViewSafe::admins).await??;
476 let creator_index = admins
478 .position(|r| r.person.id == site_view.creator.id)
479 .context(location_info!())?;
480 let creator_person = admins.remove(creator_index);
481 admins.insert(0, creator_person);
483 let banned = blocking(context.pool(), PersonViewSafe::banned).await??;
484 let federated_instances = build_federated_instances(
486 &context.settings().federation,
487 &context.settings().hostname,
492 site_view: Some(site_view),
496 version: version::VERSION.to_string(),
503 #[async_trait::async_trait(?Send)]
504 impl Perform for GetSiteConfig {
505 type Response = GetSiteConfigResponse;
509 context: &Data<LemmyContext>,
510 _websocket_id: Option<ConnectionId>,
511 ) -> Result<GetSiteConfigResponse, LemmyError> {
512 let data: &GetSiteConfig = self;
513 let local_user_view =
514 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
516 // Only let admins read this
517 is_admin(&local_user_view)?;
519 let config_hjson = Settings::read_config_file()?;
521 Ok(GetSiteConfigResponse { config_hjson })
525 #[async_trait::async_trait(?Send)]
526 impl Perform for SaveSiteConfig {
527 type Response = GetSiteConfigResponse;
531 context: &Data<LemmyContext>,
532 _websocket_id: Option<ConnectionId>,
533 ) -> Result<GetSiteConfigResponse, LemmyError> {
534 let data: &SaveSiteConfig = self;
535 let local_user_view =
536 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
538 // Only let admins read this
539 is_admin(&local_user_view)?;
541 // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
542 let config_hjson = Settings::save_config_file(&data.config_hjson)
543 .map_err(|e| ApiError::err("couldnt_update_site", e))?;
545 Ok(GetSiteConfigResponse { config_hjson })