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