Skip to main content

feder_core/
follow.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 core::fmt;
17
18use feder_vocab::{Accept, Actor, Follow, Iri, Reference};
19
20/// The transient result of accepting one valid Follow activity.
21///
22/// Core does not retain this value or write it to storage. A runtime persists
23/// `follower` and `following`, then delivers `accept` to `recipient_inbox`.
24#[derive(Clone, Debug, Eq, PartialEq)]
25pub struct FollowOutcome {
26    pub follower: Actor,
27    pub following: Iri,
28    pub accept: Accept,
29    pub recipient_inbox: Iri,
30}
31
32/// A pending outbound Follow relationship for application-owned storage.
33#[derive(Clone, Debug, Eq, PartialEq)]
34pub struct PendingFollow {
35    pub local_actor: Iri,
36    pub remote_actor: Actor,
37    pub follow_activity: Iri,
38}
39
40/// The transient result of creating one outbound Follow activity.
41///
42/// Core retains neither the activity nor its pending relationship. A runtime
43/// persists `relationship` before delivering `activity` to the remote actor.
44#[derive(Clone, Debug, Eq, PartialEq)]
45pub struct CreateFollowOutcome {
46    pub relationship: PendingFollow,
47    pub activity: Follow,
48}
49
50#[must_use]
51pub fn create_follow(
52    local_actor: &Actor,
53    remote_actor: &Actor,
54    follow_id: Iri,
55) -> CreateFollowOutcome {
56    CreateFollowOutcome {
57        relationship: PendingFollow {
58            local_actor: local_actor.id.clone(),
59            remote_actor: remote_actor.clone(),
60            follow_activity: follow_id.clone(),
61        },
62        activity: Follow::new(
63            follow_id,
64            Reference::id(local_actor.id.clone()),
65            Reference::id(remote_actor.id.clone()),
66        ),
67    }
68}
69
70pub fn receive_accept_follow(
71    local_actor: &Actor,
72    remote_actor: &Actor,
73    pending: &PendingFollow,
74    accept: Accept,
75) -> Result<(), AcceptFollowError> {
76    if pending.local_actor != local_actor.id {
77        return Err(AcceptFollowError::WrongLocalActor);
78    }
79    if pending.remote_actor.id != remote_actor.id || reference_id(&accept.actor) != &remote_actor.id
80    {
81        return Err(AcceptFollowError::WrongActor);
82    }
83    if follow_reference_id(&accept.object) != &pending.follow_activity {
84        return Err(AcceptFollowError::WrongFollow);
85    }
86    if let Reference::Object(follow) = &accept.object {
87        if reference_id(&follow.actor) != &local_actor.id {
88            return Err(AcceptFollowError::WrongFollowActor);
89        }
90        if reference_id(&follow.object) != &remote_actor.id {
91            return Err(AcceptFollowError::WrongFollowObject);
92        }
93    }
94
95    Ok(())
96}
97
98pub fn receive_follow(
99    local_actor: &Actor,
100    remote_actor: &Actor,
101    mut follow: Follow,
102    accept_id: Iri,
103) -> Result<FollowOutcome, FollowError> {
104    if reference_id(&follow.object) != &local_actor.id {
105        return Err(FollowError::WrongObject);
106    }
107    if reference_id(&follow.actor) != &remote_actor.id {
108        return Err(FollowError::WrongActor);
109    }
110
111    follow.actor = Reference::object(remote_actor.clone());
112
113    Ok(FollowOutcome {
114        follower: remote_actor.clone(),
115        following: local_actor.id.clone(),
116        accept: Accept::new(
117            accept_id,
118            Reference::id(local_actor.id.clone()),
119            Reference::object(follow),
120        ),
121        recipient_inbox: remote_actor.inbox.clone(),
122    })
123}
124
125fn reference_id(reference: &Reference<Actor>) -> &Iri {
126    match reference {
127        Reference::Id(id) => id,
128        Reference::Object(actor) => &actor.id,
129    }
130}
131
132fn follow_reference_id(reference: &Reference<Follow>) -> &Iri {
133    match reference {
134        Reference::Id(id) => id,
135        Reference::Object(follow) => &follow.id,
136    }
137}
138
139#[derive(Clone, Copy, Debug, Eq, PartialEq)]
140pub enum AcceptFollowError {
141    WrongActor,
142    WrongFollow,
143    WrongFollowActor,
144    WrongFollowObject,
145    WrongLocalActor,
146}
147
148impl fmt::Display for AcceptFollowError {
149    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
150        match self {
151            Self::WrongActor => formatter.write_str("Accept actor does not match remote actor"),
152            Self::WrongFollow => formatter.write_str("Accept does not reference pending Follow"),
153            Self::WrongFollowActor => {
154                formatter.write_str("accepted Follow actor does not match local actor")
155            }
156            Self::WrongFollowObject => {
157                formatter.write_str("accepted Follow does not target remote actor")
158            }
159            Self::WrongLocalActor => {
160                formatter.write_str("pending Follow does not belong to local actor")
161            }
162        }
163    }
164}
165
166impl core::error::Error for AcceptFollowError {}
167
168#[derive(Clone, Copy, Debug, Eq, PartialEq)]
169pub enum FollowError {
170    WrongActor,
171    WrongObject,
172}
173
174impl fmt::Display for FollowError {
175    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
176        match self {
177            Self::WrongActor => formatter.write_str("Follow actor does not match remote actor"),
178            Self::WrongObject => formatter.write_str("Follow does not target the local actor"),
179        }
180    }
181}
182
183impl core::error::Error for FollowError {}