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::{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(), SiteView::read).await? {
34 Ok(site_view) => Some(site_view),
35 // If the site isn't created yet, check the setup
37 if let Some(setup) = context.settings().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(),
48 let login_response = register.perform(context, websocket_id).await?;
49 info!("Admin {} created", setup.admin_username);
51 let create_site = CreateSite {
52 name: setup.site_name.to_owned(),
53 sidebar: setup.sidebar.to_owned(),
54 description: setup.description.to_owned(),
55 icon: setup.icon.to_owned(),
56 banner: setup.banner.to_owned(),
57 enable_downvotes: setup.enable_downvotes,
58 open_registration: setup.open_registration,
59 enable_nsfw: setup.enable_nsfw,
60 community_creation_admin_only: setup.community_creation_admin_only,
61 auth: login_response.jwt,
63 create_site.perform(context, websocket_id).await?;
64 info!("Site {} created", setup.site_name);
65 Some(blocking(context.pool(), SiteView::read).await??)
72 let mut admins = blocking(context.pool(), PersonViewSafe::admins).await??;
74 // Make sure the site creator is the top admin
75 if let Some(site_view) = site_view.to_owned() {
76 let site_creator_id = site_view.creator.id;
77 // TODO investigate why this is sometimes coming back null
78 // Maybe user_.admin isn't being set to true?
79 if let Some(creator_index) = admins.iter().position(|r| r.person.id == site_creator_id) {
80 let creator_person = admins.remove(creator_index);
81 admins.insert(0, creator_person);
85 let banned = blocking(context.pool(), PersonViewSafe::banned).await??;
93 // Build the local user
94 let my_user = if let Some(local_user_view) =
95 get_local_user_settings_view_from_jwt_opt(&data.auth, context.pool(), context.secret())
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| ApiError::err("system_err_login", e))?;
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| ApiError::err("system_err_login", e))?;
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| ApiError::err("system_err_login", e))?;
119 let moderates = blocking(context.pool(), move |conn| {
120 CommunityModeratorView::for_person(conn, person_id)
123 .map_err(|e| ApiError::err("system_err_login", e))?;
136 let federated_instances = build_federated_instances(
138 &context.settings().federation,
139 &context.settings().hostname,
148 version: version::VERSION.to_string(),