Skip to main content

feder_runtime_server/
actor.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 axum::{
17    Json,
18    extract::{Path, State},
19    http::{StatusCode, header},
20    response::{IntoResponse, Response},
21};
22
23use crate::app::AppState;
24
25pub async fn actor(
26    State(app_state): State<AppState>,
27    Path(username): Path<String>,
28) -> Result<Response, StatusCode> {
29    if username != app_state.username {
30        return Err(StatusCode::NOT_FOUND);
31    }
32    let local_actor = app_state.local_actor.clone();
33
34    Ok((
35        [(header::CONTENT_TYPE, "application/activity+json")],
36        Json(local_actor),
37    )
38        .into_response())
39}
40
41#[cfg(test)]
42mod tests {
43    use axum::{
44        body::{Body, to_bytes},
45        http::{Request, StatusCode, header},
46    };
47    use serde_json::Value;
48    use tower::ServiceExt;
49
50    use crate::{app::build_router, config::RuntimeConfig};
51
52    #[tokio::test]
53    async fn returns_local_actor() {
54        let app = build_router(&RuntimeConfig::default_local());
55
56        let response = app
57            .oneshot(
58                Request::builder()
59                    .uri("/users/alice")
60                    .body(Body::empty())
61                    .expect("valid request"),
62            )
63            .await
64            .expect("response");
65
66        assert_eq!(response.status(), StatusCode::OK);
67        assert_eq!(
68            response.headers().get(header::CONTENT_TYPE).unwrap(),
69            "application/activity+json"
70        );
71
72        let body = to_bytes(response.into_body(), 2048)
73            .await
74            .expect("read response body");
75        let json: Value = serde_json::from_slice(&body).expect("valid json");
76
77        assert_eq!(json["@context"], "https://www.w3.org/ns/activitystreams");
78        assert_eq!(json["type"], "Person");
79        assert_eq!(json["id"], "http://127.0.0.1:3000/users/alice");
80        assert_eq!(json["inbox"], "http://127.0.0.1:3000/users/alice/inbox");
81        assert_eq!(json["outbox"], "http://127.0.0.1:3000/users/alice/outbox");
82        assert_eq!(json["preferredUsername"], "alice");
83        assert_eq!(json["name"], "alice");
84    }
85
86    #[tokio::test]
87    async fn rejects_unknown_actor() {
88        let app = build_router(&RuntimeConfig::default_local());
89
90        let response = app
91            .oneshot(
92                Request::builder()
93                    .uri("/users/bob")
94                    .body(Body::empty())
95                    .expect("valid request"),
96            )
97            .await
98            .expect("response");
99
100        assert_eq!(response.status(), StatusCode::NOT_FOUND);
101    }
102}