about summary refs log tree commit diff
path: root/kittybox-rs/src/micropub/util.rs
blob: 940d7c3b0aacea1ff293592fad2117683abc7a7d (plain) (blame)
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use crate::database::Storage;
use kittybox_indieauth::TokenData;
use chrono::prelude::*;
use core::iter::Iterator;
use newbase60::num_to_sxg;
use serde_json::json;
use std::convert::TryInto;

pub(crate) const DEFAULT_CHANNEL_PATH: &str = "/feeds/main";
const DEFAULT_CHANNEL_NAME: &str = "Main feed";
pub(crate) const CONTACTS_CHANNEL_PATH: &str = "/feeds/vcards";
const CONTACTS_CHANNEL_NAME: &str = "My address book";
pub(crate) const FOOD_CHANNEL_PATH: &str = "/feeds/food";
const FOOD_CHANNEL_NAME: &str = "My recipe book";

fn get_folder_from_type(post_type: &str) -> String {
    (match post_type {
        "h-feed" => "feeds/",
        "h-card" => "vcards/",
        "h-event" => "events/",
        "h-food" => "food/",
        _ => "posts/",
    })
    .to_string()
}

/// Reset the datetime to a proper datetime.
/// Do not attempt to recover the information.
/// Do not pass GO. Do not collect $200.                
fn reset_dt(post: &mut serde_json::Value) -> DateTime<FixedOffset> {
    let curtime: DateTime<Local> = Local::now();
    post["properties"]["published"] = json!([curtime.to_rfc3339()]);
    chrono::DateTime::from(curtime)
}

pub fn normalize_mf2(mut body: serde_json::Value, user: &TokenData) -> (String, serde_json::Value) {
    // Normalize the MF2 object here.
    let me = &user.me;
    let folder = get_folder_from_type(body["type"][0].as_str().unwrap());
    let published: DateTime<FixedOffset> =
        if let Some(dt) = body["properties"]["published"][0].as_str() {
            // Check if the datetime is parsable.
            match DateTime::parse_from_rfc3339(dt) {
                Ok(dt) => dt,
                Err(_) => reset_dt(&mut body),
            }
        } else {
            // Set the datetime.
            // Note: this code block duplicates functionality with the above failsafe.
            // Consider refactoring it to a helper function?
            reset_dt(&mut body)
        };
    match body["properties"]["uid"][0].as_str() {
        None => {
            let uid = serde_json::Value::String(
                me.join(
                    &(folder.clone()
                        + &num_to_sxg(published.timestamp_millis().try_into().unwrap())),
                )
                .unwrap()
                .to_string(),
            );
            body["properties"]["uid"] = serde_json::Value::Array(vec![uid.clone()]);
            match body["properties"]["url"].as_array_mut() {
                Some(array) => array.push(uid),
                None => body["properties"]["url"] = body["properties"]["uid"].clone(),
            }
        }
        Some(uid_str) => {
            let uid = uid_str.to_string();
            match body["properties"]["url"].as_array_mut() {
                Some(array) => {
                    if !array.iter().any(|i| i.as_str().unwrap_or("") == uid) {
                        array.push(serde_json::Value::String(uid))
                    }
                }
                None => body["properties"]["url"] = body["properties"]["uid"].clone(),
            }
        }
    }
    if let Some(slugs) = body["properties"]["mp-slug"].as_array() {
        let new_urls = slugs
            .iter()
            .map(|i| i.as_str().unwrap_or(""))
            .filter(|i| i != &"")
            .map(|i| me.join(&((&folder).clone() + i)).unwrap().to_string())
            .collect::<Vec<String>>();
        let urls = body["properties"]["url"].as_array_mut().unwrap();
        new_urls.iter().for_each(|i| urls.push(json!(i)));
    }
    let props = body["properties"].as_object_mut().unwrap();
    props.remove("mp-slug");

    if body["properties"]["content"][0].is_string() {
        // Convert the content to HTML using the `markdown` crate
        body["properties"]["content"] = json!([{
            "html": markdown::to_html(body["properties"]["content"][0].as_str().unwrap()),
            "value": body["properties"]["content"][0]
        }])
    }
    // TODO: apply this normalization to editing too
    if body["properties"]["mp-channel"].is_array() {
        let mut additional_channels = body["properties"]["mp-channel"].as_array().unwrap().clone();
        if let Some(array) = body["properties"]["channel"].as_array_mut() {
            array.append(&mut additional_channels);
        } else {
            body["properties"]["channel"] = json!(additional_channels)
        }
        body["properties"]
            .as_object_mut()
            .unwrap()
            .remove("mp-channel");
    } else if body["properties"]["mp-channel"].is_string() {
        let chan = body["properties"]["mp-channel"]
            .as_str()
            .unwrap()
            .to_owned();
        if let Some(array) = body["properties"]["channel"].as_array_mut() {
            array.push(json!(chan))
        } else {
            body["properties"]["channel"] = json!([chan]);
        }
        body["properties"]
            .as_object_mut()
            .unwrap()
            .remove("mp-channel");
    }
    if body["properties"]["channel"][0].as_str().is_none() {
        match body["type"][0].as_str() {
            Some("h-entry") => {
                // Set the channel to the main channel...
                // TODO find like posts and move them to separate private channel
                let default_channel = me.join(DEFAULT_CHANNEL_PATH).unwrap().to_string();

                body["properties"]["channel"] = json!([default_channel]);
            }
            Some("h-card") => {
                let default_channel = me.join(CONTACTS_CHANNEL_PATH).unwrap().to_string();

                body["properties"]["channel"] = json!([default_channel]);
            }
            Some("h-food") => {
                let default_channel = me.join(FOOD_CHANNEL_PATH).unwrap().to_string();

                body["properties"]["channel"] = json!([default_channel]);
            }
            // TODO h-event
            /*"h-event" => {
                let default_channel
            },*/
            _ => {
                body["properties"]["channel"] = json!([]);
            }
        }
    }
    body["properties"]["posted-with"] = json!([user.client_id]);
    if body["properties"]["author"][0].as_str().is_none() {
        body["properties"]["author"] = json!([me.as_str()])
    }
    // TODO: maybe highlight #hashtags?
    // Find other processing to do and insert it here
    return (
        body["properties"]["uid"][0].as_str().unwrap().to_string(),
        body,
    );
}

pub(crate) fn form_to_mf2_json(form: Vec<(String, String)>) -> serde_json::Value {
    let mut mf2 = json!({"type": [], "properties": {}});
    for (k, v) in form {
        if k == "h" {
            mf2["type"]
                .as_array_mut()
                .unwrap()
                .push(json!("h-".to_string() + &v));
        } else if k != "access_token" {
            let key = k.strip_suffix("[]").unwrap_or(&k);
            match mf2["properties"][key].as_array_mut() {
                Some(prop) => prop.push(json!(v)),
                None => mf2["properties"][key] = json!([v]),
            }
        }
    }
    if mf2["type"].as_array().unwrap().is_empty() {
        mf2["type"].as_array_mut().unwrap().push(json!("h-entry"));
    }
    mf2
}

pub(crate) async fn create_feed(
    storage: &impl Storage,
    uid: &str,
    channel: &str,
    user: &TokenData,
) -> crate::database::Result<()> {
    let path = url::Url::parse(channel).unwrap().path().to_string();

    let name = match path.as_str() {
        DEFAULT_CHANNEL_PATH => DEFAULT_CHANNEL_NAME,
        CONTACTS_CHANNEL_PATH => CONTACTS_CHANNEL_NAME,
        FOOD_CHANNEL_PATH => FOOD_CHANNEL_NAME,
        _ => panic!("Tried to create an unknown default feed!"),
    };

    let (_, feed) = normalize_mf2(
        json!({
            "type": ["h-feed"],
            "properties": {
                "name": [name],
                "uid": [channel]
            },
        }),
        user,
    );
    storage.put_post(&feed, user.me.as_str()).await?;
    storage.add_to_feed(channel, uid).await
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn token_data() -> TokenData {
        TokenData {
            me: "https://fireburn.ru/".parse().unwrap(),
            client_id: "https://quill.p3k.io/".parse().unwrap(),
            scope: kittybox_indieauth::Scopes::new(vec![kittybox_indieauth::Scope::Create]),
            exp: Some(u64::MAX),
            iat: Some(0)
        }
    }

    #[test]
    fn test_form_to_mf2() {
        assert_eq!(
            super::form_to_mf2_json(
                serde_urlencoded::from_str("h=entry&content=something%20interesting").unwrap()
            ),
            json!({
                "type": ["h-entry"],
                "properties": {
                    "content": ["something interesting"]
                }
            })
        )
    }

    #[test]
    fn test_no_replace_uid() {
        let mf2 = json!({
            "type": ["h-card"],
            "properties": {
                "uid": ["https://fireburn.ru/"],
                "name": ["Vika Nezrimaya"],
                "note": ["A crazy programmer girl who wants some hugs"]
            }
        });

        let (uid, normalized) = normalize_mf2(
            mf2.clone(),
            &token_data(),
        );
        assert_eq!(
            normalized["properties"]["uid"][0], mf2["properties"]["uid"][0],
            "UID was replaced"
        );
        assert_eq!(
            normalized["properties"]["uid"][0], uid,
            "Returned post location doesn't match UID"
        );
    }

    #[test]
    fn test_mp_channel() {
        let mf2 = json!({
            "type": ["h-entry"],
            "properties": {
                "uid": ["https://fireburn.ru/posts/test"],
                "content": [{"html": "<p>Hello world!</p>"}],
                "mp-channel": ["https://fireburn.ru/feeds/test"]
            }
        });

        let (_, normalized) = normalize_mf2(
            mf2.clone(),
            &token_data(),
        );

        assert_eq!(
            normalized["properties"]["channel"],
            mf2["properties"]["mp-channel"]
        );
    }

    #[test]
    fn test_mp_channel_as_string() {
        let mf2 = json!({
            "type": ["h-entry"],
            "properties": {
                "uid": ["https://fireburn.ru/posts/test"],
                "content": [{"html": "<p>Hello world!</p>"}],
                "mp-channel": "https://fireburn.ru/feeds/test"
            }
        });

        let (_, normalized) = normalize_mf2(
            mf2.clone(),
            &token_data(),
        );

        assert_eq!(
            normalized["properties"]["channel"][0],
            mf2["properties"]["mp-channel"]
        );
    }

    #[test]
    fn test_normalize_mf2() {
        let mf2 = json!({
            "type": ["h-entry"],
            "properties": {
                "content": ["This is content!"]
            }
        });

        let (uid, post) = normalize_mf2(
            mf2,
            &token_data(),
        );
        assert_eq!(
            post["properties"]["published"]
                .as_array()
                .expect("post['published'] is undefined")
                .len(),
            1,
            "Post doesn't have a published time"
        );
        DateTime::parse_from_rfc3339(post["properties"]["published"][0].as_str().unwrap())
            .expect("Couldn't parse date from rfc3339");
        assert!(
            !post["properties"]["url"]
                .as_array()
                .expect("post['url'] is undefined")
                .is_empty(),
            "Post doesn't have any URLs"
        );
        assert_eq!(
            post["properties"]["uid"]
                .as_array()
                .expect("post['uid'] is undefined")
                .len(),
            1,
            "Post doesn't have a single UID"
        );
        assert_eq!(
            post["properties"]["uid"][0], uid,
            "UID of a post and its supposed location don't match"
        );
        assert!(
            uid.starts_with("https://fireburn.ru/posts/"),
            "The post namespace is incorrect"
        );
        assert_eq!(
            post["properties"]["content"][0]["html"]
                .as_str()
                .expect("Post doesn't have a rich content object")
                .trim(),
            "<p>This is content!</p>",
            "Parsed Markdown content doesn't match expected HTML"
        );
        assert_eq!(
            post["properties"]["channel"][0], "https://fireburn.ru/feeds/main",
            "Post isn't posted to the main channel"
        );
        assert_eq!(
            post["properties"]["author"][0], "https://fireburn.ru/",
            "Post author is unknown"
        );
    }

    #[test]
    fn test_mp_slug() {
        let mf2 = json!({
            "type": ["h-entry"],
            "properties": {
                "content": ["This is content!"],
                "mp-slug": ["hello-post"]
            },
        });

        let (_, post) = normalize_mf2(
            mf2,
            &token_data(),
        );
        assert!(
            post["properties"]["url"]
                .as_array()
                .unwrap()
                .iter()
                .map(|i| i.as_str().unwrap())
                .any(|i| i == "https://fireburn.ru/posts/hello-post"),
            "Didn't found an URL pointing to the location expected by the mp-slug semantics"
        );
        assert!(
            post["properties"]["mp-slug"].as_array().is_none(),
            "mp-slug wasn't deleted from the array!"
        )
    }

    #[test]
    fn test_normalize_feed() {
        let mf2 = json!({
            "type": ["h-feed"],
            "properties": {
                "name": "Main feed",
                "mp-slug": ["main"]
            }
        });

        let (uid, post) = normalize_mf2(
            mf2,
            &token_data(),
        );
        assert_eq!(
            post["properties"]["uid"][0], uid,
            "UID of a post and its supposed location don't match"
        );
        assert_eq!(post["properties"]["author"][0], "https://fireburn.ru/");
        assert!(
            post["properties"]["url"]
                .as_array()
                .unwrap()
                .iter()
                .map(|i| i.as_str().unwrap())
                .any(|i| i == "https://fireburn.ru/feeds/main"),
            "Didn't found an URL pointing to the location expected by the mp-slug semantics"
        );
        assert!(
            post["properties"]["mp-slug"].as_array().is_none(),
            "mp-slug wasn't deleted from the array!"
        )
    }
}