2 use actix_web::web::Data;
4 use lemmy_api_common::{
6 build_federated_instances,
7 get_local_user_settings_view_from_jwt,
8 get_local_user_view_from_jwt,
9 get_local_user_view_from_jwt_opt,
12 user_show_bot_accounts,
15 use lemmy_apub::fetcher::search::search_by_apub_id;
16 use lemmy_db_queries::{
17 from_opt_str_to_opt_enum,
24 use lemmy_db_schema::source::{moderator::*, site::Site};
26 comment_view::CommentQueryBuilder,
27 post_view::PostQueryBuilder,
30 use lemmy_db_views_actor::{
31 community_view::CommunityQueryBuilder,
32 person_view::{PersonQueryBuilder, PersonViewSafe},
34 use lemmy_db_views_moderator::{
35 mod_add_community_view::ModAddCommunityView,
36 mod_add_view::ModAddView,
37 mod_ban_from_community_view::ModBanFromCommunityView,
38 mod_ban_view::ModBanView,
39 mod_lock_post_view::ModLockPostView,
40 mod_remove_comment_view::ModRemoveCommentView,
41 mod_remove_community_view::ModRemoveCommunityView,
42 mod_remove_post_view::ModRemovePostView,
43 mod_sticky_post_view::ModStickyPostView,
47 settings::structs::Settings,
53 use lemmy_websocket::LemmyContext;
56 #[async_trait::async_trait(?Send)]
57 impl Perform for GetModlog {
58 type Response = GetModlogResponse;
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 // These arrays are only for the full modlog, when a community isn't given
102 let (removed_communities, banned, added) = if data.community_id.is_none() {
103 blocking(context.pool(), move |conn| {
105 ModRemoveCommunityView::list(conn, mod_person_id, page, limit)?,
106 ModBanView::list(conn, mod_person_id, page, limit)?,
107 ModAddView::list(conn, mod_person_id, page, limit)?,
108 )) as Result<_, LemmyError>
112 (Vec::new(), Vec::new(), Vec::new())
116 Ok(GetModlogResponse {
122 banned_from_community,
130 #[async_trait::async_trait(?Send)]
131 impl Perform for Search {
132 type Response = SearchResponse;
136 context: &Data<LemmyContext>,
137 _websocket_id: Option<ConnectionId>,
138 ) -> Result<SearchResponse, LemmyError> {
139 let data: &Search = &self;
141 match search_by_apub_id(&data.q, context).await {
142 Ok(r) => return Ok(r),
143 Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e),
146 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
148 let show_nsfw = user_show_nsfw(&local_user_view);
149 let show_bot_accounts = user_show_bot_accounts(&local_user_view);
151 let person_id = local_user_view.map(|u| u.person.id);
153 let mut posts = Vec::new();
154 let mut comments = Vec::new();
155 let mut communities = Vec::new();
156 let mut users = Vec::new();
158 // TODO no clean / non-nsfw searching rn
160 let q = data.q.to_owned();
161 let page = data.page;
162 let limit = data.limit;
163 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
164 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.listing_type);
165 let search_type: SearchType = from_opt_str_to_opt_enum(&data.type_).unwrap_or(SearchType::All);
166 let community_id = data.community_id;
167 let community_name = data.community_name.to_owned();
168 let creator_id = data.creator_id;
170 SearchType::Posts => {
171 posts = blocking(context.pool(), move |conn| {
172 PostQueryBuilder::create(conn)
174 .show_nsfw(show_nsfw)
175 .show_bot_accounts(show_bot_accounts)
176 .listing_type(listing_type)
177 .community_id(community_id)
178 .community_name(community_name)
179 .creator_id(creator_id)
180 .my_person_id(person_id)
188 SearchType::Comments => {
189 comments = blocking(context.pool(), move |conn| {
190 CommentQueryBuilder::create(&conn)
192 .listing_type(listing_type)
194 .show_bot_accounts(show_bot_accounts)
195 .community_id(community_id)
196 .community_name(community_name)
197 .creator_id(creator_id)
198 .my_person_id(person_id)
205 SearchType::Communities => {
206 communities = blocking(context.pool(), move |conn| {
207 CommunityQueryBuilder::create(conn)
209 .listing_type(listing_type)
211 .my_person_id(person_id)
218 SearchType::Users => {
219 users = blocking(context.pool(), move |conn| {
220 PersonQueryBuilder::create(conn)
230 // If the community is included, dont search communities or users
231 let community_included = data.community_id.is_some() || data.community_name.is_some();
233 posts = blocking(context.pool(), move |conn| {
234 PostQueryBuilder::create(conn)
236 .show_nsfw(show_nsfw)
237 .show_bot_accounts(show_bot_accounts)
238 .listing_type(listing_type)
239 .community_id(community_id)
240 .community_name(community_name)
241 .creator_id(creator_id)
242 .my_person_id(person_id)
250 let q = data.q.to_owned();
251 let community_name = data.community_name.to_owned();
253 comments = blocking(context.pool(), move |conn| {
254 CommentQueryBuilder::create(conn)
256 .listing_type(listing_type)
258 .show_bot_accounts(show_bot_accounts)
259 .community_id(community_id)
260 .community_name(community_name)
261 .creator_id(creator_id)
262 .my_person_id(person_id)
269 let q = data.q.to_owned();
271 communities = if community_included {
274 blocking(context.pool(), move |conn| {
275 CommunityQueryBuilder::create(conn)
277 .listing_type(listing_type)
279 .my_person_id(person_id)
287 let q = data.q.to_owned();
289 users = if community_included {
292 blocking(context.pool(), move |conn| {
293 PersonQueryBuilder::create(conn)
304 posts = blocking(context.pool(), move |conn| {
305 PostQueryBuilder::create(conn)
307 .show_nsfw(show_nsfw)
308 .show_bot_accounts(show_bot_accounts)
309 .listing_type(listing_type)
310 .my_person_id(person_id)
311 .community_id(community_id)
312 .community_name(community_name)
313 .creator_id(creator_id)
325 type_: search_type.to_string(),
334 #[async_trait::async_trait(?Send)]
335 impl Perform for TransferSite {
336 type Response = GetSiteResponse;
340 context: &Data<LemmyContext>,
341 _websocket_id: Option<ConnectionId>,
342 ) -> Result<GetSiteResponse, LemmyError> {
343 let data: &TransferSite = &self;
344 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
346 is_admin(&local_user_view)?;
348 let read_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
350 // Make sure user is the creator
351 if read_site.creator_id != local_user_view.person.id {
352 return Err(ApiError::err("not_an_admin").into());
355 let new_creator_id = data.person_id;
356 let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
357 if blocking(context.pool(), transfer_site).await?.is_err() {
358 return Err(ApiError::err("couldnt_update_site").into());
362 let form = ModAddForm {
363 mod_person_id: local_user_view.person.id,
364 other_person_id: data.person_id,
365 removed: Some(false),
368 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
370 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
372 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
373 let creator_index = admins
375 .position(|r| r.person.id == site_view.creator.id)
376 .context(location_info!())?;
377 let creator_person = admins.remove(creator_index);
378 admins.insert(0, creator_person);
380 let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
381 let federated_instances = build_federated_instances(context.pool()).await?;
383 let my_user = Some(get_local_user_settings_view_from_jwt(&data.auth, context.pool()).await?);
386 site_view: Some(site_view),
390 version: version::VERSION.to_string(),
397 #[async_trait::async_trait(?Send)]
398 impl Perform for GetSiteConfig {
399 type Response = GetSiteConfigResponse;
403 context: &Data<LemmyContext>,
404 _websocket_id: Option<ConnectionId>,
405 ) -> Result<GetSiteConfigResponse, LemmyError> {
406 let data: &GetSiteConfig = &self;
407 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
409 // Only let admins read this
410 is_admin(&local_user_view)?;
412 let config_hjson = Settings::read_config_file()?;
414 Ok(GetSiteConfigResponse { config_hjson })
418 #[async_trait::async_trait(?Send)]
419 impl Perform for SaveSiteConfig {
420 type Response = GetSiteConfigResponse;
424 context: &Data<LemmyContext>,
425 _websocket_id: Option<ConnectionId>,
426 ) -> Result<GetSiteConfigResponse, LemmyError> {
427 let data: &SaveSiteConfig = &self;
428 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
430 // Only let admins read this
431 is_admin(&local_user_view)?;
433 // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
434 let config_hjson = Settings::save_config_file(&data.config_hjson)
435 .map_err(|_| ApiError::err("couldnt_update_site"))?;
437 Ok(GetSiteConfigResponse { config_hjson })