use activitystreams_kinds::object::ImageType; use serde::{Deserialize, Serialize}; use url::Url; use lemmy_apub_lib::values::MediaTypeMarkdown; use lemmy_db_schema::newtypes::DbUrl; use serde_json::Value; use std::collections::HashMap; pub mod activities; pub(crate) mod collections; pub(crate) mod objects; #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct Source { pub(crate) content: String, pub(crate) media_type: MediaTypeMarkdown, } #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] #[serde(untagged)] pub(crate) enum SourceCompat { Lemmy(Source), Other(Value), } impl SourceCompat { pub(crate) fn new(content: String) -> Self { SourceCompat::Lemmy(Source { content, media_type: MediaTypeMarkdown::Markdown, }) } } #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct ImageObject { #[serde(rename = "type")] kind: ImageType, pub(crate) url: Url, } impl ImageObject { pub(crate) fn new(url: DbUrl) -> Self { ImageObject { kind: ImageType::Image, url: url.into(), } } } #[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)] #[serde(transparent)] pub struct Unparsed(HashMap); #[cfg(test)] pub(crate) mod tests { use crate::context::WithContext; use assert_json_diff::assert_json_include; use lemmy_utils::LemmyError; use serde::{de::DeserializeOwned, Serialize}; use std::{collections::HashMap, fs::File, io::BufReader}; pub(crate) fn file_to_json_object(path: &str) -> Result { let file = File::open(path)?; let reader = BufReader::new(file); Ok(serde_json::from_reader(reader)?) } pub(crate) fn test_json(path: &str) -> Result, LemmyError> { file_to_json_object::>(path) } /// Check that json deserialize -> serialize -> deserialize gives identical file as initial one. /// Ensures that there are no breaking changes in sent data. pub(crate) fn test_parse_lemmy_item( path: &str, ) -> Result { // parse file as T let parsed = file_to_json_object::(path)?; // parse file into hashmap, which ensures that every field is included let raw = file_to_json_object::>(path)?; // assert that all fields are identical, otherwise print diff assert_json_include!(actual: &parsed, expected: raw); Ok(parsed) } }