9 use actix_web::web::Data;
11 use lemmy_apub::fetcher::search_by_apub_id;
13 aggregates::site_aggregates::SiteAggregates,
14 diesel_option_overwrite,
15 source::{category::Category_, site::Site_},
17 comment_view::CommentQueryBuilder,
18 community::community_view::CommunityQueryBuilder,
20 mod_add_community_view::ModAddCommunityView,
21 mod_add_view::ModAddView,
22 mod_ban_from_community_view::ModBanFromCommunityView,
23 mod_ban_view::ModBanView,
24 mod_lock_post_view::ModLockPostView,
25 mod_remove_comment_view::ModRemoveCommentView,
26 mod_remove_community_view::ModRemoveCommunityView,
27 mod_remove_post_view::ModRemovePostView,
28 mod_sticky_post_view::ModStickyPostView,
30 post_view::PostQueryBuilder,
32 user_view::{UserQueryBuilder, UserViewSafe},
38 use lemmy_db_schema::{
46 use lemmy_structs::{blocking, site::*, user::Register};
50 utils::{check_slurs, check_slurs_opt},
55 use lemmy_websocket::{
56 messages::{GetUsersOnline, SendAllMessage},
60 use log::{debug, info};
61 use std::str::FromStr;
63 #[async_trait::async_trait(?Send)]
64 impl Perform for ListCategories {
65 type Response = ListCategoriesResponse;
69 context: &Data<LemmyContext>,
70 _websocket_id: Option<ConnectionId>,
71 ) -> Result<ListCategoriesResponse, LemmyError> {
72 let _data: &ListCategories = &self;
74 let categories = blocking(context.pool(), move |conn| Category::list_all(conn)).await??;
77 Ok(ListCategoriesResponse { categories })
81 #[async_trait::async_trait(?Send)]
82 impl Perform for GetModlog {
83 type Response = GetModlogResponse;
87 context: &Data<LemmyContext>,
88 _websocket_id: Option<ConnectionId>,
89 ) -> Result<GetModlogResponse, LemmyError> {
90 let data: &GetModlog = &self;
92 let community_id = data.community_id;
93 let mod_user_id = data.mod_user_id;
95 let limit = data.limit;
96 let removed_posts = blocking(context.pool(), move |conn| {
97 ModRemovePostView::list(conn, community_id, mod_user_id, page, limit)
101 let locked_posts = blocking(context.pool(), move |conn| {
102 ModLockPostView::list(conn, community_id, mod_user_id, page, limit)
106 let stickied_posts = blocking(context.pool(), move |conn| {
107 ModStickyPostView::list(conn, community_id, mod_user_id, page, limit)
111 let removed_comments = blocking(context.pool(), move |conn| {
112 ModRemoveCommentView::list(conn, community_id, mod_user_id, page, limit)
116 let banned_from_community = blocking(context.pool(), move |conn| {
117 ModBanFromCommunityView::list(conn, community_id, mod_user_id, page, limit)
121 let added_to_community = blocking(context.pool(), move |conn| {
122 ModAddCommunityView::list(conn, community_id, mod_user_id, page, limit)
126 // These arrays are only for the full modlog, when a community isn't given
127 let (removed_communities, banned, added) = if data.community_id.is_none() {
128 blocking(context.pool(), move |conn| {
130 ModRemoveCommunityView::list(conn, mod_user_id, page, limit)?,
131 ModBanView::list(conn, mod_user_id, page, limit)?,
132 ModAddView::list(conn, mod_user_id, page, limit)?,
133 )) as Result<_, LemmyError>
137 (Vec::new(), Vec::new(), Vec::new())
141 Ok(GetModlogResponse {
147 banned_from_community,
155 #[async_trait::async_trait(?Send)]
156 impl Perform for CreateSite {
157 type Response = SiteResponse;
161 context: &Data<LemmyContext>,
162 _websocket_id: Option<ConnectionId>,
163 ) -> Result<SiteResponse, LemmyError> {
164 let data: &CreateSite = &self;
166 let read_site = move |conn: &'_ _| Site::read(conn, 1);
167 if blocking(context.pool(), read_site).await?.is_ok() {
168 return Err(APIError::err("site_already_exists").into());
171 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
173 check_slurs(&data.name)?;
174 check_slurs_opt(&data.description)?;
176 // Make sure user is an admin
177 is_admin(context.pool(), user.id).await?;
179 let site_form = SiteForm {
180 name: data.name.to_owned(),
181 description: data.description.to_owned(),
182 icon: Some(data.icon.to_owned()),
183 banner: Some(data.banner.to_owned()),
185 enable_downvotes: data.enable_downvotes,
186 open_registration: data.open_registration,
187 enable_nsfw: data.enable_nsfw,
191 let create_site = move |conn: &'_ _| Site::create(conn, &site_form);
192 if blocking(context.pool(), create_site).await?.is_err() {
193 return Err(APIError::err("site_already_exists").into());
196 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
198 Ok(SiteResponse { site: site_view })
202 #[async_trait::async_trait(?Send)]
203 impl Perform for EditSite {
204 type Response = SiteResponse;
207 context: &Data<LemmyContext>,
208 websocket_id: Option<ConnectionId>,
209 ) -> Result<SiteResponse, LemmyError> {
210 let data: &EditSite = &self;
211 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
213 check_slurs(&data.name)?;
214 check_slurs_opt(&data.description)?;
216 // Make sure user is an admin
217 is_admin(context.pool(), user.id).await?;
219 let found_site = blocking(context.pool(), move |conn| Site::read(conn, 1)).await??;
221 let icon = diesel_option_overwrite(&data.icon);
222 let banner = diesel_option_overwrite(&data.banner);
224 let site_form = SiteForm {
225 name: data.name.to_owned(),
226 description: data.description.to_owned(),
229 creator_id: found_site.creator_id,
230 updated: Some(naive_now()),
231 enable_downvotes: data.enable_downvotes,
232 open_registration: data.open_registration,
233 enable_nsfw: data.enable_nsfw,
236 let update_site = move |conn: &'_ _| Site::update(conn, 1, &site_form);
237 if blocking(context.pool(), update_site).await?.is_err() {
238 return Err(APIError::err("couldnt_update_site").into());
241 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
243 let res = SiteResponse { site: site_view };
245 context.chat_server().do_send(SendAllMessage {
246 op: UserOperation::EditSite,
247 response: res.clone(),
255 #[async_trait::async_trait(?Send)]
256 impl Perform for GetSite {
257 type Response = GetSiteResponse;
261 context: &Data<LemmyContext>,
262 websocket_id: Option<ConnectionId>,
263 ) -> Result<GetSiteResponse, LemmyError> {
264 let data: &GetSite = &self;
266 // TODO refactor this a little
267 let res = blocking(context.pool(), move |conn| Site::read(conn, 1)).await?;
268 let site_view = if res.is_ok() {
269 Some(blocking(context.pool(), move |conn| SiteView::read(conn)).await??)
270 } else if let Some(setup) = Settings::get().setup.as_ref() {
271 let register = Register {
272 username: setup.admin_username.to_owned(),
273 email: setup.admin_email.to_owned(),
274 password: setup.admin_password.to_owned(),
275 password_verify: setup.admin_password.to_owned(),
279 captcha_answer: None,
281 let login_response = register.perform(context, websocket_id).await?;
282 info!("Admin {} created", setup.admin_username);
284 let create_site = CreateSite {
285 name: setup.site_name.to_owned(),
289 enable_downvotes: true,
290 open_registration: true,
292 auth: login_response.jwt,
294 create_site.perform(context, websocket_id).await?;
295 info!("Site {} created", setup.site_name);
296 Some(blocking(context.pool(), move |conn| SiteView::read(conn)).await??)
301 let mut admins = blocking(context.pool(), move |conn| UserViewSafe::admins(conn)).await??;
303 // Make sure the site creator is the top admin
304 if let Some(site_view) = site_view.to_owned() {
305 let site_creator_id = site_view.creator.id;
306 // TODO investigate why this is sometimes coming back null
307 // Maybe user_.admin isn't being set to true?
308 if let Some(creator_index) = admins.iter().position(|r| r.user.id == site_creator_id) {
309 let creator_user = admins.remove(creator_index);
310 admins.insert(0, creator_user);
314 let banned = blocking(context.pool(), move |conn| UserViewSafe::banned(conn)).await??;
318 .send(GetUsersOnline)
322 let my_user = get_user_from_jwt_opt(&data.auth, context.pool())
325 u.password_encrypted = "".to_string();
326 u.private_key = None;
331 let counts = blocking(context.pool(), move |conn| SiteAggregates::read(conn)).await??;
338 version: version::VERSION.to_string(),
340 federated_instances: linked_instances(context.pool()).await?,
346 #[async_trait::async_trait(?Send)]
347 impl Perform for Search {
348 type Response = SearchResponse;
352 context: &Data<LemmyContext>,
353 _websocket_id: Option<ConnectionId>,
354 ) -> Result<SearchResponse, LemmyError> {
355 let data: &Search = &self;
357 match search_by_apub_id(&data.q, context).await {
358 Ok(r) => return Ok(r),
359 Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e),
362 let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
363 let user_id = user.map(|u| u.id);
365 let type_ = SearchType::from_str(&data.type_)?;
367 let mut posts = Vec::new();
368 let mut comments = Vec::new();
369 let mut communities = Vec::new();
370 let mut users = Vec::new();
372 // TODO no clean / non-nsfw searching rn
374 let q = data.q.to_owned();
375 let page = data.page;
376 let limit = data.limit;
377 let sort = SortType::from_str(&data.sort)?;
378 let community_id = data.community_id;
379 let community_name = data.community_name.to_owned();
381 SearchType::Posts => {
382 posts = blocking(context.pool(), move |conn| {
383 PostQueryBuilder::create(conn)
386 .community_id(community_id)
387 .community_name(community_name)
396 SearchType::Comments => {
397 comments = blocking(context.pool(), move |conn| {
398 CommentQueryBuilder::create(&conn)
408 SearchType::Communities => {
409 communities = blocking(context.pool(), move |conn| {
410 CommunityQueryBuilder::create(conn)
419 SearchType::Users => {
420 users = blocking(context.pool(), move |conn| {
421 UserQueryBuilder::create(conn)
431 posts = blocking(context.pool(), move |conn| {
432 PostQueryBuilder::create(conn)
435 .community_id(community_id)
436 .community_name(community_name)
445 let q = data.q.to_owned();
446 let sort = SortType::from_str(&data.sort)?;
448 comments = blocking(context.pool(), move |conn| {
449 CommentQueryBuilder::create(conn)
459 let q = data.q.to_owned();
460 let sort = SortType::from_str(&data.sort)?;
462 communities = blocking(context.pool(), move |conn| {
463 CommunityQueryBuilder::create(conn)
472 let q = data.q.to_owned();
473 let sort = SortType::from_str(&data.sort)?;
475 users = blocking(context.pool(), move |conn| {
476 UserQueryBuilder::create(conn)
486 posts = blocking(context.pool(), move |conn| {
487 PostQueryBuilder::create(conn)
490 .community_id(community_id)
491 .community_name(community_name)
503 type_: data.type_.to_owned(),
512 #[async_trait::async_trait(?Send)]
513 impl Perform for TransferSite {
514 type Response = GetSiteResponse;
518 context: &Data<LemmyContext>,
519 _websocket_id: Option<ConnectionId>,
520 ) -> Result<GetSiteResponse, LemmyError> {
521 let data: &TransferSite = &self;
522 let mut user = get_user_from_jwt(&data.auth, context.pool()).await?;
524 is_admin(context.pool(), user.id).await?;
526 // TODO add a User_::read_safe() for this.
527 user.password_encrypted = "".to_string();
528 user.private_key = None;
529 user.public_key = None;
531 let read_site = blocking(context.pool(), move |conn| Site::read(conn, 1)).await??;
533 // Make sure user is the creator
534 if read_site.creator_id != user.id {
535 return Err(APIError::err("not_an_admin").into());
538 let new_creator_id = data.user_id;
539 let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
540 if blocking(context.pool(), transfer_site).await?.is_err() {
541 return Err(APIError::err("couldnt_update_site").into());
545 let form = ModAddForm {
546 mod_user_id: user.id,
547 other_user_id: data.user_id,
548 removed: Some(false),
551 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
553 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
555 let mut admins = blocking(context.pool(), move |conn| UserViewSafe::admins(conn)).await??;
556 let creator_index = admins
558 .position(|r| r.user.id == site_view.creator.id)
559 .context(location_info!())?;
560 let creator_user = admins.remove(creator_index);
561 admins.insert(0, creator_user);
563 let banned = blocking(context.pool(), move |conn| UserViewSafe::banned(conn)).await??;
565 let counts = blocking(context.pool(), move |conn| SiteAggregates::read(conn)).await??;
568 site: Some(site_view),
572 version: version::VERSION.to_string(),
574 federated_instances: linked_instances(context.pool()).await?,
580 #[async_trait::async_trait(?Send)]
581 impl Perform for GetSiteConfig {
582 type Response = GetSiteConfigResponse;
586 context: &Data<LemmyContext>,
587 _websocket_id: Option<ConnectionId>,
588 ) -> Result<GetSiteConfigResponse, LemmyError> {
589 let data: &GetSiteConfig = &self;
590 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
592 // Only let admins read this
593 is_admin(context.pool(), user.id).await?;
595 let config_hjson = Settings::read_config_file()?;
597 Ok(GetSiteConfigResponse { config_hjson })
601 #[async_trait::async_trait(?Send)]
602 impl Perform for SaveSiteConfig {
603 type Response = GetSiteConfigResponse;
607 context: &Data<LemmyContext>,
608 _websocket_id: Option<ConnectionId>,
609 ) -> Result<GetSiteConfigResponse, LemmyError> {
610 let data: &SaveSiteConfig = &self;
611 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
613 // Only let admins read this
614 let admins = blocking(context.pool(), move |conn| UserViewSafe::admins(conn)).await??;
615 let admin_ids: Vec<i32> = admins.into_iter().map(|m| m.user.id).collect();
617 if !admin_ids.contains(&user.id) {
618 return Err(APIError::err("not_an_admin").into());
621 // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
622 let config_hjson = match Settings::save_config_file(&data.config_hjson) {
623 Ok(config_hjson) => config_hjson,
624 Err(_e) => return Err(APIError::err("couldnt_update_site").into()),
627 Ok(GetSiteConfigResponse { config_hjson })