]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/mod.rs
GNU social compatibility (#2100)
[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 serde_json::Value;
8 use std::collections::HashMap;
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 #[derive(Clone, Debug, Deserialize, Serialize)]
22 #[serde(rename_all = "camelCase")]
23 #[serde(untagged)]
24 pub(crate) enum SourceCompat {
25   Lemmy(Source),
26   Other(Value),
27 }
28
29 impl SourceCompat {
30   pub(crate) fn new(content: String) -> Self {
31     SourceCompat::Lemmy(Source {
32       content,
33       media_type: MediaTypeMarkdown::Markdown,
34     })
35   }
36 }
37
38 #[derive(Clone, Debug, Deserialize, Serialize)]
39 #[serde(rename_all = "camelCase")]
40 pub struct ImageObject {
41   #[serde(rename = "type")]
42   kind: ImageType,
43   pub(crate) url: Url,
44 }
45
46 impl ImageObject {
47   pub(crate) fn new(url: DbUrl) -> Self {
48     ImageObject {
49       kind: ImageType::Image,
50       url: url.into(),
51     }
52   }
53 }
54
55 #[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
56 #[serde(transparent)]
57 pub struct Unparsed(HashMap<String, serde_json::Value>);
58
59 #[cfg(test)]
60 pub(crate) mod tests {
61   use crate::context::WithContext;
62   use assert_json_diff::assert_json_include;
63   use lemmy_utils::LemmyError;
64   use serde::{de::DeserializeOwned, Serialize};
65   use std::{collections::HashMap, fs::File, io::BufReader};
66
67   pub(crate) fn file_to_json_object<T: DeserializeOwned>(path: &str) -> Result<T, LemmyError> {
68     let file = File::open(path)?;
69     let reader = BufReader::new(file);
70     Ok(serde_json::from_reader(reader)?)
71   }
72
73   pub(crate) fn test_json<T: DeserializeOwned>(path: &str) -> Result<WithContext<T>, LemmyError> {
74     file_to_json_object::<WithContext<T>>(path)
75   }
76
77   /// Check that json deserialize -> serialize -> deserialize gives identical file as initial one.
78   /// Ensures that there are no breaking changes in sent data.
79   pub(crate) fn test_parse_lemmy_item<T: Serialize + DeserializeOwned + std::fmt::Debug>(
80     path: &str,
81   ) -> Result<T, LemmyError> {
82     // parse file as T
83     let parsed = file_to_json_object::<T>(path)?;
84
85     // parse file into hashmap, which ensures that every field is included
86     let raw = file_to_json_object::<HashMap<String, serde_json::Value>>(path)?;
87     // assert that all fields are identical, otherwise print diff
88     assert_json_include!(actual: &parsed, expected: raw);
89     Ok(parsed)
90   }
91 }