]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/objects/mod.rs
Add language tags for comments
[lemmy.git] / crates / apub / src / protocol / objects / mod.rs
1 use lemmy_db_schema::source::language::Language;
2 use serde::{Deserialize, Serialize};
3 use url::Url;
4
5 pub(crate) mod chat_message;
6 pub(crate) mod group;
7 pub(crate) mod instance;
8 pub(crate) mod note;
9 pub(crate) mod page;
10 pub(crate) mod person;
11 pub(crate) mod tombstone;
12
13 #[derive(Clone, Debug, Deserialize, Serialize)]
14 #[serde(rename_all = "camelCase")]
15 pub struct Endpoints {
16   pub shared_inbox: Url,
17 }
18
19 #[derive(Clone, Debug, Deserialize, Serialize)]
20 #[serde(rename_all = "camelCase")]
21 pub(crate) struct LanguageTag {
22   pub(crate) identifier: String,
23   pub(crate) name: String,
24 }
25
26 impl LanguageTag {
27   pub(crate) fn new(lang: Language) -> Option<LanguageTag> {
28     // undetermined
29     if lang.code == "und" {
30       None
31     } else {
32       Some(LanguageTag {
33         identifier: lang.code,
34         name: lang.name,
35       })
36     }
37   }
38 }
39
40 #[cfg(test)]
41 mod tests {
42   use crate::protocol::{
43     objects::{
44       chat_message::ChatMessage,
45       group::Group,
46       instance::Instance,
47       note::Note,
48       page::Page,
49       person::Person,
50       tombstone::Tombstone,
51     },
52     tests::{test_json, test_parse_lemmy_item},
53   };
54
55   #[test]
56   fn test_parse_objects_lemmy() {
57     test_parse_lemmy_item::<Instance>("assets/lemmy/objects/instance.json").unwrap();
58     test_parse_lemmy_item::<Group>("assets/lemmy/objects/group.json").unwrap();
59     test_parse_lemmy_item::<Person>("assets/lemmy/objects/person.json").unwrap();
60     test_parse_lemmy_item::<Page>("assets/lemmy/objects/page.json").unwrap();
61     test_parse_lemmy_item::<Note>("assets/lemmy/objects/note.json").unwrap();
62     test_parse_lemmy_item::<ChatMessage>("assets/lemmy/objects/chat_message.json").unwrap();
63     test_parse_lemmy_item::<Tombstone>("assets/lemmy/objects/tombstone.json").unwrap();
64   }
65
66   #[test]
67   fn test_parse_objects_pleroma() {
68     test_json::<Person>("assets/pleroma/objects/person.json").unwrap();
69     test_json::<Note>("assets/pleroma/objects/note.json").unwrap();
70     test_json::<ChatMessage>("assets/pleroma/objects/chat_message.json").unwrap();
71   }
72
73   #[test]
74   fn test_parse_objects_smithereen() {
75     test_json::<Person>("assets/smithereen/objects/person.json").unwrap();
76     test_json::<Note>("assets/smithereen/objects/note.json").unwrap();
77   }
78
79   #[test]
80   fn test_parse_objects_mastodon() {
81     test_json::<Person>("assets/mastodon/objects/person.json").unwrap();
82     test_json::<Note>("assets/mastodon/objects/note.json").unwrap();
83   }
84
85   #[test]
86   fn test_parse_objects_lotide() {
87     test_json::<Group>("assets/lotide/objects/group.json").unwrap();
88     test_json::<Person>("assets/lotide/objects/person.json").unwrap();
89     test_json::<Note>("assets/lotide/objects/note.json").unwrap();
90     test_json::<Page>("assets/lotide/objects/page.json").unwrap();
91     test_json::<Tombstone>("assets/lotide/objects/tombstone.json").unwrap();
92   }
93
94   #[test]
95   fn test_parse_object_friendica() {
96     test_json::<Person>("assets/friendica/objects/person_1.json").unwrap();
97     test_json::<Person>("assets/friendica/objects/person_2.json").unwrap();
98     test_json::<Page>("assets/friendica/objects/page_1.json").unwrap();
99     test_json::<Page>("assets/friendica/objects/page_2.json").unwrap();
100     test_json::<Note>("assets/friendica/objects/note_1.json").unwrap();
101     test_json::<Note>("assets/friendica/objects/note_2.json").unwrap();
102   }
103
104   #[test]
105   fn test_parse_object_gnusocial() {
106     test_json::<Person>("assets/gnusocial/objects/person.json").unwrap();
107     test_json::<Group>("assets/gnusocial/objects/group.json").unwrap();
108     test_json::<Page>("assets/gnusocial/objects/page.json").unwrap();
109     test_json::<Note>("assets/gnusocial/objects/note.json").unwrap();
110   }
111
112   #[test]
113   fn test_parse_object_peertube() {
114     test_json::<Person>("assets/peertube/objects/person.json").unwrap();
115     test_json::<Group>("assets/peertube/objects/group.json").unwrap();
116     test_json::<Page>("assets/peertube/objects/video.json").unwrap();
117     test_json::<Note>("assets/peertube/objects/note.json").unwrap();
118   }
119 }