1use 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#[derive(Clone, Debug, Eq, PartialEq)]
24pub enum NoteRecipient {
25 Followers(Iri),
26 Actor(Iri),
27}
28
29#[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#[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, ¬e);
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}