1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 site::{CreateSite, GetSite, GetSiteResponse, MyUserInfo},
6 utils::{blocking, build_federated_instances, get_local_user_settings_view_from_jwt_opt},
8 use lemmy_db_views::structs::SiteView;
9 use lemmy_db_views_actor::structs::{
11 CommunityFollowerView,
12 CommunityModeratorView,
16 use lemmy_utils::{error::LemmyError, version, ConnectionId};
17 use lemmy_websocket::{messages::GetUsersOnline, LemmyContext};
20 #[async_trait::async_trait(?Send)]
21 impl PerformCrud for GetSite {
22 type Response = GetSiteResponse;
24 #[tracing::instrument(skip(context, websocket_id))]
27 context: &Data<LemmyContext>,
28 websocket_id: Option<ConnectionId>,
29 ) -> Result<GetSiteResponse, LemmyError> {
30 let data: &GetSite = self;
32 let site_view = match blocking(context.pool(), SiteView::read_local).await? {
33 Ok(site_view) => Some(site_view),
34 // If the site isn't created yet, check the setup
36 if let Some(setup) = context.settings().setup.as_ref() {
37 let register = Register {
38 username: setup.admin_username.to_owned(),
39 email: setup.admin_email.clone().map(|s| s.into()),
40 password: setup.admin_password.clone().into(),
41 password_verify: setup.admin_password.clone().into(),
48 let admin_jwt = register
49 .perform(context, websocket_id)
52 .expect("jwt is returned from registration on newly created site");
53 info!("Admin {} created", setup.admin_username);
55 let create_site = CreateSite {
56 name: setup.site_name.to_owned(),
57 sidebar: setup.sidebar.to_owned(),
58 description: setup.description.to_owned(),
59 icon: setup.icon.to_owned(),
60 banner: setup.banner.to_owned(),
61 enable_downvotes: setup.enable_downvotes,
62 open_registration: setup.open_registration,
63 enable_nsfw: setup.enable_nsfw,
64 community_creation_admin_only: setup.community_creation_admin_only,
65 require_email_verification: setup.require_email_verification,
66 require_application: setup.require_application,
67 application_question: setup.application_question.to_owned(),
68 private_instance: setup.private_instance,
69 default_theme: setup.default_theme.to_owned(),
70 default_post_listing_type: setup.default_post_listing_type.to_owned(),
73 create_site.perform(context, websocket_id).await?;
74 info!("Site {} created", setup.site_name);
75 Some(blocking(context.pool(), SiteView::read_local).await??)
82 let admins = blocking(context.pool(), PersonViewSafe::admins).await??;
90 // Build the local user
91 let my_user = if let Some(local_user_view) = get_local_user_settings_view_from_jwt_opt(
98 let person_id = local_user_view.person.id;
99 let follows = blocking(context.pool(), move |conn| {
100 CommunityFollowerView::for_person(conn, person_id)
103 .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?;
105 let person_id = local_user_view.person.id;
106 let community_blocks = blocking(context.pool(), move |conn| {
107 CommunityBlockView::for_person(conn, person_id)
110 .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?;
112 let person_id = local_user_view.person.id;
113 let person_blocks = blocking(context.pool(), move |conn| {
114 PersonBlockView::for_person(conn, person_id)
117 .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?;
119 let moderates = blocking(context.pool(), move |conn| {
120 CommunityModeratorView::for_person(conn, person_id)
123 .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?;
136 let federated_instances =
137 build_federated_instances(context.pool(), &context.settings()).await?;
143 version: version::VERSION.to_string(),