xmpp_parsers/
mam_prefs.rs

1// Copyright (c) 2021 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7use xso::{AsXml, FromXml};
8
9use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
10use crate::ns;
11use jid::Jid;
12
13generate_attribute!(
14    /// Notes the default archiving preference for the user.
15    DefaultPrefs, "default", {
16        /// The default is to always log messages in the archive.
17        Always => "always",
18
19        /// The default is to never log messages in the archive.
20        Never => "never",
21
22        /// The default is to log messages in the archive only for contacts
23        /// present in the user’s [roster](../roster/index.html).
24        Roster => "roster",
25    }
26);
27
28/// Controls the archiving preferences of the user.
29#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
30#[xml(namespace = ns::MAM, name = "prefs")]
31pub struct Prefs {
32    /// The default preference for JIDs in neither
33    /// [always](#structfield.always) or [never](#structfield.never) lists.
34    #[xml(attribute = "default")]
35    pub default_: DefaultPrefs,
36
37    /// The set of JIDs for which to always store messages in the archive.
38    #[xml(extract(default, fields(extract(n = .., name = "jid", fields(text(type_ = Jid))))))]
39    pub always: Vec<Jid>,
40
41    /// The set of JIDs for which to never store messages in the archive.
42    #[xml(extract(default, fields(extract(n = .., name = "jid", fields(text(type_ = Jid))))))]
43    pub never: Vec<Jid>,
44}
45
46impl IqGetPayload for Prefs {}
47impl IqSetPayload for Prefs {}
48impl IqResultPayload for Prefs {}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53    use jid::BareJid;
54    use minidom::Element;
55
56    #[cfg(target_pointer_width = "32")]
57    #[test]
58    fn test_size() {
59        assert_size!(DefaultPrefs, 1);
60        assert_size!(Prefs, 28);
61    }
62
63    #[cfg(target_pointer_width = "64")]
64    #[test]
65    fn test_size() {
66        assert_size!(DefaultPrefs, 1);
67        assert_size!(Prefs, 56);
68    }
69
70    #[test]
71    fn test_prefs_get() {
72        let elem: Element = "<prefs xmlns='urn:xmpp:mam:2' default='always'/>"
73            .parse()
74            .unwrap();
75        let prefs = Prefs::try_from(elem).unwrap();
76        assert!(prefs.always.is_empty());
77        assert!(prefs.never.is_empty());
78
79        let elem: Element = r#"<prefs xmlns='urn:xmpp:mam:2' default='roster'>
80  <always/>
81  <never/>
82</prefs>
83"#
84        .parse()
85        .unwrap();
86        let prefs = Prefs::try_from(elem).unwrap();
87        assert!(prefs.always.is_empty());
88        assert!(prefs.never.is_empty());
89    }
90
91    #[test]
92    fn test_prefs_result() {
93        let elem: Element = r#"<prefs xmlns='urn:xmpp:mam:2' default='roster'>
94  <always>
95    <jid>romeo@montague.lit</jid>
96  </always>
97  <never>
98    <jid>montague@montague.lit</jid>
99  </never>
100</prefs>
101"#
102        .parse()
103        .unwrap();
104        let prefs = Prefs::try_from(elem).unwrap();
105        assert_eq!(prefs.always, [BareJid::new("romeo@montague.lit").unwrap()]);
106        assert_eq!(
107            prefs.never,
108            [BareJid::new("montague@montague.lit").unwrap()]
109        );
110
111        let elem2 = Element::from(prefs.clone());
112        println!("{:?}", elem2);
113        let prefs2 = Prefs::try_from(elem2).unwrap();
114        assert_eq!(prefs.default_, prefs2.default_);
115        assert_eq!(prefs.always, prefs2.always);
116        assert_eq!(prefs.never, prefs2.never);
117    }
118}