use crate::client::Client;
use crate::request::IqError;
use log::debug;
pub use wacore::iq::blocklist::BlocklistEntry;
use wacore::iq::blocklist::{GetBlocklistSpec, UpdateBlocklistSpec};
use wacore_binary::jid::Jid;
pub struct Blocking<'a> {
client: &'a Client,
}
impl<'a> Blocking<'a> {
pub(crate) fn new(client: &'a Client) -> Self {
Self { client }
}
pub async fn block(&self, jid: &Jid) -> Result<(), IqError> {
debug!(target: "Blocking", "Blocking contact: {}", jid);
self.client.execute(UpdateBlocklistSpec::block(jid)).await?;
debug!(target: "Blocking", "Successfully blocked contact: {}", jid);
Ok(())
}
pub async fn unblock(&self, jid: &Jid) -> Result<(), IqError> {
debug!(target: "Blocking", "Unblocking contact: {}", jid);
self.client
.execute(UpdateBlocklistSpec::unblock(jid))
.await?;
debug!(target: "Blocking", "Successfully unblocked contact: {}", jid);
Ok(())
}
pub async fn get_blocklist(&self) -> anyhow::Result<Vec<BlocklistEntry>> {
debug!(target: "Blocking", "Fetching blocklist...");
let entries = self.client.execute(GetBlocklistSpec).await?;
debug!(target: "Blocking", "Fetched {} blocked contacts", entries.len());
Ok(entries)
}
pub async fn is_blocked(&self, jid: &Jid) -> anyhow::Result<bool> {
let blocklist = self.get_blocklist().await?;
Ok(blocklist.iter().any(|e| e.jid.user == jid.user))
}
}
impl Client {
pub fn blocking(&self) -> Blocking<'_> {
Blocking::new(self)
}
}