2 impls::actor_language::UNDETERMINED_ID,
4 source::language::Language,
7 use lemmy_utils::error::LemmyError;
8 use serde::{Deserialize, Serialize};
11 pub(crate) mod chat_message;
13 pub(crate) mod instance;
16 pub(crate) mod person;
17 pub(crate) mod tombstone;
19 #[derive(Clone, Debug, Deserialize, Serialize)]
20 #[serde(rename_all = "camelCase")]
21 pub struct Endpoints {
22 pub shared_inbox: Url,
25 /// As specified in https://schema.org/Language
26 #[derive(Clone, Debug, Deserialize, Serialize)]
27 #[serde(rename_all = "camelCase")]
28 pub(crate) struct LanguageTag {
29 pub(crate) identifier: String,
30 pub(crate) name: String,
34 pub(crate) async fn new_single(
36 pool: &mut DbPool<'_>,
37 ) -> Result<Option<LanguageTag>, LemmyError> {
38 let lang = Language::read_from_id(pool, lang).await?;
41 if lang.id == UNDETERMINED_ID {
45 identifier: lang.code,
51 pub(crate) async fn new_multiple(
52 lang_ids: Vec<LanguageId>,
53 pool: &mut DbPool<'_>,
54 ) -> Result<Vec<LanguageTag>, LemmyError> {
55 let mut langs = Vec::<Language>::new();
58 langs.push(Language::read_from_id(pool, l).await?);
63 .map(|l| LanguageTag {
71 pub(crate) async fn to_language_id_single(
73 pool: &mut DbPool<'_>,
74 ) -> Result<Option<LanguageId>, LemmyError> {
75 let identifier = lang.map(|l| l.identifier);
76 let language = Language::read_id_from_code(pool, identifier.as_deref()).await?;
81 pub(crate) async fn to_language_id_multiple(
83 pool: &mut DbPool<'_>,
84 ) -> Result<Vec<LanguageId>, LemmyError> {
85 let mut language_ids = Vec::new();
88 let id = l.identifier;
89 language_ids.push(Language::read_id_from_code(pool, Some(&id)).await?);
92 Ok(language_ids.into_iter().flatten().collect())
98 use crate::protocol::{
100 chat_message::ChatMessage,
106 tombstone::Tombstone,
108 tests::{test_json, test_parse_lemmy_item},
112 fn test_parse_objects_lemmy() {
113 test_parse_lemmy_item::<Instance>("assets/lemmy/objects/instance.json").unwrap();
114 test_parse_lemmy_item::<Group>("assets/lemmy/objects/group.json").unwrap();
115 test_parse_lemmy_item::<Person>("assets/lemmy/objects/person.json").unwrap();
116 test_parse_lemmy_item::<Page>("assets/lemmy/objects/page.json").unwrap();
117 test_parse_lemmy_item::<Note>("assets/lemmy/objects/note.json").unwrap();
118 test_parse_lemmy_item::<ChatMessage>("assets/lemmy/objects/chat_message.json").unwrap();
119 test_parse_lemmy_item::<Tombstone>("assets/lemmy/objects/tombstone.json").unwrap();
123 fn test_parse_objects_pleroma() {
124 test_json::<Person>("assets/pleroma/objects/person.json").unwrap();
125 test_json::<Note>("assets/pleroma/objects/note.json").unwrap();
126 test_json::<ChatMessage>("assets/pleroma/objects/chat_message.json").unwrap();
130 fn test_parse_objects_smithereen() {
131 test_json::<Person>("assets/smithereen/objects/person.json").unwrap();
132 test_json::<Note>("assets/smithereen/objects/note.json").unwrap();
136 fn test_parse_objects_mastodon() {
137 test_json::<Person>("assets/mastodon/objects/person.json").unwrap();
138 test_json::<Note>("assets/mastodon/objects/note.json").unwrap();
139 test_json::<Page>("assets/mastodon/objects/page.json").unwrap();
143 fn test_parse_objects_lotide() {
144 test_json::<Group>("assets/lotide/objects/group.json").unwrap();
145 test_json::<Person>("assets/lotide/objects/person.json").unwrap();
146 test_json::<Note>("assets/lotide/objects/note.json").unwrap();
147 test_json::<Page>("assets/lotide/objects/page.json").unwrap();
148 test_json::<Tombstone>("assets/lotide/objects/tombstone.json").unwrap();
152 fn test_parse_object_friendica() {
153 test_json::<Person>("assets/friendica/objects/person_1.json").unwrap();
154 test_json::<Person>("assets/friendica/objects/person_2.json").unwrap();
155 test_json::<Page>("assets/friendica/objects/page_1.json").unwrap();
156 test_json::<Page>("assets/friendica/objects/page_2.json").unwrap();
157 test_json::<Note>("assets/friendica/objects/note_1.json").unwrap();
158 test_json::<Note>("assets/friendica/objects/note_2.json").unwrap();
162 fn test_parse_object_gnusocial() {
163 test_json::<Person>("assets/gnusocial/objects/person.json").unwrap();
164 test_json::<Group>("assets/gnusocial/objects/group.json").unwrap();
165 test_json::<Page>("assets/gnusocial/objects/page.json").unwrap();
166 test_json::<Note>("assets/gnusocial/objects/note.json").unwrap();
170 fn test_parse_object_peertube() {
171 test_json::<Person>("assets/peertube/objects/person.json").unwrap();
172 test_json::<Group>("assets/peertube/objects/group.json").unwrap();
173 test_json::<Page>("assets/peertube/objects/video.json").unwrap();
174 test_json::<Note>("assets/peertube/objects/note.json").unwrap();
178 fn test_parse_object_mobilizon() {
179 test_json::<Group>("assets/mobilizon/objects/group.json").unwrap();
180 test_json::<Page>("assets/mobilizon/objects/event.json").unwrap();
181 test_json::<Person>("assets/mobilizon/objects/person.json").unwrap();