Skip to main content

feder_core/
undo.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::{Actor, Iri, Reference, Undo};
19
20/// The transient result of undoing one valid Follow activity.
21///
22/// Core does not retain this value or remove anything from storage. A runtime
23/// passes `follower` and `following` to its follower-removal capability.
24#[derive(Clone, Debug, Eq, PartialEq)]
25pub struct UndoFollowOutcome {
26    pub follower: Iri,
27    pub following: Iri,
28}
29
30pub fn receive_undo_follow(
31    local_actor: &Actor,
32    remote_actor: &Actor,
33    undo: Undo,
34) -> Result<UndoFollowOutcome, UndoFollowError> {
35    if reference_id(&undo.actor) != &remote_actor.id {
36        return Err(UndoFollowError::WrongActor);
37    }
38
39    let Reference::Object(follow) = undo.object else {
40        return Err(UndoFollowError::LinkedFollow);
41    };
42    if reference_id(&follow.actor) != &remote_actor.id {
43        return Err(UndoFollowError::WrongActor);
44    }
45    if reference_id(&follow.object) != &local_actor.id {
46        return Err(UndoFollowError::WrongObject);
47    }
48
49    Ok(UndoFollowOutcome {
50        follower: remote_actor.id.clone(),
51        following: local_actor.id.clone(),
52    })
53}
54
55fn reference_id(reference: &Reference<Actor>) -> &Iri {
56    match reference {
57        Reference::Id(id) => id,
58        Reference::Object(actor) => &actor.id,
59    }
60}
61
62#[derive(Clone, Copy, Debug, Eq, PartialEq)]
63pub enum UndoFollowError {
64    LinkedFollow,
65    WrongActor,
66    WrongObject,
67}
68
69impl fmt::Display for UndoFollowError {
70    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
71        match self {
72            Self::LinkedFollow => formatter.write_str("Undo does not embed its Follow activity"),
73            Self::WrongActor => formatter.write_str("Undo actor does not own the embedded Follow"),
74            Self::WrongObject => formatter.write_str("undone Follow does not target local actor"),
75        }
76    }
77}
78
79impl core::error::Error for UndoFollowError {}