]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/mod.rs
4b3992fddcbe931801994a712bbe053f6034f66e
[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 lemmy_utils::LemmyError;
46   use serde::{de::DeserializeOwned, Serialize};
47   use std::collections::HashMap;
48
49   /// Check that json deserialize -> serialize -> deserialize gives identical file as initial one.
50   /// Ensures that there are no breaking changes in sent data.
51   pub(crate) fn test_parse_lemmy_item<T: Serialize + DeserializeOwned + std::fmt::Debug>(
52     path: &str,
53   ) -> Result<T, LemmyError> {
54     // parse file as T
55     let parsed = file_to_json_object::<T>(path)?;
56
57     // parse file into hashmap, which ensures that every field is included
58     let raw = file_to_json_object::<HashMap<String, serde_json::Value>>(path)?;
59     // assert that all fields are identical, otherwise print diff
60     assert_json_include!(actual: &parsed, expected: raw);
61     Ok(parsed)
62   }
63 }