Skip to content

Commit

Permalink
refactor: factor out add_gossip_peer_from_header()
Browse files Browse the repository at this point in the history
Also don't even add the peer to SQL if realtime is disabled.
  • Loading branch information
link2xt committed Oct 25, 2024
1 parent 3f1dfef commit 69b4c0c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 55 deletions.
41 changes: 41 additions & 0 deletions src/peer_channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,47 @@ pub(crate) async fn iroh_add_peer_for_topic(
Ok(())
}

/// Add gossip peer from `Iroh-Node-Addr` header to WebXDC message identified by `instance_id`.
pub async fn add_gossip_peer_from_header(
context: &Context,
instance_id: MsgId,
node_addr: &str,
) -> Result<()> {
if !context
.get_config_bool(Config::WebxdcRealtimeEnabled)
.await?
{
return Ok(());
}

info!(
context,
"Adding iroh peer with address {node_addr:?} to the topic of {instance_id}."
);
let node_addr =
serde_json::from_str::<NodeAddr>(node_addr).context("Failed to parse node address")?;

context.emit_event(EventType::WebxdcRealtimeAdvertisementReceived {
msg_id: instance_id,
});

let Some(topic) = get_iroh_topic_for_msg(context, instance_id).await? else {
warn!(
context,
"Could not add iroh peer because {instance_id} has no topic."
);
return Ok(());
};

let node_id = node_addr.node_id;
let relay_server = node_addr.relay_url().map(|relay| relay.as_str());
iroh_add_peer_for_topic(context, instance_id, topic, node_id, relay_server).await?;

let iroh = context.get_or_try_init_peer_channel().await?;
iroh.maybe_add_gossip_peers(topic, vec![node_addr]).await?;
Ok(())
}

/// Insert topicId into the database so that we can use it to retrieve the topic.
pub(crate) async fn insert_topic_stub(ctx: &Context, msg_id: MsgId, topic: TopicId) -> Result<()> {
ctx.sql
Expand Down
75 changes: 20 additions & 55 deletions src/receive_imf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::message::{
};
use crate::mimeparser::{parse_message_ids, AvatarAction, MimeMessage, SystemMessage};
use crate::param::{Param, Params};
use crate::peer_channels::{get_iroh_topic_for_msg, insert_topic_stub, iroh_add_peer_for_topic};
use crate::peer_channels::{add_gossip_peer_from_header, insert_topic_stub};
use crate::peerstate::Peerstate;
use crate::reaction::{set_msg_reaction, Reaction};
use crate::securejoin::{self, handle_securejoin_handshake, observe_securejoin_on_other_device};
Expand All @@ -41,7 +41,6 @@ use crate::sync::Sync::*;
use crate::tools::{self, buf_compress, remove_subject_prefix};
use crate::{chatlist_events, location};
use crate::{contact, imap};
use iroh_net::NodeAddr;

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

if let Some(node_addr) = mime_parser.get_header(HeaderDef::IrohNodeAddr) {
chat_id = DC_CHAT_ID_TRASH;
match serde_json::from_str::<NodeAddr>(node_addr).context("Failed to parse node address") {
Ok(node_addr) => {
info!(context, "Adding iroh peer with address {node_addr:?}.");
match mime_parser.get_header(HeaderDef::InReplyTo) {
Some(in_reply_to) => match rfc724_mid_exists(context, in_reply_to).await? {
Some((instance_id, _ts_sent)) => {
context.emit_event(EventType::WebxdcRealtimeAdvertisementReceived {
msg_id: instance_id,
});
if let Some(topic) =
get_iroh_topic_for_msg(context, instance_id).await?
{
let node_id = node_addr.node_id;
let relay_server =
node_addr.relay_url().map(|relay| relay.as_str());
iroh_add_peer_for_topic(
context,
instance_id,
topic,
node_id,
relay_server,
)
.await?;
if context
.get_config_bool(Config::WebxdcRealtimeEnabled)
.await?
{
let iroh = context.get_or_try_init_peer_channel().await?;
iroh.maybe_add_gossip_peers(topic, vec![node_addr]).await?;
}
info!(context, "Added iroh peer to the topic of {instance_id}.");
} else {
warn!(
context,
"Could not add iroh peer because {instance_id} has no topic."
);
}
}
None => {
warn!(
context,
"Cannot add iroh peer because WebXDC instance does not exist."
);
}
},
None => {
warn!(
context,
"Cannot add iroh peer because the message has no In-Reply-To."
);
match mime_parser.get_header(HeaderDef::InReplyTo) {
Some(in_reply_to) => match rfc724_mid_exists(context, in_reply_to).await? {
Some((instance_id, _ts_sent)) => {
if let Err(err) =
add_gossip_peer_from_header(context, instance_id, node_addr).await
{
warn!(context, "Failed to add iroh peer from header: {err:#}.");
}
}
}
Err(err) => {
warn!(context, "Couldn't parse NodeAddr: {err:#}.");
None => {
warn!(
context,
"Cannot add iroh peer because WebXDC instance does not exist."
);
}
},
None => {
warn!(
context,
"Cannot add iroh peer because the message has no In-Reply-To."
);
}
}
}
Expand Down

0 comments on commit 69b4c0c

Please sign in to comment.