2021-09-29 18:08:08 +00:00
|
|
|
|
// Copyright (c) 2017-2021 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
2017-04-29 21:14:34 +00:00
|
|
|
|
//
|
|
|
|
|
// 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/.
|
|
|
|
|
|
2024-07-01 16:47:37 +00:00
|
|
|
|
use xso::{
|
|
|
|
|
error::{Error, FromElementError},
|
2024-07-09 15:01:42 +00:00
|
|
|
|
AsXml, FromXml,
|
2024-07-01 16:47:37 +00:00
|
|
|
|
};
|
|
|
|
|
|
2018-12-18 14:27:30 +00:00
|
|
|
|
use crate::data_forms::DataForm;
|
2024-07-01 16:47:37 +00:00
|
|
|
|
use crate::date::DateTime;
|
2018-12-18 14:27:30 +00:00
|
|
|
|
use crate::forwarding::Forwarded;
|
2018-12-18 14:32:05 +00:00
|
|
|
|
use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
|
|
|
|
|
use crate::message::MessagePayload;
|
2024-01-31 23:12:28 +00:00
|
|
|
|
use crate::ns;
|
2018-12-18 14:32:05 +00:00
|
|
|
|
use crate::pubsub::NodeName;
|
|
|
|
|
use crate::rsm::{SetQuery, SetResult};
|
2024-01-31 23:12:28 +00:00
|
|
|
|
use crate::Element;
|
|
|
|
|
use minidom::Node;
|
2017-04-29 05:07:00 +00:00
|
|
|
|
|
2018-08-02 18:05:35 +00:00
|
|
|
|
generate_id!(
|
|
|
|
|
/// An identifier matching a result message to the query requesting it.
|
|
|
|
|
QueryId
|
|
|
|
|
);
|
|
|
|
|
|
2024-01-31 23:12:28 +00:00
|
|
|
|
/// Starts a query to the archive.
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Query {
|
|
|
|
|
/// An optional identifier for matching forwarded messages to this
|
|
|
|
|
/// query.
|
|
|
|
|
pub queryid: Option<QueryId>,
|
|
|
|
|
/// Must be set to Some when querying a PubSub node’s archive.
|
|
|
|
|
pub node: Option<NodeName>,
|
|
|
|
|
/// Used for filtering the results.
|
|
|
|
|
pub form: Option<DataForm>,
|
|
|
|
|
/// Used for paging through results.
|
|
|
|
|
pub set: Option<SetQuery>,
|
|
|
|
|
/// Used for reversing the order of the results.
|
|
|
|
|
pub flip_page: bool,
|
|
|
|
|
}
|
2017-04-29 05:07:00 +00:00
|
|
|
|
|
2018-05-16 13:08:17 +00:00
|
|
|
|
impl IqGetPayload for Query {}
|
|
|
|
|
impl IqSetPayload for Query {}
|
|
|
|
|
impl IqResultPayload for Query {}
|
|
|
|
|
|
2024-01-31 23:12:28 +00:00
|
|
|
|
impl TryFrom<Element> for Query {
|
2024-06-21 14:27:43 +00:00
|
|
|
|
type Error = FromElementError;
|
|
|
|
|
fn try_from(elem: Element) -> Result<Query, FromElementError> {
|
2024-01-31 23:12:28 +00:00
|
|
|
|
check_self!(elem, "query", MAM);
|
|
|
|
|
check_no_unknown_attributes!(elem, "query", ["queryid", "node"]);
|
|
|
|
|
|
|
|
|
|
let mut form = None;
|
|
|
|
|
let mut set = None;
|
2024-07-24 19:39:13 +00:00
|
|
|
|
let mut flip_page = false;
|
2024-01-31 23:12:28 +00:00
|
|
|
|
for child in elem.children() {
|
|
|
|
|
if child.is("x", ns::DATA_FORMS) {
|
|
|
|
|
if form.is_some() {
|
2024-06-21 14:27:43 +00:00
|
|
|
|
return Err(
|
|
|
|
|
Error::Other("Element query must not have more than one x child.").into(),
|
|
|
|
|
);
|
2024-01-31 23:12:28 +00:00
|
|
|
|
}
|
|
|
|
|
form = Some(DataForm::try_from(child.clone())?);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if child.is("set", ns::RSM) {
|
|
|
|
|
if set.is_some() {
|
2024-06-21 14:27:43 +00:00
|
|
|
|
return Err(Error::Other(
|
2024-01-31 23:12:28 +00:00
|
|
|
|
"Element query must not have more than one set child.",
|
2024-06-21 14:27:43 +00:00
|
|
|
|
)
|
|
|
|
|
.into());
|
2024-01-31 23:12:28 +00:00
|
|
|
|
}
|
|
|
|
|
set = Some(SetQuery::try_from(child.clone())?);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if child.is("flip-page", ns::MAM) {
|
2024-07-24 19:39:13 +00:00
|
|
|
|
if flip_page {
|
2024-06-21 14:27:43 +00:00
|
|
|
|
return Err(Error::Other(
|
2024-01-31 23:12:28 +00:00
|
|
|
|
"Element query must not have more than one flip-page child.",
|
2024-06-21 14:27:43 +00:00
|
|
|
|
)
|
|
|
|
|
.into());
|
2024-01-31 23:12:28 +00:00
|
|
|
|
}
|
2024-07-24 19:39:13 +00:00
|
|
|
|
flip_page = true;
|
2024-01-31 23:12:28 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-06-21 14:27:43 +00:00
|
|
|
|
return Err(Error::Other("Unknown child in query element.").into());
|
2024-01-31 23:12:28 +00:00
|
|
|
|
}
|
|
|
|
|
Ok(Query {
|
|
|
|
|
queryid: match elem.attr("queryid") {
|
|
|
|
|
Some(value) => Some(value.parse()?),
|
|
|
|
|
None => None,
|
|
|
|
|
},
|
|
|
|
|
node: match elem.attr("node") {
|
|
|
|
|
Some(value) => Some(value.parse()?),
|
|
|
|
|
None => None,
|
|
|
|
|
},
|
|
|
|
|
form,
|
|
|
|
|
set,
|
2024-07-24 19:39:13 +00:00
|
|
|
|
flip_page,
|
2024-01-31 23:12:28 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<Query> for Element {
|
|
|
|
|
fn from(elem: Query) -> Element {
|
|
|
|
|
let mut builder = Element::builder("query", ns::MAM);
|
|
|
|
|
builder = builder.attr("queryid", elem.queryid);
|
|
|
|
|
builder = builder.attr("node", elem.node);
|
|
|
|
|
builder = builder.append_all(elem.form.map(|elem| Node::Element(Element::from(elem))));
|
|
|
|
|
builder = builder.append_all(elem.set.map(|elem| Node::Element(Element::from(elem))));
|
|
|
|
|
if elem.flip_page {
|
|
|
|
|
let flip_page = Element::builder("flip-page", ns::MAM).build();
|
|
|
|
|
builder = builder.append(Node::Element(flip_page));
|
|
|
|
|
}
|
|
|
|
|
builder.build()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-28 14:45:13 +00:00
|
|
|
|
generate_element!(
|
2018-08-02 18:05:51 +00:00
|
|
|
|
/// The wrapper around forwarded stanzas.
|
2018-05-15 00:06:38 +00:00
|
|
|
|
Result_, "result", MAM,
|
|
|
|
|
attributes: [
|
2018-08-02 18:05:51 +00:00
|
|
|
|
/// The stanza-id under which the archive stored this stanza.
|
2019-02-24 19:26:40 +00:00
|
|
|
|
id: Required<String> = "id",
|
2018-08-02 18:05:51 +00:00
|
|
|
|
|
|
|
|
|
/// The same queryid as the one requested in the
|
|
|
|
|
/// [query](struct.Query.html).
|
2019-02-24 19:26:40 +00:00
|
|
|
|
queryid: Option<QueryId> = "queryid",
|
2018-05-15 00:06:38 +00:00
|
|
|
|
],
|
2018-05-28 14:29:51 +00:00
|
|
|
|
children: [
|
2018-08-02 18:05:51 +00:00
|
|
|
|
/// The actual stanza being forwarded.
|
2018-05-28 14:29:51 +00:00
|
|
|
|
forwarded: Required<Forwarded> = ("forwarded", FORWARD) => Forwarded
|
|
|
|
|
]
|
2018-05-15 00:06:38 +00:00
|
|
|
|
);
|
2017-04-29 05:07:00 +00:00
|
|
|
|
|
2018-09-20 18:58:27 +00:00
|
|
|
|
impl MessagePayload for Result_ {}
|
|
|
|
|
|
2018-05-28 14:29:51 +00:00
|
|
|
|
generate_attribute!(
|
2018-08-02 18:05:51 +00:00
|
|
|
|
/// True when the end of a MAM query has been reached.
|
2018-12-18 14:32:05 +00:00
|
|
|
|
Complete,
|
|
|
|
|
"complete",
|
|
|
|
|
bool
|
2018-05-28 14:29:51 +00:00
|
|
|
|
);
|
|
|
|
|
|
2018-05-28 14:45:13 +00:00
|
|
|
|
generate_element!(
|
2018-08-02 18:05:51 +00:00
|
|
|
|
/// Notes the end of a page in a query.
|
2018-05-28 14:29:51 +00:00
|
|
|
|
Fin, "fin", MAM,
|
|
|
|
|
attributes: [
|
2018-08-02 18:05:51 +00:00
|
|
|
|
/// True when the end of a MAM query has been reached.
|
2019-02-24 19:26:40 +00:00
|
|
|
|
complete: Default<Complete> = "complete",
|
2018-05-28 14:29:51 +00:00
|
|
|
|
],
|
|
|
|
|
children: [
|
2018-08-02 18:05:51 +00:00
|
|
|
|
/// Describes the current page, it should contain at least [first]
|
|
|
|
|
/// (with an [index]) and [last], and generally [count].
|
|
|
|
|
///
|
2018-08-02 18:35:15 +00:00
|
|
|
|
/// [first]: ../rsm/struct.SetResult.html#structfield.first
|
|
|
|
|
/// [index]: ../rsm/struct.SetResult.html#structfield.first_index
|
|
|
|
|
/// [last]: ../rsm/struct.SetResult.html#structfield.last
|
|
|
|
|
/// [count]: ../rsm/struct.SetResult.html#structfield.count
|
|
|
|
|
set: Required<SetResult> = ("set", RSM) => SetResult
|
2018-05-28 14:29:51 +00:00
|
|
|
|
]
|
|
|
|
|
);
|
2017-04-29 05:07:00 +00:00
|
|
|
|
|
2018-05-16 13:08:17 +00:00
|
|
|
|
impl IqResultPayload for Fin {}
|
|
|
|
|
|
2024-07-01 16:47:37 +00:00
|
|
|
|
/// Metadata of the first message in the archive.
|
2024-07-09 15:01:42 +00:00
|
|
|
|
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
|
2024-07-01 16:47:37 +00:00
|
|
|
|
#[xml(namespace = ns::MAM, name = "start")]
|
|
|
|
|
pub struct Start {
|
|
|
|
|
/// The id of the first message in the archive.
|
|
|
|
|
#[xml(attribute)]
|
|
|
|
|
pub id: String,
|
|
|
|
|
|
|
|
|
|
/// Time at which that message was sent.
|
|
|
|
|
#[xml(attribute)]
|
|
|
|
|
pub timestamp: DateTime,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Metadata of the last message in the archive.
|
2024-07-09 15:01:42 +00:00
|
|
|
|
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
|
2024-07-01 16:47:37 +00:00
|
|
|
|
#[xml(namespace = ns::MAM, name = "end")]
|
|
|
|
|
pub struct End {
|
|
|
|
|
/// The id of the last message in the archive.
|
|
|
|
|
#[xml(attribute)]
|
|
|
|
|
pub id: String,
|
|
|
|
|
|
|
|
|
|
/// Time at which that message was sent.
|
|
|
|
|
#[xml(attribute)]
|
|
|
|
|
pub timestamp: DateTime,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Request an archive for its metadata.
|
2024-07-09 15:01:42 +00:00
|
|
|
|
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
|
2024-07-01 16:47:37 +00:00
|
|
|
|
#[xml(namespace = ns::MAM, name = "metadata")]
|
|
|
|
|
pub struct MetadataQuery;
|
|
|
|
|
|
|
|
|
|
impl IqGetPayload for MetadataQuery {}
|
|
|
|
|
|
|
|
|
|
generate_element!(
|
|
|
|
|
/// Response from the archive, containing the start and end metadata if it isn’t empty.
|
|
|
|
|
MetadataResponse, "metadata", MAM,
|
|
|
|
|
children: [
|
|
|
|
|
/// Metadata about the first message in the archive.
|
|
|
|
|
start: Option<Start> = ("start", MAM) => Start,
|
|
|
|
|
|
|
|
|
|
/// Metadata about the last message in the archive.
|
|
|
|
|
end: Option<End> = ("end", MAM) => End,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
impl IqResultPayload for MetadataResponse {}
|
|
|
|
|
|
2017-04-29 05:07:00 +00:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2017-05-06 20:08:44 +00:00
|
|
|
|
use super::*;
|
2017-04-29 05:07:00 +00:00
|
|
|
|
|
2018-10-28 12:10:48 +00:00
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
|
|
|
|
assert_size!(QueryId, 12);
|
2024-02-01 12:03:31 +00:00
|
|
|
|
assert_size!(Query, 120);
|
2024-04-15 15:03:57 +00:00
|
|
|
|
assert_size!(Result_, 164);
|
2018-10-28 12:10:48 +00:00
|
|
|
|
assert_size!(Complete, 1);
|
|
|
|
|
assert_size!(Fin, 44);
|
2024-07-01 16:47:37 +00:00
|
|
|
|
assert_size!(Start, 28);
|
|
|
|
|
assert_size!(End, 28);
|
|
|
|
|
assert_size!(MetadataQuery, 0);
|
|
|
|
|
assert_size!(MetadataResponse, 56);
|
2018-10-28 12:10:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
2018-10-26 12:26:16 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
|
|
|
|
assert_size!(QueryId, 24);
|
2024-01-31 23:12:28 +00:00
|
|
|
|
assert_size!(Query, 240);
|
2024-04-15 15:03:57 +00:00
|
|
|
|
assert_size!(Result_, 312);
|
2018-10-26 12:26:16 +00:00
|
|
|
|
assert_size!(Complete, 1);
|
|
|
|
|
assert_size!(Fin, 88);
|
2024-07-01 16:47:37 +00:00
|
|
|
|
assert_size!(Start, 40);
|
|
|
|
|
assert_size!(End, 40);
|
|
|
|
|
assert_size!(MetadataQuery, 0);
|
|
|
|
|
assert_size!(MetadataResponse, 80);
|
2018-10-26 12:26:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-29 05:07:00 +00:00
|
|
|
|
#[test]
|
2017-04-29 05:41:55 +00:00
|
|
|
|
fn test_query() {
|
|
|
|
|
let elem: Element = "<query xmlns='urn:xmpp:mam:2'/>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
Query::try_from(elem).unwrap();
|
2017-04-29 05:07:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2017-04-29 05:41:55 +00:00
|
|
|
|
fn test_result() {
|
2017-07-29 05:49:02 +00:00
|
|
|
|
#[cfg(not(feature = "component"))]
|
2022-03-22 22:29:25 +00:00
|
|
|
|
let elem: Element = r#"<result xmlns='urn:xmpp:mam:2' queryid='f27' id='28482-98726-73623'>
|
2017-04-29 05:41:55 +00:00
|
|
|
|
<forwarded xmlns='urn:xmpp:forward:0'>
|
|
|
|
|
<delay xmlns='urn:xmpp:delay' stamp='2010-07-10T23:08:25Z'/>
|
|
|
|
|
<message xmlns='jabber:client' from="witch@shakespeare.lit" to="macbeth@shakespeare.lit">
|
|
|
|
|
<body>Hail to thee</body>
|
|
|
|
|
</message>
|
|
|
|
|
</forwarded>
|
|
|
|
|
</result>
|
2018-12-18 14:32:05 +00:00
|
|
|
|
"#
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
|
#[cfg(feature = "component")]
|
2022-03-22 22:29:25 +00:00
|
|
|
|
let elem: Element = r#"<result xmlns='urn:xmpp:mam:2' queryid='f27' id='28482-98726-73623'>
|
2017-07-29 05:49:02 +00:00
|
|
|
|
<forwarded xmlns='urn:xmpp:forward:0'>
|
|
|
|
|
<delay xmlns='urn:xmpp:delay' stamp='2010-07-10T23:08:25Z'/>
|
|
|
|
|
<message xmlns='jabber:component:accept' from="witch@shakespeare.lit" to="macbeth@shakespeare.lit">
|
|
|
|
|
<body>Hail to thee</body>
|
|
|
|
|
</message>
|
|
|
|
|
</forwarded>
|
|
|
|
|
</result>
|
2017-04-29 05:41:55 +00:00
|
|
|
|
"#.parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
Result_::try_from(elem).unwrap();
|
2017-04-29 05:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_fin() {
|
2022-03-22 22:29:25 +00:00
|
|
|
|
let elem: Element = r#"<fin xmlns='urn:xmpp:mam:2'>
|
2017-04-29 05:41:55 +00:00
|
|
|
|
<set xmlns='http://jabber.org/protocol/rsm'>
|
|
|
|
|
<first index='0'>28482-98726-73623</first>
|
|
|
|
|
<last>09af3-cc343-b409f</last>
|
|
|
|
|
</set>
|
|
|
|
|
</fin>
|
2018-12-18 14:32:05 +00:00
|
|
|
|
"#
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
Fin::try_from(elem).unwrap();
|
2017-04-29 05:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_query_x() {
|
2022-03-22 22:29:25 +00:00
|
|
|
|
let elem: Element = r#"<query xmlns='urn:xmpp:mam:2'>
|
2017-04-29 05:41:55 +00:00
|
|
|
|
<x xmlns='jabber:x:data' type='submit'>
|
|
|
|
|
<field var='FORM_TYPE' type='hidden'>
|
|
|
|
|
<value>urn:xmpp:mam:2</value>
|
|
|
|
|
</field>
|
|
|
|
|
<field var='with'>
|
|
|
|
|
<value>juliet@capulet.lit</value>
|
|
|
|
|
</field>
|
|
|
|
|
</x>
|
|
|
|
|
</query>
|
2018-12-18 14:32:05 +00:00
|
|
|
|
"#
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
Query::try_from(elem).unwrap();
|
2017-04-29 05:07:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2017-04-29 05:41:55 +00:00
|
|
|
|
fn test_query_x_set() {
|
2022-03-22 22:29:25 +00:00
|
|
|
|
let elem: Element = r#"<query xmlns='urn:xmpp:mam:2'>
|
2017-04-29 05:41:55 +00:00
|
|
|
|
<x xmlns='jabber:x:data' type='submit'>
|
|
|
|
|
<field var='FORM_TYPE' type='hidden'>
|
|
|
|
|
<value>urn:xmpp:mam:2</value>
|
|
|
|
|
</field>
|
|
|
|
|
<field var='start'>
|
|
|
|
|
<value>2010-08-07T00:00:00Z</value>
|
|
|
|
|
</field>
|
|
|
|
|
</x>
|
|
|
|
|
<set xmlns='http://jabber.org/protocol/rsm'>
|
|
|
|
|
<max>10</max>
|
|
|
|
|
</set>
|
|
|
|
|
</query>
|
2018-12-18 14:32:05 +00:00
|
|
|
|
"#
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
Query::try_from(elem).unwrap();
|
2017-04-29 05:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-31 23:12:28 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_query_x_set_flipped() {
|
|
|
|
|
let elem: Element = r#"<query xmlns='urn:xmpp:mam:2'>
|
|
|
|
|
<x xmlns='jabber:x:data' type='submit'>
|
|
|
|
|
<field var='FORM_TYPE' type='hidden'>
|
|
|
|
|
<value>urn:xmpp:mam:2</value>
|
|
|
|
|
</field>
|
|
|
|
|
<field var='start'>
|
|
|
|
|
<value>2010-08-07T00:00:00Z</value>
|
|
|
|
|
</field>
|
|
|
|
|
</x>
|
|
|
|
|
<set xmlns='http://jabber.org/protocol/rsm'>
|
|
|
|
|
<max>10</max>
|
|
|
|
|
</set>
|
|
|
|
|
<flip-page/>
|
|
|
|
|
</query>
|
|
|
|
|
"#
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
|
|
|
|
Query::try_from(elem).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-01 16:47:37 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_metadata() {
|
|
|
|
|
let elem: Element = r"<metadata xmlns='urn:xmpp:mam:2'/>".parse().unwrap();
|
|
|
|
|
MetadataQuery::try_from(elem).unwrap();
|
|
|
|
|
|
|
|
|
|
let elem: Element = r"<metadata xmlns='urn:xmpp:mam:2'>
|
|
|
|
|
<start id='YWxwaGEg' timestamp='2008-08-22T21:09:04Z' />
|
|
|
|
|
<end id='b21lZ2Eg' timestamp='2020-04-20T14:34:21Z' />
|
|
|
|
|
</metadata>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
|
|
|
|
let metadata = MetadataResponse::try_from(elem).unwrap();
|
|
|
|
|
let start = metadata.start.unwrap();
|
|
|
|
|
let end = metadata.end.unwrap();
|
|
|
|
|
assert_eq!(start.id, "YWxwaGEg");
|
|
|
|
|
assert_eq!(start.timestamp.0.timestamp(), 1219439344);
|
|
|
|
|
assert_eq!(end.id, "b21lZ2Eg");
|
|
|
|
|
assert_eq!(end.timestamp.0.timestamp(), 1587393261);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-29 05:41:55 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_child() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let elem: Element = "<query xmlns='urn:xmpp:mam:2'><coucou/></query>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = Query::try_from(elem).unwrap_err();
|
2017-04-29 05:07:00 +00:00
|
|
|
|
let message = match error {
|
2024-06-21 14:27:43 +00:00
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2017-04-29 05:07:00 +00:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2017-04-29 05:41:55 +00:00
|
|
|
|
assert_eq!(message, "Unknown child in query element.");
|
2017-04-29 05:07:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2019-11-22 16:31:25 +00:00
|
|
|
|
fn test_serialise_empty() {
|
2017-04-29 05:41:55 +00:00
|
|
|
|
let elem: Element = "<query xmlns='urn:xmpp:mam:2'/>".parse().unwrap();
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let replace = Query {
|
|
|
|
|
queryid: None,
|
|
|
|
|
node: None,
|
|
|
|
|
form: None,
|
|
|
|
|
set: None,
|
2024-01-31 23:12:28 +00:00
|
|
|
|
flip_page: false,
|
2018-12-18 14:32:05 +00:00
|
|
|
|
};
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let elem2 = replace.into();
|
2017-04-29 05:07:00 +00:00
|
|
|
|
assert_eq!(elem, elem2);
|
|
|
|
|
}
|
2019-11-22 16:31:25 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
2019-12-05 20:41:43 +00:00
|
|
|
|
fn test_serialize_query_with_form() {
|
2024-01-31 23:12:28 +00:00
|
|
|
|
let reference: Element = "<query xmlns='urn:xmpp:mam:2'><x xmlns='jabber:x:data' type='submit'><field xmlns='jabber:x:data' var='FORM_TYPE' type='hidden'><value xmlns='jabber:x:data'>urn:xmpp:mam:2</value></field><field xmlns='jabber:x:data' var='with'><value xmlns='jabber:x:data'>juliet@capulet.lit</value></field></x><flip-page/></query>"
|
2019-11-22 16:31:25 +00:00
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
2019-12-05 20:41:43 +00:00
|
|
|
|
let elem: Element = "<x xmlns='jabber:x:data' type='submit'><field xmlns='jabber:x:data' var='FORM_TYPE' type='hidden'><value xmlns='jabber:x:data'>urn:xmpp:mam:2</value></field><field xmlns='jabber:x:data' var='with'><value xmlns='jabber:x:data'>juliet@capulet.lit</value></field></x>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let form = DataForm::try_from(elem).unwrap();
|
|
|
|
|
|
2019-12-05 21:24:25 +00:00
|
|
|
|
let query = Query {
|
2019-11-22 16:31:25 +00:00
|
|
|
|
queryid: None,
|
|
|
|
|
node: None,
|
|
|
|
|
set: None,
|
|
|
|
|
form: Some(form),
|
2024-01-31 23:12:28 +00:00
|
|
|
|
flip_page: true,
|
2019-11-22 16:31:25 +00:00
|
|
|
|
};
|
2019-12-05 21:24:25 +00:00
|
|
|
|
let serialized: Element = query.into();
|
2019-11-27 17:16:51 +00:00
|
|
|
|
assert_eq!(serialized, reference);
|
2019-11-22 16:31:25 +00:00
|
|
|
|
}
|
2019-11-28 17:00:40 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_serialize_result() {
|
2019-12-05 20:41:43 +00:00
|
|
|
|
let reference: Element = "<result xmlns='urn:xmpp:mam:2' queryid='f27' id='28482-98726-73623'><forwarded xmlns='urn:xmpp:forward:0'><delay xmlns='urn:xmpp:delay' stamp='2002-09-10T23:08:25+00:00'/><message xmlns='jabber:client' to='juliet@capulet.example/balcony' from='romeo@montague.example/home'/></forwarded></result>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'><delay xmlns='urn:xmpp:delay' stamp='2002-09-10T23:08:25+00:00'/><message xmlns='jabber:client' to='juliet@capulet.example/balcony' from='romeo@montague.example/home'/></forwarded>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let forwarded = Forwarded::try_from(elem).unwrap();
|
|
|
|
|
|
|
|
|
|
let result = Result_ {
|
2019-12-30 05:55:32 +00:00
|
|
|
|
id: String::from("28482-98726-73623"),
|
|
|
|
|
queryid: Some(QueryId(String::from("f27"))),
|
2024-01-31 23:12:28 +00:00
|
|
|
|
forwarded,
|
2019-12-05 20:41:43 +00:00
|
|
|
|
};
|
|
|
|
|
let serialized: Element = result.into();
|
|
|
|
|
assert_eq!(serialized, reference);
|
2019-11-28 17:00:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_serialize_fin() {
|
2019-12-05 20:41:43 +00:00
|
|
|
|
let reference: Element = "<fin xmlns='urn:xmpp:mam:2'><set xmlns='http://jabber.org/protocol/rsm'><first index='0'>28482-98726-73623</first><last>09af3-cc343-b409f</last></set></fin>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'><first index='0'>28482-98726-73623</first><last>09af3-cc343-b409f</last></set>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let set = SetResult::try_from(elem).unwrap();
|
|
|
|
|
|
|
|
|
|
let fin = Fin {
|
2024-01-31 23:12:28 +00:00
|
|
|
|
set,
|
2019-12-30 05:55:32 +00:00
|
|
|
|
complete: Complete::default(),
|
2019-12-05 20:41:43 +00:00
|
|
|
|
};
|
|
|
|
|
let serialized: Element = fin.into();
|
|
|
|
|
assert_eq!(serialized, reference);
|
2019-11-28 17:00:40 +00:00
|
|
|
|
}
|
2017-04-29 05:07:00 +00:00
|
|
|
|
}
|