actpub-nodeinfo 0.2.3

NodeInfo 2.0/2.1 and FEP-0151 server metadata protocol for ActivityPub.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
//! `NodeInfo` schema types (2.0 / 2.1).
//!
//! See [http://nodeinfo.diaspora.software/](http://nodeinfo.diaspora.software/)
//! for the canonical JSON Schemas. All enum values are drawn directly from
//! the published schemas; unknown values round-trip losslessly through the
//! `Other(String)` variants so the crate can interoperate with forward-
//! compatible servers and third-party extensions.

use serde::{Deserialize, Serialize};
use url::Url;

/// The `NodeInfo` schema version this document conforms to.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Version {
    /// `NodeInfo` 2.0 — [schema](http://nodeinfo.diaspora.software/ns/schema/2.0).
    #[serde(rename = "2.0")]
    V2_0,

    /// `NodeInfo` 2.1 — [schema](http://nodeinfo.diaspora.software/ns/schema/2.1).
    #[serde(rename = "2.1")]
    V2_1,
}

impl Version {
    /// Returns the version as the lexical string used on the wire.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::V2_0 => "2.0",
            Self::V2_1 => "2.1",
        }
    }

    /// Returns the full schema URI this version corresponds to, as emitted
    /// in the `rel` field of the `/.well-known/nodeinfo` discovery document.
    #[must_use]
    pub const fn schema_uri(self) -> &'static str {
        match self {
            Self::V2_0 => "http://nodeinfo.diaspora.software/ns/schema/2.0",
            Self::V2_1 => "http://nodeinfo.diaspora.software/ns/schema/2.1",
        }
    }
}

/// A federation protocol supported by a NodeInfo-described server.
///
/// The ten named variants are the exact `protocols` enumeration of the
/// [NodeInfo 2.1 schema][schema]; unknown values (including community
/// extensions such as `matrix` or `bluesky` used by bridges) round-trip
/// losslessly through [`Self::Other`].
///
/// [schema]: http://nodeinfo.diaspora.software/ns/schema/2.1
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum Protocol {
    /// The W3C `ActivityPub` protocol.
    ActivityPub,
    /// Buddycloud federation.
    Buddycloud,
    /// Distributed Friendika Network.
    Dfrn,
    /// Diaspora.
    Diaspora,
    /// Libertree.
    Libertree,
    /// `OStatus` (legacy).
    OStatus,
    /// Pump.io.
    PumpIo,
    /// Tent.
    Tent,
    /// XMPP with pub-sub.
    Xmpp,
    /// Zot.
    Zot,
    /// An unrecognised protocol identifier, preserved verbatim.
    ///
    /// Common non-schema values seen in the wild include `matrix`,
    /// `bluesky`, `gnusocial`, and `nostr`. All are preserved through this
    /// fallback variant.
    #[serde(untagged)]
    Other(String),
}

/// An inbound bridge service defined in `NodeInfo`.
///
/// Enumeration matches the `inbound` values from the `services` definition
/// in the [NodeInfo 2.1 schema][schema].
///
/// [schema]: http://nodeinfo.diaspora.software/ns/schema/2.1
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum InboundService {
    /// Atom 1.0 feed.
    #[serde(rename = "atom1.0")]
    Atom1_0,
    /// GNU Social.
    GnuSocial,
    /// IMAP.
    Imap,
    /// Pnut.
    Pnut,
    /// POP3.
    Pop3,
    /// Pump.io.
    PumpIo,
    /// RSS 2.0 feed.
    #[serde(rename = "rss2.0")]
    Rss2_0,
    /// Twitter.
    Twitter,
    /// An unrecognised service identifier, preserved verbatim.
    #[serde(untagged)]
    Other(String),
}

/// An outbound bridge service defined in `NodeInfo`.
///
/// Enumeration matches the `outbound` values from the `services` definition
/// in the [NodeInfo 2.1 schema][schema].
///
/// [schema]: http://nodeinfo.diaspora.software/ns/schema/2.1
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum OutboundService {
    /// Atom 1.0 feed.
    #[serde(rename = "atom1.0")]
    Atom1_0,
    /// Blogger.
    Blogger,
    /// `BuddyCloud`.
    Buddycloud,
    /// Diaspora.
    Diaspora,
    /// Dreamwidth.
    Dreamwidth,
    /// Drupal.
    Drupal,
    /// Facebook.
    Facebook,
    /// Friendica.
    Friendica,
    /// GNU Social.
    GnuSocial,
    /// Google.
    Google,
    /// `InsaneJournal`.
    InsaneJournal,
    /// Libertree.
    Libertree,
    /// `LinkedIn`.
    LinkedIn,
    /// `LiveJournal`.
    LiveJournal,
    /// `MediaGoblin`.
    MediaGoblin,
    /// `MySpace`.
    MySpace,
    /// Pinterest.
    Pinterest,
    /// Pnut.
    Pnut,
    /// Posterous.
    Posterous,
    /// Pump.io.
    PumpIo,
    /// Redmatrix (legacy).
    RedMatrix,
    /// RSS 2.0 feed.
    #[serde(rename = "rss2.0")]
    Rss2_0,
    /// SMTP.
    Smtp,
    /// Tent.
    Tent,
    /// Tumblr.
    Tumblr,
    /// Twitter.
    Twitter,
    /// `WordPress`.
    WordPress,
    /// Xmpp.
    Xmpp,
    /// An unrecognised service identifier, preserved verbatim.
    #[serde(untagged)]
    Other(String),
}

/// Set of inbound/outbound bridge services.
///
/// Per the `NodeInfo` schema, both `inbound` and `outbound` are required
/// arrays (they may be empty).
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Services {
    /// Inbound services.
    #[serde(default)]
    pub inbound: Vec<InboundService>,

    /// Outbound services.
    #[serde(default)]
    pub outbound: Vec<OutboundService>,
}

impl Services {
    /// Constructs a [`Services`] block.
    #[must_use]
    pub const fn new(inbound: Vec<InboundService>, outbound: Vec<OutboundService>) -> Self {
        Self { inbound, outbound }
    }
}

/// Metadata about the software powering a server.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Software {
    /// Canonical software name (e.g. `mastodon`, `lemmy`, `mitra`).
    ///
    /// Per `NodeInfo` 2.0/2.1 schemas the name must match a strict regex
    /// (`^[a-z0-9-]+$`), but this crate preserves the raw string to
    /// interoperate with the relaxed FEP-0151 profile.
    pub name: String,

    /// Version string.
    pub version: String,

    /// Repository URL (`NodeInfo` 2.1 only).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub repository: Option<Url>,

    /// Homepage URL (`NodeInfo` 2.1 only).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub homepage: Option<Url>,
}

impl Software {
    /// Constructs a minimal [`Software`] description.
    pub fn new(name: impl Into<String>, version: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            version: version.into(),
            repository: None,
            homepage: None,
        }
    }

    /// Sets the repository URL and returns `self` for chaining.
    #[must_use]
    pub fn with_repository(mut self, repository: Url) -> Self {
        self.repository = Some(repository);
        self
    }

    /// Sets the homepage URL and returns `self` for chaining.
    #[must_use]
    pub fn with_homepage(mut self, homepage: Url) -> Self {
        self.homepage = Some(homepage);
        self
    }
}

/// Per-user activity counts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct UserCount {
    /// Total registered users.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub total: Option<u64>,

    /// Users active in the last 180 days.
    #[serde(
        rename = "activeHalfyear",
        default,
        skip_serializing_if = "Option::is_none"
    )]
    pub active_halfyear: Option<u64>,

    /// Users active in the last 30 days.
    #[serde(
        rename = "activeMonth",
        default,
        skip_serializing_if = "Option::is_none"
    )]
    pub active_month: Option<u64>,
}

impl UserCount {
    /// Constructs a [`UserCount`] from its components.
    #[must_use]
    pub const fn new(
        total: Option<u64>,
        active_halfyear: Option<u64>,
        active_month: Option<u64>,
    ) -> Self {
        Self {
            total,
            active_halfyear,
            active_month,
        }
    }

    /// Sets the total registered-user count and returns `self`.
    #[must_use]
    pub const fn with_total(mut self, total: u64) -> Self {
        self.total = Some(total);
        self
    }
}

/// Aggregate server usage statistics.
///
/// Per the `NodeInfo` schema the `users` field is required; the other fields
/// are optional but conventionally reported by large instances.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Usage {
    /// Per-user counts (required field; may contain all `None` values if
    /// the server chooses not to disclose them).
    #[serde(default)]
    pub users: UserCount,

    /// Total number of posts authored by local users.
    #[serde(
        rename = "localPosts",
        default,
        skip_serializing_if = "Option::is_none"
    )]
    pub local_posts: Option<u64>,

    /// Total number of comments authored by local users.
    #[serde(
        rename = "localComments",
        default,
        skip_serializing_if = "Option::is_none"
    )]
    pub local_comments: Option<u64>,
}

impl Usage {
    /// Constructs a [`Usage`] with only the required `users` field set.
    #[must_use]
    pub const fn new(users: UserCount) -> Self {
        Self {
            users,
            local_posts: None,
            local_comments: None,
        }
    }

    /// Sets the total number of posts authored by local users.
    #[must_use]
    pub const fn with_local_posts(mut self, posts: u64) -> Self {
        self.local_posts = Some(posts);
        self
    }

    /// Sets the total number of comments authored by local users.
    #[must_use]
    pub const fn with_local_comments(mut self, comments: u64) -> Self {
        self.local_comments = Some(comments);
        self
    }
}

/// A `NodeInfo` 2.0 / 2.1 document.
///
/// All seven top-level fields required by the `NodeInfo` 2.1 schema are
/// always present on the wire — empty arrays and empty `metadata` objects
/// are emitted rather than omitted, to keep the document strictly
/// conformant under both 2.0 and 2.1.
///
/// Fields that are specific to 2.1 (e.g. [`Software::repository`]) remain
/// `Option`-typed and are omitted when unset, so a document built with
/// [`Version::V2_0`] still validates under the 2.0 schema.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct NodeInfo {
    /// Schema version this document conforms to.
    pub version: Version,

    /// Software metadata.
    pub software: Software,

    /// Federation protocols implemented by this server.
    #[serde(default)]
    pub protocols: Vec<Protocol>,

    /// Bridge services (always emitted, even when both arrays are empty).
    #[serde(default)]
    pub services: Services,

    /// Whether the server accepts new registrations.
    #[serde(rename = "openRegistrations")]
    pub open_registrations: bool,

    /// Aggregate usage statistics.
    #[serde(default)]
    pub usage: Usage,

    /// Arbitrary software-specific metadata (always emitted, defaulting to
    /// an empty object per the schema).
    #[serde(default = "default_metadata")]
    pub metadata: serde_json::Value,
}

fn default_metadata() -> serde_json::Value {
    serde_json::Value::Object(serde_json::Map::new())
}

impl NodeInfo {
    /// Returns a new builder at the given schema version.
    #[must_use]
    pub fn builder(version: Version, software: Software) -> NodeInfoBuilder {
        NodeInfoBuilder {
            inner: Self {
                version,
                software,
                protocols: Vec::new(),
                services: Services::default(),
                open_registrations: false,
                usage: Usage::default(),
                metadata: default_metadata(),
            },
        }
    }
}

/// Builder for [`NodeInfo`] produced by [`NodeInfo::builder`].
#[derive(Debug)]
pub struct NodeInfoBuilder {
    inner: NodeInfo,
}

impl NodeInfoBuilder {
    /// Appends a supported protocol.
    #[must_use]
    pub fn protocol(mut self, p: Protocol) -> Self {
        self.inner.protocols.push(p);
        self
    }

    /// Replaces all protocols.
    #[must_use]
    pub fn protocols(mut self, ps: Vec<Protocol>) -> Self {
        self.inner.protocols = ps;
        self
    }

    /// Replaces the services block.
    #[must_use]
    pub fn services(mut self, services: Services) -> Self {
        self.inner.services = services;
        self
    }

    /// Sets the `openRegistrations` flag.
    #[must_use]
    pub const fn open_registrations(mut self, open: bool) -> Self {
        self.inner.open_registrations = open;
        self
    }

    /// Sets the usage block.
    #[must_use]
    pub const fn usage(mut self, usage: Usage) -> Self {
        self.inner.usage = usage;
        self
    }

    /// Attaches arbitrary metadata.
    #[must_use]
    pub fn metadata<V: Into<serde_json::Value>>(mut self, v: V) -> Self {
        self.inner.metadata = v.into();
        self
    }

    /// Attaches a single typed metadata entry, producing an object on the
    /// wire.
    #[must_use]
    pub fn metadata_entry(
        mut self,
        key: impl Into<String>,
        value: impl Into<serde_json::Value>,
    ) -> Self {
        let mut map = match self.inner.metadata {
            serde_json::Value::Object(m) => m,
            _ => serde_json::Map::new(),
        };
        map.insert(key.into(), value.into());
        self.inner.metadata = serde_json::Value::Object(map);
        self
    }

    /// Finalises the [`NodeInfo`].
    #[must_use]
    pub fn build(self) -> NodeInfo {
        self.inner
    }
}

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;
    use serde_json::json;

    use super::*;

    #[test]
    fn version_roundtrips() {
        let v = Version::V2_1;
        assert_eq!(v.as_str(), "2.1");
        assert_eq!(
            v.schema_uri(),
            "http://nodeinfo.diaspora.software/ns/schema/2.1"
        );
        let j = serde_json::to_value(v).unwrap();
        assert_eq!(j, json!("2.1"));
    }

    #[test]
    fn every_schema_protocol_roundtrips() {
        // Covers the exact 10-value `protocols` enumeration of the
        // NodeInfo 2.1 schema. A regression here means either a schema
        // drift (an enum variant was renamed/removed) or a serde rename
        // typo on our side.
        for (canonical, expected) in [
            ("activitypub", Protocol::ActivityPub),
            ("buddycloud", Protocol::Buddycloud),
            ("dfrn", Protocol::Dfrn),
            ("diaspora", Protocol::Diaspora),
            ("libertree", Protocol::Libertree),
            ("ostatus", Protocol::OStatus),
            ("pumpio", Protocol::PumpIo),
            ("tent", Protocol::Tent),
            ("xmpp", Protocol::Xmpp),
            ("zot", Protocol::Zot),
        ] {
            let p: Protocol =
                serde_json::from_value(json!(canonical)).expect("known value must deserialise");
            assert_eq!(
                p, expected,
                "{canonical} should deserialise to {expected:?}"
            );

            let back = serde_json::to_value(&p).expect("known value must serialise");
            assert_eq!(
                back,
                json!(canonical),
                "{expected:?} should serialise back to {canonical}",
            );
        }
    }

    #[test]
    fn protocol_preserves_unknown_variant() {
        // Matrix, Bluesky, Nostr and similar community extensions are not
        // part of the NodeInfo schema but appear in production documents;
        // they must be preserved verbatim through `Other(String)`.
        let p: Protocol =
            serde_json::from_value(json!("bluesky")).expect("unknown value must deserialise");
        assert_eq!(p, Protocol::Other("bluesky".to_owned()));

        let back = serde_json::to_value(&p).expect("Other variant must serialise");
        assert_eq!(back, json!("bluesky"));
    }

    #[test]
    fn outbound_service_mediagoblin_roundtrips() {
        let s: OutboundService = serde_json::from_value(json!("mediagoblin")).unwrap();
        assert_eq!(s, OutboundService::MediaGoblin);
        let back = serde_json::to_value(&s).unwrap();
        assert_eq!(back, json!("mediagoblin"));
    }

    #[test]
    fn mastodon_style_nodeinfo_roundtrips_verbatim() {
        let raw = json!({
            "version": "2.1",
            "software": {
                "name": "mastodon",
                "version": "4.5.0",
                "repository": "https://github.com/mastodon/mastodon",
                "homepage": "https://joinmastodon.org/"
            },
            "protocols": ["activitypub"],
            "services": {
                "inbound": [],
                "outbound": []
            },
            "openRegistrations": true,
            "usage": {
                "users": {
                    "total": 1234,
                    "activeHalfyear": 400,
                    "activeMonth": 50
                },
                "localPosts": 9999,
                "localComments": 8888
            },
            "metadata": {}
        });

        let info: NodeInfo = serde_json::from_value(raw.clone()).unwrap();
        assert_eq!(info.version, Version::V2_1);
        assert_eq!(info.software.name, "mastodon");
        assert_eq!(info.protocols, vec![Protocol::ActivityPub]);
        assert_eq!(info.usage.users.total, Some(1234));
        assert!(info.open_registrations);

        let back = serde_json::to_value(&info).unwrap();
        assert_eq!(back, raw, "roundtrip must preserve verbatim JSON");
    }

    #[test]
    fn builder_always_emits_required_fields() {
        let info = NodeInfo::builder(Version::V2_0, Software::new("test-server", "0.1.0"))
            .protocol(Protocol::ActivityPub)
            .open_registrations(false)
            .build();

        let v = serde_json::to_value(&info).unwrap();
        assert_eq!(v["version"], json!("2.0"));
        assert_eq!(v["protocols"], json!(["activitypub"]));
        assert_eq!(v["services"], json!({"inbound": [], "outbound": []}));
        assert_eq!(v["openRegistrations"], json!(false));
        assert_eq!(v["metadata"], json!({}));
        // `usage.users` must be present even when empty
        assert!(v["usage"].get("users").is_some());
    }

    #[test]
    fn metadata_entry_builds_object() {
        let info = NodeInfo::builder(Version::V2_1, Software::new("my-server", "1.0.0"))
            .metadata_entry("supports_feps", json!(["521a", "8b32"]))
            .build();

        assert_eq!(info.metadata["supports_feps"], json!(["521a", "8b32"]));
    }

    #[test]
    fn software_builder_sets_optional_fields() {
        let sw = Software::new("mastodon", "4.5.0")
            .with_repository("https://github.com/mastodon/mastodon".parse().unwrap())
            .with_homepage("https://joinmastodon.org/".parse().unwrap());

        let v = serde_json::to_value(&sw).unwrap();
        assert_eq!(
            v["repository"],
            json!("https://github.com/mastodon/mastodon")
        );
        assert_eq!(v["homepage"], json!("https://joinmastodon.org/"));
    }
}