1 use crate::objects::community::ApubCommunity;
2 use activitypub_federation::{
4 fetch::fetch_object_http,
5 kinds::object::ImageType,
6 protocol::values::MediaTypeMarkdown,
8 use lemmy_api_common::context::LemmyContext;
9 use lemmy_db_schema::newtypes::DbUrl;
10 use lemmy_utils::error::LemmyError;
11 use serde::{de::DeserializeOwned, Deserialize, Serialize};
12 use std::collections::HashMap;
16 pub(crate) mod collections;
17 pub(crate) mod objects;
19 #[derive(Clone, Debug, Deserialize, Serialize)]
20 #[serde(rename_all = "camelCase")]
22 pub(crate) content: String,
23 pub(crate) media_type: MediaTypeMarkdown,
27 pub(crate) fn new(content: String) -> Self {
30 media_type: MediaTypeMarkdown::Markdown,
35 #[derive(Clone, Debug, Deserialize, Serialize)]
36 #[serde(rename_all = "camelCase")]
37 pub struct ImageObject {
38 #[serde(rename = "type")]
44 pub(crate) fn new(url: DbUrl) -> Self {
46 kind: ImageType::Image,
52 #[derive(Clone, Debug, Default, Deserialize, Serialize)]
54 pub struct Unparsed(HashMap<String, serde_json::Value>);
57 fn object_id(&self) -> &Url;
60 #[derive(Clone, Debug, Deserialize, Serialize)]
62 pub(crate) enum IdOrNestedObject<Kind: Id> {
67 impl<Kind: Id + DeserializeOwned + Send> IdOrNestedObject<Kind> {
68 pub(crate) fn id(&self) -> &Url {
70 IdOrNestedObject::Id(i) => i,
71 IdOrNestedObject::NestedObject(n) => n.object_id(),
74 pub(crate) async fn object(self, context: &Data<LemmyContext>) -> Result<Kind, LemmyError> {
76 // TODO: move IdOrNestedObject struct to library and make fetch_object_http private
77 IdOrNestedObject::Id(i) => Ok(fetch_object_http(&i, context).await?),
78 IdOrNestedObject::NestedObject(o) => Ok(o),
83 #[async_trait::async_trait]
84 pub trait InCommunity {
85 // TODO: after we use audience field and remove backwards compat, it should be possible to change
86 // this to simply `fn community(&self) -> Result<ObjectId<ApubCommunity>, LemmyError>`
87 async fn community(&self, context: &Data<LemmyContext>) -> Result<ApubCommunity, LemmyError>;
91 pub(crate) mod tests {
92 #![allow(clippy::unwrap_used)]
93 #![allow(clippy::indexing_slicing)]
95 use activitypub_federation::protocol::context::WithContext;
96 use assert_json_diff::assert_json_include;
97 use lemmy_utils::error::LemmyError;
98 use serde::{de::DeserializeOwned, Serialize};
99 use std::{collections::HashMap, fs::File, io::BufReader};
101 pub(crate) fn file_to_json_object<T: DeserializeOwned>(path: &str) -> Result<T, LemmyError> {
102 let file = File::open(path)?;
103 let reader = BufReader::new(file);
104 Ok(serde_json::from_reader(reader)?)
107 pub(crate) fn test_json<T: DeserializeOwned>(path: &str) -> Result<WithContext<T>, LemmyError> {
108 file_to_json_object::<WithContext<T>>(path)
111 /// Check that json deserialize -> serialize -> deserialize gives identical file as initial one.
112 /// Ensures that there are no breaking changes in sent data.
113 pub(crate) fn test_parse_lemmy_item<T: Serialize + DeserializeOwned + std::fmt::Debug>(
115 ) -> Result<T, LemmyError> {
117 let parsed = file_to_json_object::<T>(path)?;
119 // parse file into hashmap, which ensures that every field is included
120 let raw = file_to_json_object::<HashMap<String, serde_json::Value>>(path)?;
121 // assert that all fields are identical, otherwise print diff
122 assert_json_include!(actual: &parsed, expected: raw);