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,
13 use lemmy_apub::{build_actor_id_from_shortname, fetcher::search::search_by_apub_id, EndpointType};
14 use lemmy_db_queries::{
15 from_opt_str_to_opt_enum,
18 DeleteableOrRemoveable,
23 use lemmy_db_schema::source::{moderator::*, site::Site};
25 comment_view::CommentQueryBuilder,
26 post_view::PostQueryBuilder,
29 use lemmy_db_views_actor::{
30 community_view::CommunityQueryBuilder,
31 person_view::{PersonQueryBuilder, PersonViewSafe},
33 use lemmy_db_views_moderator::{
34 mod_add_community_view::ModAddCommunityView,
35 mod_add_view::ModAddView,
36 mod_ban_from_community_view::ModBanFromCommunityView,
37 mod_ban_view::ModBanView,
38 mod_lock_post_view::ModLockPostView,
39 mod_remove_comment_view::ModRemoveCommentView,
40 mod_remove_community_view::ModRemoveCommunityView,
41 mod_remove_post_view::ModRemovePostView,
42 mod_sticky_post_view::ModStickyPostView,
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 // These arrays are only for the full modlog, when a community isn't given
101 let (removed_communities, banned, added) = if data.community_id.is_none() {
102 blocking(context.pool(), move |conn| {
104 ModRemoveCommunityView::list(conn, mod_person_id, page, limit)?,
105 ModBanView::list(conn, mod_person_id, page, limit)?,
106 ModAddView::list(conn, mod_person_id, page, limit)?,
107 )) as Result<_, LemmyError>
111 (Vec::new(), Vec::new(), Vec::new())
115 Ok(GetModlogResponse {
121 banned_from_community,
129 #[async_trait::async_trait(?Send)]
130 impl Perform for Search {
131 type Response = SearchResponse;
135 context: &Data<LemmyContext>,
136 _websocket_id: Option<ConnectionId>,
137 ) -> Result<SearchResponse, LemmyError> {
138 let data: &Search = self;
140 match search_by_apub_id(&data.q, context).await {
141 Ok(r) => return Ok(r),
142 Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e),
145 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
147 let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
148 let show_bot_accounts = local_user_view
150 .map(|t| t.local_user.show_bot_accounts);
151 let show_read_posts = local_user_view
153 .map(|t| t.local_user.show_read_posts);
155 let person_id = local_user_view.map(|u| u.person.id);
157 let mut posts = Vec::new();
158 let mut comments = Vec::new();
159 let mut communities = Vec::new();
160 let mut users = Vec::new();
162 // TODO no clean / non-nsfw searching rn
164 let q = data.q.to_owned();
165 let page = data.page;
166 let limit = data.limit;
167 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
168 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.listing_type);
169 let search_type: SearchType = from_opt_str_to_opt_enum(&data.type_).unwrap_or(SearchType::All);
170 let community_id = data.community_id;
171 let community_actor_id = data
174 .map(|t| build_actor_id_from_shortname(EndpointType::Community, t).ok())
176 let creator_id = data.creator_id;
178 SearchType::Posts => {
179 posts = blocking(context.pool(), move |conn| {
180 PostQueryBuilder::create(conn)
182 .show_nsfw(show_nsfw)
183 .show_bot_accounts(show_bot_accounts)
184 .show_read_posts(show_read_posts)
185 .listing_type(listing_type)
186 .community_id(community_id)
187 .community_actor_id(community_actor_id)
188 .creator_id(creator_id)
189 .my_person_id(person_id)
197 SearchType::Comments => {
198 comments = blocking(context.pool(), move |conn| {
199 CommentQueryBuilder::create(conn)
201 .listing_type(listing_type)
203 .show_bot_accounts(show_bot_accounts)
204 .community_id(community_id)
205 .community_actor_id(community_actor_id)
206 .creator_id(creator_id)
207 .my_person_id(person_id)
214 SearchType::Communities => {
215 communities = blocking(context.pool(), move |conn| {
216 CommunityQueryBuilder::create(conn)
218 .listing_type(listing_type)
220 .my_person_id(person_id)
227 SearchType::Users => {
228 users = blocking(context.pool(), move |conn| {
229 PersonQueryBuilder::create(conn)
239 // If the community or creator is included, dont search communities or users
240 let community_or_creator_included =
241 data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
242 let community_actor_id_2 = community_actor_id.to_owned();
244 posts = blocking(context.pool(), move |conn| {
245 PostQueryBuilder::create(conn)
247 .show_nsfw(show_nsfw)
248 .show_bot_accounts(show_bot_accounts)
249 .show_read_posts(show_read_posts)
250 .listing_type(listing_type)
251 .community_id(community_id)
252 .community_actor_id(community_actor_id_2)
253 .creator_id(creator_id)
254 .my_person_id(person_id)
262 let q = data.q.to_owned();
263 let community_actor_id = community_actor_id.to_owned();
265 comments = blocking(context.pool(), move |conn| {
266 CommentQueryBuilder::create(conn)
268 .listing_type(listing_type)
270 .show_bot_accounts(show_bot_accounts)
271 .community_id(community_id)
272 .community_actor_id(community_actor_id)
273 .creator_id(creator_id)
274 .my_person_id(person_id)
281 let q = data.q.to_owned();
283 communities = if community_or_creator_included {
286 blocking(context.pool(), move |conn| {
287 CommunityQueryBuilder::create(conn)
289 .listing_type(listing_type)
291 .my_person_id(person_id)
299 let q = data.q.to_owned();
301 users = if community_or_creator_included {
304 blocking(context.pool(), move |conn| {
305 PersonQueryBuilder::create(conn)
316 posts = blocking(context.pool(), move |conn| {
317 PostQueryBuilder::create(conn)
319 .show_nsfw(show_nsfw)
320 .show_bot_accounts(show_bot_accounts)
321 .show_read_posts(show_read_posts)
322 .listing_type(listing_type)
323 .my_person_id(person_id)
324 .community_id(community_id)
325 .community_actor_id(community_actor_id)
326 .creator_id(creator_id)
336 // Blank out deleted or removed info
339 .filter(|cv| cv.comment.deleted || cv.comment.removed)
341 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
344 for cv in communities
346 .filter(|cv| cv.community.deleted || cv.community.removed)
348 cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
353 .filter(|p| p.post.deleted || p.post.removed)
355 pv.post = pv.to_owned().post.blank_out_deleted_or_removed_info();
360 type_: search_type.to_string(),
369 #[async_trait::async_trait(?Send)]
370 impl Perform for TransferSite {
371 type Response = GetSiteResponse;
375 context: &Data<LemmyContext>,
376 _websocket_id: Option<ConnectionId>,
377 ) -> Result<GetSiteResponse, LemmyError> {
378 let data: &TransferSite = self;
379 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
381 is_admin(&local_user_view)?;
383 let read_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
385 // Make sure user is the creator
386 if read_site.creator_id != local_user_view.person.id {
387 return Err(ApiError::err("not_an_admin").into());
390 let new_creator_id = data.person_id;
391 let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
392 if blocking(context.pool(), transfer_site).await?.is_err() {
393 return Err(ApiError::err("couldnt_update_site").into());
397 let form = ModAddForm {
398 mod_person_id: local_user_view.person.id,
399 other_person_id: data.person_id,
400 removed: Some(false),
403 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
405 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
407 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
408 let creator_index = admins
410 .position(|r| r.person.id == site_view.creator.id)
411 .context(location_info!())?;
412 let creator_person = admins.remove(creator_index);
413 admins.insert(0, creator_person);
415 let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
416 let federated_instances = build_federated_instances(context.pool()).await?;
418 let my_user = Some(get_local_user_settings_view_from_jwt(&data.auth, context.pool()).await?);
421 site_view: Some(site_view),
425 version: version::VERSION.to_string(),
432 #[async_trait::async_trait(?Send)]
433 impl Perform for GetSiteConfig {
434 type Response = GetSiteConfigResponse;
438 context: &Data<LemmyContext>,
439 _websocket_id: Option<ConnectionId>,
440 ) -> Result<GetSiteConfigResponse, LemmyError> {
441 let data: &GetSiteConfig = self;
442 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
444 // Only let admins read this
445 is_admin(&local_user_view)?;
447 let config_hjson = Settings::read_config_file()?;
449 Ok(GetSiteConfigResponse { config_hjson })
453 #[async_trait::async_trait(?Send)]
454 impl Perform for SaveSiteConfig {
455 type Response = GetSiteConfigResponse;
459 context: &Data<LemmyContext>,
460 _websocket_id: Option<ConnectionId>,
461 ) -> Result<GetSiteConfigResponse, LemmyError> {
462 let data: &SaveSiteConfig = self;
463 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
465 // Only let admins read this
466 is_admin(&local_user_view)?;
468 // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
469 let config_hjson = Settings::save_config_file(&data.config_hjson)
470 .map_err(|_| ApiError::err("couldnt_update_site"))?;
472 Ok(GetSiteConfigResponse { config_hjson })