Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 71 additions & 4 deletions src/features/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,87 @@ impl<'a> Blocking<'a> {
}

/// Block a contact.
///
/// Modern WA exige `jid=LID` + `pn_jid=PN` no stanza; sem `pn_jid` o
/// servidor responde `400 bad-request`. Resolve o par via
/// `get_lid_pn_entry` antes de enviar — aceita LID ou PN como input.
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);
let bare = jid.to_non_ad();
let (lid_jid, pn_jid) = if bare.is_lid() {
let entry = self
.client
.get_lid_pn_entry(&bare)
.await
.map_err(|e| IqError::ServerError {
code: 0,
text: format!("get_lid_pn_entry: {e}"),
})?
.ok_or(IqError::ServerError {
code: 0,
text: format!("no LID↔PN mapping for {bare}"),
})?;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
(bare, Jid::pn(entry.phone_number))
} else if bare.is_pn() {
let entry = self
.client
.get_lid_pn_entry(&bare)
.await
.map_err(|e| IqError::ServerError {
code: 0,
text: format!("get_lid_pn_entry: {e}"),
})?
.ok_or(IqError::ServerError {
code: 0,
text: format!("no LID↔PN mapping for {bare}"),
})?;
(Jid::lid(entry.lid), bare)
} else {
return Err(IqError::ServerError {
code: 0,
text: format!("block: jid {bare} is neither PN nor LID"),
});
};
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
self.client
.execute(UpdateBlocklistSpec::block_with_pn(&lid_jid, &pn_jid))
.await?;
debug!(target: "Blocking", "Successfully blocked contact: lid={lid_jid} pn={pn_jid}");
Ok(())
}

/// Unblock a contact.
///
/// Modern WA exige `jid=LID` no `<item action="unblock"/>`. Resolve para
/// LID via mapping se o input for PN.
pub async fn unblock(&self, jid: &Jid) -> Result<(), IqError> {
debug!(target: "Blocking", "Unblocking contact: {}", jid);
let bare = jid.to_non_ad();
let lid_jid = if bare.is_lid() {
bare
} else if bare.is_pn() {
let entry = self
.client
.get_lid_pn_entry(&bare)
.await
.map_err(|e| IqError::ServerError {
code: 0,
text: format!("get_lid_pn_entry: {e}"),
})?
.ok_or(IqError::ServerError {
code: 0,
text: format!("no LID↔PN mapping for {bare}"),
})?;
Jid::lid(entry.lid)
} else {
return Err(IqError::ServerError {
code: 0,
text: format!("unblock: jid {bare} is neither PN nor LID"),
});
};
self.client
.execute(UpdateBlocklistSpec::unblock(jid))
.execute(UpdateBlocklistSpec::unblock(&lid_jid))
.await?;
debug!(target: "Blocking", "Successfully unblocked contact: {}", jid);
debug!(target: "Blocking", "Successfully unblocked contact: lid={lid_jid}");
Ok(())
}

Expand Down
57 changes: 56 additions & 1 deletion wacore/src/iq/blocklist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,28 @@ pub enum BlocklistAction {
}
/// Request node for updating blocklist.
///
/// Wire format: `<item action="block|unblock" jid="...@s.whatsapp.net"/>`
/// Modern WA exige `jid` em formato **LID** e, ao bloquear, `pn_jid`
/// adicional com o PN do contato. Sem `pn_jid` o servidor responde
/// `code=400 bad-request` (verificado contra Baileys e WA Web 2026-04).
///
/// Wire format: `<item action="block" jid="...@lid" pn_jid="...@s.whatsapp.net"/>`
#[derive(Debug, Clone, crate::ProtocolNode)]
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
#[protocol(tag = "item")]
pub struct BlocklistItemRequest {
#[attr(name = "jid", jid)]
pub jid: Jid,
#[attr(name = "action", string_enum)]
pub action: BlocklistAction,
#[attr(name = "pn_jid", jid, optional)]
pub pn_jid: Option<Jid>,
}

impl BlocklistItemRequest {
pub fn new(jid: &Jid, action: BlocklistAction) -> Self {
Self {
jid: jid.clone(),
action,
pn_jid: None,
}
}

Expand All @@ -50,6 +57,15 @@ impl BlocklistItemRequest {
pub fn unblock(jid: &Jid) -> Self {
Self::new(jid, BlocklistAction::Unblock)
}

/// Block usando `lid` e `pn_jid` explícitos (formato moderno do WA).
pub fn block_with_pn(lid: &Jid, pn_jid: &Jid) -> Self {
Self {
jid: lid.clone(),
action: BlocklistAction::Block,
pn_jid: Some(pn_jid.clone()),
}
}
}
/// A single blocklist entry from the response.
///
Expand Down Expand Up @@ -156,6 +172,13 @@ impl UpdateBlocklistSpec {
request: BlocklistItemRequest::unblock(jid),
}
}

/// Block com `lid` + `pn_jid` (formato moderno exigido por WA 2026+).
pub fn block_with_pn(lid: &Jid, pn_jid: &Jid) -> Self {
Self {
request: BlocklistItemRequest::block_with_pn(lid, pn_jid),
}
}
}

impl IqSpec for UpdateBlocklistSpec {
Expand Down Expand Up @@ -283,4 +306,36 @@ mod tests {
let unblock_spec = UpdateBlocklistSpec::unblock(&jid);
assert_eq!(unblock_spec.request.action, BlocklistAction::Unblock);
}

#[test]
fn test_block_with_pn_emits_pn_jid_attr() {
let lid: Jid = "12345678901234@lid".parse().unwrap();
let pn: Jid = "5511999999999@s.whatsapp.net".parse().unwrap();
let request = BlocklistItemRequest::block_with_pn(&lid, &pn);
let node = request.into_node();

assert_eq!(node.tag, "item");
assert!(node.attrs.get("action").is_some_and(|v| v == "block"));
assert!(
node.attrs
.get("jid")
.is_some_and(|v| v == "12345678901234@lid")
);
assert!(
node.attrs
.get("pn_jid")
.is_some_and(|v| v == "5511999999999@s.whatsapp.net")
);
}

#[test]
fn test_unblock_omits_pn_jid_attr() {
let lid: Jid = "12345678901234@lid".parse().unwrap();
let request = BlocklistItemRequest::unblock(&lid);
let node = request.into_node();

assert_eq!(node.tag, "item");
assert!(node.attrs.get("action").is_some_and(|v| v == "unblock"));
assert!(node.attrs.get("pn_jid").is_none());
}
}
Loading