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 fetcher::search::{search_by_apub_id, SearchableObjects},
15 get_actor_id_from_name,
17 use lemmy_apub_lib::webfinger::WebfingerType;
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 = if let Some(name) = &data.community_name {
178 get_actor_id_from_name(WebfingerType::Group, name, context)
184 let creator_id = data.creator_id;
186 SearchType::Posts => {
187 posts = blocking(context.pool(), move |conn| {
188 PostQueryBuilder::create(conn)
190 .show_nsfw(show_nsfw)
191 .show_bot_accounts(show_bot_accounts)
192 .show_read_posts(show_read_posts)
193 .listing_type(listing_type)
194 .community_id(community_id)
195 .community_actor_id(community_actor_id)
196 .creator_id(creator_id)
197 .my_person_id(person_id)
205 SearchType::Comments => {
206 comments = blocking(context.pool(), move |conn| {
207 CommentQueryBuilder::create(conn)
209 .listing_type(listing_type)
211 .show_bot_accounts(show_bot_accounts)
212 .community_id(community_id)
213 .community_actor_id(community_actor_id)
214 .creator_id(creator_id)
215 .my_person_id(person_id)
222 SearchType::Communities => {
223 communities = blocking(context.pool(), move |conn| {
224 CommunityQueryBuilder::create(conn)
226 .listing_type(listing_type)
228 .my_person_id(person_id)
235 SearchType::Users => {
236 users = blocking(context.pool(), move |conn| {
237 PersonQueryBuilder::create(conn)
247 // If the community or creator is included, dont search communities or users
248 let community_or_creator_included =
249 data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
250 let community_actor_id_2 = community_actor_id.to_owned();
252 posts = blocking(context.pool(), move |conn| {
253 PostQueryBuilder::create(conn)
255 .show_nsfw(show_nsfw)
256 .show_bot_accounts(show_bot_accounts)
257 .show_read_posts(show_read_posts)
258 .listing_type(listing_type)
259 .community_id(community_id)
260 .community_actor_id(community_actor_id_2)
261 .creator_id(creator_id)
262 .my_person_id(person_id)
270 let q = data.q.to_owned();
271 let community_actor_id = community_actor_id.to_owned();
273 comments = blocking(context.pool(), move |conn| {
274 CommentQueryBuilder::create(conn)
276 .listing_type(listing_type)
278 .show_bot_accounts(show_bot_accounts)
279 .community_id(community_id)
280 .community_actor_id(community_actor_id)
281 .creator_id(creator_id)
282 .my_person_id(person_id)
289 let q = data.q.to_owned();
291 communities = if community_or_creator_included {
294 blocking(context.pool(), move |conn| {
295 CommunityQueryBuilder::create(conn)
297 .listing_type(listing_type)
299 .my_person_id(person_id)
307 let q = data.q.to_owned();
309 users = if community_or_creator_included {
312 blocking(context.pool(), move |conn| {
313 PersonQueryBuilder::create(conn)
324 posts = blocking(context.pool(), move |conn| {
325 PostQueryBuilder::create(conn)
327 .show_nsfw(show_nsfw)
328 .show_bot_accounts(show_bot_accounts)
329 .show_read_posts(show_read_posts)
330 .listing_type(listing_type)
331 .my_person_id(person_id)
332 .community_id(community_id)
333 .community_actor_id(community_actor_id)
334 .creator_id(creator_id)
344 // Blank out deleted or removed info for non logged in users
345 if person_id.is_none() {
346 for cv in communities
348 .filter(|cv| cv.community.deleted || cv.community.removed)
350 cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
355 .filter(|p| p.post.deleted || p.post.removed)
357 pv.post = pv.to_owned().post.blank_out_deleted_or_removed_info();
362 .filter(|cv| cv.comment.deleted || cv.comment.removed)
364 cv.comment = cv.to_owned().comment.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(|e| ApiError::err("couldnt_find_object", e))?;
393 convert_response(res, local_user_view.map(|l| l.person.id), context.pool())
395 .map_err(|e| ApiError::err("couldnt_find_object", e).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(), Site::read_simple).await??;
455 // Make sure user is the creator
456 if read_site.creator_id != local_user_view.person.id {
457 return Err(ApiError::err_plain("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 blocking(context.pool(), transfer_site)
464 .map_err(|e| ApiError::err("couldnt_update_site", e))?;
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(), SiteView::read).await??;
477 let mut admins = blocking(context.pool(), PersonViewSafe::admins).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(), PersonViewSafe::banned).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(|e| ApiError::err("couldnt_update_site", e))?;
547 Ok(GetSiteConfigResponse { config_hjson })