]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/mod.rs
a7a233964c1023258a4330cea31a18c03d4ce3a7
[lemmy.git] / crates / apub / src / protocol / mod.rs
1 use activitystreams_kinds::object::ImageType;
2 use serde::{Deserialize, Serialize};
3 use url::Url;
4
5 use lemmy_apub_lib::values::MediaTypeMarkdown;
6 use lemmy_db_schema::newtypes::DbUrl;
7 use std::collections::HashMap;
8
9 pub mod activities;
10 pub(crate) mod collections;
11 pub(crate) mod objects;
12
13 #[derive(Clone, Debug, Deserialize, Serialize)]
14 #[serde(rename_all = "camelCase")]
15 pub struct Source {
16   pub(crate) content: String,
17   pub(crate) media_type: MediaTypeMarkdown,
18 }
19
20 #[derive(Clone, Debug, Deserialize, Serialize)]
21 #[serde(rename_all = "camelCase")]
22 pub struct ImageObject {
23   #[serde(rename = "type")]
24   kind: ImageType,
25   pub(crate) url: Url,
26 }
27
28 impl ImageObject {
29   pub(crate) fn new(url: DbUrl) -> Self {
30     ImageObject {
31       kind: ImageType::Image,
32       url: url.into(),
33     }
34   }
35 }
36
37 #[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
38 #[serde(transparent)]
39 pub struct Unparsed(HashMap<String, serde_json::Value>);
40
41 #[cfg(test)]
42 pub(crate) mod tests {
43   use crate::objects::tests::file_to_json_object;
44   use assert_json_diff::assert_json_include;
45   use serde::{de::DeserializeOwned, Serialize};
46   use std::collections::HashMap;
47
48   /// Check that json deserialize -> serialize -> deserialize gives identical file as initial one.
49   /// Ensures that there are no breaking changes in sent data.
50   pub(crate) fn test_parse_lemmy_item<T: Serialize + DeserializeOwned + std::fmt::Debug>(
51     path: &str,
52   ) -> T {
53     // parse file as T
54     let parsed = file_to_json_object::<T>(path).unwrap();
55
56     // parse file into hashmap, which ensures that every field is included
57     let raw = file_to_json_object::<HashMap<String, serde_json::Value>>(path).unwrap();
58     // assert that all fields are identical, otherwise print diff
59     assert_json_include!(actual: &parsed, expected: raw);
60     parsed
61   }
62 }