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 = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
156 let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
157 let show_bot_accounts = local_user_view
159 .map(|t| t.local_user.show_bot_accounts);
160 let show_read_posts = local_user_view
162 .map(|t| t.local_user.show_read_posts);
164 let person_id = local_user_view.map(|u| u.person.id);
166 let mut posts = Vec::new();
167 let mut comments = Vec::new();
168 let mut communities = Vec::new();
169 let mut users = Vec::new();
171 // TODO no clean / non-nsfw searching rn
173 let q = data.q.to_owned();
174 let page = data.page;
175 let limit = data.limit;
176 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
177 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.listing_type);
178 let search_type: SearchType = from_opt_str_to_opt_enum(&data.type_).unwrap_or(SearchType::All);
179 let community_id = data.community_id;
180 let community_actor_id = data
183 .map(|t| build_actor_id_from_shortname(EndpointType::Community, t).ok())
185 let creator_id = data.creator_id;
187 SearchType::Posts => {
188 posts = blocking(context.pool(), move |conn| {
189 PostQueryBuilder::create(conn)
191 .show_nsfw(show_nsfw)
192 .show_bot_accounts(show_bot_accounts)
193 .show_read_posts(show_read_posts)
194 .listing_type(listing_type)
195 .community_id(community_id)
196 .community_actor_id(community_actor_id)
197 .creator_id(creator_id)
198 .my_person_id(person_id)
206 SearchType::Comments => {
207 comments = blocking(context.pool(), move |conn| {
208 CommentQueryBuilder::create(conn)
210 .listing_type(listing_type)
212 .show_bot_accounts(show_bot_accounts)
213 .community_id(community_id)
214 .community_actor_id(community_actor_id)
215 .creator_id(creator_id)
216 .my_person_id(person_id)
223 SearchType::Communities => {
224 communities = blocking(context.pool(), move |conn| {
225 CommunityQueryBuilder::create(conn)
227 .listing_type(listing_type)
229 .my_person_id(person_id)
236 SearchType::Users => {
237 users = blocking(context.pool(), move |conn| {
238 PersonQueryBuilder::create(conn)
248 // If the community or creator is included, dont search communities or users
249 let community_or_creator_included =
250 data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
251 let community_actor_id_2 = community_actor_id.to_owned();
253 posts = blocking(context.pool(), move |conn| {
254 PostQueryBuilder::create(conn)
256 .show_nsfw(show_nsfw)
257 .show_bot_accounts(show_bot_accounts)
258 .show_read_posts(show_read_posts)
259 .listing_type(listing_type)
260 .community_id(community_id)
261 .community_actor_id(community_actor_id_2)
262 .creator_id(creator_id)
263 .my_person_id(person_id)
271 let q = data.q.to_owned();
272 let community_actor_id = community_actor_id.to_owned();
274 comments = blocking(context.pool(), move |conn| {
275 CommentQueryBuilder::create(conn)
277 .listing_type(listing_type)
279 .show_bot_accounts(show_bot_accounts)
280 .community_id(community_id)
281 .community_actor_id(community_actor_id)
282 .creator_id(creator_id)
283 .my_person_id(person_id)
290 let q = data.q.to_owned();
292 communities = if community_or_creator_included {
295 blocking(context.pool(), move |conn| {
296 CommunityQueryBuilder::create(conn)
298 .listing_type(listing_type)
300 .my_person_id(person_id)
308 let q = data.q.to_owned();
310 users = if community_or_creator_included {
313 blocking(context.pool(), move |conn| {
314 PersonQueryBuilder::create(conn)
325 posts = blocking(context.pool(), move |conn| {
326 PostQueryBuilder::create(conn)
328 .show_nsfw(show_nsfw)
329 .show_bot_accounts(show_bot_accounts)
330 .show_read_posts(show_read_posts)
331 .listing_type(listing_type)
332 .my_person_id(person_id)
333 .community_id(community_id)
334 .community_actor_id(community_actor_id)
335 .creator_id(creator_id)
345 // Blank out deleted or removed info
348 .filter(|cv| cv.comment.deleted || cv.comment.removed)
350 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
353 for cv in communities
355 .filter(|cv| cv.community.deleted || cv.community.removed)
357 cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
362 .filter(|p| p.post.deleted || p.post.removed)
364 pv.post = pv.to_owned().post.blank_out_deleted_or_removed_info();
369 type_: search_type.to_string(),
378 #[async_trait::async_trait(?Send)]
379 impl Perform for ResolveObject {
380 type Response = ResolveObjectResponse;
384 context: &Data<LemmyContext>,
385 _websocket_id: Option<ConnectionId>,
386 ) -> Result<ResolveObjectResponse, LemmyError> {
387 let local_user_view = get_local_user_view_from_jwt_opt(&self.auth, context.pool()).await?;
388 let res = search_by_apub_id(&self.q, context)
390 .map_err(|_| ApiError::err("couldnt_find_object"))?;
391 convert_response(res, local_user_view.map(|l| l.person.id), context.pool())
393 .map_err(|_| ApiError::err("couldnt_find_object").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 = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
448 is_admin(&local_user_view)?;
450 let read_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
452 // Make sure user is the creator
453 if read_site.creator_id != local_user_view.person.id {
454 return Err(ApiError::err("not_an_admin").into());
457 let new_creator_id = data.person_id;
458 let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
459 if blocking(context.pool(), transfer_site).await?.is_err() {
460 return Err(ApiError::err("couldnt_update_site").into());
464 let form = ModAddForm {
465 mod_person_id: local_user_view.person.id,
466 other_person_id: data.person_id,
467 removed: Some(false),
470 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
472 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
474 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
475 let creator_index = admins
477 .position(|r| r.person.id == site_view.creator.id)
478 .context(location_info!())?;
479 let creator_person = admins.remove(creator_index);
480 admins.insert(0, creator_person);
482 let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
483 let federated_instances = build_federated_instances(context.pool()).await?;
486 site_view: Some(site_view),
490 version: version::VERSION.to_string(),
497 #[async_trait::async_trait(?Send)]
498 impl Perform for GetSiteConfig {
499 type Response = GetSiteConfigResponse;
503 context: &Data<LemmyContext>,
504 _websocket_id: Option<ConnectionId>,
505 ) -> Result<GetSiteConfigResponse, LemmyError> {
506 let data: &GetSiteConfig = self;
507 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
509 // Only let admins read this
510 is_admin(&local_user_view)?;
512 let config_hjson = Settings::read_config_file()?;
514 Ok(GetSiteConfigResponse { config_hjson })
518 #[async_trait::async_trait(?Send)]
519 impl Perform for SaveSiteConfig {
520 type Response = GetSiteConfigResponse;
524 context: &Data<LemmyContext>,
525 _websocket_id: Option<ConnectionId>,
526 ) -> Result<GetSiteConfigResponse, LemmyError> {
527 let data: &SaveSiteConfig = self;
528 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
530 // Only let admins read this
531 is_admin(&local_user_view)?;
533 // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
534 let config_hjson = Settings::save_config_file(&data.config_hjson)
535 .map_err(|_| ApiError::err("couldnt_update_site"))?;
537 Ok(GetSiteConfigResponse { config_hjson })