ipx-wire converted every protobuf answer at run time through prost-reflect and serde_json::Value, so protobuf cost more than the JSON it replaced: decoding 1,488 feeds took 1.50 ms that way against 0.37 ms for serde_json and 0.09 ms for prost's own structs, and GET /api/feeds as protobuf answered about 4 ms after JSON. build.rs now generates those structs (prost-build from protox's descriptors, still no protoc). An answer's JSON goes straight into its struct with serde and out with prost, byte for byte what the runtime path wrote (a test says so); bodies holding a google.protobuf.Value keep the runtime path, as they are only ever read. ipx's calls to the directory and ipx-play build and read the structs directly, through From conversions beside the directory's own types. The page asks for protobuf too, to see what it does in practice. It reads it with web/src/wire.ts, about seventy lines and no library: protobufjs alone was larger gzipped than all protobuf saves the page. build.mts reads the .proto files into a table for it, and the answer names its message in its Content-Type, so the page keeps no table of routes. An answer of null stays JSON, since the empty message would be a feed row of defaults. The offline worker decodes a kept answer to mark what was read offline, and hands the page JSON. The decoder's test caught `pos += varint()[0]`, which adds the length to pos as it was before the varint was read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
195 lines
6.0 KiB
Rust
195 lines
6.0 KiB
Rust
//! 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<String>,
|
|
pub image: Option<String>,
|
|
/// Apple's category and the subcategory within it, from the feed's own iTunes category.
|
|
pub category: Option<String>,
|
|
pub subcategory: Option<String>,
|
|
/// 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<String>,
|
|
/// Why its last check failed, in words, when it did.
|
|
pub failing: Option<String>,
|
|
pub items: Vec<Item>,
|
|
}
|
|
|
|
/// 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<String>,
|
|
pub link: Option<String>,
|
|
/// Unix seconds.
|
|
pub published: Option<i64>,
|
|
/// Seconds.
|
|
pub duration: Option<i64>,
|
|
/// As the feed sent it, unsanitized.
|
|
pub description: Option<String>,
|
|
}
|
|
|
|
/// 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<String>,
|
|
}
|
|
|
|
/// 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<String>,
|
|
pub title: Option<String>,
|
|
/// 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<pb::Listing> 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<pb::Item> 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<pb::FeedPage> 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<pb::Me> for Me {
|
|
type Error = String;
|
|
fn try_from(m: pb::Me) -> Result<Self, String> {
|
|
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<pb::Instance> for Instance {
|
|
fn from(i: pb::Instance) -> Self {
|
|
Instance { name: i.name, feeds: i.feeds, subscribers: i.subscribers }
|
|
}
|
|
}
|
|
|
|
impl From<pb::Blocked> for Blocked {
|
|
fn from(b: pb::Blocked) -> Self {
|
|
Blocked { id: b.id, url: b.url, title: b.title, at: b.at }
|
|
}
|
|
}
|
|
|
|
impl From<pb::SnapshotResult> 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() }
|
|
}
|
|
}
|