summaryrefslogtreecommitdiffstats
path: root/src/state.rs
blob: d7c91362b78dfb35d769b8881b23bab4ce6306a8 (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
use std::{ops::Deref, sync::Arc};

use activitypub_federation::config::FederationConfig;
use tokio::sync::RwLock;

use crate::{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 users: RwLock<Vec<User>>,
}

impl AppState {
    pub async fn new() -> Result<FederationConfig<AppHandle>, AppError> {
        let user = User::new("sellershut")?;
        let domain = "localhost";

        let config = FederationConfig::builder()
            .domain(domain)
            .signed_fetch_actor(&user)
            .app_data(AppHandle(Arc::new(Self {
                users: RwLock::new(vec![user]),
            })))
            // .url_verifier(Box::new(MyUrlVerifier()))
            // TODO: could change this to env variable?
            .debug(cfg!(debug_assertions))
            .build()
            .await?;

        Ok(config)
    }
}