xmpp_parsers/
call_invites.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright (c) 2025 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use xso::{AsXml, FromXml};

use crate::message::MessagePayload;
use crate::ns;

/// An `<invite/>` element, to invite someone to a call.
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
#[xml(namespace = ns::CALL_INVITES, name = "invite")]
pub struct Invite {
    /// Indicates if the call is intended to be joined with participants sending audio or not.
    // TODO: Figure out how to default audio to true.
    #[xml(attribute(default))]
    pub audio: bool,

    /// Indicates if the call is intended to be joined with participants sending video or not.
    #[xml(attribute(default))]
    pub video: bool,

    /// A `<jingle/>` element, indicating the possibility to join this call using Jingle.
    #[xml(child)]
    pub jingle: Jingle,

    /// An external URI, indicating alternate methods to join this call.
    #[xml(extract(n = .., name = "external", fields(attribute(name = "uri", type_ = String))))]
    pub external_uris: Vec<String>,
}

impl MessagePayload for Invite {}

/// A `<jingle/>` element, indicating the possibility to join this call using Jingle.
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
#[xml(namespace = ns::CALL_INVITES, name = "jingle")]
pub struct Jingle {
    /// The ID of the Jingle session.
    #[xml(attribute)]
    pub sid: String,

    /// An optional 'jid' attribute, to indicate the JID the Jingle session will be initiated from.
    /// If no 'jid' attribute is provided, the default is the sender of the message stanza.
    #[xml(attribute(default))]
    pub jid: Option<String>,
}

/// A call invite can be retracted by sending a message containing a `<retract/>` element.
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
#[xml(namespace = ns::CALL_INVITES, name = "retract")]
pub struct Retract {
    /// Identifier of the initial [`Invite`] message to be retracted.
    #[xml(attribute)]
    pub id: String,
}

/// A call invite can be accepted by sending a message containing an `<accept/>` element.
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
#[xml(namespace = ns::CALL_INVITES, name = "accept")]
pub struct Accept {
    /// Identifier of the initial [`Invite`] message to be accepted.
    #[xml(attribute)]
    pub id: String,

    /// The payload of an `<accept/>` element, either Jingle or an external URI.
    #[xml(child)]
    pub payload: AcceptPayload,
}

/// The payload of an `<accept/>` element, either Jingle or an external URI.
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
#[xml()]
pub enum AcceptPayload {
    /// The client picked Jingle, it should then send a session-initiate.
    #[xml(transparent)]
    Jingle(Jingle),

    /// The client picked an external method, it should now handle the URI.
    // TODO: This variant doesn’t work for now.
    #[xml(namespace = ns::CALL_INVITES, name = "external")]
    ExternalUri(
        #[xml(extract(name = "external", fields(attribute(name = "uri", type_ = String))))] String,
    ),
}

/// A call invite can be rejected by sending a message containing a `<reject/>` element.
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
#[xml(namespace = ns::CALL_INVITES, name = "reject")]
pub struct Reject {
    /// Identifier of the initial [`Invite`] message to be rejected.
    #[xml(attribute)]
    pub id: String,
}

/// When a client leaves a call, it sends a message containing a `<left/>` element.
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
#[xml(namespace = ns::CALL_INVITES, name = "left")]
pub struct Left {
    /// Identifier of the initial [`Invite`] message that got left.
    #[xml(attribute)]
    pub id: String,
}

#[cfg(test)]
mod tests {
    use super::*;
    use minidom::Element;

    #[cfg(target_pointer_width = "32")]
    #[test]
    fn test_size() {
        assert_size!(Invite, 40);
        assert_size!(Jingle, 24);
        assert_size!(Retract, 12);
        assert_size!(Accept, 36);
        assert_size!(AcceptPayload, 24);
        assert_size!(Reject, 12);
        assert_size!(Left, 12);
    }

    #[cfg(target_pointer_width = "64")]
    #[test]
    fn test_size() {
        assert_size!(Invite, 80);
        assert_size!(Jingle, 48);
        assert_size!(Retract, 24);
        assert_size!(Accept, 72);
        assert_size!(AcceptPayload, 48);
        assert_size!(Reject, 24);
        assert_size!(Left, 24);
    }

    #[test]
    fn invite_example_1() {
        let elem: Element =
            "<invite video='true' xmlns='urn:xmpp:call-invites:0'><jingle sid='sid1'/></invite>"
                .parse()
                .unwrap();
        let invite = Invite::try_from(elem).unwrap();
        // TODO: Figure out how to default audio to true.
        //assert_eq!(invite.audio, true);
        assert_eq!(invite.video, true);
        assert_eq!(invite.jingle.sid, "sid1");
        assert_eq!(invite.jingle.jid, None);
        assert_eq!(invite.external_uris.len(), 0);
    }

    #[test]
    fn invite_example_2() {
        let elem: Element = "<invite xmlns='urn:xmpp:call-invites:0'><jingle sid='sid2' jid='mixer@example.com/uuid'/><external uri='https://example.com/uuid'/><external uri='tel:+12345678'/></invite>".parse().unwrap();
        let invite = Invite::try_from(elem).unwrap();
        // TODO: Figure out how to default audio to true.
        //assert_eq!(invite.audio, true);
        assert_eq!(invite.video, false);
        assert_eq!(invite.jingle.sid, "sid2");
        assert_eq!(invite.jingle.jid.unwrap(), "mixer@example.com/uuid");
        assert_eq!(invite.external_uris.len(), 2);
        assert_eq!(invite.external_uris[0], "https://example.com/uuid");
        assert_eq!(invite.external_uris[1], "tel:+12345678");
    }

    #[test]
    fn retract_example_3() {
        let elem: Element = "<retract id='id1' xmlns='urn:xmpp:call-invites:0'/>"
            .parse()
            .unwrap();
        let retract = Retract::try_from(elem).unwrap();
        assert_eq!(retract.id, "id1");
    }

    #[test]
    fn accept_example_4() {
        let elem: Element = "<accept id='id1' xmlns='urn:xmpp:call-invites:0'><jingle sid='sid1' jid='mixer@example.com/uuid'/></accept>".parse().unwrap();
        let accept = Accept::try_from(elem).unwrap();
        assert_eq!(accept.id, "id1");
        let AcceptPayload::Jingle(jingle) = accept.payload else {
            panic!("Unexpected payload.")
        };
        assert_eq!(jingle.sid, "sid1");
        assert_eq!(jingle.jid.unwrap(), "mixer@example.com/uuid");
    }

    // TODO: Fix the AcceptPayload::ExternalUri to make this example work.
    #[test]
    #[ignore]
    fn accept_external() {
        let elem: Element = "<accept id='id2' xmlns='urn:xmpp:call-invites:0'><external uri='tel:+12345678'/></accept>".parse().unwrap();
        let accept = Accept::try_from(elem).unwrap();
        assert_eq!(accept.id, "id2");
        let AcceptPayload::ExternalUri(uri) = accept.payload else {
            panic!("Unexpected payload.")
        };
        assert_eq!(uri, "tel:+12345678");
    }

    #[test]
    fn reject_example_5() {
        let elem: Element = "<reject id='id1' xmlns='urn:xmpp:call-invites:0'/>"
            .parse()
            .unwrap();
        let reject = Reject::try_from(elem).unwrap();
        assert_eq!(reject.id, "id1");
    }

    #[test]
    fn left_example_6() {
        let elem: Element = "<left id='id1' xmlns='urn:xmpp:call-invites:0'/>"
            .parse()
            .unwrap();
        let left = Left::try_from(elem).unwrap();
        assert_eq!(left.id, "id1");
    }
}