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(), 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) = 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(),
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(),
52 sidebar: setup.sidebar.to_owned(),
53 description: setup.description.to_owned(),
54 icon: setup.icon.to_owned(),
55 banner: setup.banner.to_owned(),
56 enable_downvotes: setup.enable_downvotes,
57 open_registration: setup.open_registration,
58 enable_nsfw: setup.enable_nsfw,
59 community_creation_admin_only: setup.community_creation_admin_only,
60 auth: login_response.jwt,
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(), context.secret())
97 let person_id = local_user_view.person.id;
98 let follows = blocking(context.pool(), move |conn| {
99 CommunityFollowerView::for_person(conn, person_id)
102 .map_err(|_| ApiError::err("system_err_login"))?;
104 let person_id = local_user_view.person.id;
105 let community_blocks = blocking(context.pool(), move |conn| {
106 CommunityBlockView::for_person(conn, person_id)
109 .map_err(|_| ApiError::err("system_err_login"))?;
111 let person_id = local_user_view.person.id;
112 let person_blocks = blocking(context.pool(), move |conn| {
113 PersonBlockView::for_person(conn, person_id)
116 .map_err(|_| ApiError::err("system_err_login"))?;
118 let moderates = blocking(context.pool(), move |conn| {
119 CommunityModeratorView::for_person(conn, person_id)
122 .map_err(|_| ApiError::err("system_err_login"))?;
135 let federated_instances = build_federated_instances(
137 &context.settings().federation,
138 &context.settings().hostname,
147 version: version::VERSION.to_string(),