Files
spacedrive/core/src/api/p2p.rs
Oscar Beaumont 1cef8ebfdb [ENG-994] Minor P2P fixes (#1238)
* mdns advertisement on detection

* fix bug in P2P discovery

* Add `P2PEvent::ExpirePeer`

* React properly

* Consistent tracing versions

* better logger config

* Reduce excessive logging from P2P

* Fix panic log hook

* Remove `dbg`

* Fix bug in thumbnailer remover

* wip: Connected peers in UI

* `Sync` is large pain

* init services after library load

* improve error handling of logger

* Sync protocol shutdown

* Add "updater" feature

* fix commands bindings

* nlm state debug query

* nlm debug status

* Make TS happy

* Add `RemoteIdentity` to libraries debug info

* Improve debug data presentation

* Among us level sus

* minor fix panic hook

* Fix EOF issue

* fix instance not being added on pairing

---------

Co-authored-by: Utku <74243531+utkubakir@users.noreply.github.com>
Co-authored-by: Ericson "Fogo" Soares <ericson.ds999@gmail.com>
2023-08-27 06:41:26 +00:00

94 lines
2.4 KiB
Rust

use rspc::{alpha::AlphaRouter, ErrorCode};
use sd_p2p::PeerId;
use serde::Deserialize;
use specta::Type;
use std::path::PathBuf;
use uuid::Uuid;
use crate::p2p::{P2PEvent, PairingDecision};
use super::{Ctx, R};
pub(crate) fn mount() -> AlphaRouter<Ctx> {
R.router()
.procedure("events", {
R.subscription(|node, _: ()| async move {
let mut rx = node.p2p.subscribe();
async_stream::stream! {
// TODO: Don't block subscription start
for peer in node.p2p.manager.get_discovered_peers().await {
yield P2PEvent::DiscoveredPeer {
peer_id: peer.peer_id,
metadata: peer.metadata,
};
}
// TODO: Don't block subscription start
for peer_id in node.p2p.manager.get_connected_peers().await.unwrap() {
yield P2PEvent::ConnectedPeer {
peer_id,
};
}
while let Ok(event) = rx.recv().await {
yield event;
}
}
})
})
.procedure("nlmState", {
R.query(|node, _: ()| async move { node.nlm.state().await })
})
.procedure("spacedrop", {
#[derive(Type, Deserialize)]
pub struct SpacedropArgs {
peer_id: PeerId,
file_path: Vec<String>,
}
R.mutation(|node, args: SpacedropArgs| async move {
// TODO: Handle multiple files path and error if zero paths
node.p2p
.big_bad_spacedrop(
args.peer_id,
PathBuf::from(
args.file_path
.first()
.expect("https://linear.app/spacedriveapp/issue/ENG-625/spacedrop-multiple-files"),
),
)
.await
.map_err(|_| {
rspc::Error::new(ErrorCode::InternalServerError, "todo".to_string())
})
})
})
.procedure("acceptSpacedrop", {
R.mutation(|node, (id, path): (Uuid, Option<String>)| async move {
match path {
Some(path) => node.p2p.accept_spacedrop(id, path).await,
None => node.p2p.reject_spacedrop(id).await,
}
})
})
// TODO: Send this over `p2p.events`
.procedure("spacedropProgress", {
R.subscription(|node, id: Uuid| async move {
node.p2p.spacedrop_progress(id).await.ok_or_else(|| {
rspc::Error::new(ErrorCode::BadRequest, "Spacedrop not found!".into())
})
})
})
.procedure("pair", {
R.mutation(|node, id: PeerId| async move {
node.p2p.pairing.clone().originator(id, node).await
})
})
.procedure("pairingResponse", {
R.mutation(|node, (pairing_id, decision): (u16, PairingDecision)| {
node.p2p.pairing.decision(pairing_id, decision);
})
})
}