summaryrefslogtreecommitdiffstats
path: root/entities/src/status/mod.rs
blob: 502ee67c7ee5f73f95ce5c19aff752bfda04854c (plain)
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
//! Module containing all info relating to a status.

pub mod edit;
/// For building a new status
pub mod new;
pub mod poll;
pub mod scheduled;
pub mod source;

pub use edit::Edit;
use isolang::Language;
pub use new::{NewStatus, NewStatusBuilder};
pub use poll::{Poll, PollBuilder};
pub use scheduled::Status as Scheduled;
pub use source::Source;

use crate::{custom_emoji::CustomEmoji, filter};

use super::prelude::*;
use serde::{Deserialize, Serialize};
use time::{serde::iso8601, OffsetDateTime};
use url::Url;

/// Represents a status posted by an account.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Status {
    /// The ID of the status.
    pub id: StatusId,
    /// A Fediverse-unique resource ID.
    pub uri: Url,
    /// A link to the status’s HTML representation.
    pub url: Option<Url>,
    /// The Account which posted the status.
    pub account: Account,
    /// The ID of the status this status is replying to, if the status is
    /// a reply.
    pub in_reply_to_id: Option<StatusId>,
    /// The ID of the account this status is replying to, if the status is
    /// a reply.
    pub in_reply_to_account_id: Option<AccountId>,
    /// If this status is a reblogged Status of another User.
    pub reblog: Option<Box<Status>>,
    /// Body of the status; this will contain HTML
    /// (remote HTML already sanitized)
    pub content: String,
    /// The time the status was created.
    #[serde(with = "iso8601")]
    pub created_at: OffsetDateTime,
    /// Timestamp of when the status was last edited.
    #[serde(
        with = "iso8601::option",
        skip_serializing_if = "Option::is_none",
        default
    )]
    pub edited_at: Option<OffsetDateTime>,
    /// Custom emoji to be used when rendering status content.
    pub emojis: Vec<CustomEmoji>,
    /// The number of replies to this status.
    pub replies_count: u64,
    /// How many boosts this status has received.
    pub reblogs_count: u64,
    /// The number of favourites for the status.
    pub favourites_count: u64,
    /// Whether the application client has reblogged the status.
    pub reblogged: Option<bool>,
    /// Whether the application client has favourited the status.
    pub favourited: Option<bool>,
    /// If the current token has an authorized user: Have you muted
    /// notifications for this status’s conversation?
    pub muted: Option<bool>,
    /// If the current token has an authorized user: Have you bookmarked this
    /// status?
    pub bookmarked: Option<bool>,
    /// If the current token has an authorized user: Have you pinned this
    /// status? Only appears if the status is pinnable.
    pub pinned: Option<bool>,
    /// Whether media attachments should be hidden by default.
    pub sensitive: bool,
    /// If not empty, warning text that should be displayed before the actual
    /// content.
    pub spoiler_text: String,
    /// The visibilty of the status.
    pub visibility: Visibility,
    /// An array of attachments.
    pub media_attachments: Vec<Attachment>,
    /// Hashtags used within the status content.
    pub mentions: Vec<Mention>,
    /// An array of tags.
    pub tags: Vec<Tag>,
    /// Media that is attached to this status.
    pub application: Option<Application>,
    /// The detected language for the status, if detected.
    pub language: Option<Language>,
    /// The poll attached to the status.
    pub poll: Option<Poll>,
    /// Preview card for links included within status content.
    pub card: Option<Card>,
    /// Plain-text source of a status. Returned instead of content when status
    /// is deleted, so the user may redraft from the source text without the
    /// client having to reverse-engineer the original text from the HTML
    /// content.
    pub text: Option<String>,
    /// If the current token has an authorized user: The filter and keywords
    /// that matched this status.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub filtered: Vec<filter::Result>,
}

/// Represents a hashtag used within the content of a status.
///
/// See also [the API documentation](https://docs.joinmastodon.org/entities/Status/#Tag)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Tag {
    /// The hashtag, not including the preceding `#`.
    pub name: String,
    /// The URL of the hashtag.
    pub url: String,
}

/// Application details.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Application {
    /// Name of the application.
    pub name: String,
    /// Homepage URL of the application.
    pub website: Option<String>,
}

/// Represents a hashtag that is featured on a profile.
///
/// See also [the API documentation](https://docs.joinmastodon.org/entities/FeaturedTag/)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct FeaturedTag {
    /// The internal ID of the featured tag in the database.
    pub id: TagId,
    /// The name of the hashtag being featured.
    pub name: String,
    /// A link to all statuses by a user that contain this hashtag.
    pub url: Url,
    /// The number of authored statuses containing this hashtag.
    pub statuses_count: u64,
    /// The timestamp of the last authored status containing this hashtag.
    #[serde(with = "iso8601")]
    pub last_status_at: OffsetDateTime,
}

#[cfg(test)]
mod tests {
    use time::format_description::well_known::Iso8601;

    use super::*;

    #[test]
    fn test_deserialize_example() {
        let example = r#"{
            "id": "103270115826048975",
            "created_at": "2019-12-08T03:48:33.901Z",
            "in_reply_to_id": null,
            "in_reply_to_account_id": null,
            "sensitive": false,
            "spoiler_text": "",
            "visibility": "public",
            "language": "en",
            "uri": "https://mastodon.social/users/Gargron/statuses/103270115826048975",
            "url": "https://mastodon.social/@Gargron/103270115826048975",
            "replies_count": 5,
            "reblogs_count": 6,
            "favourites_count": 11,
            "favourited": false,
            "reblogged": false,
            "muted": false,
            "bookmarked": false,
            "content": "<p>&quot;I lost my inheritance with one wrong digit on my sort code&quot;</p><p><a href=\"https://www.theguardian.com/money/2019/dec/07/i-lost-my-193000-inheritance-with-one-wrong-digit-on-my-sort-code\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"><span class=\"invisible\">https://www.</span><span class=\"ellipsis\">theguardian.com/money/2019/dec</span><span class=\"invisible\">/07/i-lost-my-193000-inheritance-with-one-wrong-digit-on-my-sort-code</span}</p>",
            "reblog": null,
            "application": {
              "name": "Web",
              "website": null
            },
            "account": {
              "id": "1",
              "username": "Gargron",
              "acct": "Gargron",
              "display_name": "Eugen",
              "locked": false,
              "bot": false,
              "discoverable": true,
              "group": false,
              "created_at": "+002016-03-16T14:34:26.392000000Z",
              "note": "<p>Developer of Mastodon and administrator of mastodon.social. I post service announcements, development updates, and personal stuff.</p>",
              "url": "https://mastodon.social/@Gargron",
              "avatar": "https://files.mastodon.social/accounts/avatars/000/000/001/original/d96d39a0abb45b92.jpg",
              "avatar_static": "https://files.mastodon.social/accounts/avatars/000/000/001/original/d96d39a0abb45b92.jpg",
              "header": "https://files.mastodon.social/accounts/headers/000/000/001/original/c91b871f294ea63e.png",
              "header_static": "https://files.mastodon.social/accounts/headers/000/000/001/original/c91b871f294ea63e.png",
              "followers_count": 322930,
              "following_count": 459,
              "statuses_count": 61323,
              "last_status_at": "2019-12-10T08:14:44.811Z",
              "emojis": [],
              "fields": [
                {
                  "name": "Patreon",
                  "value": "<a href=\"https://www.patreon.com/mastodon\" rel=\"me nofollow noopener noreferrer\" target=\"_blank\"><span class=\"invisible\">https://www.</span><span class=\"\">patreon.com/mastodon</span><span class=\"invisible\"></span}",
                  "verified_at": null
                },
                {
                  "name": "Homepage",
                  "value": "<a href=\"https://zeonfederated.com\" rel=\"me nofollow noopener noreferrer\" target=\"_blank\"><span class=\"invisible\">https://</span><span class=\"\">zeonfederated.com</span><span class=\"invisible\"></span}",
                  "verified_at": "+002019-07-15T18:29:57.191000000Z"
                }
              ]
            },
            "media_attachments": [],
            "mentions": [],
            "tags": [],
            "emojis": [],
            "card": {
              "url": "https://www.theguardian.com/money/2019/dec/07/i-lost-my-193000-inheritance-with-one-wrong-digit-on-my-sort-code",
              "title": "‘I lost my £193,000 inheritance – with one wrong digit on my sort code’",
              "description": "When Peter Teich’s money went to another Barclays customer, the bank offered £25 as a token gesture",
              "type": "link",
              "author_name": "",
              "author_url": "",
              "provider_name": "",
              "provider_url": "",
              "html": "",
              "width": 0,
              "height": 0,
              "image": null,
              "embed_url": ""
            },
            "poll": null
        }"#;
        let status: Status = serde_json::from_str(example).expect("deserialize");
        assert_eq!(status.id, StatusId::new("103270115826048975"));
        assert_eq!(
            status.created_at,
            OffsetDateTime::parse("2019-12-08T03:48:33.901Z", &Iso8601::PARSING)
                .expect("parse time example")
        );
        assert!(status.in_reply_to_id.is_none());
        assert!(status.in_reply_to_account_id.is_none());
        assert!(!status.sensitive);
        assert!(
            status.spoiler_text.is_empty(),
            "spoiler text was {:?}",
            status.spoiler_text
        );
        assert!(status.visibility.is_public());
        assert_eq!(status.language, Some(Language::Eng));
        assert_eq!(
            status.uri.as_ref(),
            "https://mastodon.social/users/Gargron/statuses/103270115826048975"
        );
        assert_eq!(
            status.url.expect("url").as_ref(),
            "https://mastodon.social/@Gargron/103270115826048975"
        );
        assert_eq!(status.replies_count, 5);
        assert_eq!(status.reblogs_count, 6);
        assert_eq!(status.favourites_count, 11);
        assert!(!status.favourited.expect("favourited"));
        assert!(!status.reblogged.expect("reblogged"));
        assert!(!status.muted.expect("muted"));
        assert!(!status.bookmarked.expect("bookmarked"));
        assert_eq!(status.content, "<p>&quot;I lost my inheritance with one wrong digit on my sort code&quot;</p><p><a href=\"https://www.theguardian.com/money/2019/dec/07/i-lost-my-193000-inheritance-with-one-wrong-digit-on-my-sort-code\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"><span class=\"invisible\">https://www.</span><span class=\"ellipsis\">theguardian.com/money/2019/dec</span><span class=\"invisible\">/07/i-lost-my-193000-inheritance-with-one-wrong-digit-on-my-sort-code</span}</p>");
        assert!(status.reblog.is_none());
        let app = status.application.expect("application");
        assert_eq!(app.name, "Web");
        assert!(
            app.website.is_none(),
            "subject.application.website was {:?}",
            app.website
        );
        let acct = status.account;
        assert_eq!(acct.id, AccountId::new("1"));
        assert_eq!(acct.username, "Gargron");
        assert_eq!(acct.acct, "Gargron");
        assert_eq!(acct.display_name, "Eugen");
        assert!(!acct.locked);
        assert!(!acct.bot);
        assert!(acct.discoverable.expect("discoverable"));
        assert!(!acct.group);
        <