1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
mod templates;
pub use templates::{ErrorPage, MainPage, Template, POSTS_PER_PAGE, Entry, VCard, Feed};
mod onboarding;
pub use onboarding::OnboardingPage;
mod login;
pub use login::LoginPage;
#[cfg(test)]
mod tests {
use serde_json::json;
use microformats::types::{Document, Item, PropertyValue, Url};
use std::cell::RefCell;
use std::rc::Rc;
use faker_rand::en_us::internet::Domain;
enum PostType {
Note,
Article,
ReplyTo(serde_json::Value),
ReplyToLink(String),
LikeOf(serde_json::Value),
LikeOfLink(String)
}
fn gen_hcard(domain: &str) -> serde_json::Value {
use faker_rand::en_us::names::FirstName;
json!({
"type": ["h-card"],
"properties": {
"name": [rand::random::<FirstName>().to_string()],
"photo": [format!("https://{domain}/media/me.png")],
"uid": [format!("https://{domain}/")],
"url": [format!("https://{domain}/")]
}
})
}
fn gen_random_post(domain: &str, kind: PostType) -> serde_json::Value {
use faker_rand::lorem::{Paragraph, Word, Sentence};
fn html(content: Paragraph) -> serde_json::Value {
json!({
"html": format!("<p>{}</p>", content),
"value": content.to_string()
})
}
let uid = format!(
"https://{domain}/posts/{}-{}-{}",
rand::random::<Word>(), rand::random::<Word>(), rand::random::<Word>()
);
let dt = chrono::offset::Local::now()
.to_rfc3339_opts(chrono::SecondsFormat::Secs, true);
match kind {
PostType::Note => {
let content = rand::random::<Paragraph>();
json!({
"type": ["h-entry"],
"properties": {
"content": [html(content)],
"published": [dt],
"uid": [&uid], "url": [&uid],
"author": [gen_hcard(domain)]
}
})
}
PostType::Article => {
let content = rand::random::<Paragraph>();
let name = rand::random::<Sentence>();
json!({
"type": ["h-entry"],
"properties": {
"content": [html(content)],
"published": [dt],
"uid": [&uid], "url": [&uid],
"author": [gen_hcard(domain)],
"name": [name.to_string()]
}
})
}
PostType::ReplyTo(ctx) => {
let content = rand::random::<Paragraph>();
json!({
"type": ["h-entry"],
"properties": {
"content": [html(content)],
"published": [dt],
"uid": [&uid], "url": [&uid],
"author": [gen_hcard(domain)],
"in-reply-to": [{
"type": ["h-cite"],
"properties": ctx["properties"]
}]
}
})
},
PostType::ReplyToLink(link) => {
let content = rand::random::<Paragraph>();
json!({
"type": ["h-entry"],
"properties": {
"content": [html(content)],
"published": [dt],
"uid": [&uid], "url": [&uid],
"author": [gen_hcard(domain)],
"in-reply-to": [link]
}
})
},
PostType::LikeOf(ctx) => {
json!({
"type": ["h-entry"],
"properties": {
"published": [dt],
"author": [gen_hcard(domain)],
"uid": [&uid], "url": [&uid],
"like-of": [{
"type": ["h-cite"],
"properties": ctx["properties"]
}]
}
})
},
PostType::LikeOfLink(link) => {
json!({
"type": ["h-entry"],
"properties": {
"published": [dt],
"author": [gen_hcard(domain)],
"uid": [&uid], "url": [&uid],
"like-of": [link]
}
})
}
}
}
fn check_dt_published(
mf2: &serde_json::Value,
item: &Rc<RefCell<Item>>
) {
use microformats::types::temporal::Value as TemporalValue;
let _item = item.borrow();
let props = _item.properties.borrow();
assert!(props.contains_key("published"));
if let Some(PropertyValue::Temporal(
TemporalValue::Timestamp(item)
)) = props.get("published")
.and_then(|v| v.first())
{
use chrono::{DateTime, FixedOffset, NaiveDateTime};
// Faithfully reconstruct the original datetime
// I wonder why not just have an Enum that would
// get you either date, time or a datetime,
// potentially with an offset?
let offset = item.as_offset().unwrap().data;
let ndt: NaiveDateTime = item.as_date().unwrap().data
.and_time(item.as_time().unwrap().data)
// subtract the offset here, since we will add it back
- offset;
let dt = DateTime::<FixedOffset>::from_utc(ndt, offset);
let expected: DateTime<FixedOffset> = chrono::DateTime::parse_from_rfc3339(
mf2["properties"]["published"][0].as_str().unwrap()
).unwrap();
assert_eq!(dt, expected);
} else {
unreachable!()
}
}
fn check_e_content(
mf2: &serde_json::Value,
item: &Rc<RefCell<Item>>
) {
let _item = item.borrow();
let props = _item.properties.borrow();
assert!(props.contains_key("content"));
if let Some(PropertyValue::Fragment(content)) =
props.get("content")
.and_then(|v| v.first())
{
assert_eq!(
content.html,
mf2["properties"]["content"][0]["html"].as_str().unwrap()
);
} else {
unreachable!()
}
}
#[test]
#[ignore = "see https://gitlab.com/maxburon/microformats-parser/-/issues/7"]
fn test_note() {
test_logger::ensure_env_logger_initialized();
let mf2 = gen_random_post(
&rand::random::<Domain>().to_string(),
PostType::Note
);
let html = crate::templates::Entry {
post: &mf2
}.to_string();
let url: Url = mf2.pointer("/properties/uid/0")
.and_then(|i| i.as_str())
.and_then(|u| u.parse().ok())
.unwrap();
let parsed: Document = microformats::from_html(&html, url.clone()).unwrap();
if let Some(PropertyValue::Item(item)) = parsed.get_item_by_url(&url) {
let _item = item.borrow();
let props = _item.properties.borrow();
check_e_content(&mf2, &item);
check_dt_published(&mf2, &item);
assert!(props.contains_key("uid"));
assert!(props.contains_key("url"));
assert!(props.get("url")
.unwrap()
.iter()
.any(|i| i == props.get("uid").and_then(|v| v.first()).unwrap()));
// XXX: fails because of https://gitlab.com/maxburon/microformats-parser/-/issues/7
assert!(!props.contains_key("name"));
} else {
unreachable!()
}
}
#[test]
fn test_article() {
test_logger::ensure_env_logger_initialized();
let mf2 = gen_random_post(
&rand::random::<Domain>().to_string(),
PostType::Article
);
let html = crate::templates::Entry {
post: &mf2
}.to_string();
let url: Url = mf2.pointer("/properties/uid/0")
.and_then(|i| i.as_str())
.and_then(|u| u.parse().ok())
.unwrap();
let parsed: Document = microformats::from_html(&html, url.clone()).unwrap();
if let Some(PropertyValue::Item(item)) = parsed.get_item_by_url(&url) {
let _item = item.borrow();
let props = _item.properties.borrow();
check_e_content(&mf2, &item);
check_dt_published(&mf2, &item);
assert!(props.contains_key("uid"));
assert!(props.contains_key("url"));
assert!(props.get("url")
.unwrap()
.iter()
.any(|i| i == props.get("uid").and_then(|v| v.first()).unwrap()));
assert!(props.contains_key("name"));
if let Some(PropertyValue::Plain(name)) = props.get("name").and_then(|v| v.first()) {
assert_eq!(
name,
mf2.pointer("/properties/name/0")
.and_then(|v| v.as_str())
.unwrap()
);
} else {
panic!("Name wasn't a plain property!");
}
} else {
unreachable!()
}
}
}
|