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
/*!
These are the link previews that iMessage generates when sending links. They may
contain metadata, even if the page the link points to no longer exists on the internet.
*/

use plist::Value;

use crate::{
    error::plist::PlistParseError,
    message_types::{
        app_store::AppStoreMessage,
        collaboration::CollaborationMessage,
        music::MusicMessage,
        variants::{BalloonProvider, URLOverride},
    },
    util::plist::{get_bool_from_dict, get_string_from_dict, get_string_from_nested_dict},
};

/// This struct is not documented by Apple, but represents messages created by
/// `com.apple.messages.URLBalloonProvider`.
#[derive(Debug, PartialEq, Eq)]
pub struct URLMessage<'a> {
    /// The webpage's `<og:title>` attribute
    pub title: Option<&'a str>,
    /// The webpage's `<og:description>` attribute
    pub summary: Option<&'a str>,
    /// The URL that ended up serving content, after all redirects
    pub url: Option<&'a str>,
    /// The original url, before any redirects
    pub original_url: Option<&'a str>,
    /// The type of webpage Apple thinks the link represents
    pub item_type: Option<&'a str>,
    /// Up to 4 image previews displayed in the background of the bubble
    pub images: Vec<&'a str>,
    /// Icons that represent the website, generally the favicon or apple-touch-icon
    pub icons: Vec<&'a str>,
    /// The name of a website
    pub site_name: Option<&'a str>,
    pub placeholder: bool,
}

impl<'a> BalloonProvider<'a> for URLMessage<'a> {
    fn from_map(payload: &'a Value) -> Result<Self, PlistParseError> {
        let url_metadata = URLMessage::get_body(payload)?;
        Ok(URLMessage {
            title: get_string_from_dict(url_metadata, "title"),
            summary: get_string_from_dict(url_metadata, "summary"),
            url: get_string_from_nested_dict(url_metadata, "URL"),
            original_url: get_string_from_nested_dict(url_metadata, "originalURL"),
            item_type: get_string_from_dict(url_metadata, "itemType"),
            images: URLMessage::get_array_from_nested_dict(url_metadata, "images")
                .unwrap_or_default(),
            icons: URLMessage::get_array_from_nested_dict(url_metadata, "icons")
                .unwrap_or_default(),
            site_name: get_string_from_dict(url_metadata, "siteName"),
            placeholder: get_bool_from_dict(url_metadata, "richLinkIsPlaceholder").unwrap_or(false),
        })
    }
}

impl<'a> URLMessage<'a> {
    /// Gets the subtype of the URL message based on the payload
    pub fn get_url_message_override(payload: &'a Value) -> Result<URLOverride, PlistParseError> {
        if let Ok(balloon) = CollaborationMessage::from_map(payload) {
            return Ok(URLOverride::Collaboration(balloon));
        }
        if let Ok(balloon) = MusicMessage::from_map(payload) {
            return Ok(URLOverride::AppleMusic(balloon));
        }
        if let Ok(balloon) = AppStoreMessage::from_map(payload) {
            return Ok(URLOverride::AppStore(balloon));
        }
        if let Ok(balloon) = URLMessage::from_map(payload) {
            return Ok(URLOverride::Normal(balloon));
        }
        Err(PlistParseError::NoPayload)
    }

    /// Extract the main dictionary of data from the body of the payload
    ///
    /// There are two known ways this data is stored: the more recent `richLinkMetadata` style,
    /// or some kind of social integration stored under a `metadata` key
    fn get_body(payload: &'a Value) -> Result<&'a Value, PlistParseError> {
        let root_dict = payload.as_dictionary().ok_or_else(|| {
            PlistParseError::InvalidType("root".to_string(), "dictionary".to_string())
        })?;

        if let Some(meta) = root_dict.get("richLinkMetadata") {
            return Ok(meta);
        };
        if let Some(meta) = root_dict.get("metadata") {
            return Ok(meta);
        };
        Err(PlistParseError::NoPayload)
    }
    /// Extract the array of image URLs from a URL message payload.
    ///
    /// The array consists of dictionaries that look like this:
    /// ```json
    /// [
    ///     {
    ///         "size": String("{0, 0}"),
    ///         "URL": {
    ///              "URL": String("https://chrissardegna.com/example.png")
    ///          },
    ///         "images": Integer(1)
    ///     },
    ///     ...
    /// ]
    /// ```
    fn get_array_from_nested_dict(payload: &'a Value, key: &str) -> Option<Vec<&'a str>> {
        payload
            .as_dictionary()?
            .get(key)?
            .as_dictionary()?
            .get(key)?
            .as_array()?
            .iter()
            .map(|item| get_string_from_nested_dict(item, "URL"))
            .collect()
    }

    /// Get the redirected URL from a URL message, falling back to the original URL, if it exists
    pub fn get_url(&self) -> Option<&str> {
        self.url.or(self.original_url)
    }
}

#[cfg(test)]
mod url_tests {
    use crate::{
        message_types::{url::URLMessage, variants::BalloonProvider},
        util::plist::parse_plist,
    };
    use plist::Value;
    use std::env::current_dir;
    use std::fs::File;

    #[test]
    fn test_parse_url_me() {
        let plist_path = current_dir()
            .unwrap()
            .as_path()
            .join("test_data/url_message/URL.plist");
        let plist_data = File::open(plist_path).unwrap();
        let plist = Value::from_reader(plist_data).unwrap();
        let parsed = parse_plist(&plist).unwrap();

        let balloon = URLMessage::from_map(&parsed).unwrap();
        let expected = URLMessage {
            title: Some("Christopher Sardegna"),
            summary: None,
            url: Some("https://chrissardegna.com/"),
            original_url: Some("https://chrissardegna.com"),
            item_type: None,
            images: vec![],
            icons: vec!["https://chrissardegna.com/favicon.ico"],
            site_name: None,
            placeholder: false,
        };

        assert_eq!(balloon, expected);
    }

    #[test]
    fn test_parse_url_me_metadata() {
        let plist_path = current_dir()
            .unwrap()
            .as_path()
            .join("test_data/url_message/MetadataURL.plist");
        let plist_data = File::open(plist_path).unwrap();
        let plist = Value::from_reader(plist_data).unwrap();
        let parsed = parse_plist(&plist).unwrap();

        let balloon = URLMessage::from_map(&parsed).unwrap();
        let expected = URLMessage {
            title: Some("Christopher Sardegna"),
            summary: Some("Sample page description"),
            url: Some("https://chrissardegna.com"),
            original_url: Some("https://chrissardegna.com"),
            item_type: Some("article"),
            images: vec!["https://chrissardegna.com/ddc-facebook-icon.png"],
            icons: vec![
                "https://chrissardegna.com/apple-touch-icon-180x180.png",
                "https://chrissardegna.com/ddc-icon-32x32.png",
                "https://chrissardegna.com/ddc-icon-16x16.png",
            ],
            site_name: Some("Christopher Sardegna"),
            placeholder: false,
        };

        assert_eq!(balloon, expected);
    }

    #[test]
    fn test_parse_url_twitter() {
        let plist_path = current_dir()
            .unwrap()
            .as_path()
            .join("test_data/url_message/Twitter.plist");
        let plist_data = File::open(plist_path).unwrap();
        let plist = Value::from_reader(plist_data).unwrap();
        let parsed = parse_plist(&plist).unwrap();

        let balloon = URLMessage::from_map(&parsed).unwrap();
        let expected = URLMessage {
            title: Some("Christopher Sardegna on Twitter"),
            summary: Some("“Hello Twitter, meet Bella”"),
            url: Some("https://twitter.com/rxcs/status/1175874352946077696"),
            original_url: Some("https://twitter.com/rxcs/status/1175874352946077696"),
            item_type: Some("article"),
            images: vec![
                "https://pbs.twimg.com/media/EFGLfR2X4AE8ItK.jpg:large",
                "https://pbs.twimg.com/media/EFGLfRmX4AMnwqW.jpg:large",
                "https://pbs.twimg.com/media/EFGLfRlXYAYn9Ce.jpg:large",
            ],
            icons: vec![
                "https://abs.twimg.com/icons/apple-touch-icon-192x192.png",
                "https://abs.twimg.com/favicons/favicon.ico",
            ],
            site_name: Some("Twitter"),
            placeholder: false,
        };

        assert_eq!(balloon, expected);
    }

    #[test]
    fn test_parse_url_reminder() {
        let plist_path = current_dir()
            .unwrap()
            .as_path()
            .join("test_data/url_message/Reminder.plist");
        let plist_data = File::open(plist_path).unwrap();
        let plist = Value::from_reader(plist_data).unwrap();
        let parsed = parse_plist(&plist).unwrap();

        let balloon = URLMessage::from_map(&parsed).unwrap();
        let expected = URLMessage {
            title: None,
            summary: None,
            url: None,
            original_url: Some(
                "https://www.icloud.com/reminders/ZmFrZXVybF9mb3JfcmVtaW5kZXI#TestList",
            ),
            item_type: None,
            images: vec![],
            icons: vec![],
            site_name: None,
            placeholder: false,
        };

        assert_eq!(balloon, expected);
    }

    #[test]
    fn test_get_url() {
        let expected = URLMessage {
            title: Some("Christopher Sardegna"),
            summary: None,
            url: Some("https://chrissardegna.com/"),
            original_url: Some("https://chrissardegna.com"),
            item_type: None,
            images: vec![],
            icons: vec!["https://chrissardegna.com/favicon.ico"],
            site_name: None,
            placeholder: false,
        };
        assert_eq!(expected.get_url(), Some("https://chrissardegna.com/"))
    }

    #[test]
    fn test_get_original_url() {
        let expected = URLMessage {
            title: Some("Christopher Sardegna"),
            summary: None,
            url: None,
            original_url: Some("https://chrissardegna.com"),
            item_type: None,
            images: vec![],
            icons: vec!["https://chrissardegna.com/favicon.ico"],
            site_name: None,
            placeholder: false,
        };
        assert_eq!(expected.get_url(), Some("https://chrissardegna.com"))
    }

    #[test]
    fn test_get_no_url() {
        let expected = URLMessage {
            title: Some("Christopher Sardegna"),
            summary: None,
            url: None,
            original_url: None,
            item_type: None,
            images: vec![],
            icons: vec!["https://chrissardegna.com/favicon.ico"],
            site_name: None,
            placeholder: false,
        };
        assert_eq!(expected.get_url(), None)
    }
}

#[cfg(test)]
mod url_override_tests {
    use crate::{
        message_types::{url::URLMessage, variants::URLOverride},
        util::plist::parse_plist,
    };
    use plist::Value;
    use std::env::current_dir;
    use std::fs::File;

    #[test]
    fn can_parse_normal() {
        let plist_path = current_dir()
            .unwrap()
            .as_path()
            .join("test_data/url_message/URL.plist");
        let plist_data = File::open(plist_path).unwrap();
        let plist = Value::from_reader(plist_data).unwrap();
        let parsed = parse_plist(&plist).unwrap();

        let balloon = URLMessage::get_url_message_override(&parsed).unwrap();
        assert!(matches!(balloon, URLOverride::Normal(_)))
    }

    #[test]
    fn can_parse_music() {
        let plist_path = current_dir()
            .unwrap()
            .as_path()
            .join("test_data/music_message/AppleMusic.plist");
        let plist_data = File::open(plist_path).unwrap();
        let plist = Value::from_reader(plist_data).unwrap();
        let parsed = parse_plist(&plist).unwrap();

        let balloon = URLMessage::get_url_message_override(&parsed).unwrap();
        assert!(matches!(balloon, URLOverride::AppleMusic(_)))
    }

    #[test]
    fn can_parse_app_store() {
        let plist_path = current_dir()
            .unwrap()
            .as_path()
            .join("test_data/app_store/AppStoreLink.plist");
        let plist_data = File::open(plist_path).unwrap();
        let plist = Value::from_reader(plist_data).unwrap();
        let parsed = parse_plist(&plist).unwrap();

        let balloon = URLMessage::get_url_message_override(&parsed).unwrap();
        assert!(matches!(balloon, URLOverride::AppStore(_)))
    }

    #[test]
    fn can_parse_collaboration() {
        let plist_path = current_dir()
            .unwrap()
            .as_path()
            .join("test_data/collaboration_message/Freeform.plist");
        let plist_data = File::open(plist_path).unwrap();
        let plist = Value::from_reader(plist_data).unwrap();
        let parsed = parse_plist(&plist).unwrap();

        let balloon = URLMessage::get_url_message_override(&parsed).unwrap();
        assert!(matches!(balloon, URLOverride::Collaboration(_)))
    }
}