Skip to content

Commit

Permalink
add endpoint ID for Participant
Browse files Browse the repository at this point in the history
  • Loading branch information
jbg committed Sep 14, 2022
1 parent ff1c2f5 commit 640ed75
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/conference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::{
source::MediaType,
stanza_filter::StanzaFilter,
util::generate_id,
xmpp::{self, connection::Connection},
xmpp::{self, connection::Connection, jid::JidEndpointIdExt},
};

const SEND_STATS_INTERVAL: Duration = Duration::from_secs(10);
Expand Down Expand Up @@ -107,6 +107,12 @@ pub struct Participant {
pub nick: Option<String>,
}

impl Participant {
pub fn endpoint_id(&self) -> Result<&str> {
self.jid.as_ref().context("no JID")?.endpoint_id()
}
}

pub(crate) struct ConferenceInner {
participants: HashMap<String, Participant>,
presence: Vec<xmpp_parsers::Element>,
Expand Down Expand Up @@ -246,14 +252,7 @@ impl Conference {
}

pub fn endpoint_id(&self) -> Result<&str> {
self
.jid
.node
.as_ref()
.ok_or_else(|| anyhow!("invalid jid"))?
.split('-')
.next()
.ok_or_else(|| anyhow!("invalid jid"))
self.jid.endpoint_id()
}

pub fn jid_in_muc(&self) -> Result<FullJid> {
Expand Down
32 changes: 32 additions & 0 deletions src/xmpp/jid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use anyhow::{Context, Result};
use xmpp_parsers::{BareJid, FullJid, Jid};

pub(crate) trait JidEndpointIdExt {
fn endpoint_id(&self) -> Result<&str>;
}

impl JidEndpointIdExt for Jid {
fn endpoint_id(&self) -> Result<&str> {
match self {
Jid::Bare(bare_jid) => bare_jid.endpoint_id(),
Jid::Full(full_jid) => full_jid.endpoint_id(),
}
}
}

impl JidEndpointIdExt for FullJid {
fn endpoint_id(&self) -> Result<&str> {
endpoint_id_from_node(self.node.as_ref().context("invalid JID: no node")?)
}
}

impl JidEndpointIdExt for BareJid {
fn endpoint_id(&self) -> Result<&str> {
endpoint_id_from_node(self.node.as_ref().context("invalid JID: no node")?)
}
}

fn endpoint_id_from_node(node: &str) -> Result<&str> {
let (before, _after) = node.split_once('-').context("invalid JID: invalid node format")?;
Ok(before)
}
1 change: 1 addition & 0 deletions src/xmpp/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod connection;
pub(crate) mod extdisco;
pub(crate) mod jid;
pub(crate) mod jitsi;
mod ns;

0 comments on commit 640ed75

Please sign in to comment.