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::fetcher::search::search_by_apub_id;
14 use lemmy_db_queries::{
15 from_opt_str_to_opt_enum,
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,
45 settings::structs::Settings,
51 use lemmy_websocket::LemmyContext;
54 #[async_trait::async_trait(?Send)]
55 impl Perform for GetModlog {
56 type Response = GetModlogResponse;
60 context: &Data<LemmyContext>,
61 _websocket_id: Option<ConnectionId>,
62 ) -> Result<GetModlogResponse, LemmyError> {
63 let data: &GetModlog = &self;
65 let community_id = data.community_id;
66 let mod_person_id = data.mod_person_id;
68 let limit = data.limit;
69 let removed_posts = blocking(context.pool(), move |conn| {
70 ModRemovePostView::list(conn, community_id, mod_person_id, page, limit)
74 let locked_posts = blocking(context.pool(), move |conn| {
75 ModLockPostView::list(conn, community_id, mod_person_id, page, limit)
79 let stickied_posts = blocking(context.pool(), move |conn| {
80 ModStickyPostView::list(conn, community_id, mod_person_id, page, limit)
84 let removed_comments = blocking(context.pool(), move |conn| {
85 ModRemoveCommentView::list(conn, community_id, mod_person_id, page, limit)
89 let banned_from_community = blocking(context.pool(), move |conn| {
90 ModBanFromCommunityView::list(conn, community_id, mod_person_id, page, limit)
94 let added_to_community = blocking(context.pool(), move |conn| {
95 ModAddCommunityView::list(conn, community_id, mod_person_id, page, limit)
99 // These arrays are only for the full modlog, when a community isn't given
100 let (removed_communities, banned, added) = if data.community_id.is_none() {
101 blocking(context.pool(), move |conn| {
103 ModRemoveCommunityView::list(conn, mod_person_id, page, limit)?,
104 ModBanView::list(conn, mod_person_id, page, limit)?,
105 ModAddView::list(conn, mod_person_id, page, limit)?,
106 )) as Result<_, LemmyError>
110 (Vec::new(), Vec::new(), Vec::new())
114 Ok(GetModlogResponse {
120 banned_from_community,
128 #[async_trait::async_trait(?Send)]
129 impl Perform for Search {
130 type Response = SearchResponse;
134 context: &Data<LemmyContext>,
135 _websocket_id: Option<ConnectionId>,
136 ) -> Result<SearchResponse, LemmyError> {
137 let data: &Search = &self;
139 match search_by_apub_id(&data.q, context).await {
140 Ok(r) => return Ok(r),
141 Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e),
144 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
146 let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
147 let show_bot_accounts = local_user_view
149 .map(|t| t.local_user.show_bot_accounts);
150 let show_read_posts = local_user_view
152 .map(|t| t.local_user.show_read_posts);
154 let person_id = local_user_view.map(|u| u.person.id);
156 let mut posts = Vec::new();
157 let mut comments = Vec::new();
158 let mut communities = Vec::new();
159 let mut users = Vec::new();
161 // TODO no clean / non-nsfw searching rn
163 let q = data.q.to_owned();
164 let page = data.page;
165 let limit = data.limit;
166 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
167 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.listing_type);
168 let search_type: SearchType = from_opt_str_to_opt_enum(&data.type_).unwrap_or(SearchType::All);
169 let community_id = data.community_id;
170 let community_name = data.community_name.to_owned();
171 let creator_id = data.creator_id;
173 SearchType::Posts => {
174 posts = blocking(context.pool(), move |conn| {
175 PostQueryBuilder::create(conn)
177 .show_nsfw(show_nsfw)
178 .show_bot_accounts(show_bot_accounts)
179 .show_read_posts(show_read_posts)
180 .listing_type(listing_type)
181 .community_id(community_id)
182 .community_name(community_name)
183 .creator_id(creator_id)
184 .my_person_id(person_id)
192 SearchType::Comments => {
193 comments = blocking(context.pool(), move |conn| {
194 CommentQueryBuilder::create(&conn)
196 .listing_type(listing_type)
198 .show_bot_accounts(show_bot_accounts)
199 .community_id(community_id)
200 .community_name(community_name)
201 .creator_id(creator_id)
202 .my_person_id(person_id)
209 SearchType::Communities => {
210 communities = blocking(context.pool(), move |conn| {
211 CommunityQueryBuilder::create(conn)
213 .listing_type(listing_type)
215 .my_person_id(person_id)
222 SearchType::Users => {
223 users = blocking(context.pool(), move |conn| {
224 PersonQueryBuilder::create(conn)
234 // If the community or creator is included, dont search communities or users
235 let community_or_creator_included =
236 data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
238 posts = blocking(context.pool(), move |conn| {
239 PostQueryBuilder::create(conn)
241 .show_nsfw(show_nsfw)
242 .show_bot_accounts(show_bot_accounts)
243 .show_read_posts(show_read_posts)
244 .listing_type(listing_type)
245 .community_id(community_id)
246 .community_name(community_name)
247 .creator_id(creator_id)
248 .my_person_id(person_id)
256 let q = data.q.to_owned();
257 let community_name = data.community_name.to_owned();
259 comments = blocking(context.pool(), move |conn| {
260 CommentQueryBuilder::create(conn)
262 .listing_type(listing_type)
264 .show_bot_accounts(show_bot_accounts)
265 .community_id(community_id)
266 .community_name(community_name)
267 .creator_id(creator_id)
268 .my_person_id(person_id)
275 let q = data.q.to_owned();
277 communities = if community_or_creator_included {
280 blocking(context.pool(), move |conn| {
281 CommunityQueryBuilder::create(conn)
283 .listing_type(listing_type)
285 .my_person_id(person_id)
293 let q = data.q.to_owned();
295 users = if community_or_creator_included {
298 blocking(context.pool(), move |conn| {
299 PersonQueryBuilder::create(conn)
310 posts = blocking(context.pool(), move |conn| {
311 PostQueryBuilder::create(conn)
313 .show_nsfw(show_nsfw)
314 .show_bot_accounts(show_bot_accounts)
315 .show_read_posts(show_read_posts)
316 .listing_type(listing_type)
317 .my_person_id(person_id)
318 .community_id(community_id)
319 .community_name(community_name)
320 .creator_id(creator_id)
332 type_: search_type.to_string(),
341 #[async_trait::async_trait(?Send)]
342 impl Perform for TransferSite {
343 type Response = GetSiteResponse;
347 context: &Data<LemmyContext>,
348 _websocket_id: Option<ConnectionId>,
349 ) -> Result<GetSiteResponse, LemmyError> {
350 let data: &TransferSite = &self;
351 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
353 is_admin(&local_user_view)?;
355 let read_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
357 // Make sure user is the creator
358 if read_site.creator_id != local_user_view.person.id {
359 return Err(ApiError::err("not_an_admin").into());
362 let new_creator_id = data.person_id;
363 let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
364 if blocking(context.pool(), transfer_site).await?.is_err() {
365 return Err(ApiError::err("couldnt_update_site").into());
369 let form = ModAddForm {
370 mod_person_id: local_user_view.person.id,
371 other_person_id: data.person_id,
372 removed: Some(false),
375 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
377 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
379 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
380 let creator_index = admins
382 .position(|r| r.person.id == site_view.creator.id)
383 .context(location_info!())?;
384 let creator_person = admins.remove(creator_index);
385 admins.insert(0, creator_person);
387 let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
388 let federated_instances = build_federated_instances(context.pool()).await?;
390 let my_user = Some(get_local_user_settings_view_from_jwt(&data.auth, context.pool()).await?);
393 site_view: Some(site_view),
397 version: version::VERSION.to_string(),
404 #[async_trait::async_trait(?Send)]
405 impl Perform for GetSiteConfig {
406 type Response = GetSiteConfigResponse;
410 context: &Data<LemmyContext>,
411 _websocket_id: Option<ConnectionId>,
412 ) -> Result<GetSiteConfigResponse, LemmyError> {
413 let data: &GetSiteConfig = &self;
414 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
416 // Only let admins read this
417 is_admin(&local_user_view)?;
419 let config_hjson = Settings::read_config_file()?;
421 Ok(GetSiteConfigResponse { config_hjson })
425 #[async_trait::async_trait(?Send)]
426 impl Perform for SaveSiteConfig {
427 type Response = GetSiteConfigResponse;
431 context: &Data<LemmyContext>,
432 _websocket_id: Option<ConnectionId>,
433 ) -> Result<GetSiteConfigResponse, LemmyError> {
434 let data: &SaveSiteConfig = &self;
435 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
437 // Only let admins read this
438 is_admin(&local_user_view)?;
440 // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
441 let config_hjson = Settings::save_config_file(&data.config_hjson)
442 .map_err(|_| ApiError::err("couldnt_update_site"))?;
444 Ok(GetSiteConfigResponse { config_hjson })