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, ConnectionId, LemmyError};
19 use lemmy_websocket::{messages::GetUsersOnline, LemmyContext};
22 #[async_trait::async_trait(?Send)]
23 impl PerformCrud for GetSite {
24 type Response = GetSiteResponse;
26 #[tracing::instrument(skip(context, websocket_id))]
29 context: &Data<LemmyContext>,
30 websocket_id: Option<ConnectionId>,
31 ) -> Result<GetSiteResponse, LemmyError> {
32 let data: &GetSite = self;
34 let site_view = match blocking(context.pool(), SiteView::read_local).await? {
35 Ok(site_view) => Some(site_view),
36 // If the site isn't created yet, check the setup
38 if let Some(setup) = context.settings().setup.as_ref() {
39 let register = Register {
40 username: setup.admin_username.to_owned(),
41 email: setup.admin_email.clone().map(|s| s.into()),
42 password: setup.admin_password.clone().into(),
43 password_verify: setup.admin_password.clone().into(),
50 let admin_jwt = register
51 .perform(context, websocket_id)
54 .expect("jwt is returned from registration on newly created site");
55 info!("Admin {} created", setup.admin_username);
57 let create_site = CreateSite {
58 name: setup.site_name.to_owned(),
59 sidebar: setup.sidebar.to_owned(),
60 description: setup.description.to_owned(),
61 icon: setup.icon.to_owned(),
62 banner: setup.banner.to_owned(),
63 enable_downvotes: setup.enable_downvotes,
64 open_registration: setup.open_registration,
65 enable_nsfw: setup.enable_nsfw,
66 community_creation_admin_only: setup.community_creation_admin_only,
67 require_email_verification: setup.require_email_verification,
68 require_application: setup.require_application,
69 application_question: setup.application_question.to_owned(),
70 private_instance: setup.private_instance,
71 default_theme: setup.default_theme.to_owned(),
74 create_site.perform(context, websocket_id).await?;
75 info!("Site {} created", setup.site_name);
76 Some(blocking(context.pool(), SiteView::read_local).await??)
83 let admins = blocking(context.pool(), PersonViewSafe::admins).await??;
91 // Build the local user
92 let my_user = if let Some(local_user_view) = get_local_user_settings_view_from_jwt_opt(
99 let person_id = local_user_view.person.id;
100 let follows = blocking(context.pool(), move |conn| {
101 CommunityFollowerView::for_person(conn, person_id)
104 .map_err(LemmyError::from)
105 .map_err(|e| e.with_message("system_err_login"))?;
107 let person_id = local_user_view.person.id;
108 let community_blocks = blocking(context.pool(), move |conn| {
109 CommunityBlockView::for_person(conn, person_id)
112 .map_err(LemmyError::from)
113 .map_err(|e| e.with_message("system_err_login"))?;
115 let person_id = local_user_view.person.id;
116 let person_blocks = blocking(context.pool(), move |conn| {
117 PersonBlockView::for_person(conn, person_id)
120 .map_err(LemmyError::from)
121 .map_err(|e| e.with_message("system_err_login"))?;
123 let moderates = blocking(context.pool(), move |conn| {
124 CommunityModeratorView::for_person(conn, person_id)
127 .map_err(LemmyError::from)
128 .map_err(|e| e.with_message("system_err_login"))?;
141 let federated_instances = build_federated_instances(
143 &context.settings().federation,
144 &context.settings().hostname,
152 version: version::VERSION.to_string(),