X-Git-Url: http://these/git/?a=blobdiff_plain;f=crates%2Fapub%2Fsrc%2Fprotocol%2Fobjects%2Fmod.rs;h=9a3dab1859949d91c1c3b9f542fd0eb5970df1d8;hb=92568956353f21649ed9aff68b42699c9d036f30;hp=864c13d9ef104ac490c242378cdf855441dbbba3;hpb=c71888247948a69fc7a772ca397603d0b44e5222;p=lemmy.git diff --git a/crates/apub/src/protocol/objects/mod.rs b/crates/apub/src/protocol/objects/mod.rs index 864c13d9..9a3dab18 100644 --- a/crates/apub/src/protocol/objects/mod.rs +++ b/crates/apub/src/protocol/objects/mod.rs @@ -1,3 +1,10 @@ +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; @@ -15,8 +22,82 @@ pub struct Endpoints { 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 { + #![allow(clippy::unwrap_used)] + #![allow(clippy::indexing_slicing)] + use crate::protocol::{ objects::{ chat_message::ChatMessage, @@ -58,6 +139,7 @@ mod tests { 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] @@ -86,4 +168,19 @@ mod tests { test_json::("assets/gnusocial/objects/page.json").unwrap(); test_json::("assets/gnusocial/objects/note.json").unwrap(); } + + #[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(); + } + + #[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(); + } }