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_queries::{
19 from_opt_str_to_opt_enum,
23 DeleteableOrRemoveable,
28 use lemmy_db_schema::{
29 source::{moderator::*, site::Site},
33 comment_view::{CommentQueryBuilder, CommentView},
34 post_view::{PostQueryBuilder, PostView},
37 use lemmy_db_views_actor::{
38 community_view::{CommunityQueryBuilder, CommunityView},
39 person_view::{PersonQueryBuilder, PersonViewSafe},
41 use lemmy_db_views_moderator::{
42 mod_add_community_view::ModAddCommunityView,
43 mod_add_view::ModAddView,
44 mod_ban_from_community_view::ModBanFromCommunityView,
45 mod_ban_view::ModBanView,
46 mod_lock_post_view::ModLockPostView,
47 mod_remove_comment_view::ModRemoveCommentView,
48 mod_remove_community_view::ModRemoveCommunityView,
49 mod_remove_post_view::ModRemovePostView,
50 mod_sticky_post_view::ModStickyPostView,
51 mod_transfer_community_view::ModTransferCommunityView,
55 settings::structs::Settings,
61 use lemmy_websocket::LemmyContext;
63 #[async_trait::async_trait(?Send)]
64 impl Perform for GetModlog {
65 type Response = GetModlogResponse;
69 context: &Data<LemmyContext>,
70 _websocket_id: Option<ConnectionId>,
71 ) -> Result<GetModlogResponse, LemmyError> {
72 let data: &GetModlog = self;
74 let community_id = data.community_id;
75 let mod_person_id = data.mod_person_id;
77 let limit = data.limit;
78 let removed_posts = blocking(context.pool(), move |conn| {
79 ModRemovePostView::list(conn, community_id, mod_person_id, page, limit)
83 let locked_posts = blocking(context.pool(), move |conn| {
84 ModLockPostView::list(conn, community_id, mod_person_id, page, limit)
88 let stickied_posts = blocking(context.pool(), move |conn| {
89 ModStickyPostView::list(conn, community_id, mod_person_id, page, limit)
93 let removed_comments = blocking(context.pool(), move |conn| {
94 ModRemoveCommentView::list(conn, community_id, mod_person_id, page, limit)
98 let banned_from_community = blocking(context.pool(), move |conn| {
99 ModBanFromCommunityView::list(conn, community_id, mod_person_id, page, limit)
103 let added_to_community = blocking(context.pool(), move |conn| {
104 ModAddCommunityView::list(conn, community_id, mod_person_id, page, limit)
108 let transferred_to_community = blocking(context.pool(), move |conn| {
109 ModTransferCommunityView::list(conn, community_id, mod_person_id, page, limit)
113 // These arrays are only for the full modlog, when a community isn't given
114 let (removed_communities, banned, added) = if data.community_id.is_none() {
115 blocking(context.pool(), move |conn| {
117 ModRemoveCommunityView::list(conn, mod_person_id, page, limit)?,
118 ModBanView::list(conn, mod_person_id, page, limit)?,
119 ModAddView::list(conn, mod_person_id, page, limit)?,
120 )) as Result<_, LemmyError>
124 (Vec::new(), Vec::new(), Vec::new())
128 Ok(GetModlogResponse {
134 banned_from_community,
138 transferred_to_community,
143 #[async_trait::async_trait(?Send)]
144 impl Perform for Search {
145 type Response = SearchResponse;
149 context: &Data<LemmyContext>,
150 _websocket_id: Option<ConnectionId>,
151 ) -> Result<SearchResponse, LemmyError> {
152 let data: &Search = self;
154 let local_user_view =
155 get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
157 let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
158 let show_bot_accounts = local_user_view
160 .map(|t| t.local_user.show_bot_accounts);
161 let show_read_posts = local_user_view
163 .map(|t| t.local_user.show_read_posts);
165 let person_id = local_user_view.map(|u| u.person.id);
167 let mut posts = Vec::new();
168 let mut comments = Vec::new();
169 let mut communities = Vec::new();
170 let mut users = Vec::new();
172 // TODO no clean / non-nsfw searching rn
174 let q = data.q.to_owned();
175 let page = data.page;
176 let limit = data.limit;
177 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
178 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.listing_type);
179 let search_type: SearchType = from_opt_str_to_opt_enum(&data.type_).unwrap_or(SearchType::All);
180 let community_id = data.community_id;
181 let community_actor_id = data
184 .map(|t| build_actor_id_from_shortname(EndpointType::Community, t, &context.settings()).ok())
186 let creator_id = data.creator_id;
188 SearchType::Posts => {
189 posts = blocking(context.pool(), move |conn| {
190 PostQueryBuilder::create(conn)
192 .show_nsfw(show_nsfw)
193 .show_bot_accounts(show_bot_accounts)
194 .show_read_posts(show_read_posts)
195 .listing_type(listing_type)
196 .community_id(community_id)
197 .community_actor_id(community_actor_id)
198 .creator_id(creator_id)
199 .my_person_id(person_id)
207 SearchType::Comments => {
208 comments = blocking(context.pool(), move |conn| {
209 CommentQueryBuilder::create(conn)
211 .listing_type(listing_type)
213 .show_bot_accounts(show_bot_accounts)
214 .community_id(community_id)
215 .community_actor_id(community_actor_id)
216 .creator_id(creator_id)
217 .my_person_id(person_id)
224 SearchType::Communities => {
225 communities = blocking(context.pool(), move |conn| {
226 CommunityQueryBuilder::create(conn)
228 .listing_type(listing_type)
230 .my_person_id(person_id)
237 SearchType::Users => {
238 users = blocking(context.pool(), move |conn| {
239 PersonQueryBuilder::create(conn)
249 // If the community or creator is included, dont search communities or users
250 let community_or_creator_included =
251 data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
252 let community_actor_id_2 = community_actor_id.to_owned();
254 posts = blocking(context.pool(), move |conn| {
255 PostQueryBuilder::create(conn)
257 .show_nsfw(show_nsfw)
258 .show_bot_accounts(show_bot_accounts)
259 .show_read_posts(show_read_posts)
260 .listing_type(listing_type)
261 .community_id(community_id)
262 .community_actor_id(community_actor_id_2)
263 .creator_id(creator_id)
264 .my_person_id(person_id)
272 let q = data.q.to_owned();
273 let community_actor_id = community_actor_id.to_owned();
275 comments = blocking(context.pool(), move |conn| {
276 CommentQueryBuilder::create(conn)
278 .listing_type(listing_type)
280 .show_bot_accounts(show_bot_accounts)
281 .community_id(community_id)
282 .community_actor_id(community_actor_id)
283 .creator_id(creator_id)
284 .my_person_id(person_id)
291 let q = data.q.to_owned();
293 communities = if community_or_creator_included {
296 blocking(context.pool(), move |conn| {
297 CommunityQueryBuilder::create(conn)
299 .listing_type(listing_type)
301 .my_person_id(person_id)
309 let q = data.q.to_owned();
311 users = if community_or_creator_included {
314 blocking(context.pool(), move |conn| {
315 PersonQueryBuilder::create(conn)
326 posts = blocking(context.pool(), move |conn| {
327 PostQueryBuilder::create(conn)
329 .show_nsfw(show_nsfw)
330 .show_bot_accounts(show_bot_accounts)
331 .show_read_posts(show_read_posts)
332 .listing_type(listing_type)
333 .my_person_id(person_id)
334 .community_id(community_id)
335 .community_actor_id(community_actor_id)
336 .creator_id(creator_id)
346 // Blank out deleted or removed info
349 .filter(|cv| cv.comment.deleted || cv.comment.removed)
351 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
354 for cv in communities
356 .filter(|cv| cv.community.deleted || cv.community.removed)
358 cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
363 .filter(|p| p.post.deleted || p.post.removed)
365 pv.post = pv.to_owned().post.blank_out_deleted_or_removed_info();
370 type_: search_type.to_string(),
379 #[async_trait::async_trait(?Send)]
380 impl Perform for ResolveObject {
381 type Response = ResolveObjectResponse;
385 context: &Data<LemmyContext>,
386 _websocket_id: Option<ConnectionId>,
387 ) -> Result<ResolveObjectResponse, LemmyError> {
388 let local_user_view =
389 get_local_user_view_from_jwt_opt(&self.auth, context.pool(), context.secret()).await?;
390 let res = search_by_apub_id(&self.q, context)
392 .map_err(|_| ApiError::err("couldnt_find_object"))?;
393 convert_response(res, local_user_view.map(|l| l.person.id), context.pool())
395 .map_err(|_| ApiError::err("couldnt_find_object").into())
399 async fn convert_response(
400 object: SearchableObjects,
401 user_id: Option<PersonId>,
403 ) -> Result<ResolveObjectResponse, LemmyError> {
404 let removed_or_deleted;
405 let mut res = ResolveObjectResponse {
411 use SearchableObjects::*;
414 removed_or_deleted = p.deleted;
415 res.person = Some(blocking(pool, move |conn| PersonViewSafe::read(conn, p.id)).await??)
418 removed_or_deleted = c.deleted || c.removed;
420 Some(blocking(pool, move |conn| CommunityView::read(conn, c.id, user_id)).await??)
423 removed_or_deleted = p.deleted || p.removed;
424 res.post = Some(blocking(pool, move |conn| PostView::read(conn, p.id, user_id)).await??)
427 removed_or_deleted = c.deleted || c.removed;
428 res.comment = Some(blocking(pool, move |conn| CommentView::read(conn, c.id, user_id)).await??)
431 // if the object was deleted from database, dont return it
432 if removed_or_deleted {
433 return Err(NotFound {}.into());
438 #[async_trait::async_trait(?Send)]
439 impl Perform for TransferSite {
440 type Response = GetSiteResponse;
444 context: &Data<LemmyContext>,
445 _websocket_id: Option<ConnectionId>,
446 ) -> Result<GetSiteResponse, LemmyError> {
447 let data: &TransferSite = self;
448 let local_user_view =
449 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
451 is_admin(&local_user_view)?;
453 let read_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
455 // Make sure user is the creator
456 if read_site.creator_id != local_user_view.person.id {
457 return Err(ApiError::err("not_an_admin").into());
460 let new_creator_id = data.person_id;
461 let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
462 if blocking(context.pool(), transfer_site).await?.is_err() {
463 return Err(ApiError::err("couldnt_update_site").into());
467 let form = ModAddForm {
468 mod_person_id: local_user_view.person.id,
469 other_person_id: data.person_id,
470 removed: Some(false),
473 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
475 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
477 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
478 let creator_index = admins
480 .position(|r| r.person.id == site_view.creator.id)
481 .context(location_info!())?;
482 let creator_person = admins.remove(creator_index);
483 admins.insert(0, creator_person);
485 let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
486 let federated_instances = build_federated_instances(
488 &context.settings().federation,
489 &context.settings().hostname,
494 site_view: Some(site_view),
498 version: version::VERSION.to_string(),
505 #[async_trait::async_trait(?Send)]
506 impl Perform for GetSiteConfig {
507 type Response = GetSiteConfigResponse;
511 context: &Data<LemmyContext>,
512 _websocket_id: Option<ConnectionId>,
513 ) -> Result<GetSiteConfigResponse, LemmyError> {
514 let data: &GetSiteConfig = self;
515 let local_user_view =
516 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
518 // Only let admins read this
519 is_admin(&local_user_view)?;
521 let config_hjson = Settings::read_config_file()?;
523 Ok(GetSiteConfigResponse { config_hjson })
527 #[async_trait::async_trait(?Send)]
528 impl Perform for SaveSiteConfig {
529 type Response = GetSiteConfigResponse;
533 context: &Data<LemmyContext>,
534 _websocket_id: Option<ConnectionId>,
535 ) -> Result<GetSiteConfigResponse, LemmyError> {
536 let data: &SaveSiteConfig = self;
537 let local_user_view =
538 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
540 // Only let admins read this
541 is_admin(&local_user_view)?;
543 // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
544 let config_hjson = Settings::save_config_file(&data.config_hjson)
545 .map_err(|_| ApiError::err("couldnt_update_site"))?;
547 Ok(GetSiteConfigResponse { config_hjson })