Skip to main content

vector_core/xdc/
mod.rs

1//! Mini App (WebXDC) realtime sessions for headless clients: a bot can join
2//! the game a player shared, speak its protocol, and leave.
3//!
4//! A Mini App message carries a `webxdc-topic`, minted once at send time. Every
5//! participant who opens it joins that topic on an Iroh gossip mesh
6//! ([`mesh`]), and announces its node into the chat over Nostr ([`signal`]) so
7//! the others can dial it. [`session::join`] does both for a headless peer;
8//! what flows on the channel is the app's own protocol, opaque here.
9//!
10//! The realtime channel is ephemeral and best-effort, reaching only peers
11//! online at the time.
12
13pub mod mesh;
14pub mod package;
15pub mod session;
16pub mod signal;
17pub mod wire;
18
19pub use session::{allow_outside_tor, allow_outside_transport, is_joined, join, join_with, JoinOptions, XdcEvent, XdcFrame, XdcPeer, XdcSender, XdcSession};
20
21use crate::types::Attachment;
22
23/// A Mini App message, as found by its realtime topic.
24#[derive(Debug, Clone)]
25pub struct AppMessage {
26    /// The DM npub or Community channel id it was shared in.
27    pub chat_id: String,
28    pub message_id: String,
29    pub attachment: Attachment,
30}
31
32/// The Mini App message in `chat_id` whose session runs on `topic`. Live state
33/// first: a headless client's own sends live there, not in its database.
34pub async fn find_by_topic_in(chat_id: &str, topic: &str) -> Option<AppMessage> {
35    {
36        let state = crate::state::STATE.lock().await;
37        if let Some(chat) = state.chats.iter().find(|c| c.id == chat_id) {
38            let hit = chat.messages.iter().find(|m| m.attachments.iter().any(|a| a.webxdc_topic() == Some(topic)));
39            if let Some(compact) = hit {
40                let msg = compact.to_message(&state.interner);
41                let attachment = msg.attachments.into_iter().find(|a| a.webxdc_topic.as_deref() == Some(topic))?;
42                return Some(AppMessage { chat_id: chat_id.to_string(), message_id: msg.id, attachment });
43            }
44        }
45    }
46    let (message_id, attachment) = crate::db::attachments::find_by_webxdc_topic_in(chat_id, topic).ok().flatten()?;
47    Some(AppMessage { chat_id: chat_id.to_string(), message_id, attachment })
48}
49
50/// Take in a peer signal from an inbound hook (`on_webxdc_signal` for a DM,
51/// `on_community_webxdc` for a channel): persist it, and feed it to the session
52/// on its topic if this account is in one. Returns the signal, or `None` for garbage.
53pub async fn on_signal(
54    chat_id: &str,
55    npub: &str,
56    topic: &str,
57    node_addr: Option<&str>,
58    event_id: &str,
59    created_at: u64,
60) -> Option<signal::Signal> {
61    let sig = signal::ingest(chat_id, npub, topic, node_addr, event_id, created_at).await?;
62    session::apply_signal(&sig).await;
63    Some(sig)
64}
65
66/// Whether an attachment is a Mini App.
67pub fn is_app(att: &Attachment) -> bool {
68    att.extension.eq_ignore_ascii_case("xdc")
69}