1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//! Interface to fetch remote objects and send local objects to remote servers.

use crate::activitypub::Object;
use crate::http_api::activity_json::CT_ACTIVITY_JSON;
use actix_web::error::PayloadError;
use actix_web::http::uri::InvalidUri;
use actix_web::http::{StatusCode, Uri};
use awc::error::SendRequestError;
use awc::Client;

const USER_AGENT: &str = "Notedealer/0.1.0 No auth in this version, \
not for public internet use (feel free to block requests with this UA)";

/// Wrapper around a HTTP [Client] for sending and receiving ActivityPub-shaped
/// JSON.
pub struct RemoteAccessor {
    http_client: Client,
}

impl Default for RemoteAccessor {
    fn default() -> Self {
        RemoteAccessor {
            http_client: Client::new(),
        }
    }
}

impl RemoteAccessor {
    /// Attempt to fetch the ActivityPub object with the given id. ActivityPub id's are
    /// URLs, so this just sends a GET request to the id.
    pub async fn dereference_ap_object(&self, id: &str) -> Result<Option<Object>, RemoteAccessError> {
        let uri = Uri::try_from(id)?;
        let mut response = self
            .http_client
            .get(uri)
            .append_header(("Accept", CT_ACTIVITY_JSON))
            .append_header(("User-Agent", USER_AGENT))
            .send()
            .await?;
        match response.status() {
            StatusCode::NOT_FOUND | StatusCode::FORBIDDEN => Ok(None),
            StatusCode::OK => match serde_json::from_slice(&response.body().await?) {
                Ok(obj) => Ok(Some(obj)),
                Err(err) => {
                    tracing::warn!("Could not parse remote object: {err}");
                    Err(RemoteAccessError::InvalidActivityPubObject)
                }
            },
            status => Err(RemoteAccessError::UnexpectedResponse(status)),
        }
    }

    /// Attempt to send the ActivityPub object to the given inbox. This
    /// serializes the object and POSTs it to the inbox URL.
    pub async fn send_ap_object(&self, inbox: &str, object: &Object<'_>) -> Result<(), RemoteAccessError> {
        let uri = Uri::try_from(inbox)?;
        let mut response = self
            .http_client
            .post(uri)
            .append_header(("Content-Type", CT_ACTIVITY_JSON))
            .append_header(("User-Agent", USER_AGENT))
            .send_body(serde_json::to_string(object).unwrap())
            .await?;
        match response.status() {
            StatusCode::OK | StatusCode::CREATED | StatusCode::METHOD_NOT_ALLOWED => {
                // 405 Method Not Allowed means the server isn't federating: everything working as expected.
                Ok(())
            }
            status => {
                let body = response.body().await?;
                let body = String::from_utf8_lossy(&body);
                tracing::warn!("Delivery to {inbox} failed: {body:?}");
                Err(RemoteAccessError::UnexpectedResponse(status))
            }
        }
    }
}

#[derive(Debug, thiserror::Error)]
pub enum RemoteAccessError {
    #[error("the id provided is not a valid URI")]
    IdNotAnUri(#[from] InvalidUri),
    #[error("failed to send http request")]
    HttpRequest(#[from] SendRequestError),
    #[error("failed to receive http response body")]
    Payload(#[from] PayloadError),
    #[error("remote server did not reply with a well-formed activitypub object")]
    InvalidActivityPubObject,
    #[error("remote server responded with an unexpected status code: {0}")]
    UnexpectedResponse(StatusCode),
}