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;
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 let transferred_to_community = blocking(context.pool(), move |conn| {
100 ModTransferCommunityView::list(conn, community_id, mod_person_id, page, limit)
104 // These arrays are only for the full modlog, when a community isn't given
105 let (removed_communities, banned, added) = if data.community_id.is_none() {
106 blocking(context.pool(), move |conn| {
108 ModRemoveCommunityView::list(conn, mod_person_id, page, limit)?,
109 ModBanView::list(conn, mod_person_id, page, limit)?,
110 ModAddView::list(conn, mod_person_id, page, limit)?,
111 )) as Result<_, LemmyError>
115 (Vec::new(), Vec::new(), Vec::new())
119 Ok(GetModlogResponse {
125 banned_from_community,
129 transferred_to_community,
134 #[async_trait::async_trait(?Send)]
135 impl Perform for Search {
136 type Response = SearchResponse;
140 context: &Data<LemmyContext>,
141 _websocket_id: Option<ConnectionId>,
142 ) -> Result<SearchResponse, LemmyError> {
143 let data: &Search = self;
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 ResolveObject {
371 type Response = ResolveObjectResponse;
375 context: &Data<LemmyContext>,
376 _websocket_id: Option<ConnectionId>,
377 ) -> Result<ResolveObjectResponse, LemmyError> {
378 let local_user_view = get_local_user_view_from_jwt_opt(&self.auth, context.pool()).await?;
379 let res = search_by_apub_id(&self.q, local_user_view, context)
381 .map_err(|_| ApiError::err("couldnt_find_object"))?;
386 #[async_trait::async_trait(?Send)]
387 impl Perform for TransferSite {
388 type Response = GetSiteResponse;
392 context: &Data<LemmyContext>,
393 _websocket_id: Option<ConnectionId>,
394 ) -> Result<GetSiteResponse, LemmyError> {
395 let data: &TransferSite = self;
396 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
398 is_admin(&local_user_view)?;
400 let read_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
402 // Make sure user is the creator
403 if read_site.creator_id != local_user_view.person.id {
404 return Err(ApiError::err("not_an_admin").into());
407 let new_creator_id = data.person_id;
408 let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
409 if blocking(context.pool(), transfer_site).await?.is_err() {
410 return Err(ApiError::err("couldnt_update_site").into());
414 let form = ModAddForm {
415 mod_person_id: local_user_view.person.id,
416 other_person_id: data.person_id,
417 removed: Some(false),
420 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
422 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
424 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
425 let creator_index = admins
427 .position(|r| r.person.id == site_view.creator.id)
428 .context(location_info!())?;
429 let creator_person = admins.remove(creator_index);
430 admins.insert(0, creator_person);
432 let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
433 let federated_instances = build_federated_instances(context.pool()).await?;
436 site_view: Some(site_view),
440 version: version::VERSION.to_string(),
447 #[async_trait::async_trait(?Send)]
448 impl Perform for GetSiteConfig {
449 type Response = GetSiteConfigResponse;
453 context: &Data<LemmyContext>,
454 _websocket_id: Option<ConnectionId>,
455 ) -> Result<GetSiteConfigResponse, LemmyError> {
456 let data: &GetSiteConfig = self;
457 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
459 // Only let admins read this
460 is_admin(&local_user_view)?;
462 let config_hjson = Settings::read_config_file()?;
464 Ok(GetSiteConfigResponse { config_hjson })
468 #[async_trait::async_trait(?Send)]
469 impl Perform for SaveSiteConfig {
470 type Response = GetSiteConfigResponse;
474 context: &Data<LemmyContext>,
475 _websocket_id: Option<ConnectionId>,
476 ) -> Result<GetSiteConfigResponse, LemmyError> {
477 let data: &SaveSiteConfig = self;
478 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
480 // Only let admins read this
481 is_admin(&local_user_view)?;
483 // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
484 let config_hjson = Settings::save_config_file(&data.config_hjson)
485 .map_err(|_| ApiError::err("couldnt_update_site"))?;
487 Ok(GetSiteConfigResponse { config_hjson })