mirror of
https://github.com/chatmail/core.git
synced 2026-04-23 08:26:30 +03:00
refactor: factor out add_gossip_peer_from_header()
Also don't even add the peer to SQL if realtime is disabled.
This commit is contained in:
@@ -314,6 +314,47 @@ pub(crate) async fn iroh_add_peer_for_topic(
|
|||||||
Ok(())
|
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.
|
/// 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<()> {
|
pub(crate) async fn insert_topic_stub(ctx: &Context, msg_id: MsgId, topic: TopicId) -> Result<()> {
|
||||||
ctx.sql
|
ctx.sql
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ use crate::message::{
|
|||||||
};
|
};
|
||||||
use crate::mimeparser::{parse_message_ids, AvatarAction, MimeMessage, SystemMessage};
|
use crate::mimeparser::{parse_message_ids, AvatarAction, MimeMessage, SystemMessage};
|
||||||
use crate::param::{Param, Params};
|
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::peerstate::Peerstate;
|
||||||
use crate::reaction::{set_msg_reaction, Reaction};
|
use crate::reaction::{set_msg_reaction, Reaction};
|
||||||
use crate::securejoin::{self, handle_securejoin_handshake, observe_securejoin_on_other_device};
|
use crate::securejoin::{self, handle_securejoin_handshake, observe_securejoin_on_other_device};
|
||||||
@@ -41,7 +41,6 @@ use crate::sync::Sync::*;
|
|||||||
use crate::tools::{self, buf_compress, remove_subject_prefix};
|
use crate::tools::{self, buf_compress, remove_subject_prefix};
|
||||||
use crate::{chatlist_events, location};
|
use crate::{chatlist_events, location};
|
||||||
use crate::{contact, imap};
|
use crate::{contact, imap};
|
||||||
use iroh_net::NodeAddr;
|
|
||||||
|
|
||||||
/// This is the struct that is returned after receiving one email (aka MIME message).
|
/// This is the struct that is returned after receiving one email (aka MIME message).
|
||||||
///
|
///
|
||||||
@@ -1446,61 +1445,27 @@ async fn add_parts(
|
|||||||
|
|
||||||
if let Some(node_addr) = mime_parser.get_header(HeaderDef::IrohNodeAddr) {
|
if let Some(node_addr) = mime_parser.get_header(HeaderDef::IrohNodeAddr) {
|
||||||
chat_id = DC_CHAT_ID_TRASH;
|
chat_id = DC_CHAT_ID_TRASH;
|
||||||
match serde_json::from_str::<NodeAddr>(node_addr).context("Failed to parse node address") {
|
match mime_parser.get_header(HeaderDef::InReplyTo) {
|
||||||
Ok(node_addr) => {
|
Some(in_reply_to) => match rfc724_mid_exists(context, in_reply_to).await? {
|
||||||
info!(context, "Adding iroh peer with address {node_addr:?}.");
|
Some((instance_id, _ts_sent)) => {
|
||||||
match mime_parser.get_header(HeaderDef::InReplyTo) {
|
if let Err(err) =
|
||||||
Some(in_reply_to) => match rfc724_mid_exists(context, in_reply_to).await? {
|
add_gossip_peer_from_header(context, instance_id, node_addr).await
|
||||||
Some((instance_id, _ts_sent)) => {
|
{
|
||||||
context.emit_event(EventType::WebxdcRealtimeAdvertisementReceived {
|
warn!(context, "Failed to add iroh peer from header: {err:#}.");
|
||||||
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."
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
None => {
|
||||||
Err(err) => {
|
warn!(
|
||||||
warn!(context, "Couldn't parse NodeAddr: {err:#}.");
|
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."
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user