4 get_user_safe_settings_from_jwt,
5 get_user_safe_settings_from_jwt_opt,
11 use actix_web::web::Data;
13 use lemmy_apub::fetcher::search::search_by_apub_id;
14 use lemmy_db_queries::{
15 diesel_option_overwrite,
16 source::{category::Category_, site::Site_},
21 use lemmy_db_schema::{
30 comment_view::CommentQueryBuilder,
31 post_view::PostQueryBuilder,
34 use lemmy_db_views_actor::{
35 community_view::CommunityQueryBuilder,
36 user_view::{UserQueryBuilder, UserViewSafe},
38 use lemmy_db_views_moderator::{
39 mod_add_community_view::ModAddCommunityView,
40 mod_add_view::ModAddView,
41 mod_ban_from_community_view::ModBanFromCommunityView,
42 mod_ban_view::ModBanView,
43 mod_lock_post_view::ModLockPostView,
44 mod_remove_comment_view::ModRemoveCommentView,
45 mod_remove_community_view::ModRemoveCommunityView,
46 mod_remove_post_view::ModRemovePostView,
47 mod_sticky_post_view::ModStickyPostView,
49 use lemmy_structs::{blocking, site::*, user::Register};
53 utils::{check_slurs, check_slurs_opt},
58 use lemmy_websocket::{
59 messages::{GetUsersOnline, SendAllMessage},
63 use log::{debug, info};
64 use std::str::FromStr;
66 #[async_trait::async_trait(?Send)]
67 impl Perform for ListCategories {
68 type Response = ListCategoriesResponse;
72 context: &Data<LemmyContext>,
73 _websocket_id: Option<ConnectionId>,
74 ) -> Result<ListCategoriesResponse, LemmyError> {
75 let _data: &ListCategories = &self;
77 let categories = blocking(context.pool(), move |conn| Category::list_all(conn)).await??;
80 Ok(ListCategoriesResponse { categories })
84 #[async_trait::async_trait(?Send)]
85 impl Perform for GetModlog {
86 type Response = GetModlogResponse;
90 context: &Data<LemmyContext>,
91 _websocket_id: Option<ConnectionId>,
92 ) -> Result<GetModlogResponse, LemmyError> {
93 let data: &GetModlog = &self;
95 let community_id = data.community_id;
96 let mod_user_id = data.mod_user_id;
98 let limit = data.limit;
99 let removed_posts = blocking(context.pool(), move |conn| {
100 ModRemovePostView::list(conn, community_id, mod_user_id, page, limit)
104 let locked_posts = blocking(context.pool(), move |conn| {
105 ModLockPostView::list(conn, community_id, mod_user_id, page, limit)
109 let stickied_posts = blocking(context.pool(), move |conn| {
110 ModStickyPostView::list(conn, community_id, mod_user_id, page, limit)
114 let removed_comments = blocking(context.pool(), move |conn| {
115 ModRemoveCommentView::list(conn, community_id, mod_user_id, page, limit)
119 let banned_from_community = blocking(context.pool(), move |conn| {
120 ModBanFromCommunityView::list(conn, community_id, mod_user_id, page, limit)
124 let added_to_community = blocking(context.pool(), move |conn| {
125 ModAddCommunityView::list(conn, community_id, mod_user_id, page, limit)
129 // These arrays are only for the full modlog, when a community isn't given
130 let (removed_communities, banned, added) = if data.community_id.is_none() {
131 blocking(context.pool(), move |conn| {
133 ModRemoveCommunityView::list(conn, mod_user_id, page, limit)?,
134 ModBanView::list(conn, mod_user_id, page, limit)?,
135 ModAddView::list(conn, mod_user_id, page, limit)?,
136 )) as Result<_, LemmyError>
140 (Vec::new(), Vec::new(), Vec::new())
144 Ok(GetModlogResponse {
150 banned_from_community,
158 #[async_trait::async_trait(?Send)]
159 impl Perform for CreateSite {
160 type Response = SiteResponse;
164 context: &Data<LemmyContext>,
165 _websocket_id: Option<ConnectionId>,
166 ) -> Result<SiteResponse, LemmyError> {
167 let data: &CreateSite = &self;
169 let read_site = move |conn: &'_ _| Site::read_simple(conn);
170 if blocking(context.pool(), read_site).await?.is_ok() {
171 return Err(APIError::err("site_already_exists").into());
174 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
176 check_slurs(&data.name)?;
177 check_slurs_opt(&data.description)?;
179 // Make sure user is an admin
180 is_admin(context.pool(), user.id).await?;
182 let site_form = SiteForm {
183 name: data.name.to_owned(),
184 description: data.description.to_owned(),
185 icon: Some(data.icon.to_owned()),
186 banner: Some(data.banner.to_owned()),
188 enable_downvotes: data.enable_downvotes,
189 open_registration: data.open_registration,
190 enable_nsfw: data.enable_nsfw,
194 let create_site = move |conn: &'_ _| Site::create(conn, &site_form);
195 if blocking(context.pool(), create_site).await?.is_err() {
196 return Err(APIError::err("site_already_exists").into());
199 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
201 Ok(SiteResponse { site_view })
205 #[async_trait::async_trait(?Send)]
206 impl Perform for EditSite {
207 type Response = SiteResponse;
210 context: &Data<LemmyContext>,
211 websocket_id: Option<ConnectionId>,
212 ) -> Result<SiteResponse, LemmyError> {
213 let data: &EditSite = &self;
214 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
216 check_slurs(&data.name)?;
217 check_slurs_opt(&data.description)?;
219 // Make sure user is an admin
220 is_admin(context.pool(), user.id).await?;
222 let found_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
224 let icon = diesel_option_overwrite(&data.icon);
225 let banner = diesel_option_overwrite(&data.banner);
227 let site_form = SiteForm {
228 name: data.name.to_owned(),
229 description: data.description.to_owned(),
232 creator_id: found_site.creator_id,
233 updated: Some(naive_now()),
234 enable_downvotes: data.enable_downvotes,
235 open_registration: data.open_registration,
236 enable_nsfw: data.enable_nsfw,
239 let update_site = move |conn: &'_ _| Site::update(conn, 1, &site_form);
240 if blocking(context.pool(), update_site).await?.is_err() {
241 return Err(APIError::err("couldnt_update_site").into());
244 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
246 let res = SiteResponse { site_view };
248 context.chat_server().do_send(SendAllMessage {
249 op: UserOperation::EditSite,
250 response: res.clone(),
258 #[async_trait::async_trait(?Send)]
259 impl Perform for GetSite {
260 type Response = GetSiteResponse;
264 context: &Data<LemmyContext>,
265 websocket_id: Option<ConnectionId>,
266 ) -> Result<GetSiteResponse, LemmyError> {
267 let data: &GetSite = &self;
269 let site_view = match blocking(context.pool(), move |conn| SiteView::read(conn)).await? {
270 Ok(site_view) => Some(site_view),
271 // If the site isn't created yet, check the setup
273 if let Some(setup) = Settings::get().setup.as_ref() {
274 let register = Register {
275 username: setup.admin_username.to_owned(),
276 email: setup.admin_email.to_owned(),
277 password: setup.admin_password.to_owned(),
278 password_verify: setup.admin_password.to_owned(),
281 captcha_answer: None,
283 let login_response = register.perform(context, websocket_id).await?;
284 info!("Admin {} created", setup.admin_username);
286 let create_site = CreateSite {
287 name: setup.site_name.to_owned(),
291 enable_downvotes: true,
292 open_registration: true,
294 auth: login_response.jwt,
296 create_site.perform(context, websocket_id).await?;
297 info!("Site {} created", setup.site_name);
298 Some(blocking(context.pool(), move |conn| SiteView::read(conn)).await??)
305 let mut admins = blocking(context.pool(), move |conn| UserViewSafe::admins(conn)).await??;
307 // Make sure the site creator is the top admin
308 if let Some(site_view) = site_view.to_owned() {
309 let site_creator_id = site_view.creator.id;
310 // TODO investigate why this is sometimes coming back null
311 // Maybe user_.admin isn't being set to true?
312 if let Some(creator_index) = admins.iter().position(|r| r.user.id == site_creator_id) {
313 let creator_user = admins.remove(creator_index);
314 admins.insert(0, creator_user);
318 let banned = blocking(context.pool(), move |conn| UserViewSafe::banned(conn)).await??;
322 .send(GetUsersOnline)
326 let my_user = get_user_safe_settings_from_jwt_opt(&data.auth, context.pool()).await?;
333 version: version::VERSION.to_string(),
335 federated_instances: linked_instances(context.pool()).await?,
340 #[async_trait::async_trait(?Send)]
341 impl Perform for Search {
342 type Response = SearchResponse;
346 context: &Data<LemmyContext>,
347 _websocket_id: Option<ConnectionId>,
348 ) -> Result<SearchResponse, LemmyError> {
349 let data: &Search = &self;
351 match search_by_apub_id(&data.q, context).await {
352 Ok(r) => return Ok(r),
353 Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e),
356 let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
357 let user_id = user.map(|u| u.id);
359 let type_ = SearchType::from_str(&data.type_)?;
361 let mut posts = Vec::new();
362 let mut comments = Vec::new();
363 let mut communities = Vec::new();
364 let mut users = Vec::new();
366 // TODO no clean / non-nsfw searching rn
368 let q = data.q.to_owned();
369 let page = data.page;
370 let limit = data.limit;
371 let sort = SortType::from_str(&data.sort)?;
372 let community_id = data.community_id;
373 let community_name = data.community_name.to_owned();
375 SearchType::Posts => {
376 posts = blocking(context.pool(), move |conn| {
377 PostQueryBuilder::create(conn)
380 .community_id(community_id)
381 .community_name(community_name)
390 SearchType::Comments => {
391 comments = blocking(context.pool(), move |conn| {
392 CommentQueryBuilder::create(&conn)
402 SearchType::Communities => {
403 communities = blocking(context.pool(), move |conn| {
404 CommunityQueryBuilder::create(conn)
413 SearchType::Users => {
414 users = blocking(context.pool(), move |conn| {
415 UserQueryBuilder::create(conn)
425 posts = blocking(context.pool(), move |conn| {
426 PostQueryBuilder::create(conn)
429 .community_id(community_id)
430 .community_name(community_name)
439 let q = data.q.to_owned();
440 let sort = SortType::from_str(&data.sort)?;
442 comments = blocking(context.pool(), move |conn| {
443 CommentQueryBuilder::create(conn)
453 let q = data.q.to_owned();
454 let sort = SortType::from_str(&data.sort)?;
456 communities = blocking(context.pool(), move |conn| {
457 CommunityQueryBuilder::create(conn)
466 let q = data.q.to_owned();
467 let sort = SortType::from_str(&data.sort)?;
469 users = blocking(context.pool(), move |conn| {
470 UserQueryBuilder::create(conn)
480 posts = blocking(context.pool(), move |conn| {
481 PostQueryBuilder::create(conn)
484 .community_id(community_id)
485 .community_name(community_name)
497 type_: data.type_.to_owned(),
506 #[async_trait::async_trait(?Send)]
507 impl Perform for TransferSite {
508 type Response = GetSiteResponse;
512 context: &Data<LemmyContext>,
513 _websocket_id: Option<ConnectionId>,
514 ) -> Result<GetSiteResponse, LemmyError> {
515 let data: &TransferSite = &self;
516 let user = get_user_safe_settings_from_jwt(&data.auth, context.pool()).await?;
518 is_admin(context.pool(), user.id).await?;
520 let read_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
522 // Make sure user is the creator
523 if read_site.creator_id != user.id {
524 return Err(APIError::err("not_an_admin").into());
527 let new_creator_id = data.user_id;
528 let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
529 if blocking(context.pool(), transfer_site).await?.is_err() {
530 return Err(APIError::err("couldnt_update_site").into());
534 let form = ModAddForm {
535 mod_user_id: user.id,
536 other_user_id: data.user_id,
537 removed: Some(false),
540 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
542 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
544 let mut admins = blocking(context.pool(), move |conn| UserViewSafe::admins(conn)).await??;
545 let creator_index = admins
547 .position(|r| r.user.id == site_view.creator.id)
548 .context(location_info!())?;
549 let creator_user = admins.remove(creator_index);
550 admins.insert(0, creator_user);
552 let banned = blocking(context.pool(), move |conn| UserViewSafe::banned(conn)).await??;
555 site_view: Some(site_view),
559 version: version::VERSION.to_string(),
561 federated_instances: linked_instances(context.pool()).await?,
566 #[async_trait::async_trait(?Send)]
567 impl Perform for GetSiteConfig {
568 type Response = GetSiteConfigResponse;
572 context: &Data<LemmyContext>,
573 _websocket_id: Option<ConnectionId>,
574 ) -> Result<GetSiteConfigResponse, LemmyError> {
575 let data: &GetSiteConfig = &self;
576 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
578 // Only let admins read this
579 is_admin(context.pool(), user.id).await?;
581 let config_hjson = Settings::read_config_file()?;
583 Ok(GetSiteConfigResponse { config_hjson })
587 #[async_trait::async_trait(?Send)]
588 impl Perform for SaveSiteConfig {
589 type Response = GetSiteConfigResponse;
593 context: &Data<LemmyContext>,
594 _websocket_id: Option<ConnectionId>,
595 ) -> Result<GetSiteConfigResponse, LemmyError> {
596 let data: &SaveSiteConfig = &self;
597 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
599 // Only let admins read this
600 let user_id = user.id;
601 is_admin(context.pool(), user_id).await?;
603 // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
604 let config_hjson = match Settings::save_config_file(&data.config_hjson) {
605 Ok(config_hjson) => config_hjson,
606 Err(_e) => return Err(APIError::err("couldnt_update_site").into()),
609 Ok(GetSiteConfigResponse { config_hjson })