spoolframe_protocol/federation/media.rs
1//! The media path (P8-04): what the client protocol carries for playing a
2//! paired server's item.
3//!
4//! The device decides how it plays a paired server's (SA's) item, not the
5//! user's home server (SB): it asks SB where SA is (`RemoteHints`), dials
6//! SA with a short budget, and, reached, asks SB for a token for itself
7//! (`RemoteGrant`). Not reached, it plays the item from SB (`StartPlayback`
8//! of the `fed:` id), which plays from SA in its own name and on to the
9//! device.
10//!
11//! SA minted the token for one of SB's users ([`super::TokenGranted`]).
12//! Whoever plays
13//! redeems it on SA's own `spoolframe/client/1`
14//! (`Request::RedeemPlayToken`): the device itself when it can reach SA
15//! (direct), or SB, which plays on to the device (proxy). Either way SA
16//! answers with a session that opens that one item's playback and its
17//! controls, and nothing else; `spoolframe/federation/1` carries no media.
18//!
19//! Experimental ([`crate::experimental::FEDERATION`]): used only by a
20//! client that names the feature, without a `PROTOCOL_VERSION` bump.
21
22use std::fmt;
23
24use serde::{Deserialize, Serialize};
25use spoolframe_types::MediaId;
26
27use super::TokenScope;
28use crate::SessionId;
29
30/// `RedeemPlayToken`: a play token, redeemed on the server that minted it.
31#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
32pub struct RedeemPlayToken {
33 pub token: String,
34}
35
36impl fmt::Debug for RedeemPlayToken {
37 // The token opens an item until it is used: keep it out of logs.
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 f.write_str("RedeemPlayToken { token: <redacted> }")
40 }
41}
42
43/// What a redeemed play token opened (`Reply::PlayTokenRedeemed`). The
44/// session's `Debug` keeps it out of logs.
45#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
46pub struct PlayTokenRedeemed {
47 /// The session to name in `StartPlayback` and the playback's
48 /// controls: bound to the endpoint that redeemed it, and kept while
49 /// it is used. It reaches nothing else.
50 pub session: SessionId,
51 /// The item it opens, by this server's own id.
52 pub item: MediaId,
53 pub scope: TokenScope,
54}
55
56/// Where a paired server is, as its pair keeps it (`RemoteHints`): what a
57/// device dials it at. SB's own record of SA, from the pairing.
58#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
59pub struct RemoteHints {
60 /// SA's [`crate::ServerId`].
61 pub server_id: String,
62 /// SA's endpoint id, hex: the key the device pins for the dial.
63 pub endpoint_id: String,
64 /// `ip:port` addresses SA was paired at.
65 #[serde(default)]
66 pub addrs: Vec<String>,
67 /// Relay URLs SA named.
68 #[serde(default)]
69 pub relays: Vec<String>,
70 /// Whether SB holds a connection to SA now. Offline, a grant is
71 /// refused at once (`OriginOffline`).
72 pub online: bool,
73}
74
75/// SA's token for the device (`RemoteGrant`): redeem it on SA's
76/// `client/1` (`RedeemPlayToken`) from the device that asked.
77#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
78pub struct RemoteGranted {
79 pub token: String,
80 /// The item, by SA's own id: what to name in `StartPlayback` on SA.
81 pub item: MediaId,
82 /// SA's endpoint id, hex.
83 pub endpoint_id: String,
84 /// When the token stops working, Unix seconds: minutes from now.
85 pub expires_at: i64,
86 /// For a download token (P8-07, `RemoteGrant` with a `download`): the
87 /// download's id on SA, what the device reads there
88 /// (`ResourceRequest::Download`) with the session the token opens.
89 #[serde(default, skip_serializing_if = "Option::is_none")]
90 pub download: Option<String>,
91}
92
93impl fmt::Debug for RemoteGranted {
94 // The token opens an item until it is used: keep it out of logs.
95 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96 f.debug_struct("RemoteGranted")
97 .field("token", &"<redacted>")
98 .field("item", &self.item)
99 .field("endpoint_id", &self.endpoint_id)
100 .field("expires_at", &self.expires_at)
101 .field("download", &self.download)
102 .finish()
103 }
104}