2021-09-29 18:08:08 +00:00
|
|
|
|
// Copyright (c) 2021 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 crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
|
|
|
|
|
use crate::ns;
|
|
|
|
|
use jid::Jid;
|
|
|
|
|
use minidom::{Element, Node};
|
2024-06-21 14:27:43 +00:00
|
|
|
|
use xso::error::{Error, FromElementError};
|
2021-09-29 18:08:08 +00:00
|
|
|
|
|
|
|
|
|
generate_attribute!(
|
|
|
|
|
/// Notes the default archiving preference for the user.
|
|
|
|
|
DefaultPrefs, "default", {
|
|
|
|
|
/// The default is to always log messages in the archive.
|
|
|
|
|
Always => "always",
|
|
|
|
|
|
|
|
|
|
/// The default is to never log messages in the archive.
|
|
|
|
|
Never => "never",
|
|
|
|
|
|
|
|
|
|
/// The default is to log messages in the archive only for contacts
|
|
|
|
|
/// present in the user’s [roster](../roster/index.html).
|
|
|
|
|
Roster => "roster",
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/// Controls the archiving preferences of the user.
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct Prefs {
|
|
|
|
|
/// The default preference for JIDs in neither
|
|
|
|
|
/// [always](#structfield.always) or [never](#structfield.never) lists.
|
|
|
|
|
pub default_: DefaultPrefs,
|
|
|
|
|
|
|
|
|
|
/// The set of JIDs for which to always store messages in the archive.
|
|
|
|
|
pub always: Vec<Jid>,
|
|
|
|
|
|
|
|
|
|
/// The set of JIDs for which to never store messages in the archive.
|
|
|
|
|
pub never: Vec<Jid>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl IqGetPayload for Prefs {}
|
|
|
|
|
impl IqSetPayload for Prefs {}
|
|
|
|
|
impl IqResultPayload for Prefs {}
|
|
|
|
|
|
|
|
|
|
impl TryFrom<Element> for Prefs {
|
2024-06-21 14:27:43 +00:00
|
|
|
|
type Error = FromElementError;
|
2021-09-29 18:08:08 +00:00
|
|
|
|
|
2024-06-21 14:27:43 +00:00
|
|
|
|
fn try_from(elem: Element) -> Result<Prefs, FromElementError> {
|
2021-09-29 18:08:08 +00:00
|
|
|
|
check_self!(elem, "prefs", MAM);
|
|
|
|
|
check_no_unknown_attributes!(elem, "prefs", ["default"]);
|
|
|
|
|
let mut always = vec![];
|
|
|
|
|
let mut never = vec![];
|
|
|
|
|
for child in elem.children() {
|
|
|
|
|
if child.is("always", ns::MAM) {
|
|
|
|
|
for jid_elem in child.children() {
|
|
|
|
|
if !jid_elem.is("jid", ns::MAM) {
|
2024-06-21 14:27:43 +00:00
|
|
|
|
return Err(Error::Other("Invalid jid element in always.").into());
|
2021-09-29 18:08:08 +00:00
|
|
|
|
}
|
2024-06-21 14:27:43 +00:00
|
|
|
|
always.push(jid_elem.text().parse().map_err(Error::text_parse_error)?);
|
2021-09-29 18:08:08 +00:00
|
|
|
|
}
|
|
|
|
|
} else if child.is("never", ns::MAM) {
|
|
|
|
|
for jid_elem in child.children() {
|
|
|
|
|
if !jid_elem.is("jid", ns::MAM) {
|
2024-06-21 14:27:43 +00:00
|
|
|
|
return Err(Error::Other("Invalid jid element in never.").into());
|
2021-09-29 18:08:08 +00:00
|
|
|
|
}
|
2024-06-21 14:27:43 +00:00
|
|
|
|
never.push(jid_elem.text().parse().map_err(Error::text_parse_error)?);
|
2021-09-29 18:08:08 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2024-06-21 14:27:43 +00:00
|
|
|
|
return Err(Error::Other("Unknown child in prefs element.").into());
|
2021-09-29 18:08:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let default_ = get_attr!(elem, "default", Required);
|
|
|
|
|
Ok(Prefs {
|
|
|
|
|
default_,
|
|
|
|
|
always,
|
|
|
|
|
never,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn serialise_jid_list(name: &str, jids: Vec<Jid>) -> ::std::option::IntoIter<Node> {
|
|
|
|
|
if jids.is_empty() {
|
|
|
|
|
None.into_iter()
|
|
|
|
|
} else {
|
|
|
|
|
Some(
|
|
|
|
|
Element::builder(name, ns::MAM)
|
|
|
|
|
.append_all(
|
|
|
|
|
jids.into_iter()
|
2023-06-20 12:07:50 +00:00
|
|
|
|
.map(|jid| Element::builder("jid", ns::MAM).append(jid)),
|
2021-09-29 18:08:08 +00:00
|
|
|
|
)
|
|
|
|
|
.into(),
|
|
|
|
|
)
|
|
|
|
|
.into_iter()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<Prefs> for Element {
|
|
|
|
|
fn from(prefs: Prefs) -> Element {
|
|
|
|
|
Element::builder("prefs", ns::MAM)
|
|
|
|
|
.attr("default", prefs.default_)
|
|
|
|
|
.append_all(serialise_jid_list("always", prefs.always))
|
|
|
|
|
.append_all(serialise_jid_list("never", prefs.never))
|
|
|
|
|
.build()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
use jid::BareJid;
|
|
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
|
|
|
|
assert_size!(DefaultPrefs, 1);
|
|
|
|
|
assert_size!(Prefs, 28);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
|
|
|
|
assert_size!(DefaultPrefs, 1);
|
|
|
|
|
assert_size!(Prefs, 56);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_prefs_get() {
|
|
|
|
|
let elem: Element = "<prefs xmlns='urn:xmpp:mam:2' default='always'/>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
|
|
|
|
let prefs = Prefs::try_from(elem).unwrap();
|
|
|
|
|
assert!(prefs.always.is_empty());
|
|
|
|
|
assert!(prefs.never.is_empty());
|
|
|
|
|
|
2022-03-22 22:29:25 +00:00
|
|
|
|
let elem: Element = r#"<prefs xmlns='urn:xmpp:mam:2' default='roster'>
|
2021-09-29 18:08:08 +00:00
|
|
|
|
<always/>
|
|
|
|
|
<never/>
|
|
|
|
|
</prefs>
|
|
|
|
|
"#
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
|
|
|
|
let prefs = Prefs::try_from(elem).unwrap();
|
|
|
|
|
assert!(prefs.always.is_empty());
|
|
|
|
|
assert!(prefs.never.is_empty());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_prefs_result() {
|
2022-03-22 22:29:25 +00:00
|
|
|
|
let elem: Element = r#"<prefs xmlns='urn:xmpp:mam:2' default='roster'>
|
2021-09-29 18:08:08 +00:00
|
|
|
|
<always>
|
|
|
|
|
<jid>romeo@montague.lit</jid>
|
|
|
|
|
</always>
|
|
|
|
|
<never>
|
|
|
|
|
<jid>montague@montague.lit</jid>
|
|
|
|
|
</never>
|
|
|
|
|
</prefs>
|
|
|
|
|
"#
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
|
|
|
|
let prefs = Prefs::try_from(elem).unwrap();
|
2023-06-20 12:07:50 +00:00
|
|
|
|
assert_eq!(prefs.always, [BareJid::new("romeo@montague.lit").unwrap()]);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
prefs.never,
|
|
|
|
|
[BareJid::new("montague@montague.lit").unwrap()]
|
|
|
|
|
);
|
2021-09-29 18:08:08 +00:00
|
|
|
|
|
|
|
|
|
let elem2 = Element::from(prefs.clone());
|
|
|
|
|
println!("{:?}", elem2);
|
|
|
|
|
let prefs2 = Prefs::try_from(elem2).unwrap();
|
|
|
|
|
assert_eq!(prefs.default_, prefs2.default_);
|
|
|
|
|
assert_eq!(prefs.always, prefs2.always);
|
|
|
|
|
assert_eq!(prefs.never, prefs2.never);
|
|
|
|
|
}
|
|
|
|
|
}
|