]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/mod.rs
Federate with Peertube (#2244)
[lemmy.git] / crates / apub / src / protocol / mod.rs
1 use activitystreams_kinds::object::ImageType;
2 use lemmy_apub_lib::{utils::fetch_object_http, values::MediaTypeMarkdown};
3 use lemmy_db_schema::newtypes::DbUrl;
4 use lemmy_utils::LemmyError;
5 use lemmy_websocket::LemmyContext;
6 use serde::{de::DeserializeOwned, Deserialize, Serialize};
7 use std::collections::HashMap;
8 use url::Url;
9
10 pub mod activities;
11 pub(crate) mod collections;
12 pub(crate) mod objects;
13
14 #[derive(Clone, Debug, Deserialize, Serialize)]
15 #[serde(rename_all = "camelCase")]
16 pub struct Source {
17   pub(crate) content: String,
18   pub(crate) media_type: MediaTypeMarkdown,
19 }
20
21 impl Source {
22   pub(crate) fn new(content: String) -> Self {
23     Source {
24       content,
25       media_type: MediaTypeMarkdown::Markdown,
26     }
27   }
28 }
29
30 #[derive(Clone, Debug, Deserialize, Serialize)]
31 #[serde(rename_all = "camelCase")]
32 pub struct ImageObject {
33   #[serde(rename = "type")]
34   kind: ImageType,
35   pub(crate) url: Url,
36 }
37
38 impl ImageObject {
39   pub(crate) fn new(url: DbUrl) -> Self {
40     ImageObject {
41       kind: ImageType::Image,
42       url: url.into(),
43     }
44   }
45 }
46
47 #[derive(Clone, Debug, Default, Deserialize, Serialize)]
48 #[serde(transparent)]
49 pub struct Unparsed(HashMap<String, serde_json::Value>);
50
51 pub(crate) trait Id {
52   fn id(&self) -> &Url;
53 }
54
55 #[derive(Clone, Debug, Deserialize, Serialize)]
56 #[serde(untagged)]
57 pub(crate) enum IdOrNestedObject<Kind: Id> {
58   Id(Url),
59   NestedObject(Kind),
60 }
61
62 impl<Kind: Id + DeserializeOwned> IdOrNestedObject<Kind> {
63   pub(crate) fn id(&self) -> &Url {
64     match self {
65       IdOrNestedObject::Id(i) => i,
66       IdOrNestedObject::NestedObject(n) => n.id(),
67     }
68   }
69   pub(crate) async fn object(
70     self,
71     context: &LemmyContext,
72     request_counter: &mut i32,
73   ) -> Result<Kind, LemmyError> {
74     match self {
75       IdOrNestedObject::Id(i) => fetch_object_http(&i, context.client(), request_counter).await,
76       IdOrNestedObject::NestedObject(o) => Ok(o),
77     }
78   }
79 }
80
81 #[cfg(test)]
82 pub(crate) mod tests {
83   use crate::context::WithContext;
84   use assert_json_diff::assert_json_include;
85   use lemmy_utils::LemmyError;
86   use serde::{de::DeserializeOwned, Serialize};
87   use std::{collections::HashMap, fs::File, io::BufReader};
88
89   pub(crate) fn file_to_json_object<T: DeserializeOwned>(path: &str) -> Result<T, LemmyError> {
90     let file = File::open(path)?;
91     let reader = BufReader::new(file);
92     Ok(serde_json::from_reader(reader)?)
93   }
94
95   pub(crate) fn test_json<T: DeserializeOwned>(path: &str) -> Result<WithContext<T>, LemmyError> {
96     file_to_json_object::<WithContext<T>>(path)
97   }
98
99   /// Check that json deserialize -> serialize -> deserialize gives identical file as initial one.
100   /// Ensures that there are no breaking changes in sent data.
101   pub(crate) fn test_parse_lemmy_item<T: Serialize + DeserializeOwned + std::fmt::Debug>(
102     path: &str,
103   ) -> Result<T, LemmyError> {
104     // parse file as T
105     let parsed = file_to_json_object::<T>(path)?;
106
107     // parse file into hashmap, which ensures that every field is included
108     let raw = file_to_json_object::<HashMap<String, serde_json::Value>>(path)?;
109     // assert that all fields are identical, otherwise print diff
110     assert_json_include!(actual: &parsed, expected: raw);
111     Ok(parsed)
112   }
113 }