Skip to main content

feder_core/
lib.rs

1// Feder: A portable ActivityPub core for many runtimes.
2// Copyright (C) 2026 Feder contributors
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU Affero General Public License as published by
6// the Free Software Foundation, version 3.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11// GNU Affero General Public License for more details.
12//
13// You should have received a copy of the GNU Affero General Public License
14// along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
16//! Portable ActivityPub core logic for Feder.
17#![no_std]
18
19extern crate alloc;
20
21use alloc::{string::String, vec::Vec};
22
23pub use feder_vocab as vocab;
24
25/// Portable core state and decision logic.
26#[derive(Debug)]
27pub struct FederCore {
28    state: FederState,
29}
30
31impl FederCore {
32    #[must_use]
33    pub fn new(config: FederConfig) -> Self {
34        Self {
35            state: FederState::new(config),
36        }
37    }
38
39    #[must_use]
40    pub fn state(&self) -> &FederState {
41        &self.state
42    }
43
44    /// Handle one core input and return runtime actions to perform later.
45    ///
46    /// This method intentionally performs no I/O. Returned actions describe
47    /// work for a runtime or test harness to perform later.
48    #[must_use]
49    pub fn handle(&mut self, input: Input) -> HandleResult {
50        match input {
51            Input::ReceivedFollow(input) => {
52                let actions = self.state.record_follow(input);
53                HandleResult::new(actions)
54            }
55            Input::UserCreateNote(input) => {
56                let actions = self.state.record_created_note(input);
57                HandleResult::new(actions)
58            }
59        }
60    }
61}
62
63/// Runtime-provided configuration for portable core state.
64#[derive(Clone, Debug, Eq, PartialEq)]
65pub struct FederConfig {
66    pub local_actor: vocab::Actor,
67}
68
69impl FederConfig {
70    #[must_use]
71    pub fn new(local_actor: vocab::Actor) -> Self {
72        Self { local_actor }
73    }
74}
75
76/// In-memory state used by portable core flows.
77#[derive(Clone, Debug, Eq, PartialEq)]
78pub struct FederState {
79    local_actor: vocab::Actor,
80    followers: Vec<Follower>,
81    delivery_targets: Vec<DeliveryTarget>,
82    objects: Vec<Object>,
83    activities: Vec<Activity>,
84}
85
86impl FederState {
87    #[must_use]
88    pub fn new(config: FederConfig) -> Self {
89        Self {
90            local_actor: config.local_actor,
91            followers: Vec::new(),
92            delivery_targets: Vec::new(),
93            objects: Vec::new(),
94            activities: Vec::new(),
95        }
96    }
97
98    #[must_use]
99    pub fn local_actor(&self) -> &vocab::Actor {
100        &self.local_actor
101    }
102
103    #[must_use]
104    pub fn followers(&self) -> &[Follower] {
105        &self.followers
106    }
107
108    #[must_use]
109    /// Delivery targets known from embedded actor data.
110    ///
111    /// ID-only followers are tracked in `followers`, but they do not produce a
112    /// delivery target until a runtime or later core flow resolves actor data.
113    pub fn delivery_targets(&self) -> &[DeliveryTarget] {
114        &self.delivery_targets
115    }
116
117    #[must_use]
118    pub fn objects(&self) -> &[Object] {
119        &self.objects
120    }
121
122    #[must_use]
123    pub fn activities(&self) -> &[Activity] {
124        &self.activities
125    }
126
127    fn record_follow(&mut self, input: ReceivedFollow) -> Vec<Action> {
128        let follow = input.follow;
129        let Some(following) = reference_id(&follow.object) else {
130            return Vec::new();
131        };
132
133        if following != &self.local_actor.id {
134            return Vec::new();
135        }
136
137        let Some(follower) = reference_id(&follow.actor).cloned() else {
138            return Vec::new();
139        };
140
141        let relation = Follower {
142            follower: follower.clone(),
143            following: following.clone(),
144        };
145        let mut actions = Vec::new();
146
147        if !self.followers.contains(&relation) {
148            self.followers.push(relation.clone());
149
150            actions.push(Action::StoreFollower(StoreFollower {
151                follower: follow.actor.clone(),
152                following: follow.object.clone(),
153            }));
154        }
155
156        let mut inbox = self
157            .delivery_targets
158            .iter()
159            .find(|target| target.actor == follower)
160            .map(|target| target.inbox.clone());
161
162        if let vocab::Reference::Object(actor) = &follow.actor {
163            let target = DeliveryTarget {
164                actor: follower,
165                inbox: actor.inbox.clone(),
166            };
167            let mut should_store_target = false;
168
169            if let Some(existing) = self
170                .delivery_targets
171                .iter_mut()
172                .find(|existing| existing.actor == target.actor)
173            {
174                if existing.inbox != target.inbox {
175                    existing.inbox = target.inbox.clone();
176                    should_store_target = true;
177                }
178            } else {
179                self.delivery_targets.push(target.clone());
180                should_store_target = true;
181            }
182
183            if should_store_target {
184                actions.push(Action::StoreDeliveryTarget(StoreDeliveryTarget { target }));
185            }
186
187            inbox = Some(actor.inbox.clone());
188        }
189
190        if let Some(inbox) = inbox {
191            let accept = vocab::Accept::new(
192                input.accept_id,
193                vocab::Reference::id(self.local_actor.id.clone()),
194                vocab::Reference::object(follow),
195            );
196
197            actions.push(Action::SendActivity(SendActivity {
198                activity: Activity::Accept(accept),
199                inbox,
200            }));
201        }
202
203        actions
204    }
205
206    fn record_created_note(&mut self, input: UserCreateNote) -> Vec<Action> {
207        let Some(actor) = reference_id(&input.actor) else {
208            return Vec::new();
209        };
210
211        if actor != &self.local_actor.id {
212            return Vec::new();
213        }
214
215        let actor = vocab::Reference::id(self.local_actor.id.clone());
216
217        let mut note = vocab::Note::new(input.note_id);
218        note.attributed_to = Some(actor.clone());
219        note.content = Some(input.content);
220        note.published = input.published;
221
222        let create = vocab::Create::new(
223            input.create_id,
224            actor,
225            vocab::Reference::object(note.clone()),
226        );
227
228        let object = Object::Note(note);
229        self.objects.push(object.clone());
230        self.activities.push(Activity::CreateNote(create.clone()));
231
232        let mut actions = Vec::from([Action::StoreObject(StoreObject { object })]);
233
234        actions.extend(self.delivery_targets.iter().map(|target| {
235            Action::SendActivity(SendActivity {
236                activity: Activity::CreateNote(create.clone()),
237                inbox: target.inbox.clone(),
238            })
239        }));
240
241        actions
242    }
243}
244
245fn reference_id<T>(reference: &vocab::Reference<T>) -> Option<&vocab::Iri>
246where
247    T: HasId,
248{
249    match reference {
250        vocab::Reference::Id(id) => Some(id),
251        vocab::Reference::Object(object) => Some(object.id()),
252    }
253}
254
255trait HasId {
256    fn id(&self) -> &vocab::Iri;
257}
258
259impl HasId for vocab::Actor {
260    fn id(&self) -> &vocab::Iri {
261        &self.id
262    }
263}
264
265/// Something entering the portable core from a runtime.
266#[derive(Clone, Debug, Eq, PartialEq)]
267#[non_exhaustive]
268pub enum Input {
269    ReceivedFollow(ReceivedFollow),
270    UserCreateNote(UserCreateNote),
271}
272
273/// Runtime-provided data for handling a received Follow.
274///
275/// The Accept activity ID is an input so the core does not depend on clocks,
276/// randomness, or platform-specific ID generation.
277#[derive(Clone, Debug, Eq, PartialEq)]
278pub struct ReceivedFollow {
279    pub follow: vocab::Follow,
280    pub accept_id: vocab::Iri,
281}
282
283/// Runtime-provided data for creating a local note.
284///
285/// IDs and timestamps are inputs so the core does not depend on clocks,
286/// randomness, or platform-specific ID generation.
287#[derive(Clone, Debug, Eq, PartialEq)]
288pub struct UserCreateNote {
289    pub note_id: vocab::Iri,
290    pub create_id: vocab::Iri,
291    pub actor: vocab::Reference<vocab::Actor>,
292    pub content: String,
293    pub published: Option<String>,
294}
295
296#[derive(Clone, Debug, Eq, PartialEq)]
297pub struct Follower {
298    pub follower: vocab::Iri,
299    pub following: vocab::Iri,
300}
301
302/// A known actor inbox for future delivery.
303///
304/// Core records this only when an incoming object embeds enough actor data to
305/// expose an inbox. It does not imply every follower has been resolved.
306#[derive(Clone, Debug, Eq, PartialEq)]
307pub struct DeliveryTarget {
308    pub actor: vocab::Iri,
309    pub inbox: vocab::Iri,
310}
311
312/// Something the runtime should perform after core handling.
313#[derive(Clone, Debug, Eq, PartialEq)]
314#[non_exhaustive]
315pub enum Action {
316    StoreFollower(StoreFollower),
317    StoreDeliveryTarget(StoreDeliveryTarget),
318    StoreObject(StoreObject),
319    SendActivity(SendActivity),
320}
321
322#[derive(Clone, Debug, Eq, PartialEq)]
323pub struct StoreFollower {
324    pub follower: vocab::Reference<vocab::Actor>,
325    pub following: vocab::Reference<vocab::Actor>,
326}
327
328#[derive(Clone, Debug, Eq, PartialEq)]
329pub struct StoreDeliveryTarget {
330    pub target: DeliveryTarget,
331}
332
333#[derive(Clone, Debug, Eq, PartialEq)]
334pub struct StoreObject {
335    pub object: Object,
336}
337
338#[derive(Clone, Debug, Eq, PartialEq)]
339pub struct SendActivity {
340    pub activity: Activity,
341    pub inbox: vocab::Iri,
342}
343
344#[derive(Clone, Debug, Eq, PartialEq)]
345#[non_exhaustive]
346pub enum Activity {
347    Accept(vocab::Accept),
348    CreateNote(vocab::Create<vocab::Note>),
349}
350
351#[derive(Clone, Debug, Eq, PartialEq)]
352#[non_exhaustive]
353pub enum Object {
354    Note(vocab::Note),
355}
356
357#[derive(Clone, Debug, Default, Eq, PartialEq)]
358pub struct HandleResult {
359    pub actions: Vec<Action>,
360}
361
362impl HandleResult {
363    #[must_use]
364    pub fn new(actions: Vec<Action>) -> Self {
365        Self { actions }
366    }
367
368    #[must_use]
369    pub fn is_empty(&self) -> bool {
370        self.actions.is_empty()
371    }
372}
373
374#[cfg(test)]
375mod tests {
376    use super::*;
377    use alloc::format;
378    use alloc::string::ToString;
379
380    fn iri(value: &str) -> vocab::Iri {
381        value.parse().expect("valid test IRI")
382    }
383
384    fn actor(id: &str) -> vocab::Actor {
385        vocab::Actor::person(
386            iri(id),
387            iri(&format!("{id}/inbox")),
388            iri(&format!("{id}/outbox")),
389        )
390    }
391
392    fn core() -> FederCore {
393        FederCore::new(FederConfig::new(actor("https://example.com/users/alice")))
394    }
395
396    fn received_follow(follow: vocab::Follow, id: &str) -> Input {
397        Input::ReceivedFollow(ReceivedFollow {
398            follow,
399            accept_id: iri(id),
400        })
401    }
402
403    #[test]
404    fn core_is_created_with_local_actor_state() {
405        let core = core();
406
407        assert_eq!(
408            core.state().local_actor().id,
409            iri("https://example.com/users/alice")
410        );
411        assert!(core.state().followers().is_empty());
412        assert!(core.state().delivery_targets().is_empty());
413        assert!(core.state().objects().is_empty());
414        assert!(core.state().activities().is_empty());
415    }
416
417    #[test]
418    fn received_follow_records_follower_and_emits_accept_actions() {
419        let mut core = core();
420        let follow = vocab::Follow::new(
421            iri("https://remote.example/activities/follow/1"),
422            vocab::Reference::object(actor("https://remote.example/users/bob")),
423            vocab::Reference::id(iri("https://example.com/users/alice")),
424        );
425
426        let result = core.handle(received_follow(
427            follow,
428            "https://example.com/activities/accept/1",
429        ));
430
431        assert_eq!(result.actions.len(), 3);
432        assert_eq!(
433            core.state().followers(),
434            &[Follower {
435                follower: iri("https://remote.example/users/bob"),
436                following: iri("https://example.com/users/alice"),
437            }]
438        );
439        assert_eq!(
440            core.state().delivery_targets(),
441            &[DeliveryTarget {
442                actor: iri("https://remote.example/users/bob"),
443                inbox: iri("https://remote.example/users/bob/inbox"),
444            }]
445        );
446        assert_eq!(
447            result.actions[0],
448            Action::StoreFollower(StoreFollower {
449                follower: vocab::Reference::object(actor("https://remote.example/users/bob")),
450                following: vocab::Reference::id(iri("https://example.com/users/alice")),
451            })
452        );
453        assert_eq!(
454            result.actions[1],
455            Action::StoreDeliveryTarget(StoreDeliveryTarget {
456                target: DeliveryTarget {
457                    actor: iri("https://remote.example/users/bob"),
458                    inbox: iri("https://remote.example/users/bob/inbox"),
459                },
460            })
461        );
462
463        let Action::SendActivity(send) = &result.actions[2] else {
464            panic!("expected SendActivity action");
465        };
466        assert_eq!(send.inbox, iri("https://remote.example/users/bob/inbox"));
467
468        let Activity::Accept(accept) = &send.activity else {
469            panic!("expected Accept activity");
470        };
471        assert_eq!(accept.id, iri("https://example.com/activities/accept/1"));
472        assert_eq!(
473            accept.actor,
474            vocab::Reference::id(iri("https://example.com/users/alice"))
475        );
476        let vocab::Reference::Object(accepted_follow) = &accept.object else {
477            panic!("expected embedded Follow object");
478        };
479        assert_eq!(
480            accepted_follow.id,
481            iri("https://remote.example/activities/follow/1")
482        );
483    }
484
485    #[test]
486    fn received_follow_updates_existing_delivery_target_by_actor() {
487        let mut core = core();
488        let first_follow = vocab::Follow::new(
489            iri("https://remote.example/activities/follow/1"),
490            vocab::Reference::object(actor("https://remote.example/users/bob")),
491            vocab::Reference::id(iri("https://example.com/users/alice")),
492        );
493
494        let mut updated_actor = actor("https://remote.example/users/bob");
495        updated_actor.inbox = iri("https://remote.example/inboxes/bob");
496        let second_follow = vocab::Follow::new(
497            iri("https://remote.example/activities/follow/2"),
498            vocab::Reference::object(updated_actor),
499            vocab::Reference::id(iri("https://example.com/users/alice")),
500        );
501
502        let first_result = core.handle(received_follow(
503            first_follow,
504            "https://example.com/activities/accept/1",
505        ));
506        let second_result = core.handle(received_follow(
507            second_follow,
508            "https://example.com/activities/accept/2",
509        ));
510
511        assert_eq!(first_result.actions.len(), 3);
512        assert_eq!(second_result.actions.len(), 2);
513        assert_eq!(
514            second_result.actions[0],
515            Action::StoreDeliveryTarget(StoreDeliveryTarget {
516                target: DeliveryTarget {
517                    actor: iri("https://remote.example/users/bob"),
518                    inbox: iri("https://remote.example/inboxes/bob"),
519                },
520            })
521        );
522
523        let Action::SendActivity(send) = &second_result.actions[1] else {
524            panic!("expected SendActivity action");
525        };
526        assert_eq!(send.inbox, iri("https://remote.example/inboxes/bob"));
527
528        let Activity::Accept(accept) = &send.activity else {
529            panic!("expected Accept activity");
530        };
531        assert_eq!(accept.id, iri("https://example.com/activities/accept/2"));
532
533        assert_eq!(
534            core.state().followers(),
535            &[Follower {
536                follower: iri("https://remote.example/users/bob"),
537                following: iri("https://example.com/users/alice"),
538            }]
539        );
540        assert_eq!(
541            core.state().delivery_targets(),
542            &[DeliveryTarget {
543                actor: iri("https://remote.example/users/bob"),
544                inbox: iri("https://remote.example/inboxes/bob"),
545            }]
546        );
547    }
548
549    #[test]
550    fn received_follow_with_actor_id_records_follower_without_delivery_target() {
551        let mut core = core();
552        let follow = vocab::Follow::new(
553            iri("https://remote.example/activities/follow/1"),
554            vocab::Reference::id(iri("https://remote.example/users/bob")),
555            vocab::Reference::id(iri("https://example.com/users/alice")),
556        );
557
558        let result = core.handle(received_follow(
559            follow,
560            "https://example.com/activities/accept/1",
561        ));
562
563        assert_eq!(
564            result.actions,
565            Vec::from([Action::StoreFollower(StoreFollower {
566                follower: vocab::Reference::id(iri("https://remote.example/users/bob")),
567                following: vocab::Reference::id(iri("https://example.com/users/alice")),
568            })])
569        );
570        assert_eq!(
571            core.state().followers(),
572            &[Follower {
573                follower: iri("https://remote.example/users/bob"),
574                following: iri("https://example.com/users/alice"),
575            }]
576        );
577        assert!(core.state().delivery_targets().is_empty());
578    }
579
580    #[test]
581    fn received_follow_for_other_actor_is_ignored() {
582        let mut core = core();
583        let follow = vocab::Follow::new(
584            iri("https://remote.example/activities/follow/1"),
585            vocab::Reference::object(actor("https://remote.example/users/bob")),
586            vocab::Reference::id(iri("https://example.com/users/other")),
587        );
588
589        let result = core.handle(received_follow(
590            follow,
591            "https://example.com/activities/accept/1",
592        ));
593
594        assert!(result.is_empty());
595        assert!(core.state().followers().is_empty());
596        assert!(core.state().delivery_targets().is_empty());
597    }
598
599    #[test]
600    fn user_create_note_records_created_object_and_emits_store_action() {
601        let input = UserCreateNote {
602            note_id: iri("https://example.com/notes/1"),
603            create_id: iri("https://example.com/activities/create/1"),
604            actor: vocab::Reference::id(iri("https://example.com/users/alice")),
605            content: "Hello from Feder.".to_string(),
606            published: Some("2026-06-10T00:00:00Z".to_string()),
607        };
608
609        let mut core = core();
610        let result = core.handle(Input::UserCreateNote(input));
611
612        assert_eq!(result.actions.len(), 1);
613        assert_eq!(core.state().objects().len(), 1);
614        assert_eq!(core.state().activities().len(), 1);
615
616        let Object::Note(note) = &core.state().objects()[0];
617        assert_eq!(note.id, iri("https://example.com/notes/1"));
618        assert_eq!(
619            note.attributed_to,
620            Some(vocab::Reference::id(iri("https://example.com/users/alice")))
621        );
622        assert_eq!(note.content, Some("Hello from Feder.".to_string()));
623        assert_eq!(note.published, Some("2026-06-10T00:00:00Z".to_string()));
624
625        match &core.state().activities()[0] {
626            Activity::CreateNote(create) => {
627                assert_eq!(create.id, iri("https://example.com/activities/create/1"));
628                assert_eq!(
629                    create.actor,
630                    vocab::Reference::id(iri("https://example.com/users/alice"))
631                );
632            }
633            Activity::Accept(_) => panic!("expected Create<Note> activity"),
634        }
635
636        assert_eq!(
637            result.actions[0],
638            Action::StoreObject(StoreObject {
639                object: Object::Note(note.clone()),
640            })
641        );
642    }
643
644    #[test]
645    fn user_create_note_emits_create_activity_for_known_delivery_targets() {
646        let mut core = core();
647        let follow = vocab::Follow::new(
648            iri("https://remote.example/activities/follow/1"),
649            vocab::Reference::object(actor("https://remote.example/users/bob")),
650            vocab::Reference::id(iri("https://example.com/users/alice")),
651        );
652        let _ = core.handle(received_follow(
653            follow,
654            "https://example.com/activities/accept/1",
655        ));
656
657        let input = UserCreateNote {
658            note_id: iri("https://example.com/notes/1"),
659            create_id: iri("https://example.com/activities/create/1"),
660            actor: vocab::Reference::id(iri("https://example.com/users/alice")),
661            content: "Hello from Feder.".to_string(),
662            published: Some("2026-06-10T00:00:00Z".to_string()),
663        };
664
665        let result = core.handle(Input::UserCreateNote(input));
666
667        assert_eq!(result.actions.len(), 2);
668        let Action::StoreObject(store) = &result.actions[0] else {
669            panic!("expected StoreObject action");
670        };
671        let Object::Note(note) = &store.object;
672        assert_eq!(note.id, iri("https://example.com/notes/1"));
673
674        let Action::SendActivity(send) = &result.actions[1] else {
675            panic!("expected SendActivity action");
676        };
677        assert_eq!(send.inbox, iri("https://remote.example/users/bob/inbox"));
678
679        let Activity::CreateNote(create) = &send.activity else {
680            panic!("expected Create<Note> activity");
681        };
682        assert_eq!(create.id, iri("https://example.com/activities/create/1"));
683        assert_eq!(
684            create.actor,
685            vocab::Reference::id(iri("https://example.com/users/alice"))
686        );
687        let vocab::Reference::Object(created_note) = &create.object else {
688            panic!("expected embedded Note object");
689        };
690        assert_eq!(created_note.id, iri("https://example.com/notes/1"));
691    }
692
693    #[test]
694    fn user_create_note_emits_create_activity_for_each_known_delivery_target() {
695        let mut core = core();
696        for (index, follower) in [
697            "https://remote.example/users/bob",
698            "https://another.example/users/carol",
699        ]
700        .into_iter()
701        .enumerate()
702        {
703            let follow = vocab::Follow::new(
704                iri(&format!("https://example.com/activities/follow/{index}")),
705                vocab::Reference::object(actor(follower)),
706                vocab::Reference::id(iri("https://example.com/users/alice")),
707            );
708            let _ = core.handle(received_follow(
709                follow,
710                &format!("https://example.com/activities/accept/{index}"),
711            ));
712        }
713
714        let input = UserCreateNote {
715            note_id: iri("https://example.com/notes/1"),
716            create_id: iri("https://example.com/activities/create/1"),
717            actor: vocab::Reference::id(iri("https://example.com/users/alice")),
718            content: "Hello from Feder.".to_string(),
719            published: None,
720        };
721
722        let result = core.handle(Input::UserCreateNote(input));
723
724        assert_eq!(result.actions.len(), 3);
725        assert!(matches!(result.actions[0], Action::StoreObject(_)));
726
727        let expected_inboxes = [
728            iri("https://remote.example/users/bob/inbox"),
729            iri("https://another.example/users/carol/inbox"),
730        ];
731
732        for (action, expected_inbox) in result.actions[1..].iter().zip(expected_inboxes) {
733            let Action::SendActivity(send) = action else {
734                panic!("expected SendActivity action");
735            };
736            assert_eq!(send.inbox, expected_inbox);
737            assert!(matches!(send.activity, Activity::CreateNote(_)));
738        }
739    }
740
741    #[test]
742    fn mocked_core_flow_accepts_follow_then_delivers_created_note() {
743        let mut core = core();
744        let follow = vocab::Follow::new(
745            iri("https://remote.example/activities/follow/1"),
746            vocab::Reference::object(actor("https://remote.example/users/bob")),
747            vocab::Reference::id(iri("https://example.com/users/alice")),
748        );
749
750        let follow_result = core.handle(received_follow(
751            follow,
752            "https://example.com/activities/accept/1",
753        ));
754
755        assert_eq!(follow_result.actions.len(), 3);
756        assert!(matches!(follow_result.actions[0], Action::StoreFollower(_)));
757        assert!(matches!(
758            follow_result.actions[1],
759            Action::StoreDeliveryTarget(_)
760        ));
761        let Action::SendActivity(accept_delivery) = &follow_result.actions[2] else {
762            panic!("expected Accept delivery action");
763        };
764        assert_eq!(
765            accept_delivery.inbox,
766            iri("https://remote.example/users/bob/inbox")
767        );
768        assert!(matches!(accept_delivery.activity, Activity::Accept(_)));
769
770        let create_result = core.handle(Input::UserCreateNote(UserCreateNote {
771            note_id: iri("https://example.com/notes/1"),
772            create_id: iri("https://example.com/activities/create/1"),
773            actor: vocab::Reference::id(iri("https://example.com/users/alice")),
774            content: "Hello from Feder.".to_string(),
775            published: Some("2026-06-10T00:00:00Z".to_string()),
776        }));
777
778        assert_eq!(create_result.actions.len(), 2);
779        assert!(matches!(create_result.actions[0], Action::StoreObject(_)));
780        let Action::SendActivity(create_delivery) = &create_result.actions[1] else {
781            panic!("expected Create<Note> delivery action");
782        };
783        assert_eq!(
784            create_delivery.inbox,
785            iri("https://remote.example/users/bob/inbox")
786        );
787        assert!(matches!(create_delivery.activity, Activity::CreateNote(_)));
788
789        assert_eq!(core.state().followers().len(), 1);
790        assert_eq!(core.state().delivery_targets().len(), 1);
791        assert_eq!(core.state().objects().len(), 1);
792        assert_eq!(core.state().activities().len(), 1);
793    }
794
795    #[test]
796    fn user_create_note_normalizes_embedded_local_actor_to_local_actor_id() {
797        let mut supplied_actor = actor("https://example.com/users/alice");
798        supplied_actor.inbox = iri("https://untrusted.example/inbox");
799
800        let input = UserCreateNote {
801            note_id: iri("https://example.com/notes/1"),
802            create_id: iri("https://example.com/activities/create/1"),
803            actor: vocab::Reference::object(supplied_actor),
804            content: "Hello from Feder.".to_string(),
805            published: None,
806        };
807
808        let mut core = core();
809        let result = core.handle(Input::UserCreateNote(input));
810
811        assert_eq!(result.actions.len(), 1);
812
813        let Object::Note(note) = &core.state().objects()[0];
814        assert_eq!(
815            note.attributed_to,
816            Some(vocab::Reference::id(iri("https://example.com/users/alice")))
817        );
818
819        let Activity::CreateNote(create) = &core.state().activities()[0] else {
820            panic!("expected Create<Note> activity");
821        };
822        assert_eq!(
823            create.actor,
824            vocab::Reference::id(iri("https://example.com/users/alice"))
825        );
826    }
827
828    #[test]
829    fn user_create_note_for_non_local_actor_is_ignored() {
830        let input = UserCreateNote {
831            note_id: iri("https://remote.example/notes/1"),
832            create_id: iri("https://remote.example/activities/create/1"),
833            actor: vocab::Reference::id(iri("https://remote.example/users/bob")),
834            content: "Hello from elsewhere.".to_string(),
835            published: Some("2026-06-10T00:00:00Z".to_string()),
836        };
837
838        let mut core = core();
839        let result = core.handle(Input::UserCreateNote(input));
840
841        assert!(result.is_empty());
842        assert!(core.state().objects().is_empty());
843        assert!(core.state().activities().is_empty());
844    }
845
846    #[test]
847    fn handle_result_wraps_action_lists() {
848        let result = HandleResult::new(Vec::from([Action::StoreFollower(StoreFollower {
849            follower: vocab::Reference::id(iri("https://remote.example/users/bob")),
850            following: vocab::Reference::id(iri("https://example.com/users/alice")),
851        })]));
852
853        assert_eq!(result.actions.len(), 1);
854    }
855}