use std::borrow::Cow;
use std::future::IntoFuture;
use nostr::types::url::RelayUrl;
use crate::client::url::RelayUrlArg;
use crate::client::{Client, Error};
use crate::future::BoxedFuture;
#[must_use = "Does nothing unless you await!"]
pub struct RemoveRelay<'client, 'url> {
client: &'client Client,
url: RelayUrlArg<'url>,
force: bool,
}
impl<'client, 'url> RemoveRelay<'client, 'url> {
pub(crate) fn new(client: &'client Client, url: RelayUrlArg<'url>) -> Self {
Self {
client,
url,
force: false,
}
}
#[inline]
pub fn force(mut self) -> Self {
self.force = true;
self
}
}
impl<'client, 'url> IntoFuture for RemoveRelay<'client, 'url>
where
'url: 'client,
{
type Output = Result<(), Error>;
type IntoFuture = BoxedFuture<'client, Self::Output>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move {
let url: Cow<RelayUrl> = self.url.try_into_relay_url()?;
self.client.pool().remove_relay(url, self.force).await
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::error::ErrorKind;
use crate::relay::RelayCapabilities;
#[tokio::test]
async fn test_remove_nonexistent_relay() {
let client = Client::default();
client.add_relay("ws://127.0.0.1:6666").await.unwrap();
let err = client
.remove_relay("ws://127.0.0.1:7777")
.await
.unwrap_err();
assert_eq!(err.kind(), ErrorKind::NotFound);
assert_eq!(err.to_string(), "relay not found");
}
#[tokio::test]
async fn test_remove_relay() {
let client = Client::default();
client.add_relay("ws://127.0.0.1:6666").await.unwrap();
client
.add_relay("ws://127.0.0.1:8888")
.capabilities(RelayCapabilities::default() | RelayCapabilities::GOSSIP)
.await
.unwrap();
assert_eq!(client.relays().await.len(), 2);
assert_eq!(client.pool().all_relays().await.len(), 2);
assert!(client.remove_relay("ws://127.0.0.1:6666").await.is_ok());
assert!(client.relay("ws://127.0.0.1:6666").await.unwrap().is_none());
assert_eq!(client.relays().await.len(), 1);
assert_eq!(client.pool().all_relays().await.len(), 1);
assert!(client.remove_relay("ws://127.0.0.1:8888").await.is_ok());
assert!(client.relay("ws://127.0.0.1:8888").await.is_ok()); assert!(client.relays().await.is_empty()); assert_eq!(client.pool().all_relays().await.len(), 1);
}
#[tokio::test]
async fn test_force_remove_relay() {
let client = Client::default();
client.add_relay("ws://127.0.0.1:6666").await.unwrap();
client
.add_relay("ws://127.0.0.1:8888")
.capabilities(RelayCapabilities::default() | RelayCapabilities::GOSSIP)
.await
.unwrap();
assert_eq!(client.relays().await.len(), 2);
assert_eq!(client.pool().all_relays().await.len(), 2);
assert!(
client
.remove_relay("ws://127.0.0.1:6666")
.force()
.await
.is_ok()
);
assert!(client.relay("ws://127.0.0.1:6666").await.unwrap().is_none());
assert_eq!(client.relays().await.len(), 1);
assert_eq!(client.pool().all_relays().await.len(), 1);
assert!(
client
.remove_relay("ws://127.0.0.1:8888")
.force()
.await
.is_ok()
);
assert!(client.relay("ws://127.0.0.1:8888").await.unwrap().is_none());
assert!(client.relays().await.is_empty());
assert!(client.pool().all_relays().await.is_empty());
}
}