netget-tool 0.3.4

Network protocol stubbing toolkit: ~280 protocol servers and clients whose responses come from an LLM, a script, a static rule or a person.
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
//! One small ActivityPub instance, used by both roles: its local actors (each with an RSA
//! key), WebFinger and actor documents, followers and an outbox per actor, fetching and
//! caching remote actors, signed delivery, and verifying what arrives at an inbox. What the
//! actors *say* comes from the model; this is the federation machinery around it.
use super::sig;
use anyhow::{bail, ensure, Context, Result};
use rsa::RsaPrivateKey;
use serde_json::{json, Value};
use std::collections::{BTreeSet, HashMap, VecDeque};
use std::sync::Mutex;
use std::time::Duration;

pub const AS_CONTEXT: &str = "https://www.w3.org/ns/activitystreams";
pub const SECURITY_CONTEXT: &str = "https://w3id.org/security/v1";
pub const PUBLIC: &str = "https://www.w3.org/ns/activitystreams#Public";
/// A fetched document or an inbox POST is at most this long.
pub const MAX_DOCUMENT: usize = 1024 * 1024;
pub const HTTP_TIMEOUT: Duration = Duration::from_secs(10);
pub const MAX_FOLLOWERS: usize = 10_000;
pub const OUTBOX_KEPT: usize = 50;
pub const MAX_OBJECTS: usize = 1000;
pub const MAX_REMOTE_ACTORS: usize = 1000;
/// Inboxes one post is delivered to at most.
pub const MAX_DELIVERIES: usize = 100;

pub struct LocalActor {
    pub name: String,
    key: RsaPrivateKey,
    pem: String,
    pub followers: BTreeSet<String>,
    /// Remote actors this one follows: id → the Follow activity's id, and whether accepted.
    pub following: HashMap<String, (String, bool)>,
    pub outbox: VecDeque<Value>,
}

#[derive(Clone, Debug)]
pub struct Remote {
    pub id: String,
    pub inbox: String,
    pub shared_inbox: Option<String>,
    pub preferred_username: Option<String>,
    pub name: Option<String>,
    pub key_id: Option<String>,
    pub key_pem: Option<String>,
}

/// An activity that arrived at an inbox with a valid signature.
pub struct Inbound {
    /// The local actor whose inbox it was, or None for the shared inbox.
    pub to: Option<String>,
    pub signer: String,
    pub activity: Value,
}

pub struct Instance {
    /// `http://host:port`, no trailing slash.
    pub base: String,
    /// The host part of handles: `host:port`.
    pub host: String,
    pub actors: Mutex<HashMap<String, LocalActor>>,
    objects: Mutex<(HashMap<String, Value>, VecDeque<String>)>,
    remote: Mutex<HashMap<String, Remote>>,
    proxied: reqwest::Client,
    direct: reqwest::Client,
}

fn loopback(url: &str) -> bool {
    reqwest::Url::parse(url)
        .ok()
        .and_then(|u| u.host_str().map(str::to_string))
        .is_some_and(|h| {
            h == "localhost"
                || h.parse::<std::net::IpAddr>()
                    .is_ok_and(|ip| ip.is_loopback())
                || h.trim_matches(['[', ']'])
                    .parse::<std::net::IpAddr>()
                    .is_ok_and(|ip| ip.is_loopback())
        })
}

pub fn escape_html(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
}

/// HTML content as the model reads it: tags dropped, the common entities decoded.
pub fn plain_text(html: &str) -> String {
    let mut out = String::new();
    let mut in_tag = false;
    let mut tag = String::new();
    for c in html.chars() {
        match c {
            '<' => {
                in_tag = true;
                tag.clear();
            }
            '>' if in_tag => {
                in_tag = false;
                let t = tag.trim_start_matches('/').to_ascii_lowercase();
                if (t.starts_with("br") || t.starts_with('p'))
                    && !out.is_empty()
                    && !out.ends_with('\n')
                {
                    out.push('\n');
                }
            }
            c if in_tag => tag.push(c),
            c => out.push(c),
        }
    }
    out.replace("&lt;", "<")
        .replace("&gt;", ">")
        .replace("&quot;", "\"")
        .replace("&#39;", "'")
        .replace("&amp;", "&")
        .trim()
        .to_string()
}

impl Instance {
    pub fn new(base: &str, names: &[String]) -> Result<Self> {
        let base = base.trim_end_matches('/').to_string();
        let host = reqwest::Url::parse(&base)
            .ok()
            .and_then(|u| {
                u.host_str().map(|h| match u.port() {
                    Some(p) => format!("{h}:{p}"),
                    None => h.to_string(),
                })
            })
            .context("base_url must be an http(s) URL")?;
        let mut actors = HashMap::new();
        for name in names {
            ensure!(
                !name.is_empty()
                    && name.len() <= 64
                    && name
                        .chars()
                        .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-' || c == '.'),
                "actor name {name:?} must be letters, digits, '_', '-' or '.'"
            );
            let key = sig::generate_key()?;
            let pem = sig::public_pem(&key)?;
            actors.insert(
                name.clone(),
                LocalActor {
                    name: name.clone(),
                    key,
                    pem,
                    followers: BTreeSet::new(),
                    following: HashMap::new(),
                    outbox: VecDeque::new(),
                },
            );
        }
        let build = |proxy: bool| {
            let b = reqwest::Client::builder()
                .timeout(HTTP_TIMEOUT)
                .redirect(reqwest::redirect::Policy::limited(3))
                .user_agent(concat!(
                    "NetGet/",
                    env!("CARGO_PKG_VERSION"),
                    " (ActivityPub)"
                ));
            if proxy { b } else { b.no_proxy() }.build()
        };
        Ok(Self {
            base,
            host,
            actors: Mutex::new(actors),
            objects: Mutex::new(Default::default()),
            remote: Mutex::new(HashMap::new()),
            proxied: build(true)?,
            direct: build(false)?,
        })
    }

    fn http(&self, url: &str) -> &reqwest::Client {
        if loopback(url) {
            &self.direct
        } else {
            &self.proxied
        }
    }

    pub fn actor_url(&self, name: &str) -> String {
        format!("{}/users/{name}", self.base)
    }

    pub fn handle(&self, name: &str) -> String {
        format!("{name}@{}", self.host)
    }

    pub fn has_actor(&self, name: &str) -> bool {
        self.actors
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .contains_key(name)
    }

    pub fn first_actor(&self) -> Option<String> {
        self.actors
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .keys()
            .min()
            .cloned()
    }

    /// The local actor an `acct:` resource or actor URL names.
    pub fn local_name(&self, s: &str) -> Option<String> {
        let s = s.trim_start_matches("acct:").trim_start_matches('@');
        let name = if let Some(rest) = s.strip_prefix(&format!("{}/users/", self.base)) {
            rest.to_string()
        } else {
            let (user, host) = s.split_once('@')?;
            if !host.eq_ignore_ascii_case(&self.host) {
                return None;
            }
            user.to_string()
        };
        self.has_actor(&name).then_some(name)
    }

    pub fn webfinger(&self, resource: &str) -> Option<Value> {
        let name = self.local_name(resource)?;
        Some(json!({
            "subject": format!("acct:{}", self.handle(&name)),
            "aliases": [self.actor_url(&name)],
            "links": [
                {"rel": "self", "type": "application/activity+json", "href": self.actor_url(&name)},
                {"rel": "http://webfinger.net/rel/profile-page", "type": "text/html", "href": self.actor_url(&name)}
            ]
        }))
    }

    pub fn actor_document(&self, name: &str) -> Option<Value> {
        let actors = self.actors.lock().unwrap_or_else(|e| e.into_inner());
        let a = actors.get(name)?;
        let id = self.actor_url(name);
        Some(json!({
            "@context": [AS_CONTEXT, SECURITY_CONTEXT],
            "id": id,
            "type": "Person",
            "preferredUsername": name,
            "name": name,
            "summary": "<p>An actor served by NetGet</p>",
            "url": id,
            "inbox": format!("{id}/inbox"),
            "outbox": format!("{id}/outbox"),
            "followers": format!("{id}/followers"),
            "following": format!("{id}/following"),
            "endpoints": {"sharedInbox": format!("{}/inbox", self.base)},
            "manuallyApprovesFollowers": true,
            "publicKey": {"id": format!("{id}#main-key"), "owner": id, "publicKeyPem": a.pem}
        }))
    }

    pub fn collection(&self, name: &str, which: &str) -> Option<Value> {
        let actors = self.actors.lock().unwrap_or_else(|e| e.into_inner());
        let a = actors.get(name)?;
        let id = format!("{}/{which}", self.actor_url(name));
        let items: Vec<Value> = match which {
            "followers" => a.followers.iter().map(|f| json!(f)).collect(),
            "following" => a
                .following
                .iter()
                .filter(|(_, (_, ok))| *ok)
                .map(|(f, _)| json!(f))
                .collect(),
            "outbox" => a.outbox.iter().rev().cloned().collect(),
            _ => return None,
        };
        Some(
            json!({"@context": AS_CONTEXT, "id": id, "type": "OrderedCollection",
                    "totalItems": items.len(), "orderedItems": items}),
        )
    }

    pub fn object(&self, id: &str) -> Option<Value> {
        self.objects
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .0
            .get(id)
            .cloned()
    }

    fn remember_object(&self, id: &str, v: &Value) {
        let mut o = self.objects.lock().unwrap_or_else(|e| e.into_inner());
        if o.0.insert(id.to_string(), v.clone()).is_none() {
            o.1.push_back(id.to_string());
        }
        while o.1.len() > MAX_OBJECTS {
            if let Some(old) = o.1.pop_front() {
                o.0.remove(&old);
            }
        }
    }

    pub fn new_id(&self, kind: &str) -> String {
        let n: u64 = rand::random();
        format!("{}/{kind}/{n:016x}", self.base)
    }

    // -----------------------------------------------------------------------------------
    // Outgoing HTTP

    fn signed_headers(
        &self,
        from: &str,
        method: &str,
        url: &str,
        body: Option<&[u8]>,
    ) -> Result<Vec<(String, String)>> {
        let u = reqwest::Url::parse(url).context("not a URL")?;
        let host = match u.port() {
            Some(p) => format!("{}:{p}", u.host_str().unwrap_or_default()),
            None => u.host_str().unwrap_or_default().to_string(),
        };
        let path = match u.query() {
            Some(q) => format!("{}?{q}", u.path()),
            None => u.path().to_string(),
        };
        let mut values = HashMap::new();
        values.insert("host".to_string(), host.clone());
        values.insert("date".to_string(), sig::http_date());
        if let Some(b) = body {
            values.insert("digest".to_string(), sig::digest(b));
        }
        let key = {
            let actors = self.actors.lock().unwrap_or_else(|e| e.into_inner());
            actors.get(from).context("no such local actor")?.key.clone()
        };
        let signature = sig::sign(
            &key,
            &format!("{}#main-key", self.actor_url(from)),
            method,
            &path,
            &values,
        )?;
        let mut out: Vec<(String, String)> = values.into_iter().collect();
        out.push(("signature".into(), signature));
        Ok(out)
    }

    /// GET an ActivityStreams document, signed by `from` when given (for servers that
    /// require authorized fetch).
    pub async fn fetch(&self, url: &str, from: Option<&str>) -> Result<Value> {
        ensure!(
            url.starts_with("http://") || url.starts_with("https://"),
            "only http(s) URLs can be fetched"
        );
        let mut req = self
            .http(url)
            .get(url)
            .header("accept", "application/activity+json, application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\", application/jrd+json, application/json");
        if let Some(from) = from {
            for (k, v) in self.signed_headers(from, "get", url, None)? {
                if k != "host" {
                    req = req.header(k, v);
                }
            }
        }
        let resp = req.send().await.with_context(|| format!("GET {url}"))?;
        let status = resp.status();
        ensure!(
            resp.content_length().unwrap_or(0) as usize <= MAX_DOCUMENT,
            "{url} is larger than {MAX_DOCUMENT} bytes"
        );
        let mut body = Vec::new();
        let mut resp = resp;
        while let Some(chunk) = resp.chunk().await? {
            body.extend_from_slice(&chunk);
            ensure!(
                body.len() <= MAX_DOCUMENT,
                "{url} is larger than {MAX_DOCUMENT} bytes"
            );
        }
        ensure!(status.is_success(), "GET {url}: {status}");
        serde_json::from_slice(&body).with_context(|| format!("{url} is not JSON"))
    }

    /// A remote actor, fetched once and cached.
    pub async fn remote_actor(&self, id: &str, from: Option<&str>) -> Result<Remote> {
        if let Some(r) = self
            .remote
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .get(id)
        {
            return Ok(r.clone());
        }
        let doc = self.fetch(id, from).await?;
        let r = Remote {
            id: doc["id"].as_str().unwrap_or(id).to_string(),
            inbox: doc["inbox"]
                .as_str()
                .context("the actor has no inbox")?
                .to_string(),
            shared_inbox: doc["endpoints"]["sharedInbox"].as_str().map(str::to_string),
            preferred_username: doc["preferredUsername"].as_str().map(str::to_string),
            name: doc["name"].as_str().map(str::to_string),
            key_id: doc["publicKey"]["id"].as_str().map(str::to_string),
            key_pem: doc["publicKey"]["publicKeyPem"]
                .as_str()
                .map(str::to_string),
        };
        let mut cache = self.remote.lock().unwrap_or_else(|e| e.into_inner());
        if cache.len() >= MAX_REMOTE_ACTORS {
            cache.clear();
        }
        cache.insert(id.to_string(), r.clone());
        Ok(r)
    }

    /// An actor id from a handle (`user@host`, `@user@host`, `acct:…`) via WebFinger, or a URL.
    pub async fn resolve(&self, target: &str) -> Result<String> {
        if target.starts_with("http://") || target.starts_with("https://") {
            return Ok(target.to_string());
        }
        let acct = target.trim_start_matches("acct:").trim_start_matches('@');
        let (_, host) = acct.split_once('@').context("a handle is user@host")?;
        let scheme = if loopback(&format!("http://{host}/")) {
            "http"
        } else {
            "https"
        };
        let url = format!(
            "{scheme}://{host}/.well-known/webfinger?resource=acct:{}",
            urlencoding::encode(acct)
        );
        let jrd = self.fetch(&url, None).await?;
        jrd["links"]
            .as_array()
            .into_iter()
            .flatten()
            .find(|l| {
                l["rel"] == "self"
                    && l["type"]
                        .as_str()
                        .is_some_and(|t| t.contains("activity+json") || t.contains("ld+json"))
            })
            .and_then(|l| l["href"].as_str())
            .map(str::to_string)
            .with_context(|| format!("WebFinger for {acct} names no ActivityPub actor"))
    }

    /// POST an activity to an inbox, signed by `from`; the HTTP status.
    pub async fn deliver(&self, from: &str, inbox: &str, activity: &Value) -> Result<u16> {
        let body = serde_json::to_vec(activity)?;
        let mut req = self
            .http(inbox)
            .post(inbox)
            .header("content-type", "application/activity+json");
        for (k, v) in self.signed_headers(from, "post", inbox, Some(&body))? {
            if k != "host" {
                req = req.header(k, v);
            }
        }
        let resp = req
            .body(body)
            .send()
            .await
            .with_context(|| format!("POST {inbox}"))?;
        Ok(resp.status().as_u16())
    }

    // -----------------------------------------------------------------------------------
    // What actors do

    pub fn add_follower(&self, name: &str, follower: &str) -> bool {
        let mut actors = self.actors.lock().unwrap_or_else(|e| e.into_inner());
        match actors.get_mut(name) {
            Some(a) if a.followers.len() < MAX_FOLLOWERS || a.followers.contains(follower) => {
                a.followers.insert(follower.to_string());
                true
            }
            _ => false,
        }
    }

    pub fn remove_follower(&self, name: &str, follower: &str) {
        if let Some(a) = self
            .actors
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .get_mut(name)
        {
            a.followers.remove(follower);
        }
    }

    pub fn followers(&self, name: &str) -> Vec<String> {
        self.actors
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .get(name)
            .map(|a| a.followers.iter().cloned().collect())
            .unwrap_or_default()
    }

    /// Answer a Follow with Accept or Reject, delivered to the follower.
    pub async fn answer_follow(&self, name: &str, follow: &Value, accept: bool) -> Result<u16> {
        let follower = follow["actor"]
            .as_str()
            .context("the Follow has no actor")?
            .to_string();
        if accept {
            ensure!(
                self.add_follower(name, &follower),
                "{name} has too many followers"
            );
        }
        let activity = json!({
            "@context": AS_CONTEXT,
            "id": self.new_id(if accept { "accepts" } else { "rejects" }),
            "type": if accept { "Accept" } else { "Reject" },
            "actor": self.actor_url(name),
            "object": follow,
        });
        let remote = self.remote_actor(&follower, Some(name)).await?;
        self.deliver(name, &remote.inbox, &activity).await
    }

    /// Follow a remote actor; the Follow's id.
    pub async fn follow(&self, name: &str, target: &str) -> Result<(String, u16)> {
        let id = self.resolve(target).await?;
        let remote = self.remote_actor(&id, Some(name)).await?;
        let follow_id = self.new_id("follows");
        let activity = json!({"@context": AS_CONTEXT, "id": follow_id, "type": "Follow",
                              "actor": self.actor_url(name), "object": remote.id});
        if let Some(a) = self
            .actors
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .get_mut(name)
        {
            a.following
                .insert(remote.id.clone(), (follow_id.clone(), false));
        }
        let status = self.deliver(name, &remote.inbox, &activity).await?;
        Ok((remote.id, status))
    }

    pub async fn unfollow(&self, name: &str, target: &str) -> Result<(String, u16)> {
        let id = self.resolve(target).await?;
        let follow_id = self
            .actors
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .get_mut(name)
            .and_then(|a| a.following.remove(&id))
            .map(|(f, _)| f)
            .with_context(|| format!("{name} does not follow {id}"))?;
        let remote = self.remote_actor(&id, Some(name)).await?;
        let activity = json!({"@context": AS_CONTEXT, "id": self.new_id("undos"), "type": "Undo",
                              "actor": self.actor_url(name),
                              "object": {"id": follow_id, "type": "Follow", "actor": self.actor_url(name), "object": id}});
        Ok((id, self.deliver(name, &remote.inbox, &activity).await?))
    }

    /// Mark a Follow accepted (an Accept arrived for it).
    pub fn follow_answered(&self, name: &str, remote: &str, accepted: bool) {
        if let Some(a) = self
            .actors
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .get_mut(name)
        {
            if accepted {
                if let Some(f) = a.following.get_mut(remote) {
                    f.1 = true;
                }
            } else {
                a.following.remove(remote);
            }
        }
    }

    /// Publish a Note as `name`: to its followers and to `to` (actor ids or handles); the
    /// Create activity and each delivery's status.
    pub async fn post(
        &self,
        name: &str,
        content: &str,
        to: &[String],
        public: bool,
        in_reply_to: Option<&str>,
    ) -> Result<(Value, Vec<(String, Result<u16, String>)>)> {
        let me = self.actor_url(name);
        let mut recipients = Vec::new();
        for t in to {
            recipients.push(self.resolve(t).await?);
        }
        let mut audience: Vec<Value> = recipients.iter().map(|r| json!(r)).collect();
        if public {
            audience.insert(0, json!(PUBLIC));
        }
        let note_id = self.new_id("notes");
        let html = content
            .split('\n')
            .map(|l| format!("<p>{}</p>", escape_html(l)))
            .collect::<String>();
        let mut note = json!({
            "id": note_id, "type": "Note", "attributedTo": me, "content": html,
            "published": chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true),
            "to": audience, "cc": [format!("{me}/followers")],
        });
        if let Some(r) = in_reply_to {
            note["inReplyTo"] = json!(r);
        }
        let create = json!({
            "@context": AS_CONTEXT, "id": format!("{note_id}/activity"), "type": "Create",
            "actor": me, "published": note["published"], "to": note["to"], "cc": note["cc"],
            "object": note,
        });
        self.remember_object(&note_id, &{
            let mut n = create["object"].clone();
            n["@context"] = json!(AS_CONTEXT);
            n
        });
        if let Some(a) = self
            .actors
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .get_mut(name)
        {
            a.outbox.push_back(create.clone());
            while a.outbox.len() > OUTBOX_KEPT {
                a.outbox.pop_front();
            }
        }
        let mut targets: Vec<String> = self.followers(name);
        targets.extend(recipients);
        targets.sort();
        targets.dedup();
        let mut inboxes = Vec::new();
        for t in targets.into_iter().take(MAX_DELIVERIES) {
            match self.remote_actor(&t, Some(name)).await {
                Ok(r) => inboxes.push(r.shared_inbox.unwrap_or(r.inbox)),
                Err(e) => inboxes.push(format!("!{t}: {e:#}")),
            }
        }
        inboxes.sort();
        inboxes.dedup();
        let mut results = Vec::new();
        for inbox in inboxes {
            if let Some(err) = inbox.strip_prefix('!') {
                results.push((err.to_string(), Err("actor not reachable".to_string())));
                continue;
            }
            let r = self
                .deliver(name, &inbox, &create)
                .await
                .map_err(|e| format!("{e:#}"));
            results.push((inbox, r));
        }
        Ok((create, results))
    }

    /// Like an object, delivered to its author (or `to`).
    pub async fn like(&self, name: &str, object: &str, to: &str) -> Result<u16> {
        let target = self.resolve(to).await?;
        let remote = self.remote_actor(&target, Some(name)).await?;
        let activity = json!({"@context": AS_CONTEXT, "id": self.new_id("likes"), "type": "Like",
                              "actor": self.actor_url(name), "object": object});
        self.deliver(name, &remote.inbox, &activity).await
    }

    // -----------------------------------------------------------------------------------
    // Incoming

    /// Verify a POST to an inbox: the signature (with the signer's key, fetched), the
    /// digest, the date, and that the activity's actor is the signer.
    pub async fn verify_inbound(
        &self,
        to: Option<String>,
        path: &str,
        headers: &HashMap<String, String>,
        body: &[u8],
    ) -> Result<Inbound> {
        let header = headers
            .get("signature")
            .context("unsigned: the inbox needs an HTTP Signature")?;
        let parsed = sig::parse(header)?;
        let owner_hint = parsed
            .key_id
            .split('#')
            .next()
            .unwrap_or_default()
            .to_string();
        let (pem, owner) = self
            .key_of(&parsed.key_id, &owner_hint, to.as_deref())
            .await?;
        sig::verify(&parsed, &pem, "post", path, headers, Some(body))?;
        let activity: Value = serde_json::from_slice(body).context("the body is not JSON")?;
        let actor = activity["actor"]
            .as_str()
            .or_else(|| activity["actor"]["id"].as_str())
            .context("the activity has no actor")?;
        if actor != owner {
            bail!("signed by {owner} but the activity's actor is {actor}");
        }
        Ok(Inbound {
            to,
            signer: owner,
            activity,
        })
    }

    /// A key's PEM and owner: the actor document with that key, or a key document.
    async fn key_of(
        &self,
        key_id: &str,
        owner_hint: &str,
        from: Option<&str>,
    ) -> Result<(String, String)> {
        if let Ok(r) = self.remote_actor(owner_hint, from).await {
            if r.key_id.as_deref() == Some(key_id) {
                if let Some(pem) = r.key_pem {
                    return Ok((pem, r.id));
                }
            }
        }
        let doc = self.fetch(key_id, from).await?;
        let key = if doc["publicKeyPem"].is_string() {
            &doc
        } else {
            &doc["publicKey"]
        };
        let pem = key["publicKeyPem"]
            .as_str()
            .context("the key document has no publicKeyPem")?;
        let owner = key["owner"]
            .as_str()
            .or_else(|| doc["id"].as_str())
            .context("the key has no owner")?;
        Ok((pem.to_string(), owner.to_string()))
    }
}

/// What the model is told about an activity.
pub fn summary(a: &Value) -> Value {
    let kind = a["type"].as_str().unwrap_or("?");
    let mut s = json!({"type": kind, "id": a["id"], "actor": a["actor"]});
    let obj = &a["object"];
    match kind {
        "Create" | "Update" => {
            s["object_type"] = obj["type"].clone();
            s["object_id"] = obj["id"].clone();
            if let Some(c) = obj["content"].as_str() {
                s["content"] = json!(plain_text(c));
            }
            s["in_reply_to"] = obj["inReplyTo"].clone();
            s["to"] = obj["to"].clone();
        }
        "Undo" | "Accept" | "Reject" => {
            s["object_type"] = obj["type"].clone();
            s["object_id"] = if obj.is_string() {
                obj.clone()
            } else {
                obj["id"].clone()
            };
        }
        _ => {
            s["object_id"] = if obj.is_string() {
                obj.clone()
            } else {
                obj["id"].clone()
            };
        }
    }
    s
}