summaryrefslogtreecommitdiffstats
path: root/src/state.rs
blob: 1ae6caa22764220469062fb2e4f61ebbbac17d49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use std::{ops::Deref, sync::Arc};

use activitypub_federation::config::FederationConfig;
use stack_up::{Configuration, Environment, Services};

use crate::{cnfg::LocalConfig, entity::user::User, error::AppError};

#[derive(Clone)]
pub struct AppHandle(Arc<AppState>);

impl Deref for AppHandle {
    type Target = Arc<AppState>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

pub struct AppState {
    pub services: Services,
}

impl AppState {
    pub async fn create(
        services: Services,
        configuration: &Configuration,
    ) -> Result<FederationConfig<AppHandle>, AppError> {
        let warden_config: LocalConfig = serde_json::from_value(configuration.misc.clone())?;

        let user = User::new(
            &warden_config.instance_name,
            &warden_config.hostname,
            &services,
            configuration.application.env,
        )
        .await?;

        let config = FederationConfig::builder()
            .domain(&warden_config.hostname)
            .signed_fetch_actor(&user)
            .app_data(AppHandle(Arc::new(Self { services })))
            // .url_verifier(Box::new(MyUrlVerifier()))
            // TODO: could change this to env variable?
            .debug(configuration.application.env == Environment::Development)
            .build()
            .await?;

        Ok(config)
    }
}