Skip to content

Commit 69b4c0c

Browse files
committed
refactor: factor out add_gossip_peer_from_header()
Also don't even add the peer to SQL if realtime is disabled.
1 parent 3f1dfef commit 69b4c0c

File tree

2 files changed

+61
-55
lines changed

2 files changed

+61
-55
lines changed

src/peer_channels.rs

+41
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,47 @@ pub(crate) async fn iroh_add_peer_for_topic(
314314
Ok(())
315315
}
316316

317+
/// Add gossip peer from `Iroh-Node-Addr` header to WebXDC message identified by `instance_id`.
318+
pub async fn add_gossip_peer_from_header(
319+
context: &Context,
320+
instance_id: MsgId,
321+
node_addr: &str,
322+
) -> Result<()> {
323+
if !context
324+
.get_config_bool(Config::WebxdcRealtimeEnabled)
325+
.await?
326+
{
327+
return Ok(());
328+
}
329+
330+
info!(
331+
context,
332+
"Adding iroh peer with address {node_addr:?} to the topic of {instance_id}."
333+
);
334+
let node_addr =
335+
serde_json::from_str::<NodeAddr>(node_addr).context("Failed to parse node address")?;
336+
337+
context.emit_event(EventType::WebxdcRealtimeAdvertisementReceived {
338+
msg_id: instance_id,
339+
});
340+
341+
let Some(topic) = get_iroh_topic_for_msg(context, instance_id).await? else {
342+
warn!(
343+
context,
344+
"Could not add iroh peer because {instance_id} has no topic."
345+
);
346+
return Ok(());
347+
};
348+
349+
let node_id = node_addr.node_id;
350+
let relay_server = node_addr.relay_url().map(|relay| relay.as_str());
351+
iroh_add_peer_for_topic(context, instance_id, topic, node_id, relay_server).await?;
352+
353+
let iroh = context.get_or_try_init_peer_channel().await?;
354+
iroh.maybe_add_gossip_peers(topic, vec![node_addr]).await?;
355+
Ok(())
356+
}
357+
317358
/// Insert topicId into the database so that we can use it to retrieve the topic.
318359
pub(crate) async fn insert_topic_stub(ctx: &Context, msg_id: MsgId, topic: TopicId) -> Result<()> {
319360
ctx.sql

src/receive_imf.rs

+20-55
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::message::{
3030
};
3131
use crate::mimeparser::{parse_message_ids, AvatarAction, MimeMessage, SystemMessage};
3232
use crate::param::{Param, Params};
33-
use crate::peer_channels::{get_iroh_topic_for_msg, insert_topic_stub, iroh_add_peer_for_topic};
33+
use crate::peer_channels::{add_gossip_peer_from_header, insert_topic_stub};
3434
use crate::peerstate::Peerstate;
3535
use crate::reaction::{set_msg_reaction, Reaction};
3636
use crate::securejoin::{self, handle_securejoin_handshake, observe_securejoin_on_other_device};
@@ -41,7 +41,6 @@ use crate::sync::Sync::*;
4141
use crate::tools::{self, buf_compress, remove_subject_prefix};
4242
use crate::{chatlist_events, location};
4343
use crate::{contact, imap};
44-
use iroh_net::NodeAddr;
4544

4645
/// This is the struct that is returned after receiving one email (aka MIME message).
4746
///
@@ -1446,61 +1445,27 @@ async fn add_parts(
14461445

14471446
if let Some(node_addr) = mime_parser.get_header(HeaderDef::IrohNodeAddr) {
14481447
chat_id = DC_CHAT_ID_TRASH;
1449-
match serde_json::from_str::<NodeAddr>(node_addr).context("Failed to parse node address") {
1450-
Ok(node_addr) => {
1451-
info!(context, "Adding iroh peer with address {node_addr:?}.");
1452-
match mime_parser.get_header(HeaderDef::InReplyTo) {
1453-
Some(in_reply_to) => match rfc724_mid_exists(context, in_reply_to).await? {
1454-
Some((instance_id, _ts_sent)) => {
1455-
context.emit_event(EventType::WebxdcRealtimeAdvertisementReceived {
1456-
msg_id: instance_id,
1457-
});
1458-
if let Some(topic) =
1459-
get_iroh_topic_for_msg(context, instance_id).await?
1460-
{
1461-
let node_id = node_addr.node_id;
1462-
let relay_server =
1463-
node_addr.relay_url().map(|relay| relay.as_str());
1464-
iroh_add_peer_for_topic(
1465-
context,
1466-
instance_id,
1467-
topic,
1468-
node_id,
1469-
relay_server,
1470-
)
1471-
.await?;
1472-
if context
1473-
.get_config_bool(Config::WebxdcRealtimeEnabled)
1474-
.await?
1475-
{
1476-
let iroh = context.get_or_try_init_peer_channel().await?;
1477-
iroh.maybe_add_gossip_peers(topic, vec![node_addr]).await?;
1478-
}
1479-
info!(context, "Added iroh peer to the topic of {instance_id}.");
1480-
} else {
1481-
warn!(
1482-
context,
1483-
"Could not add iroh peer because {instance_id} has no topic."
1484-
);
1485-
}
1486-
}
1487-
None => {
1488-
warn!(
1489-
context,
1490-
"Cannot add iroh peer because WebXDC instance does not exist."
1491-
);
1492-
}
1493-
},
1494-
None => {
1495-
warn!(
1496-
context,
1497-
"Cannot add iroh peer because the message has no In-Reply-To."
1498-
);
1448+
match mime_parser.get_header(HeaderDef::InReplyTo) {
1449+
Some(in_reply_to) => match rfc724_mid_exists(context, in_reply_to).await? {
1450+
Some((instance_id, _ts_sent)) => {
1451+
if let Err(err) =
1452+
add_gossip_peer_from_header(context, instance_id, node_addr).await
1453+
{
1454+
warn!(context, "Failed to add iroh peer from header: {err:#}.");
14991455
}
15001456
}
1501-
}
1502-
Err(err) => {
1503-
warn!(context, "Couldn't parse NodeAddr: {err:#}.");
1457+
None => {
1458+
warn!(
1459+
context,
1460+
"Cannot add iroh peer because WebXDC instance does not exist."
1461+
);
1462+
}
1463+
},
1464+
None => {
1465+
warn!(
1466+
context,
1467+
"Cannot add iroh peer because the message has no In-Reply-To."
1468+
);
15041469
}
15051470
}
15061471
}

0 commit comments

Comments
 (0)