Skip to main content

feder_core/
note.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
16use alloc::{string::String, vec::Vec};
17
18use feder_vocab::{Actor, Create, Iri, Note, Reference, References};
19
20pub const PUBLIC_COLLECTION: &str = "https://www.w3.org/ns/activitystreams#Public";
21
22/// A transient delivery intent derived from a Note's addressing fields.
23#[derive(Clone, Debug, Eq, PartialEq)]
24pub enum NoteRecipient {
25    Followers(Iri),
26    Actor(Iri),
27}
28
29/// Runtime-provided facts for constructing one local Note and Create activity.
30///
31/// IDs and timestamps are inputs so core does not depend on clocks, randomness,
32/// or an operating-system-specific identifier source.
33#[derive(Clone, Debug, Eq, PartialEq)]
34pub struct CreateNoteInput {
35    pub note_id: Iri,
36    pub create_id: Iri,
37    pub to: References<Iri>,
38    pub cc: References<Iri>,
39    pub content: String,
40    pub media_type: Option<String>,
41    pub published: Option<String>,
42    pub url: Option<Iri>,
43}
44
45/// The transient result of constructing one local Note.
46///
47/// Core retains neither value. A runtime persists `note`; `activity` remains
48/// available for subsequent delivery orchestration.
49#[derive(Clone, Debug, Eq, PartialEq)]
50pub struct CreateNoteOutcome {
51    pub note: Note,
52    pub activity: Create<Note>,
53    pub recipients: Vec<NoteRecipient>,
54}
55
56#[must_use]
57pub fn create_note(local_actor: &Actor, input: CreateNoteInput) -> CreateNoteOutcome {
58    let actor = Reference::id(local_actor.id.clone());
59    let mut note = Note::new(input.note_id);
60    note.attributed_to = Some(actor.clone());
61    note.to = input.to;
62    note.cc = input.cc;
63    note.content = Some(input.content);
64    note.media_type = input.media_type;
65    note.published = input.published;
66    note.url = input.url;
67
68    let mut activity = Create::new(input.create_id, actor, Reference::object(note.clone()));
69    activity.to = note.to.clone();
70    activity.cc = note.cc.clone();
71
72    let recipients = note_recipients(local_actor, &note);
73
74    CreateNoteOutcome {
75        note,
76        activity,
77        recipients,
78    }
79}
80
81#[must_use]
82pub fn is_public_note(note: &Note) -> bool {
83    note.to
84        .iter()
85        .chain(note.cc.iter())
86        .any(|recipient| recipient.as_str() == PUBLIC_COLLECTION)
87}
88
89fn note_recipients(local_actor: &Actor, note: &Note) -> Vec<NoteRecipient> {
90    let mut recipients = Vec::new();
91
92    for address in note.to.iter().chain(note.cc.iter()) {
93        let recipient = if address.as_str() == PUBLIC_COLLECTION || address == &local_actor.id {
94            continue;
95        } else if local_actor.followers.as_ref() == Some(address) {
96            NoteRecipient::Followers(local_actor.id.clone())
97        } else {
98            NoteRecipient::Actor(address.clone())
99        };
100
101        if !recipients.contains(&recipient) {
102            recipients.push(recipient);
103        }
104    }
105
106    recipients
107}