2022-12-01 20:52:49 +00:00
|
|
|
use crate::{local_instance, objects::community::ApubCommunity};
|
2022-06-02 14:33:41 +00:00
|
|
|
use activitypub_federation::{deser::values::MediaTypeMarkdown, utils::fetch_object_http};
|
2021-11-19 17:47:06 +00:00
|
|
|
use activitystreams_kinds::object::ImageType;
|
|
|
|
use lemmy_db_schema::newtypes::DbUrl;
|
2022-06-02 14:33:41 +00:00
|
|
|
use lemmy_utils::error::LemmyError;
|
2022-05-06 23:53:33 +00:00
|
|
|
use lemmy_websocket::LemmyContext;
|
|
|
|
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
2021-11-19 17:47:06 +00:00
|
|
|
use std::collections::HashMap;
|
2022-04-01 18:25:19 +00:00
|
|
|
use url::Url;
|
2021-10-28 21:17:59 +00:00
|
|
|
|
2021-10-29 10:32:42 +00:00
|
|
|
pub mod activities;
|
2021-10-28 21:17:59 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2022-04-01 18:25:19 +00:00
|
|
|
impl Source {
|
2022-02-07 19:23:12 +00:00
|
|
|
pub(crate) fn new(content: String) -> Self {
|
2022-04-01 18:25:19 +00:00
|
|
|
Source {
|
2022-02-07 19:23:12 +00:00
|
|
|
content,
|
|
|
|
media_type: MediaTypeMarkdown::Markdown,
|
2022-04-01 18:25:19 +00:00
|
|
|
}
|
2022-02-07 19:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-28 21:17:59 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct ImageObject {
|
|
|
|
#[serde(rename = "type")]
|
2021-11-19 17:47:06 +00:00
|
|
|
kind: ImageType,
|
2021-10-28 21:17:59 +00:00
|
|
|
pub(crate) url: Url,
|
|
|
|
}
|
2021-11-01 13:05:20 +00:00
|
|
|
|
2021-11-19 17:47:06 +00:00
|
|
|
impl ImageObject {
|
|
|
|
pub(crate) fn new(url: DbUrl) -> Self {
|
|
|
|
ImageObject {
|
|
|
|
kind: ImageType::Image,
|
|
|
|
url: url.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-06 23:53:33 +00:00
|
|
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
2021-11-19 17:47:06 +00:00
|
|
|
#[serde(transparent)]
|
|
|
|
pub struct Unparsed(HashMap<String, serde_json::Value>);
|
|
|
|
|
2022-05-06 23:53:33 +00:00
|
|
|
pub(crate) trait Id {
|
2022-06-02 14:33:41 +00:00
|
|
|
fn object_id(&self) -> &Url;
|
2022-05-06 23:53:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
pub(crate) enum IdOrNestedObject<Kind: Id> {
|
|
|
|
Id(Url),
|
|
|
|
NestedObject(Kind),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Kind: Id + DeserializeOwned> IdOrNestedObject<Kind> {
|
|
|
|
pub(crate) fn id(&self) -> &Url {
|
|
|
|
match self {
|
|
|
|
IdOrNestedObject::Id(i) => i,
|
2022-06-02 14:33:41 +00:00
|
|
|
IdOrNestedObject::NestedObject(n) => n.object_id(),
|
2022-05-06 23:53:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pub(crate) async fn object(
|
|
|
|
self,
|
|
|
|
context: &LemmyContext,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<Kind, LemmyError> {
|
|
|
|
match self {
|
2022-06-02 14:33:41 +00:00
|
|
|
IdOrNestedObject::Id(i) => {
|
2022-11-09 10:05:00 +00:00
|
|
|
Ok(fetch_object_http(&i, local_instance(context).await, request_counter).await?)
|
2022-06-02 14:33:41 +00:00
|
|
|
}
|
2022-05-06 23:53:33 +00:00
|
|
|
IdOrNestedObject::NestedObject(o) => Ok(o),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 20:52:49 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
pub trait InCommunity {
|
|
|
|
// TODO: after we use audience field and remove backwards compat, it should be possible to change
|
|
|
|
// this to simply `fn community(&self) -> Result<ObjectId<ApubCommunity>, LemmyError>`
|
|
|
|
async fn community(
|
|
|
|
&self,
|
|
|
|
context: &LemmyContext,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<ApubCommunity, LemmyError>;
|
|
|
|
}
|
|
|
|
|
2021-11-01 13:05:20 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) mod tests {
|
2022-06-02 14:33:41 +00:00
|
|
|
use activitypub_federation::deser::context::WithContext;
|
2021-11-01 13:05:20 +00:00
|
|
|
use assert_json_diff::assert_json_include;
|
2022-06-02 14:33:41 +00:00
|
|
|
use lemmy_utils::error::LemmyError;
|
2021-11-01 13:05:20 +00:00
|
|
|
use serde::{de::DeserializeOwned, Serialize};
|
2022-02-17 22:04:01 +00:00
|
|
|
use std::{collections::HashMap, fs::File, io::BufReader};
|
|
|
|
|
|
|
|
pub(crate) fn file_to_json_object<T: DeserializeOwned>(path: &str) -> Result<T, LemmyError> {
|
|
|
|
let file = File::open(path)?;
|
|
|
|
let reader = BufReader::new(file);
|
|
|
|
Ok(serde_json::from_reader(reader)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn test_json<T: DeserializeOwned>(path: &str) -> Result<WithContext<T>, LemmyError> {
|
|
|
|
file_to_json_object::<WithContext<T>>(path)
|
|
|
|
}
|
2021-11-01 13:05:20 +00:00
|
|
|
|
2022-01-17 14:40:47 +00:00
|
|
|
/// Check that json deserialize -> serialize -> deserialize gives identical file as initial one.
|
|
|
|
/// Ensures that there are no breaking changes in sent data.
|
2021-11-15 22:54:25 +00:00
|
|
|
pub(crate) fn test_parse_lemmy_item<T: Serialize + DeserializeOwned + std::fmt::Debug>(
|
|
|
|
path: &str,
|
2022-01-20 14:13:29 +00:00
|
|
|
) -> Result<T, LemmyError> {
|
2022-01-17 14:40:47 +00:00
|
|
|
// parse file as T
|
2022-01-20 14:13:29 +00:00
|
|
|
let parsed = file_to_json_object::<T>(path)?;
|
2021-11-01 13:05:20 +00:00
|
|
|
|
2022-01-17 14:40:47 +00:00
|
|
|
// parse file into hashmap, which ensures that every field is included
|
2022-01-20 14:13:29 +00:00
|
|
|
let raw = file_to_json_object::<HashMap<String, serde_json::Value>>(path)?;
|
2022-01-17 14:40:47 +00:00
|
|
|
// assert that all fields are identical, otherwise print diff
|
2021-11-01 13:05:20 +00:00
|
|
|
assert_json_include!(actual: &parsed, expected: raw);
|
2022-01-20 14:13:29 +00:00
|
|
|
Ok(parsed)
|
2021-11-01 13:05:20 +00:00
|
|
|
}
|
|
|
|
}
|