relay/src/data/state.rs

108 lines
2.8 KiB
Rust
Raw Normal View History

use crate::{
config::{Config, UrlKind},
2020-03-23 22:17:53 +00:00
data::NodeCache,
db::Db,
error::MyError,
requests::{Breakers, Requests},
};
2020-09-07 21:51:02 +00:00
use activitystreams::url::Url;
use actix_web::web;
use async_rwlock::RwLock;
2021-02-10 04:05:06 +00:00
use log::info;
2020-03-15 16:29:01 +00:00
use lru::LruCache;
2020-03-16 03:36:46 +00:00
use rand::thread_rng;
use rsa::{RSAPrivateKey, RSAPublicKey};
2021-02-10 04:05:06 +00:00
use std::sync::Arc;
2020-03-15 02:05:40 +00:00
#[derive(Clone)]
pub struct State {
2021-02-10 04:05:06 +00:00
pub(crate) public_key: RSAPublicKey,
private_key: RSAPrivateKey,
config: Config,
2021-02-10 04:05:06 +00:00
object_cache: Arc<RwLock<LruCache<Url, Url>>>,
node_cache: NodeCache,
breakers: Breakers,
2021-02-10 04:05:06 +00:00
pub(crate) db: Db,
2020-03-15 02:05:40 +00:00
}
2020-03-16 03:36:46 +00:00
impl State {
2021-02-10 04:05:06 +00:00
pub(crate) fn node_cache(&self) -> NodeCache {
self.node_cache.clone()
}
2021-02-10 04:05:06 +00:00
pub(crate) fn requests(&self) -> Requests {
2020-03-18 04:35:20 +00:00
Requests::new(
self.config.generate_url(UrlKind::MainKey).to_string(),
self.private_key.clone(),
format!(
"Actix Web 3.0.0-alpha.1 ({}/{}; +{})",
self.config.software_name(),
self.config.software_version(),
self.config.generate_url(UrlKind::Index),
),
self.breakers.clone(),
2020-03-18 04:35:20 +00:00
)
}
2021-02-10 04:05:06 +00:00
pub(crate) async fn inboxes_without(
&self,
existing_inbox: &Url,
domain: &str,
) -> Result<Vec<Url>, MyError> {
Ok(self
.db
.inboxes()
.await?
2020-03-15 22:37:53 +00:00
.iter()
2021-02-10 04:05:06 +00:00
.filter_map(|inbox| {
if let Some(dom) = inbox.domain() {
if inbox != existing_inbox && dom != domain {
return Some(inbox.clone());
2020-03-15 22:37:53 +00:00
}
}
None
})
2021-02-10 04:05:06 +00:00
.collect())
2020-03-15 22:37:53 +00:00
}
2021-02-10 04:05:06 +00:00
pub(crate) async fn is_cached(&self, object_id: &Url) -> bool {
self.object_cache.read().await.contains(object_id)
2020-03-15 17:49:27 +00:00
}
2021-02-10 04:05:06 +00:00
pub(crate) async fn cache(&self, object_id: Url, actor_id: Url) {
self.object_cache.write().await.put(object_id, actor_id);
2020-03-15 17:49:27 +00:00
}
2021-02-10 04:05:06 +00:00
pub(crate) async fn build(config: Config, db: Db) -> Result<Self, MyError> {
let private_key = if let Ok(Some(key)) = db.private_key().await {
key
} else {
info!("Generating new keys");
let key = web::block(move || {
let mut rng = thread_rng();
RSAPrivateKey::new(&mut rng, 4096)
})
.await?;
2021-02-10 04:05:06 +00:00
db.update_private_key(&key).await?;
2021-02-10 04:05:06 +00:00
key
};
let public_key = private_key.to_public_key();
2020-03-15 02:05:40 +00:00
2020-03-23 17:38:39 +00:00
let state = State {
public_key,
private_key,
config,
2021-02-10 04:05:06 +00:00
object_cache: Arc::new(RwLock::new(LruCache::new(1024 * 8))),
node_cache: NodeCache::new(db.clone()),
breakers: Breakers::default(),
2021-02-10 04:05:06 +00:00
db,
2020-03-23 17:38:39 +00:00
};
Ok(state)
}
2020-03-15 02:05:40 +00:00
}