1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 build_federated_instances,
6 get_local_user_settings_view_from_jwt_opt,
10 use lemmy_db_views::site_view::SiteView;
11 use lemmy_db_views_actor::person_view::PersonViewSafe;
12 use lemmy_utils::{settings::structs::Settings, version, ConnectionId, LemmyError};
13 use lemmy_websocket::{messages::GetUsersOnline, LemmyContext};
16 #[async_trait::async_trait(?Send)]
17 impl PerformCrud for GetSite {
18 type Response = GetSiteResponse;
22 context: &Data<LemmyContext>,
23 websocket_id: Option<ConnectionId>,
24 ) -> Result<GetSiteResponse, LemmyError> {
25 let data: &GetSite = &self;
27 let site_view = match blocking(context.pool(), move |conn| SiteView::read(conn)).await? {
28 Ok(site_view) => Some(site_view),
29 // If the site isn't created yet, check the setup
31 if let Some(setup) = Settings::get().setup().as_ref() {
32 let register = Register {
33 username: setup.admin_username.to_owned(),
34 email: setup.admin_email.to_owned(),
35 password: setup.admin_password.to_owned(),
36 password_verify: setup.admin_password.to_owned(),
41 let login_response = register.perform(context, websocket_id).await?;
42 info!("Admin {} created", setup.admin_username);
44 let create_site = CreateSite {
45 name: setup.site_name.to_owned(),
50 enable_downvotes: true,
51 open_registration: true,
53 auth: login_response.jwt,
55 create_site.perform(context, websocket_id).await?;
56 info!("Site {} created", setup.site_name);
57 Some(blocking(context.pool(), move |conn| SiteView::read(conn)).await??)
64 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
66 // Make sure the site creator is the top admin
67 if let Some(site_view) = site_view.to_owned() {
68 let site_creator_id = site_view.creator.id;
69 // TODO investigate why this is sometimes coming back null
70 // Maybe user_.admin isn't being set to true?
71 if let Some(creator_index) = admins.iter().position(|r| r.person.id == site_creator_id) {
72 let creator_person = admins.remove(creator_index);
73 admins.insert(0, creator_person);
77 let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
85 let my_user = get_local_user_settings_view_from_jwt_opt(&data.auth, context.pool()).await?;
86 let federated_instances = build_federated_instances(context.pool()).await?;
93 version: version::VERSION.to_string(),