diff options
author | metamuffin <metamuffin@disroot.org> | 2023-08-02 23:07:55 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2023-08-02 23:07:55 +0200 |
commit | 8e33fcdfbd9df042c0cfd8e9a2084993313961c9 (patch) | |
tree | 9b18183237177b6c2b7060140ed92e62581ab588 /server/src/federation.rs | |
parent | c81d8bbfd46d53fba6e0086b5f859f8af8639f4a (diff) | |
download | jellything-8e33fcdfbd9df042c0cfd8e9a2084993313961c9.tar jellything-8e33fcdfbd9df042c0cfd8e9a2084993313961c9.tar.bz2 jellything-8e33fcdfbd9df042c0cfd8e9a2084993313961c9.tar.zst |
federated import works but relies on private data
Diffstat (limited to 'server/src/federation.rs')
-rw-r--r-- | server/src/federation.rs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/server/src/federation.rs b/server/src/federation.rs new file mode 100644 index 0000000..578261b --- /dev/null +++ b/server/src/federation.rs @@ -0,0 +1,55 @@ +use crate::CONF; +use anyhow::anyhow; +use jellyclient::{Instance, Session}; +use std::{collections::HashMap, sync::Arc, time::Duration}; +use tokio::sync::RwLock; + +pub struct Federation { + instances: HashMap<String, Instance>, + sessions: RwLock<HashMap<String, Arc<Session>>>, +} + +impl Federation { + pub fn initialize() -> Self { + let instances = CONF + .remote_credentials + .iter() + .map(|(k, (_, _, tls))| (k.to_owned(), Instance::new(k.to_owned(), *tls))) + .collect::<HashMap<_, _>>(); + + Self { + instances, + sessions: Default::default(), + } + } + + pub fn get_instance(&self, host: &String) -> anyhow::Result<&Instance> { + Ok(self + .instances + .get(host) + .ok_or(anyhow!("unknown instance"))?) + } + pub async fn get_session(&self, host: &String) -> anyhow::Result<Arc<Session>> { + let mut w = self.sessions.write().await; + if let Some(s) = w.get(host) { + Ok(s.to_owned()) + } else { + let (username, password, _) = CONF + .remote_credentials + .get(host) + .ok_or(anyhow!("no credentials of the remote server"))?; + let s = Arc::new( + self.get_instance(host)? + .to_owned() + .login( + username.to_owned(), + password.to_owned(), + Duration::from_secs(60 * 60 * 24 * 356), + ) + .await?, + ); + w.insert(host.to_owned(), s.clone()); + Ok(s) + } + } +} |