2018-05-29 15:04:16 +02:00
// Copyright (c) 2018 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/.
2023-12-16 13:02:32 +01:00
//!
2023-12-16 15:23:20 +01:00
//! Chatroom bookmarks from [XEP-0048](https://xmpp.org/extensions/attic/xep-0048-1.0.html). You should never use this, but use
//! [`bookmarks2`][`crate::bookmarks2`], or [`private::Query`][`crate::private::Query`] for legacy servers which do not advertise
//! `urn:xmpp:bookmarks:1#compat` on the user's BareJID in a disco info request.
2023-12-16 13:02:32 +01:00
//!
//! See [ModernXMPP docs](https://docs.modernxmpp.org/client/groupchat/#bookmarks) on how to handle all historic
//! and newer specifications for your clients.
//!
2023-12-16 15:23:20 +01:00
//! The [`Conference`][crate::bookmarks::Conference] struct used in [`private::Query`][`crate::private::Query`] is the one from this module. Only the querying mechanism changes from a legacy PubSub implementation here, to a legacy Private XML Query implementation in that other module. The [`Conference`][crate::bookmarks2::Conference] element from the [`bookmarks2`][crate::bookmarks2] module is a different structure, but conversion is possible from [`bookmarks::Conference`][crate::bookmarks::Conference] to [`bookmarks2::Conference`][crate::bookmarks2::Conference] via the [`Conference::into_bookmarks2`][crate::bookmarks::Conference::into_bookmarks2] method.
2023-12-16 13:02:32 +01:00
2024-07-09 17:01:42 +02:00
use xso ::{ AsXml , FromXml } ;
2024-06-29 16:34:27 +02:00
2019-06-10 23:17:09 +02:00
use jid ::BareJid ;
2018-05-29 15:04:16 +02:00
2024-08-03 20:02:06 +02:00
pub use crate ::bookmarks2 ;
2024-06-29 16:34:27 +02:00
use crate ::ns ;
2018-05-29 15:04:16 +02:00
2024-08-04 17:42:01 +02:00
/// A conference bookmark.
#[ derive(FromXml, AsXml, PartialEq, Debug, Clone) ]
#[ xml(namespace = ns::BOOKMARKS, name = " conference " ) ]
pub struct Conference {
/// Whether a conference bookmark should be joined automatically.
#[ xml(attribute(default)) ]
2024-08-03 20:02:06 +02:00
pub autojoin : bool ,
2024-08-04 17:42:01 +02:00
/// The JID of the conference.
#[ xml(attribute) ]
pub jid : BareJid ,
/// A user-defined name for this conference.
#[ xml(attribute(default)) ]
pub name : Option < String > ,
/// The nick the user will use to join this conference.
#[ xml(extract(default, fields(text(type_ = String)))) ]
pub nick : Option < String > ,
/// The password required to join this conference.
#[ xml(extract(default, fields(text(type_ = String)))) ]
pub password : Option < String > ,
}
2018-05-29 15:04:16 +02:00
2023-12-16 13:02:32 +01:00
impl Conference {
/// Turns a XEP-0048 Conference element into a XEP-0402 "Bookmarks2" Conference element, in a
/// tuple with the room JID.
2024-08-05 17:42:23 +02:00
pub fn into_bookmarks2 ( self ) -> ( BareJid , bookmarks2 ::Conference ) {
2023-12-16 13:02:32 +01:00
(
self . jid ,
2024-08-05 17:42:23 +02:00
bookmarks2 ::Conference {
2023-12-16 13:02:32 +01:00
autojoin : self . autojoin ,
name : self . name ,
nick : self . nick ,
password : self . password ,
2024-08-05 17:42:23 +02:00
extensions : None ,
2023-12-16 13:02:32 +01:00
} ,
)
}
}
2024-06-29 16:34:27 +02:00
/// An URL bookmark.
2024-07-09 17:01:42 +02:00
#[ derive(FromXml, AsXml, PartialEq, Debug, Clone) ]
2024-06-29 16:34:27 +02:00
#[ xml(namespace = ns::BOOKMARKS, name = " url " ) ]
pub struct Url {
/// A user-defined name for this URL.
#[ xml(attribute(default)) ]
pub name : Option < String > ,
2018-05-29 15:04:16 +02:00
2024-06-29 16:34:27 +02:00
/// The URL of this bookmark.
#[ xml(attribute) ]
pub url : String ,
}
2018-05-29 15:04:16 +02:00
2024-08-03 14:41:33 +02:00
/// Container element for multiple bookmarks.
#[ derive(FromXml, AsXml, PartialEq, Debug, Clone, Default) ]
#[ xml(namespace = ns::BOOKMARKS, name = " storage " ) ]
pub struct Storage {
/// Conferences the user has expressed an interest in.
#[ xml(child(n = ..)) ]
pub conferences : Vec < Conference > ,
/// URLs the user is interested in.
#[ xml(child(n = ..)) ]
pub urls : Vec < Url > ,
}
2018-05-29 15:04:16 +02:00
impl Storage {
/// Create an empty bookmarks storage.
pub fn new ( ) -> Storage {
2019-02-21 21:00:58 +01:00
Storage ::default ( )
2018-05-29 15:04:16 +02:00
}
}
#[ cfg(test) ]
mod tests {
use super ::* ;
2024-07-24 20:28:22 +02:00
use minidom ::Element ;
2018-05-29 15:04:16 +02:00
2018-10-28 13:10:48 +01:00
#[ cfg(target_pointer_width = " 32 " ) ]
#[ test ]
fn test_size ( ) {
2023-06-20 14:14:15 +02:00
assert_size! ( Conference , 56 ) ;
2018-10-28 13:10:48 +01:00
assert_size! ( Url , 24 ) ;
assert_size! ( Storage , 24 ) ;
}
#[ cfg(target_pointer_width = " 64 " ) ]
2018-10-26 14:26:16 +02:00
#[ test ]
fn test_size ( ) {
2023-06-20 14:08:58 +02:00
assert_size! ( Conference , 112 ) ;
2018-10-26 14:26:16 +02:00
assert_size! ( Url , 48 ) ;
assert_size! ( Storage , 48 ) ;
}
2018-05-29 15:04:16 +02:00
#[ test ]
fn empty ( ) {
let elem : Element = " <storage xmlns='storage:bookmarks'/> " . parse ( ) . unwrap ( ) ;
let elem1 = elem . clone ( ) ;
let storage = Storage ::try_from ( elem ) . unwrap ( ) ;
assert_eq! ( storage . conferences . len ( ) , 0 ) ;
assert_eq! ( storage . urls . len ( ) , 0 ) ;
let elem2 = Element ::from ( Storage ::new ( ) ) ;
2019-11-29 14:33:17 +01:00
assert_eq! ( elem1 , elem2 ) ;
2018-05-29 15:04:16 +02:00
}
2018-07-31 22:35:02 +02:00
#[ test ]
fn complete ( ) {
let elem : Element = " <storage xmlns='storage:bookmarks'><url name='Example' url='https://example.org/'/><conference autojoin='true' jid='test-muc@muc.localhost' name='Test MUC'><nick>Coucou</nick><password>secret</password></conference></storage> " . parse ( ) . unwrap ( ) ;
let storage = Storage ::try_from ( elem ) . unwrap ( ) ;
assert_eq! ( storage . urls . len ( ) , 1 ) ;
2020-10-16 22:02:14 +02:00
assert_eq! ( storage . urls [ 0 ] . clone ( ) . name . unwrap ( ) , " Example " ) ;
2018-07-31 22:35:02 +02:00
assert_eq! ( storage . urls [ 0 ] . url , " https://example.org/ " ) ;
assert_eq! ( storage . conferences . len ( ) , 1 ) ;
2024-08-03 20:02:06 +02:00
assert_eq! ( storage . conferences [ 0 ] . autojoin , true ) ;
2018-12-18 15:32:05 +01:00
assert_eq! (
storage . conferences [ 0 ] . jid ,
2023-06-20 14:07:50 +02:00
BareJid ::new ( " test-muc@muc.localhost " ) . unwrap ( )
2018-12-18 15:32:05 +01:00
) ;
2020-10-16 22:02:14 +02:00
assert_eq! ( storage . conferences [ 0 ] . clone ( ) . name . unwrap ( ) , " Test MUC " ) ;
2018-07-31 22:35:02 +02:00
assert_eq! ( storage . conferences [ 0 ] . clone ( ) . nick . unwrap ( ) , " Coucou " ) ;
assert_eq! ( storage . conferences [ 0 ] . clone ( ) . password . unwrap ( ) , " secret " ) ;
}
2018-05-29 15:04:16 +02:00
}