diff options
author | metamuffin <metamuffin@disroot.org> | 2023-12-21 23:57:42 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2023-12-21 23:57:42 +0100 |
commit | 3a29113e965a94bdef06655f1583cc6e86edd606 (patch) | |
tree | a0910fa9687a9935ba1ca85a9cb5def1a0bc9069 /base/src | |
parent | a8b2480e898e269e7e0d41dbd46d9a18c7d1e4ba (diff) | |
download | jellything-3a29113e965a94bdef06655f1583cc6e86edd606.tar jellything-3a29113e965a94bdef06655f1583cc6e86edd606.tar.bz2 jellything-3a29113e965a94bdef06655f1583cc6e86edd606.tar.zst |
rework import system pt. 1
Diffstat (limited to 'base/src')
-rw-r--r-- | base/src/database.rs | 3 | ||||
-rw-r--r-- | base/src/federation.rs | 61 | ||||
-rw-r--r-- | base/src/lib.rs | 2 |
3 files changed, 66 insertions, 0 deletions
diff --git a/base/src/database.rs b/base/src/database.rs index f46f0fb..3f81cca 100644 --- a/base/src/database.rs +++ b/base/src/database.rs @@ -22,6 +22,8 @@ pub struct Database { pub user_node: Tree<(String, String), NodeUserData>, pub invite: Tree<String, ()>, pub node: Tree<String, Node>, + + pub node_import: Tree<String, Vec<(Vec<usize>, Node)>>, } impl Database { @@ -34,6 +36,7 @@ impl Database { invite: Tree::open(&db, "invite"), node: Tree::open(&db, "node"), user_node: Tree::open(&db, "user_node"), + node_import: Tree::open(&db, "node_import"), db, }); info!("ready"); diff --git a/base/src/federation.rs b/base/src/federation.rs new file mode 100644 index 0000000..509b87c --- /dev/null +++ b/base/src/federation.rs @@ -0,0 +1,61 @@ +/* + This file is part of jellything (https://codeberg.org/metamuffin/jellything) + which is licensed under the GNU Affero General Public License (version 3); see /COPYING. + Copyright (C) 2023 metamuffin <metamuffin.org> +*/ +use anyhow::anyhow; +use crate::CONF; +use jellyclient::{Instance, LoginDetails, Session}; +use std::{collections::HashMap, sync::Arc}; +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(LoginDetails { + username: username.to_owned(), + password: password.to_owned(), + expire: None, + drop_permissions: None, + }) + .await?, + ); + w.insert(host.to_owned(), s.clone()); + Ok(s) + } + } +} diff --git a/base/src/lib.rs b/base/src/lib.rs index cfc5a11..a7b15c5 100644 --- a/base/src/lib.rs +++ b/base/src/lib.rs @@ -6,6 +6,7 @@ #![feature(lazy_cell)] pub mod cache; pub mod database; +pub mod federation; pub mod permission; pub mod temp; @@ -34,6 +35,7 @@ impl AssetLocationExt for AssetLocation { AssetLocation::Cache(p) => CONF.cache_path.join(p), AssetLocation::Library(p) => CONF.library_path.join(p), AssetLocation::Temp(p) => CONF.temp_path.join(p), + AssetLocation::Media(p) => CONF.media_path.join(p), } } } |