2 build_federated_instances,
5 get_user_safe_settings_from_jwt,
6 get_user_safe_settings_from_jwt_opt,
10 use actix_web::web::Data;
12 use lemmy_api_structs::{blocking, site::*, user::Register};
13 use lemmy_apub::fetcher::search::search_by_apub_id;
14 use lemmy_db_queries::{
15 diesel_option_overwrite_to_url,
21 use lemmy_db_schema::{
29 comment_view::CommentQueryBuilder,
30 post_view::PostQueryBuilder,
33 use lemmy_db_views_actor::{
34 community_view::CommunityQueryBuilder,
35 user_view::{UserQueryBuilder, UserViewSafe},
37 use lemmy_db_views_moderator::{
38 mod_add_community_view::ModAddCommunityView,
39 mod_add_view::ModAddView,
40 mod_ban_from_community_view::ModBanFromCommunityView,
41 mod_ban_view::ModBanView,
42 mod_lock_post_view::ModLockPostView,
43 mod_remove_comment_view::ModRemoveCommentView,
44 mod_remove_community_view::ModRemoveCommunityView,
45 mod_remove_post_view::ModRemovePostView,
46 mod_sticky_post_view::ModStickyPostView,
50 settings::structs::Settings,
51 utils::{check_slurs, check_slurs_opt},
57 use lemmy_websocket::{
58 messages::{GetUsersOnline, SendAllMessage},
62 use log::{debug, info};
63 use std::str::FromStr;
65 #[async_trait::async_trait(?Send)]
66 impl Perform for GetModlog {
67 type Response = GetModlogResponse;
71 context: &Data<LemmyContext>,
72 _websocket_id: Option<ConnectionId>,
73 ) -> Result<GetModlogResponse, LemmyError> {
74 let data: &GetModlog = &self;
76 let community_id = data.community_id;
77 let mod_user_id = data.mod_user_id;
79 let limit = data.limit;
80 let removed_posts = blocking(context.pool(), move |conn| {
81 ModRemovePostView::list(conn, community_id, mod_user_id, page, limit)
85 let locked_posts = blocking(context.pool(), move |conn| {
86 ModLockPostView::list(conn, community_id, mod_user_id, page, limit)
90 let stickied_posts = blocking(context.pool(), move |conn| {
91 ModStickyPostView::list(conn, community_id, mod_user_id, page, limit)
95 let removed_comments = blocking(context.pool(), move |conn| {
96 ModRemoveCommentView::list(conn, community_id, mod_user_id, page, limit)
100 let banned_from_community = blocking(context.pool(), move |conn| {
101 ModBanFromCommunityView::list(conn, community_id, mod_user_id, page, limit)
105 let added_to_community = blocking(context.pool(), move |conn| {
106 ModAddCommunityView::list(conn, community_id, mod_user_id, page, limit)
110 // These arrays are only for the full modlog, when a community isn't given
111 let (removed_communities, banned, added) = if data.community_id.is_none() {
112 blocking(context.pool(), move |conn| {
114 ModRemoveCommunityView::list(conn, mod_user_id, page, limit)?,
115 ModBanView::list(conn, mod_user_id, page, limit)?,
116 ModAddView::list(conn, mod_user_id, page, limit)?,
117 )) as Result<_, LemmyError>
121 (Vec::new(), Vec::new(), Vec::new())
125 Ok(GetModlogResponse {
131 banned_from_community,
139 #[async_trait::async_trait(?Send)]
140 impl Perform for CreateSite {
141 type Response = SiteResponse;
145 context: &Data<LemmyContext>,
146 _websocket_id: Option<ConnectionId>,
147 ) -> Result<SiteResponse, LemmyError> {
148 let data: &CreateSite = &self;
150 let read_site = move |conn: &'_ _| Site::read_simple(conn);
151 if blocking(context.pool(), read_site).await?.is_ok() {
152 return Err(ApiError::err("site_already_exists").into());
155 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
157 check_slurs(&data.name)?;
158 check_slurs_opt(&data.description)?;
160 // Make sure user is an admin
161 is_admin(context.pool(), user.id).await?;
163 let site_form = SiteForm {
164 name: data.name.to_owned(),
165 description: data.description.to_owned(),
166 icon: Some(data.icon.to_owned().map(|url| url.into())),
167 banner: Some(data.banner.to_owned().map(|url| url.into())),
169 enable_downvotes: data.enable_downvotes,
170 open_registration: data.open_registration,
171 enable_nsfw: data.enable_nsfw,
175 let create_site = move |conn: &'_ _| Site::create(conn, &site_form);
176 if blocking(context.pool(), create_site).await?.is_err() {
177 return Err(ApiError::err("site_already_exists").into());
180 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
182 Ok(SiteResponse { site_view })
186 #[async_trait::async_trait(?Send)]
187 impl Perform for EditSite {
188 type Response = SiteResponse;
191 context: &Data<LemmyContext>,
192 websocket_id: Option<ConnectionId>,
193 ) -> Result<SiteResponse, LemmyError> {
194 let data: &EditSite = &self;
195 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
197 check_slurs(&data.name)?;
198 check_slurs_opt(&data.description)?;
200 // Make sure user is an admin
201 is_admin(context.pool(), user.id).await?;
203 let found_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
205 let icon = diesel_option_overwrite_to_url(&data.icon)?;
206 let banner = diesel_option_overwrite_to_url(&data.banner)?;
208 let site_form = SiteForm {
209 name: data.name.to_owned(),
210 description: data.description.to_owned(),
213 creator_id: found_site.creator_id,
214 updated: Some(naive_now()),
215 enable_downvotes: data.enable_downvotes,
216 open_registration: data.open_registration,
217 enable_nsfw: data.enable_nsfw,
220 let update_site = move |conn: &'_ _| Site::update(conn, 1, &site_form);
221 if blocking(context.pool(), update_site).await?.is_err() {
222 return Err(ApiError::err("couldnt_update_site").into());
225 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
227 let res = SiteResponse { site_view };
229 context.chat_server().do_send(SendAllMessage {
230 op: UserOperation::EditSite,
231 response: res.clone(),
239 #[async_trait::async_trait(?Send)]
240 impl Perform for GetSite {
241 type Response = GetSiteResponse;
245 context: &Data<LemmyContext>,
246 websocket_id: Option<ConnectionId>,
247 ) -> Result<GetSiteResponse, LemmyError> {
248 let data: &GetSite = &self;
250 let site_view = match blocking(context.pool(), move |conn| SiteView::read(conn)).await? {
251 Ok(site_view) => Some(site_view),
252 // If the site isn't created yet, check the setup
254 if let Some(setup) = Settings::get().setup().as_ref() {
255 let register = Register {
256 username: setup.admin_username.to_owned(),
257 email: setup.admin_email.to_owned(),
258 password: setup.admin_password.to_owned(),
259 password_verify: setup.admin_password.to_owned(),
262 captcha_answer: None,
264 let login_response = register.perform(context, websocket_id).await?;
265 info!("Admin {} created", setup.admin_username);
267 let create_site = CreateSite {
268 name: setup.site_name.to_owned(),
272 enable_downvotes: true,
273 open_registration: true,
275 auth: login_response.jwt,
277 create_site.perform(context, websocket_id).await?;
278 info!("Site {} created", setup.site_name);
279 Some(blocking(context.pool(), move |conn| SiteView::read(conn)).await??)
286 let mut admins = blocking(context.pool(), move |conn| UserViewSafe::admins(conn)).await??;
288 // Make sure the site creator is the top admin
289 if let Some(site_view) = site_view.to_owned() {
290 let site_creator_id = site_view.creator.id;
291 // TODO investigate why this is sometimes coming back null
292 // Maybe user_.admin isn't being set to true?
293 if let Some(creator_index) = admins.iter().position(|r| r.user.id == site_creator_id) {
294 let creator_user = admins.remove(creator_index);
295 admins.insert(0, creator_user);
299 let banned = blocking(context.pool(), move |conn| UserViewSafe::banned(conn)).await??;
303 .send(GetUsersOnline)
307 let my_user = get_user_safe_settings_from_jwt_opt(&data.auth, context.pool()).await?;
308 let federated_instances = build_federated_instances(context.pool()).await?;
315 version: version::VERSION.to_string(),
322 #[async_trait::async_trait(?Send)]
323 impl Perform for Search {
324 type Response = SearchResponse;
328 context: &Data<LemmyContext>,
329 _websocket_id: Option<ConnectionId>,
330 ) -> Result<SearchResponse, LemmyError> {
331 let data: &Search = &self;
333 match search_by_apub_id(&data.q, context).await {
334 Ok(r) => return Ok(r),
335 Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e),
338 let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
339 let user_id = user.map(|u| u.id);
341 let type_ = SearchType::from_str(&data.type_)?;
343 let mut posts = Vec::new();
344 let mut comments = Vec::new();
345 let mut communities = Vec::new();
346 let mut users = Vec::new();
348 // TODO no clean / non-nsfw searching rn
350 let q = data.q.to_owned();
351 let page = data.page;
352 let limit = data.limit;
353 let sort = SortType::from_str(&data.sort)?;
354 let community_id = data.community_id;
355 let community_name = data.community_name.to_owned();
357 SearchType::Posts => {
358 posts = blocking(context.pool(), move |conn| {
359 PostQueryBuilder::create(conn)
362 .community_id(community_id)
363 .community_name(community_name)
372 SearchType::Comments => {
373 comments = blocking(context.pool(), move |conn| {
374 CommentQueryBuilder::create(&conn)
384 SearchType::Communities => {
385 communities = blocking(context.pool(), move |conn| {
386 CommunityQueryBuilder::create(conn)
396 SearchType::Users => {
397 users = blocking(context.pool(), move |conn| {
398 UserQueryBuilder::create(conn)
408 posts = blocking(context.pool(), move |conn| {
409 PostQueryBuilder::create(conn)
412 .community_id(community_id)
413 .community_name(community_name)
422 let q = data.q.to_owned();
423 let sort = SortType::from_str(&data.sort)?;
425 comments = blocking(context.pool(), move |conn| {
426 CommentQueryBuilder::create(conn)
436 let q = data.q.to_owned();
437 let sort = SortType::from_str(&data.sort)?;
439 communities = blocking(context.pool(), move |conn| {
440 CommunityQueryBuilder::create(conn)
450 let q = data.q.to_owned();
451 let sort = SortType::from_str(&data.sort)?;
453 users = blocking(context.pool(), move |conn| {
454 UserQueryBuilder::create(conn)
464 posts = blocking(context.pool(), move |conn| {
465 PostQueryBuilder::create(conn)
469 .community_id(community_id)
470 .community_name(community_name)
482 type_: data.type_.to_owned(),
491 #[async_trait::async_trait(?Send)]
492 impl Perform for TransferSite {
493 type Response = GetSiteResponse;
497 context: &Data<LemmyContext>,
498 _websocket_id: Option<ConnectionId>,
499 ) -> Result<GetSiteResponse, LemmyError> {
500 let data: &TransferSite = &self;
501 let user = get_user_safe_settings_from_jwt(&data.auth, context.pool()).await?;
503 is_admin(context.pool(), user.id).await?;
505 let read_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
507 // Make sure user is the creator
508 if read_site.creator_id != user.id {
509 return Err(ApiError::err("not_an_admin").into());
512 let new_creator_id = data.user_id;
513 let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
514 if blocking(context.pool(), transfer_site).await?.is_err() {
515 return Err(ApiError::err("couldnt_update_site").into());
519 let form = ModAddForm {
520 mod_user_id: user.id,
521 other_user_id: data.user_id,
522 removed: Some(false),
525 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
527 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
529 let mut admins = blocking(context.pool(), move |conn| UserViewSafe::admins(conn)).await??;
530 let creator_index = admins
532 .position(|r| r.user.id == site_view.creator.id)
533 .context(location_info!())?;
534 let creator_user = admins.remove(creator_index);
535 admins.insert(0, creator_user);
537 let banned = blocking(context.pool(), move |conn| UserViewSafe::banned(conn)).await??;
538 let federated_instances = build_federated_instances(context.pool()).await?;
541 site_view: Some(site_view),
545 version: version::VERSION.to_string(),
552 #[async_trait::async_trait(?Send)]
553 impl Perform for GetSiteConfig {
554 type Response = GetSiteConfigResponse;
558 context: &Data<LemmyContext>,
559 _websocket_id: Option<ConnectionId>,
560 ) -> Result<GetSiteConfigResponse, LemmyError> {
561 let data: &GetSiteConfig = &self;
562 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
564 // Only let admins read this
565 is_admin(context.pool(), user.id).await?;
567 let config_hjson = Settings::read_config_file()?;
569 Ok(GetSiteConfigResponse { config_hjson })
573 #[async_trait::async_trait(?Send)]
574 impl Perform for SaveSiteConfig {
575 type Response = GetSiteConfigResponse;
579 context: &Data<LemmyContext>,
580 _websocket_id: Option<ConnectionId>,
581 ) -> Result<GetSiteConfigResponse, LemmyError> {
582 let data: &SaveSiteConfig = &self;
583 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
585 // Only let admins read this
586 let user_id = user.id;
587 is_admin(context.pool(), user_id).await?;
589 // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
590 let config_hjson = match Settings::save_config_file(&data.config_hjson) {
591 Ok(config_hjson) => config_hjson,
592 Err(_e) => return Err(ApiError::err("couldnt_update_site").into()),
595 Ok(GetSiteConfigResponse { config_hjson })