X-Git-Url: http://these/git/?a=blobdiff_plain;f=crates%2Fapub%2Fsrc%2Fprotocol%2Fobjects%2Fmod.rs;h=9a3dab1859949d91c1c3b9f542fd0eb5970df1d8;hb=92568956353f21649ed9aff68b42699c9d036f30;hp=4ee13b775a21273bfec540f22ee4ec97f4d6756f;hpb=c6ac606f604b2d72063caf25fb54d3ab6a1cdcfc;p=lemmy.git diff --git a/crates/apub/src/protocol/objects/mod.rs b/crates/apub/src/protocol/objects/mod.rs index 4ee13b77..9a3dab18 100644 --- a/crates/apub/src/protocol/objects/mod.rs +++ b/crates/apub/src/protocol/objects/mod.rs @@ -1,8 +1,16 @@ +use lemmy_db_schema::{ + impls::actor_language::UNDETERMINED_ID, + newtypes::LanguageId, + source::language::Language, + utils::DbPool, +}; +use lemmy_utils::error::LemmyError; use serde::{Deserialize, Serialize}; use url::Url; pub(crate) mod chat_message; pub(crate) mod group; +pub(crate) mod instance; pub(crate) mod note; pub(crate) mod page; pub(crate) mod person; @@ -11,37 +19,168 @@ pub(crate) mod tombstone; #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct Endpoints { - #[serde(skip_serializing_if = "Option::is_none")] - pub shared_inbox: Option, + pub shared_inbox: Url, +} + +/// As specified in https://schema.org/Language +#[derive(Clone, Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub(crate) struct LanguageTag { + pub(crate) identifier: String, + pub(crate) name: String, +} + +impl LanguageTag { + pub(crate) async fn new_single( + lang: LanguageId, + pool: &mut DbPool<'_>, + ) -> Result, LemmyError> { + let lang = Language::read_from_id(pool, lang).await?; + + // undetermined + if lang.id == UNDETERMINED_ID { + Ok(None) + } else { + Ok(Some(LanguageTag { + identifier: lang.code, + name: lang.name, + })) + } + } + + pub(crate) async fn new_multiple( + lang_ids: Vec, + pool: &mut DbPool<'_>, + ) -> Result, LemmyError> { + let mut langs = Vec::::new(); + + for l in lang_ids { + langs.push(Language::read_from_id(pool, l).await?); + } + + let langs = langs + .into_iter() + .map(|l| LanguageTag { + identifier: l.code, + name: l.name, + }) + .collect(); + Ok(langs) + } + + pub(crate) async fn to_language_id_single( + lang: Option, + pool: &mut DbPool<'_>, + ) -> Result, LemmyError> { + let identifier = lang.map(|l| l.identifier); + let language = Language::read_id_from_code(pool, identifier.as_deref()).await?; + + Ok(language) + } + + pub(crate) async fn to_language_id_multiple( + langs: Vec, + pool: &mut DbPool<'_>, + ) -> Result, LemmyError> { + let mut language_ids = Vec::new(); + + for l in langs { + let id = l.identifier; + language_ids.push(Language::read_id_from_code(pool, Some(&id)).await?); + } + + Ok(language_ids.into_iter().flatten().collect()) + } } #[cfg(test)] mod tests { - use crate::{ - context::WithContext, - objects::tests::file_to_json_object, - protocol::{ - objects::{chat_message::ChatMessage, group::Group, note::Note, page::Page, person::Person}, - tests::test_parse_lemmy_item, + #![allow(clippy::unwrap_used)] + #![allow(clippy::indexing_slicing)] + + use crate::protocol::{ + objects::{ + chat_message::ChatMessage, + group::Group, + instance::Instance, + note::Note, + page::Page, + person::Person, + tombstone::Tombstone, }, + tests::{test_json, test_parse_lemmy_item}, }; - #[actix_rt::test] - async fn test_parse_object() { - test_parse_lemmy_item::("assets/lemmy/objects/person.json"); - test_parse_lemmy_item::("assets/lemmy/objects/group.json"); - test_parse_lemmy_item::("assets/lemmy/objects/page.json"); - test_parse_lemmy_item::("assets/lemmy/objects/note.json"); - test_parse_lemmy_item::("assets/lemmy/objects/chat_message.json"); + #[test] + fn test_parse_objects_lemmy() { + test_parse_lemmy_item::("assets/lemmy/objects/instance.json").unwrap(); + test_parse_lemmy_item::("assets/lemmy/objects/group.json").unwrap(); + test_parse_lemmy_item::("assets/lemmy/objects/person.json").unwrap(); + test_parse_lemmy_item::("assets/lemmy/objects/page.json").unwrap(); + test_parse_lemmy_item::("assets/lemmy/objects/note.json").unwrap(); + test_parse_lemmy_item::("assets/lemmy/objects/chat_message.json").unwrap(); + test_parse_lemmy_item::("assets/lemmy/objects/tombstone.json").unwrap(); + } + + #[test] + fn test_parse_objects_pleroma() { + test_json::("assets/pleroma/objects/person.json").unwrap(); + test_json::("assets/pleroma/objects/note.json").unwrap(); + test_json::("assets/pleroma/objects/chat_message.json").unwrap(); + } + + #[test] + fn test_parse_objects_smithereen() { + test_json::("assets/smithereen/objects/person.json").unwrap(); + test_json::("assets/smithereen/objects/note.json").unwrap(); + } + + #[test] + fn test_parse_objects_mastodon() { + test_json::("assets/mastodon/objects/person.json").unwrap(); + test_json::("assets/mastodon/objects/note.json").unwrap(); + test_json::("assets/mastodon/objects/page.json").unwrap(); + } + + #[test] + fn test_parse_objects_lotide() { + test_json::("assets/lotide/objects/group.json").unwrap(); + test_json::("assets/lotide/objects/person.json").unwrap(); + test_json::("assets/lotide/objects/note.json").unwrap(); + test_json::("assets/lotide/objects/page.json").unwrap(); + test_json::("assets/lotide/objects/tombstone.json").unwrap(); + } + + #[test] + fn test_parse_object_friendica() { + test_json::("assets/friendica/objects/person_1.json").unwrap(); + test_json::("assets/friendica/objects/person_2.json").unwrap(); + test_json::("assets/friendica/objects/page_1.json").unwrap(); + test_json::("assets/friendica/objects/page_2.json").unwrap(); + test_json::("assets/friendica/objects/note_1.json").unwrap(); + test_json::("assets/friendica/objects/note_2.json").unwrap(); + } - file_to_json_object::>("assets/pleroma/objects/person.json"); - file_to_json_object::>("assets/pleroma/objects/note.json"); - file_to_json_object::>("assets/pleroma/objects/chat_message.json"); + #[test] + fn test_parse_object_gnusocial() { + test_json::("assets/gnusocial/objects/person.json").unwrap(); + test_json::("assets/gnusocial/objects/group.json").unwrap(); + test_json::("assets/gnusocial/objects/page.json").unwrap(); + test_json::("assets/gnusocial/objects/note.json").unwrap(); + } - file_to_json_object::>("assets/smithereen/objects/person.json"); - file_to_json_object::("assets/smithereen/objects/note.json"); + #[test] + fn test_parse_object_peertube() { + test_json::("assets/peertube/objects/person.json").unwrap(); + test_json::("assets/peertube/objects/group.json").unwrap(); + test_json::("assets/peertube/objects/video.json").unwrap(); + test_json::("assets/peertube/objects/note.json").unwrap(); + } - file_to_json_object::("assets/mastodon/objects/person.json"); - file_to_json_object::("assets/mastodon/objects/note.json"); + #[test] + fn test_parse_object_mobilizon() { + test_json::("assets/mobilizon/objects/group.json").unwrap(); + test_json::("assets/mobilizon/objects/event.json").unwrap(); + test_json::("assets/mobilizon/objects/person.json").unwrap(); } }