Skip to main content

feder_runtime_server/
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 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 note(
26    State(app_state): State<AppState>,
27    Path(id): Path<String>,
28) -> Result<Response, StatusCode> {
29    // TODO(#25): Replace this seeded preview note with durable runtime storage.
30    let expected_suffix = format!("/notes/{id}");
31    let note = app_state
32        .notes
33        .iter()
34        .find(|note| note.id.as_str().ends_with(&expected_suffix))
35        .cloned()
36        .ok_or(StatusCode::NOT_FOUND)?;
37
38    Ok((
39        [(header::CONTENT_TYPE, "application/activity+json")],
40        Json(note),
41    )
42        .into_response())
43}
44
45#[cfg(test)]
46mod tests {
47    use axum::{
48        body::{Body, to_bytes},
49        http::{Request, StatusCode, header},
50    };
51    use serde_json::Value;
52    use tower::ServiceExt;
53
54    use crate::{app::build_router, config::RuntimeConfig};
55
56    #[tokio::test]
57    async fn returns_public_note() {
58        let app = build_router(&RuntimeConfig::default_local());
59
60        let response = app
61            .oneshot(
62                Request::builder()
63                    .uri("/notes/1")
64                    .body(Body::empty())
65                    .expect("valid request"),
66            )
67            .await
68            .expect("response");
69
70        assert_eq!(response.status(), StatusCode::OK);
71        assert_eq!(
72            response.headers().get(header::CONTENT_TYPE).unwrap(),
73            "application/activity+json"
74        );
75
76        let body = to_bytes(response.into_body(), 2048)
77            .await
78            .expect("read response body");
79        let json: Value = serde_json::from_slice(&body).expect("valid json");
80
81        assert_eq!(json["@context"], "https://www.w3.org/ns/activitystreams");
82        assert_eq!(json["type"], "Note");
83        assert_eq!(json["id"], "http://127.0.0.1:3000/notes/1");
84        assert_eq!(json["attributedTo"], "http://127.0.0.1:3000/users/alice");
85        assert_eq!(
86            json["content"],
87            "Hello, World! This is Feder, a portable AP core for many runtimes."
88        );
89    }
90
91    #[tokio::test]
92    async fn rejects_unknown_note() {
93        let app = build_router(&RuntimeConfig::default_local());
94
95        let response = app
96            .oneshot(
97                Request::builder()
98                    .uri("/notes/missing")
99                    .body(Body::empty())
100                    .expect("valid request"),
101            )
102            .await
103            .expect("response");
104
105        assert_eq!(response.status(), StatusCode::NOT_FOUND);
106    }
107}