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
/*!
 These are the link previews that iMessage generates when sending locations or points of interest from the Maps app.
*/

use plist::Value;

use crate::{
    error::plist::PlistParseError,
    message_types::variants::BalloonProvider,
    util::plist::{get_string_from_dict, get_string_from_nested_dict},
};

/// Representation of Apple's [`CLPlacemark`](https://developer.apple.com/documentation/corelocation/clplacemark) object
#[derive(Debug, PartialEq, Eq, Default)]
pub struct Placemark<'a> {
    pub name: Option<&'a str>,
    pub address: Option<&'a str>,
    pub state: Option<&'a str>,
    pub city: Option<&'a str>,
    pub iso_country_code: Option<&'a str>,
    pub postal_code: Option<&'a str>,
    pub country: Option<&'a str>,
    pub street: Option<&'a str>,
    pub sub_administrative_area: Option<&'a str>,
    pub sub_locality: Option<&'a str>,
}

impl<'a> Placemark<'a> {
    /// Create a Placemark from a `specialization2` payload
    fn new(payload: &'a Value) -> Result<Self, PlistParseError> {
        // Parse out the address components dict
        let address_components = payload
            .as_dictionary()
            .ok_or_else(|| {
                PlistParseError::InvalidType(
                    "specialization2".to_string(),
                    "dictionary".to_string(),
                )
            })?
            .get("addressComponents")
            .ok_or_else(|| PlistParseError::MissingKey("addressComponents".to_string()))?;
        Ok(Self {
            name: get_string_from_dict(payload, "name"),
            address: get_string_from_dict(payload, "address"),
            state: get_string_from_dict(address_components, "_state"),
            city: get_string_from_dict(address_components, "_city"),
            iso_country_code: get_string_from_dict(address_components, "_ISOCountryCode"),
            postal_code: get_string_from_dict(address_components, "_postalCode"),
            country: get_string_from_dict(address_components, "_country"),
            street: get_string_from_dict(address_components, "_street"),
            sub_administrative_area: get_string_from_dict(
                address_components,
                "_subAdministrativeArea",
            ),
            sub_locality: get_string_from_dict(address_components, "_subLocality"),
        })
    }
}

/// This struct is not documented by Apple, but represents messages displayed as
/// `com.apple.messages.URLBalloonProvider` but for the Maps app
#[derive(Debug, PartialEq, Eq)]
pub struct PlacemarkMessage<'a> {
    /// 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 full street address of the location
    pub place_name: Option<&'a str>,
    /// The short description of the app in the App Store
    pub placemark: Placemark<'a>,
}

impl<'a> BalloonProvider<'a> for PlacemarkMessage<'a> {
    fn from_map(payload: &'a Value) -> Result<Self, PlistParseError> {
        if let Ok((placemark, body)) = PlacemarkMessage::get_body_and_url(payload) {
            // Ensure the message is a placemark
            if get_string_from_dict(placemark, "address").is_none() {
                return Err(PlistParseError::WrongMessageType);
            }

            return Ok(Self {
                url: get_string_from_nested_dict(body, "URL"),
                original_url: get_string_from_nested_dict(body, "originalURL"),
                place_name: get_string_from_dict(body, "title"),
                placemark: Placemark::new(placemark).unwrap_or_default(),
            });
        }
        Err(PlistParseError::NoPayload)
    }
}

impl<'a> PlacemarkMessage<'a> {
    /// Extract the main dictionary of data from the body of the payload
    ///
    /// Placemark messages store the URL under `richLinkMetadata` like a normal URL, but has some
    /// extra data stored under `specialization2` that contains the placemark's metadata.
    fn get_body_and_url(payload: &'a Value) -> Result<(&'a Value, &'a Value), PlistParseError> {
        let base = payload
            .as_dictionary()
            .ok_or_else(|| {
                PlistParseError::InvalidType("root".to_string(), "dictionary".to_string())
            })?
            .get("richLinkMetadata")
            .ok_or_else(|| PlistParseError::MissingKey("richLinkMetadata".to_string()))?;
        Ok((
            base.as_dictionary()
                .ok_or_else(|| {
                    PlistParseError::InvalidType("root".to_string(), "dictionary".to_string())
                })?
                .get("specialization2")
                .ok_or_else(|| PlistParseError::MissingKey("specialization2".to_string()))?,
            base,
        ))
    }

    /// 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 tests {
    use crate::{
        message_types::{
            placemark::{Placemark, PlacemarkMessage},
            variants::BalloonProvider,
        },
        util::plist::parse_plist,
    };
    use plist::Value;
    use std::env::current_dir;
    use std::fs::File;

    #[test]
    fn test_parse_app_store_link() {
        let plist_path = current_dir()
            .unwrap()
            .as_path()
            .join("test_data/shared_placemark/SharedPlacemark.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 = PlacemarkMessage::from_map(&parsed).unwrap();
        let expected = PlacemarkMessage {
            url: Some("https://maps.apple.com/?address=Cherry%20Cove,%20Avalon,%20CA%20%2090704,%20United%20States&ll=33.450858,-118.508212&q=Cherry%20Cove&t=m"),
            original_url: Some("https://maps.apple.com/?address=Cherry%20Cove,%20Avalon,%20CA%20%2090704,%20United%20States&ll=33.450858,-118.508212&q=Cherry%20Cove&t=m"),
            place_name: Some("Cherry Cove Avalon CA 90704 United States"),
            placemark: Placemark {
                name: Some("Cherry Cove"),
                address: Some("Cherry Cove, Avalon"),
                state: Some("CA"),
                city: Some("Avalon"),
                iso_country_code: Some("US"),
                postal_code: Some("90704"),
                country: Some("United States"),
                street: Some("Cherry Cove"),
                sub_administrative_area: Some("Los Angeles County"),
                sub_locality: Some("Santa Catalina Island"),
            },
        };

        assert_eq!(balloon, expected);
    }

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

        let (placemark_data, _) = PlacemarkMessage::get_body_and_url(&parsed).unwrap();

        let placemark = Placemark::new(placemark_data).unwrap();
        let expected = Placemark {
            name: Some("Cherry Cove"),
            address: Some("Cherry Cove, Avalon"),
            state: Some("CA"),
            city: Some("Avalon"),
            iso_country_code: Some("US"),
            postal_code: Some("90704"),
            country: Some("United States"),
            street: Some("Cherry Cove"),
            sub_administrative_area: Some("Los Angeles County"),
            sub_locality: Some("Santa Catalina Island"),
        };

        assert_eq!(placemark, expected);
    }
}