//! What ipx servers and the directory send each other: listings, feed pages, snapshots and their results. use crate::config::Access; use serde::{Deserialize, Serialize}; /// A feed as the directory lists it. #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Listing { /// The first 64 bits of the SHA-256 of its normalised address (`store::feed_id`). pub id: String, pub url: String, pub title: Option, pub image: Option, /// Apple's category and the subcategory within it, from the feed's own iTunes category. pub category: Option, pub subcategory: Option, /// Any audio or video in its latest items. pub podcast: bool, /// Summed over every instance's latest snapshot. pub subscribers: i64, } /// One feed's page: what it says it is, and its latest items. #[derive(Debug, Clone, Serialize, Deserialize)] pub struct FeedPage { #[serde(flatten)] pub listing: Listing, /// The feed's own text, HTML as often as not, as it was sent: whoever shows it sanitizes it. pub description: Option, /// Why its last check failed, in words, when it did. pub failing: Option, pub items: Vec, } /// One of a feed's latest items: what it is, never its files. #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Item { pub guid: String, pub title: Option, pub link: Option, /// Unix seconds. pub published: Option, /// Seconds. pub duration: Option, /// As the feed sent it, unsanitized. pub description: Option, } /// One feed in a snapshot: its address, and how many people on the sending server subscribe. #[derive(Debug, Clone, Serialize, Deserialize)] pub struct SnapshotEntry { pub url: String, pub subscribers: i64, /// The category the sending server's admin gave a feed that names none itself: used only /// while the feed names none. #[serde(default, skip_serializing_if = "Option::is_none")] pub category: Option, } /// What became of a snapshot's entries. #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)] pub struct SnapshotResult { pub accepted: usize, /// Addresses that look like they carry a subscriber's own access. pub refused_private: usize, /// Feeds an admin removed from the directory. pub refused_blocked: usize, /// Not an http(s) address. pub refused_invalid: usize, } /// Who a token belongs to and what it may do: `/v1/me`'s answer. #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Me { pub name: String, pub access: Access, } /// A token whose snapshot the directory holds: how many feeds it gave, and how many people. #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Instance { pub name: String, pub feeds: i64, pub subscribers: i64, } /// A feed an admin removed from the directory, which snapshots may not bring back. #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Blocked { pub id: String, pub url: Option, pub title: Option, /// When it was blocked, Unix seconds. pub at: i64, } /// The most a snapshot may list: far more than one server's subscriptions, few enough that one /// request cannot hold the database for long. pub const MAX_SNAPSHOT: usize = 20_000; // The same in protobuf (#470): ipx reads and writes these through ipx-wire's structs, which // mirror the ones above field for field. use ipx_wire::pb::directory as pb; impl From for Listing { fn from(l: pb::Listing) -> Self { Listing { id: l.id, url: l.url, title: l.title, image: l.image, category: l.category, subcategory: l.subcategory, podcast: l.podcast, subscribers: l.subscribers, } } } impl From for Item { fn from(i: pb::Item) -> Self { Item { guid: i.guid, title: i.title, link: i.link, published: i.published, duration: i.duration, description: i.description, } } } impl From for FeedPage { fn from(p: pb::FeedPage) -> Self { FeedPage { listing: Listing { id: p.id, url: p.url, title: p.title, image: p.image, category: p.category, subcategory: p.subcategory, podcast: p.podcast, subscribers: p.subscribers, }, description: p.description, failing: p.failing, items: p.items.into_iter().map(Item::from).collect(), } } } impl TryFrom for Me { type Error = String; fn try_from(m: pb::Me) -> Result { let access = match m.access.as_str() { "ro" => Access::Ro, "rw" => Access::Rw, "rwa" => Access::Rwa, other => return Err(format!("an access level not known here: {other:?}")), }; Ok(Me { name: m.name, access }) } } impl From for Instance { fn from(i: pb::Instance) -> Self { Instance { name: i.name, feeds: i.feeds, subscribers: i.subscribers } } } impl From for Blocked { fn from(b: pb::Blocked) -> Self { Blocked { id: b.id, url: b.url, title: b.title, at: b.at } } } impl From for SnapshotResult { fn from(r: pb::SnapshotResult) -> Self { let n = |v: u64| usize::try_from(v).unwrap_or(usize::MAX); SnapshotResult { accepted: n(r.accepted), refused_private: n(r.refused_private), refused_blocked: n(r.refused_blocked), refused_invalid: n(r.refused_invalid), } } } impl From<&SnapshotEntry> for pb::SnapshotEntry { fn from(e: &SnapshotEntry) -> Self { pb::SnapshotEntry { url: e.url.clone(), subscribers: e.subscribers, category: e.category.clone() } } }