2 use actix_web::web::Data;
4 use lemmy_api_common::{
6 build_federated_instances,
7 get_local_user_view_from_jwt,
8 get_local_user_view_from_jwt_opt,
12 use lemmy_apub::{build_actor_id_from_shortname, fetcher::search::search_by_apub_id, EndpointType};
13 use lemmy_db_queries::{
14 from_opt_str_to_opt_enum,
17 DeleteableOrRemoveable,
22 use lemmy_db_schema::source::{moderator::*, site::Site};
24 comment_view::CommentQueryBuilder,
25 post_view::PostQueryBuilder,
28 use lemmy_db_views_actor::{
29 community_view::CommunityQueryBuilder,
30 person_view::{PersonQueryBuilder, PersonViewSafe},
32 use lemmy_db_views_moderator::{
33 mod_add_community_view::ModAddCommunityView,
34 mod_add_view::ModAddView,
35 mod_ban_from_community_view::ModBanFromCommunityView,
36 mod_ban_view::ModBanView,
37 mod_lock_post_view::ModLockPostView,
38 mod_remove_comment_view::ModRemoveCommentView,
39 mod_remove_community_view::ModRemoveCommunityView,
40 mod_remove_post_view::ModRemovePostView,
41 mod_sticky_post_view::ModStickyPostView,
42 mod_transfer_community_view::ModTransferCommunityView,
46 settings::structs::Settings,
52 use lemmy_websocket::LemmyContext;
55 #[async_trait::async_trait(?Send)]
56 impl Perform for GetModlog {
57 type Response = GetModlogResponse;
61 context: &Data<LemmyContext>,
62 _websocket_id: Option<ConnectionId>,
63 ) -> Result<GetModlogResponse, LemmyError> {
64 let data: &GetModlog = self;
66 let community_id = data.community_id;
67 let mod_person_id = data.mod_person_id;
69 let limit = data.limit;
70 let removed_posts = blocking(context.pool(), move |conn| {
71 ModRemovePostView::list(conn, community_id, mod_person_id, page, limit)
75 let locked_posts = blocking(context.pool(), move |conn| {
76 ModLockPostView::list(conn, community_id, mod_person_id, page, limit)
80 let stickied_posts = blocking(context.pool(), move |conn| {
81 ModStickyPostView::list(conn, community_id, mod_person_id, page, limit)
85 let removed_comments = blocking(context.pool(), move |conn| {
86 ModRemoveCommentView::list(conn, community_id, mod_person_id, page, limit)
90 let banned_from_community = blocking(context.pool(), move |conn| {
91 ModBanFromCommunityView::list(conn, community_id, mod_person_id, page, limit)
95 let added_to_community = blocking(context.pool(), move |conn| {
96 ModAddCommunityView::list(conn, community_id, mod_person_id, page, limit)
100 let transferred_to_community = blocking(context.pool(), move |conn| {
101 ModTransferCommunityView::list(conn, community_id, mod_person_id, page, limit)
105 // These arrays are only for the full modlog, when a community isn't given
106 let (removed_communities, banned, added) = if data.community_id.is_none() {
107 blocking(context.pool(), move |conn| {
109 ModRemoveCommunityView::list(conn, mod_person_id, page, limit)?,
110 ModBanView::list(conn, mod_person_id, page, limit)?,
111 ModAddView::list(conn, mod_person_id, page, limit)?,
112 )) as Result<_, LemmyError>
116 (Vec::new(), Vec::new(), Vec::new())
120 Ok(GetModlogResponse {
126 banned_from_community,
130 transferred_to_community,
135 #[async_trait::async_trait(?Send)]
136 impl Perform for Search {
137 type Response = SearchResponse;
141 context: &Data<LemmyContext>,
142 _websocket_id: Option<ConnectionId>,
143 ) -> Result<SearchResponse, LemmyError> {
144 let data: &Search = self;
146 match search_by_apub_id(&data.q, context).await {
147 Ok(r) => return Ok(r),
148 Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e),
151 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).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).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
345 .filter(|cv| cv.comment.deleted || cv.comment.removed)
347 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
350 for cv in communities
352 .filter(|cv| cv.community.deleted || cv.community.removed)
354 cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
359 .filter(|p| p.post.deleted || p.post.removed)
361 pv.post = pv.to_owned().post.blank_out_deleted_or_removed_info();
366 type_: search_type.to_string(),
375 #[async_trait::async_trait(?Send)]
376 impl Perform for TransferSite {
377 type Response = GetSiteResponse;
381 context: &Data<LemmyContext>,
382 _websocket_id: Option<ConnectionId>,
383 ) -> Result<GetSiteResponse, LemmyError> {
384 let data: &TransferSite = self;
385 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
387 is_admin(&local_user_view)?;
389 let read_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
391 // Make sure user is the creator
392 if read_site.creator_id != local_user_view.person.id {
393 return Err(ApiError::err("not_an_admin").into());
396 let new_creator_id = data.person_id;
397 let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
398 if blocking(context.pool(), transfer_site).await?.is_err() {
399 return Err(ApiError::err("couldnt_update_site").into());
403 let form = ModAddForm {
404 mod_person_id: local_user_view.person.id,
405 other_person_id: data.person_id,
406 removed: Some(false),
409 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
411 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
413 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
414 let creator_index = admins
416 .position(|r| r.person.id == site_view.creator.id)
417 .context(location_info!())?;
418 let creator_person = admins.remove(creator_index);
419 admins.insert(0, creator_person);
421 let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
422 let federated_instances = build_federated_instances(context.pool()).await?;
425 site_view: Some(site_view),
429 version: version::VERSION.to_string(),
436 #[async_trait::async_trait(?Send)]
437 impl Perform for GetSiteConfig {
438 type Response = GetSiteConfigResponse;
442 context: &Data<LemmyContext>,
443 _websocket_id: Option<ConnectionId>,
444 ) -> Result<GetSiteConfigResponse, LemmyError> {
445 let data: &GetSiteConfig = self;
446 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
448 // Only let admins read this
449 is_admin(&local_user_view)?;
451 let config_hjson = Settings::read_config_file()?;
453 Ok(GetSiteConfigResponse { config_hjson })
457 #[async_trait::async_trait(?Send)]
458 impl Perform for SaveSiteConfig {
459 type Response = GetSiteConfigResponse;
463 context: &Data<LemmyContext>,
464 _websocket_id: Option<ConnectionId>,
465 ) -> Result<GetSiteConfigResponse, LemmyError> {
466 let data: &SaveSiteConfig = self;
467 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
469 // Only let admins read this
470 is_admin(&local_user_view)?;
472 // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
473 let config_hjson = Settings::save_config_file(&data.config_hjson)
474 .map_err(|_| ApiError::err("couldnt_update_site"))?;
476 Ok(GetSiteConfigResponse { config_hjson })