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,
15 search::{search_by_apub_id, SearchableObjects},
16 webfinger::webfinger_resolve,
18 objects::community::ApubCommunity,
21 use lemmy_db_schema::{
22 from_opt_str_to_opt_enum,
24 source::{moderator::*, site::Site},
25 traits::{Crud, DeleteableOrRemoveable},
32 comment_view::{CommentQueryBuilder, CommentView},
33 post_view::{PostQueryBuilder, PostView},
36 use lemmy_db_views_actor::{
37 community_view::{CommunityQueryBuilder, CommunityView},
38 person_view::{PersonQueryBuilder, PersonViewSafe},
40 use lemmy_db_views_moderator::{
41 mod_add_community_view::ModAddCommunityView,
42 mod_add_view::ModAddView,
43 mod_ban_from_community_view::ModBanFromCommunityView,
44 mod_ban_view::ModBanView,
45 mod_lock_post_view::ModLockPostView,
46 mod_remove_comment_view::ModRemoveCommentView,
47 mod_remove_community_view::ModRemoveCommunityView,
48 mod_remove_post_view::ModRemovePostView,
49 mod_sticky_post_view::ModStickyPostView,
50 mod_transfer_community_view::ModTransferCommunityView,
52 use lemmy_utils::{location_info, settings::structs::Settings, version, ConnectionId, LemmyError};
53 use lemmy_websocket::LemmyContext;
55 #[async_trait::async_trait(?Send)]
56 impl Perform for GetModlog {
57 type Response = GetModlogResponse;
59 #[tracing::instrument(skip(context, _websocket_id))]
62 context: &Data<LemmyContext>,
63 _websocket_id: Option<ConnectionId>,
64 ) -> Result<GetModlogResponse, LemmyError> {
65 let data: &GetModlog = self;
67 let community_id = data.community_id;
68 let mod_person_id = data.mod_person_id;
70 let limit = data.limit;
71 let removed_posts = blocking(context.pool(), move |conn| {
72 ModRemovePostView::list(conn, community_id, mod_person_id, page, limit)
76 let locked_posts = blocking(context.pool(), move |conn| {
77 ModLockPostView::list(conn, community_id, mod_person_id, page, limit)
81 let stickied_posts = blocking(context.pool(), move |conn| {
82 ModStickyPostView::list(conn, community_id, mod_person_id, page, limit)
86 let removed_comments = blocking(context.pool(), move |conn| {
87 ModRemoveCommentView::list(conn, community_id, mod_person_id, page, limit)
91 let banned_from_community = blocking(context.pool(), move |conn| {
92 ModBanFromCommunityView::list(conn, community_id, mod_person_id, page, limit)
96 let added_to_community = blocking(context.pool(), move |conn| {
97 ModAddCommunityView::list(conn, community_id, mod_person_id, page, limit)
101 let transferred_to_community = blocking(context.pool(), move |conn| {
102 ModTransferCommunityView::list(conn, community_id, mod_person_id, page, limit)
106 // These arrays are only for the full modlog, when a community isn't given
107 let (removed_communities, banned, added) = if data.community_id.is_none() {
108 blocking(context.pool(), move |conn| {
110 ModRemoveCommunityView::list(conn, mod_person_id, page, limit)?,
111 ModBanView::list(conn, mod_person_id, page, limit)?,
112 ModAddView::list(conn, mod_person_id, page, limit)?,
113 )) as Result<_, LemmyError>
117 (Vec::new(), Vec::new(), Vec::new())
121 Ok(GetModlogResponse {
127 banned_from_community,
131 transferred_to_community,
136 #[async_trait::async_trait(?Send)]
137 impl Perform for Search {
138 type Response = SearchResponse;
140 #[tracing::instrument(skip(context, _websocket_id))]
143 context: &Data<LemmyContext>,
144 _websocket_id: Option<ConnectionId>,
145 ) -> Result<SearchResponse, LemmyError> {
146 let data: &Search = self;
148 let local_user_view =
149 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
152 let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
153 let show_bot_accounts = local_user_view
155 .map(|t| t.local_user.show_bot_accounts);
156 let show_read_posts = local_user_view
158 .map(|t| t.local_user.show_read_posts);
160 let person_id = local_user_view.map(|u| u.person.id);
162 let mut posts = Vec::new();
163 let mut comments = Vec::new();
164 let mut communities = Vec::new();
165 let mut users = Vec::new();
167 // TODO no clean / non-nsfw searching rn
169 let q = data.q.to_owned();
170 let page = data.page;
171 let limit = data.limit;
172 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
173 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.listing_type);
174 let search_type: SearchType = from_opt_str_to_opt_enum(&data.type_).unwrap_or(SearchType::All);
175 let community_id = data.community_id;
176 let community_actor_id = if let Some(name) = &data.community_name {
177 webfinger_resolve::<ApubCommunity>(name, EndpointType::Community, context, &mut 0)
183 let creator_id = data.creator_id;
185 SearchType::Posts => {
186 posts = blocking(context.pool(), move |conn| {
187 PostQueryBuilder::create(conn)
189 .show_nsfw(show_nsfw)
190 .show_bot_accounts(show_bot_accounts)
191 .show_read_posts(show_read_posts)
192 .listing_type(listing_type)
193 .community_id(community_id)
194 .community_actor_id(community_actor_id)
195 .creator_id(creator_id)
196 .my_person_id(person_id)
204 SearchType::Comments => {
205 comments = blocking(context.pool(), move |conn| {
206 CommentQueryBuilder::create(conn)
208 .listing_type(listing_type)
210 .show_bot_accounts(show_bot_accounts)
211 .community_id(community_id)
212 .community_actor_id(community_actor_id)
213 .creator_id(creator_id)
214 .my_person_id(person_id)
221 SearchType::Communities => {
222 communities = blocking(context.pool(), move |conn| {
223 CommunityQueryBuilder::create(conn)
225 .listing_type(listing_type)
227 .my_person_id(person_id)
234 SearchType::Users => {
235 users = blocking(context.pool(), move |conn| {
236 PersonQueryBuilder::create(conn)
246 // If the community or creator is included, dont search communities or users
247 let community_or_creator_included =
248 data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
249 let community_actor_id_2 = community_actor_id.to_owned();
251 posts = blocking(context.pool(), move |conn| {
252 PostQueryBuilder::create(conn)
254 .show_nsfw(show_nsfw)
255 .show_bot_accounts(show_bot_accounts)
256 .show_read_posts(show_read_posts)
257 .listing_type(listing_type)
258 .community_id(community_id)
259 .community_actor_id(community_actor_id_2)
260 .creator_id(creator_id)
261 .my_person_id(person_id)
269 let q = data.q.to_owned();
270 let community_actor_id = community_actor_id.to_owned();
272 comments = blocking(context.pool(), move |conn| {
273 CommentQueryBuilder::create(conn)
275 .listing_type(listing_type)
277 .show_bot_accounts(show_bot_accounts)
278 .community_id(community_id)
279 .community_actor_id(community_actor_id)
280 .creator_id(creator_id)
281 .my_person_id(person_id)
288 let q = data.q.to_owned();
290 communities = if community_or_creator_included {
293 blocking(context.pool(), move |conn| {
294 CommunityQueryBuilder::create(conn)
296 .listing_type(listing_type)
298 .my_person_id(person_id)
306 let q = data.q.to_owned();
308 users = if community_or_creator_included {
311 blocking(context.pool(), move |conn| {
312 PersonQueryBuilder::create(conn)
323 posts = blocking(context.pool(), move |conn| {
324 PostQueryBuilder::create(conn)
326 .show_nsfw(show_nsfw)
327 .show_bot_accounts(show_bot_accounts)
328 .show_read_posts(show_read_posts)
329 .listing_type(listing_type)
330 .my_person_id(person_id)
331 .community_id(community_id)
332 .community_actor_id(community_actor_id)
333 .creator_id(creator_id)
343 // Blank out deleted or removed info for non logged in users
344 if person_id.is_none() {
345 for cv in communities
347 .filter(|cv| cv.community.deleted || cv.community.removed)
349 cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
354 .filter(|p| p.post.deleted || p.post.removed)
356 pv.post = pv.to_owned().post.blank_out_deleted_or_removed_info();
361 .filter(|cv| cv.comment.deleted || cv.comment.removed)
363 cv.comment = cv.to_owned().comment.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;
382 #[tracing::instrument(skip(context, _websocket_id))]
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.as_ref(), context.pool(), context.secret())
391 let res = search_by_apub_id(&self.q, context)
393 .map_err(LemmyError::from)
394 .map_err(|e| e.with_message("couldnt_find_object"))?;
395 convert_response(res, local_user_view.map(|l| l.person.id), context.pool())
397 .map_err(LemmyError::from)
398 .map_err(|e| e.with_message("couldnt_find_object"))
402 async fn convert_response(
403 object: SearchableObjects,
404 user_id: Option<PersonId>,
406 ) -> Result<ResolveObjectResponse, LemmyError> {
407 let removed_or_deleted;
408 let mut res = ResolveObjectResponse {
414 use SearchableObjects::*;
417 removed_or_deleted = p.deleted;
418 res.person = Some(blocking(pool, move |conn| PersonViewSafe::read(conn, p.id)).await??)
421 removed_or_deleted = c.deleted || c.removed;
423 Some(blocking(pool, move |conn| CommunityView::read(conn, c.id, user_id)).await??)
426 removed_or_deleted = p.deleted || p.removed;
427 res.post = Some(blocking(pool, move |conn| PostView::read(conn, p.id, user_id)).await??)
430 removed_or_deleted = c.deleted || c.removed;
431 res.comment = Some(blocking(pool, move |conn| CommentView::read(conn, c.id, user_id)).await??)
434 // if the object was deleted from database, dont return it
435 if removed_or_deleted {
436 return Err(NotFound {}.into());
441 #[async_trait::async_trait(?Send)]
442 impl Perform for TransferSite {
443 type Response = GetSiteResponse;
445 #[tracing::instrument(skip(context, _websocket_id))]
448 context: &Data<LemmyContext>,
449 _websocket_id: Option<ConnectionId>,
450 ) -> Result<GetSiteResponse, LemmyError> {
451 let data: &TransferSite = self;
452 let local_user_view =
453 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
455 is_admin(&local_user_view)?;
457 let read_site = blocking(context.pool(), Site::read_simple).await??;
459 // Make sure user is the creator
460 if read_site.creator_id != local_user_view.person.id {
461 return Err(LemmyError::from_message("not_an_admin"));
464 let new_creator_id = data.person_id;
465 let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
466 blocking(context.pool(), transfer_site)
468 .map_err(LemmyError::from)
469 .map_err(|e| e.with_message("couldnt_update_site"))?;
472 let form = ModAddForm {
473 mod_person_id: local_user_view.person.id,
474 other_person_id: data.person_id,
475 removed: Some(false),
478 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
480 let site_view = blocking(context.pool(), SiteView::read).await??;
482 let mut admins = blocking(context.pool(), PersonViewSafe::admins).await??;
483 let creator_index = admins
485 .position(|r| r.person.id == site_view.creator.id)
486 .context(location_info!())?;
487 let creator_person = admins.remove(creator_index);
488 admins.insert(0, creator_person);
490 let banned = blocking(context.pool(), PersonViewSafe::banned).await??;
491 let federated_instances = build_federated_instances(
493 &context.settings().federation,
494 &context.settings().hostname,
499 site_view: Some(site_view),
503 version: version::VERSION.to_string(),
510 #[async_trait::async_trait(?Send)]
511 impl Perform for GetSiteConfig {
512 type Response = GetSiteConfigResponse;
514 #[tracing::instrument(skip(context, _websocket_id))]
517 context: &Data<LemmyContext>,
518 _websocket_id: Option<ConnectionId>,
519 ) -> Result<GetSiteConfigResponse, LemmyError> {
520 let data: &GetSiteConfig = self;
521 let local_user_view =
522 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
524 // Only let admins read this
525 is_admin(&local_user_view)?;
527 let config_hjson = Settings::read_config_file()?;
529 Ok(GetSiteConfigResponse { config_hjson })
533 #[async_trait::async_trait(?Send)]
534 impl Perform for SaveSiteConfig {
535 type Response = GetSiteConfigResponse;
537 #[tracing::instrument(skip(context, _websocket_id))]
540 context: &Data<LemmyContext>,
541 _websocket_id: Option<ConnectionId>,
542 ) -> Result<GetSiteConfigResponse, LemmyError> {
543 let data: &SaveSiteConfig = self;
544 let local_user_view =
545 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
547 // Only let admins read this
548 is_admin(&local_user_view)?;
550 // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
551 let config_hjson = Settings::save_config_file(&data.config_hjson)
552 .map_err(LemmyError::from)
553 .map_err(|e| e.with_message("couldnt_update_site"))?;
555 Ok(GetSiteConfigResponse { config_hjson })