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(),
49 enable_downvotes: true,
50 open_registration: true,
52 auth: login_response.jwt,
54 create_site.perform(context, websocket_id).await?;
55 info!("Site {} created", setup.site_name);
56 Some(blocking(context.pool(), move |conn| SiteView::read(conn)).await??)
63 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
65 // Make sure the site creator is the top admin
66 if let Some(site_view) = site_view.to_owned() {
67 let site_creator_id = site_view.creator.id;
68 // TODO investigate why this is sometimes coming back null
69 // Maybe user_.admin isn't being set to true?
70 if let Some(creator_index) = admins.iter().position(|r| r.person.id == site_creator_id) {
71 let creator_person = admins.remove(creator_index);
72 admins.insert(0, creator_person);
76 let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
84 let my_user = get_local_user_settings_view_from_jwt_opt(&data.auth, context.pool()).await?;
85 let federated_instances = build_federated_instances(context.pool()).await?;
92 version: version::VERSION.to_string(),