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::{
12 community_block_view::CommunityBlockView,
13 community_follower_view::CommunityFollowerView,
14 community_moderator_view::CommunityModeratorView,
15 person_block_view::PersonBlockView,
16 person_view::PersonViewSafe,
18 use lemmy_utils::{settings::structs::Settings, version, ApiError, ConnectionId, LemmyError};
19 use lemmy_websocket::{messages::GetUsersOnline, LemmyContext};
22 #[async_trait::async_trait(?Send)]
23 impl PerformCrud for GetSite {
24 type Response = GetSiteResponse;
28 context: &Data<LemmyContext>,
29 websocket_id: Option<ConnectionId>,
30 ) -> Result<GetSiteResponse, LemmyError> {
31 let data: &GetSite = self;
33 let site_view = match blocking(context.pool(), move |conn| SiteView::read(conn)).await? {
34 Ok(site_view) => Some(site_view),
35 // If the site isn't created yet, check the setup
37 if let Some(setup) = Settings::get().setup.as_ref() {
38 let register = Register {
39 username: setup.admin_username.to_owned(),
40 email: setup.admin_email.to_owned(),
41 password: setup.admin_password.to_owned(),
42 password_verify: setup.admin_password.to_owned(),
47 let login_response = register.perform(context, websocket_id).await?;
48 info!("Admin {} created", setup.admin_username);
50 let create_site = CreateSite {
51 name: setup.site_name.to_owned(),
56 enable_downvotes: None,
57 open_registration: None,
59 auth: login_response.jwt,
60 community_creation_admin_only: None,
62 create_site.perform(context, websocket_id).await?;
63 info!("Site {} created", setup.site_name);
64 Some(blocking(context.pool(), move |conn| SiteView::read(conn)).await??)
71 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
73 // Make sure the site creator is the top admin
74 if let Some(site_view) = site_view.to_owned() {
75 let site_creator_id = site_view.creator.id;
76 // TODO investigate why this is sometimes coming back null
77 // Maybe user_.admin isn't being set to true?
78 if let Some(creator_index) = admins.iter().position(|r| r.person.id == site_creator_id) {
79 let creator_person = admins.remove(creator_index);
80 admins.insert(0, creator_person);
84 let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
92 // Build the local user
93 let my_user = if let Some(local_user_view) =
94 get_local_user_settings_view_from_jwt_opt(&data.auth, context.pool()).await?
96 let person_id = local_user_view.person.id;
97 let follows = blocking(context.pool(), move |conn| {
98 CommunityFollowerView::for_person(conn, person_id)
101 .map_err(|_| ApiError::err("system_err_login"))?;
103 let person_id = local_user_view.person.id;
104 let community_blocks = blocking(context.pool(), move |conn| {
105 CommunityBlockView::for_person(conn, person_id)
108 .map_err(|_| ApiError::err("system_err_login"))?;
110 let person_id = local_user_view.person.id;
111 let person_blocks = blocking(context.pool(), move |conn| {
112 PersonBlockView::for_person(conn, person_id)
115 .map_err(|_| ApiError::err("system_err_login"))?;
117 let moderates = blocking(context.pool(), move |conn| {
118 CommunityModeratorView::for_person(conn, person_id)
121 .map_err(|_| ApiError::err("system_err_login"))?;
134 let federated_instances = build_federated_instances(context.pool()).await?;
141 version: version::VERSION.to_string(),