Skip to main content

pubhubs/
hub.rs

1//! Information about hubs
2
3use crate::handle::{Handle, Handles};
4use crate::id::Id;
5use crate::servers::config::host_aliases::UrlPwa;
6
7/// Basic public details about hub, as provided by PubHubs Central.
8#[derive(serde::Serialize, serde::Deserialize, Debug, Eq, PartialEq, Clone)]
9pub struct BasicInfo<UrlT = url::Url> {
10    /// The handles for this hub, using in URLs and other places to be understood by both human and
11    /// machine. The first one is the one that's used by default.
12    /// **WARNING:**  Handles may be added, but should not be removed.
13    pub handles: Handles,
14
15    /// Human-readable short name for this hub
16    pub name: String,
17
18    /// Short description for this hub.  This is stored centrally to facilitate searching.
19    /// May be changed freely.
20    pub description: String,
21
22    /// Hub client API url, likely of the form `https://<some domain>/_synapse/client/`.
23    /// Must end with a `/`.  Can be changed freely.
24    pub url: UrlT,
25
26    /// Immutable and unique identifier
27    pub id: Id,
28}
29
30impl From<BasicInfo<UrlPwa>> for BasicInfo {
31    fn from(hi: BasicInfo<UrlPwa>) -> Self {
32        Self {
33            url: hi.url.as_ref().clone(),
34            name: hi.name,
35            description: hi.description,
36            id: hi.id,
37            handles: hi.handles,
38        }
39    }
40}
41
42impl crate::map::Handled for BasicInfo {
43    fn handles(&self) -> &[Handle] {
44        &self.handles
45    }
46
47    fn id(&self) -> &Id {
48        &self.id
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn basic_info_serde() {
58        assert_eq!(
59            serde_json::from_str::<BasicInfo>(
60                r#"{"handles": [], "name": "Hub 1", "url": "https://example.com", "description": "some hub",
61                "id": "bLAPDnkcYj8S5hZ8NuH9OFTWKzypLqSakexoRvlZ_aA"}"#,
62            )
63            .unwrap_err()
64            .to_string(),
65            "must have at least one handle at line 1 column 14",
66        );
67        assert_eq!(
68            serde_json::from_str::<BasicInfo>(
69                r#"{"handles": ["hub_1"], "name": "Hub 1", "url": "https://example.com", "description": "some hub", "id": "bLAPDnkcYj8S5hZ8NuH9OFTWKzypLqSakexoRvlZ_aA"}"#,
70            )
71            .unwrap(),
72            BasicInfo{
73                handles: vec!["hub_1".parse().unwrap()].into(),
74                name: "Hub 1".to_string(),
75                url: "https://example.com".parse().unwrap(),
76                description: "some hub".to_string(),
77                id: "bLAPDnkcYj8S5hZ8NuH9OFTWKzypLqSakexoRvlZ_aA".parse().unwrap(),
78            }
79        );
80    }
81}