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,
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_actor_id = data
173 .map(|t| build_actor_id_from_shortname(EndpointType::Community, t).ok())
175 let creator_id = data.creator_id;
177 SearchType::Posts => {
178 posts = blocking(context.pool(), move |conn| {
179 PostQueryBuilder::create(conn)
181 .show_nsfw(show_nsfw)
182 .show_bot_accounts(show_bot_accounts)
183 .show_read_posts(show_read_posts)
184 .listing_type(listing_type)
185 .community_id(community_id)
186 .community_actor_id(community_actor_id)
187 .creator_id(creator_id)
188 .my_person_id(person_id)
196 SearchType::Comments => {
197 comments = blocking(context.pool(), move |conn| {
198 CommentQueryBuilder::create(conn)
200 .listing_type(listing_type)
202 .show_bot_accounts(show_bot_accounts)
203 .community_id(community_id)
204 .community_actor_id(community_actor_id)
205 .creator_id(creator_id)
206 .my_person_id(person_id)
213 SearchType::Communities => {
214 communities = blocking(context.pool(), move |conn| {
215 CommunityQueryBuilder::create(conn)
217 .listing_type(listing_type)
219 .my_person_id(person_id)
226 SearchType::Users => {
227 users = blocking(context.pool(), move |conn| {
228 PersonQueryBuilder::create(conn)
238 // If the community or creator is included, dont search communities or users
239 let community_or_creator_included =
240 data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
241 let community_actor_id_2 = community_actor_id.to_owned();
243 posts = blocking(context.pool(), move |conn| {
244 PostQueryBuilder::create(conn)
246 .show_nsfw(show_nsfw)
247 .show_bot_accounts(show_bot_accounts)
248 .show_read_posts(show_read_posts)
249 .listing_type(listing_type)
250 .community_id(community_id)
251 .community_actor_id(community_actor_id_2)
252 .creator_id(creator_id)
253 .my_person_id(person_id)
261 let q = data.q.to_owned();
262 let community_actor_id = community_actor_id.to_owned();
264 comments = blocking(context.pool(), move |conn| {
265 CommentQueryBuilder::create(conn)
267 .listing_type(listing_type)
269 .show_bot_accounts(show_bot_accounts)
270 .community_id(community_id)
271 .community_actor_id(community_actor_id)
272 .creator_id(creator_id)
273 .my_person_id(person_id)
280 let q = data.q.to_owned();
282 communities = if community_or_creator_included {
285 blocking(context.pool(), move |conn| {
286 CommunityQueryBuilder::create(conn)
288 .listing_type(listing_type)
290 .my_person_id(person_id)
298 let q = data.q.to_owned();
300 users = if community_or_creator_included {
303 blocking(context.pool(), move |conn| {
304 PersonQueryBuilder::create(conn)
315 posts = blocking(context.pool(), move |conn| {
316 PostQueryBuilder::create(conn)
318 .show_nsfw(show_nsfw)
319 .show_bot_accounts(show_bot_accounts)
320 .show_read_posts(show_read_posts)
321 .listing_type(listing_type)
322 .my_person_id(person_id)
323 .community_id(community_id)
324 .community_actor_id(community_actor_id)
325 .creator_id(creator_id)
337 type_: search_type.to_string(),
346 #[async_trait::async_trait(?Send)]
347 impl Perform for TransferSite {
348 type Response = GetSiteResponse;
352 context: &Data<LemmyContext>,
353 _websocket_id: Option<ConnectionId>,
354 ) -> Result<GetSiteResponse, LemmyError> {
355 let data: &TransferSite = self;
356 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
358 is_admin(&local_user_view)?;
360 let read_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
362 // Make sure user is the creator
363 if read_site.creator_id != local_user_view.person.id {
364 return Err(ApiError::err("not_an_admin").into());
367 let new_creator_id = data.person_id;
368 let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
369 if blocking(context.pool(), transfer_site).await?.is_err() {
370 return Err(ApiError::err("couldnt_update_site").into());
374 let form = ModAddForm {
375 mod_person_id: local_user_view.person.id,
376 other_person_id: data.person_id,
377 removed: Some(false),
380 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
382 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
384 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
385 let creator_index = admins
387 .position(|r| r.person.id == site_view.creator.id)
388 .context(location_info!())?;
389 let creator_person = admins.remove(creator_index);
390 admins.insert(0, creator_person);
392 let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
393 let federated_instances = build_federated_instances(context.pool()).await?;
395 let my_user = Some(get_local_user_settings_view_from_jwt(&data.auth, context.pool()).await?);
398 site_view: Some(site_view),
402 version: version::VERSION.to_string(),
409 #[async_trait::async_trait(?Send)]
410 impl Perform for GetSiteConfig {
411 type Response = GetSiteConfigResponse;
415 context: &Data<LemmyContext>,
416 _websocket_id: Option<ConnectionId>,
417 ) -> Result<GetSiteConfigResponse, LemmyError> {
418 let data: &GetSiteConfig = self;
419 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
421 // Only let admins read this
422 is_admin(&local_user_view)?;
424 let config_hjson = Settings::read_config_file()?;
426 Ok(GetSiteConfigResponse { config_hjson })
430 #[async_trait::async_trait(?Send)]
431 impl Perform for SaveSiteConfig {
432 type Response = GetSiteConfigResponse;
436 context: &Data<LemmyContext>,
437 _websocket_id: Option<ConnectionId>,
438 ) -> Result<GetSiteConfigResponse, LemmyError> {
439 let data: &SaveSiteConfig = self;
440 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
442 // Only let admins read this
443 is_admin(&local_user_view)?;
445 // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
446 let config_hjson = Settings::save_config_file(&data.config_hjson)
447 .map_err(|_| ApiError::err("couldnt_update_site"))?;
449 Ok(GetSiteConfigResponse { config_hjson })