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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
use crate::{NanoKind, PrivacySetting, ProjectStatus, EventType, GroupType, EntryMethod, AdminLevel, ActionType, DisplayStatus, WritingType, ContentType, RegistrationPath, BadgeType, JoiningRule, UnitType, AdheresTo, Feeling, How, Where, InvitationStatus};
use crate::utils::*;

use std::collections::HashMap;

use chrono::{DateTime, Utc, NaiveDate};
use paste::paste;
use serde::{Serialize, Deserialize};
use serde::de::DeserializeOwned;

// TODO: A lot of these shouldn't be pub, constructing them yourself is dangerous
// TODO: May be possible to make time_zone a type from chrono

#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged, bound(deserialize = "T: DeserializeOwned"))]
pub(crate) enum NanoResponse<T: DeserializeOwned> {
    Success(T),
    Error(NanoError),
    Unknown(serde_json::Value)
}

/// The response of the Nano API when a command results in an expected error
#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged, deny_unknown_fields)]
pub enum NanoError {
    /// A simple error with just a basic message
    SimpleError { error: String },
    /// A response with multiple complex errors
    ErrorList { errors: Vec<ErrorData> }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ErrorData {
    #[serde(deserialize_with = "de_str_num")]
    pub code: u64,
    pub detail: String,
    #[serde(deserialize_with = "de_str_num")]
    pub status: u64,
    pub title: String,
}

/// The response from logging into the Nano API
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct LoginResponse {
    /// The authorization token for this log-in session
    pub auth_token: String
}

/// Information about Nano's current funraising goals
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct Fundometer {
    /// The current end-goal
    pub goal: u64,
    /// The current total raised
    #[serde(deserialize_with = "de_str_num")]
    pub raised: f64,
    /// The number of people who have donated
    #[serde(rename = "donorCount")]
    pub donor_count: u64
}

/// An item from the Nano store
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct StoreItem {
    /// The unique slug for this item
    pub handle: String,
    /// The URL for the image tied to this item
    #[serde(deserialize_with = "de_heighten_img")]
    pub image: String,
    /// The user-facing title of this item
    pub title: String
}

/// A successful response from a call to the API which returns multiple items.
/// Is generic over the inner data type, which allows for the case of a known return type
/// to avoid needing an unwrap. Defaults to the generic Object
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct CollectionResponse<D: ObjectInfo = Object> {
    /// The array of returned objects
    pub data: Vec<D>,
    /// Any included linked objects
    pub included: Option<Vec<Object>>,

    /// Extra info provided for Post objects
    #[serde(flatten)]
    pub post_info: Option<Box<PostInfo>>
}

impl<D: ObjectInfo> CollectionResponse<D> {
    /// Find the instance of an ObjectRef in this response's included list, if
    /// an instance exists. Otherwise returns None
    pub fn get_ref(&self, obj_ref: &ObjectRef) -> Option<&Object> {
        self.included
            .as_ref()
            .and_then(
                |val| val.iter().find(
                    |obj| obj.id() == obj_ref.id && obj.kind() == obj_ref.kind
                )
            )
    }
}

/// A successful response from a call to the API which returns a single item.
/// Is generic over the inner data type, which allows for the case of a known return type
/// to avoid needing an unwrap. Defaults to the generic Object
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct ItemResponse<D: ObjectInfo = Object> {
    /// The returned object
    pub data: D,
    /// Any included linked objects
    pub included: Option<Vec<Object>>,

    /// Extra info provided for Post/Page objects
    #[serde(flatten)]
    pub post_info: Option<Box<PostInfo>>
}

impl<D: ObjectInfo> ItemResponse<D> {
    /// Find the instance of an ObjectRef in this response's included list, if
    /// an instance exists. Otherwise returns None
    pub fn get_ref(&self, obj_ref: &ObjectRef) -> Option<&Object> {
        self.included
            .as_ref()
            .and_then(
                |val| val.iter().find(
                    |obj| obj.id() == obj_ref.id && obj.kind() == obj_ref.kind
                )
            )
    }
}

/// The extra info provided when getting a Post/Page object
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct PostInfo {
    /// Posts that come after this one
    pub after_posts: Vec<ItemResponse<PostObject>>,
    /// Info about the author(s) of this post
    pub author_cards: CollectionResponse<PostObject>,
    /// Posts that come before this one
    pub before_posts: Vec<ItemResponse<PostObject>>
}

/// A reference to an included [`Object`]. Declares the kind and ID of the Object,
/// so that it can be uniquely located in the include list
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct ObjectRef {
    /// The ID of the referenced Object
    #[serde(deserialize_with = "de_str_num")]
    pub id: u64,
    /// The kind of the referenced Object
    #[serde(rename = "type", deserialize_with = "de_nanokind", serialize_with = "se_nanokind")]
    pub kind: NanoKind
}

/// A trait for all types that represent an 'Object' in the Nano API. See [`Object`] for the
/// most general form of this.
pub trait ObjectInfo {
    /// Retrive the kind of this Object
    fn kind(&self) -> NanoKind;
    /// Retrieve the ID of this object
    fn id(&self) -> u64;
    /// Get the relationships of this Object, if it has any
    fn relationships(&self) -> &Option<RelationInfo>;
    /// Get the links for this Object, of which there should always be at least a link for `self`
    fn links(&self) -> &LinkInfo;
}

/// A common type for all Nano API objects. Most useful when you're either not sure of an API type,
/// or want to accept multiple types in your program. See [`ObjectInfo`] for the kind of things
/// all these objects have in common
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "type")]
pub enum Object {
    #[serde(rename = "badges")]
    Badge(BadgeObject),
    #[serde(rename = "challenges")]
    Challenge(ChallengeObject),
    #[serde(rename = "daily-aggregates")]
    DailyAggregate(DailyAggregateObject),
    #[serde(rename = "favorite-authors")]
    FavoriteAuthor(FavoriteAuthorObject),
    #[serde(rename = "favorite-books")]
    FavoriteBook(FavoriteBookObject),
    #[serde(rename = "genres")]
    Genre(GenreObject),
    #[serde(rename = "groups")]
    Group(GroupObject),
    #[serde(rename = "group-external-links")]
    GroupExternalLink(GroupExternalLinkObject),
    #[serde(rename = "locations")]
    Location(LocationObject),
    #[serde(rename = "nanomessages")]
    NanoMessage(NanoMessageObject),
    #[serde(rename = "notifications")]
    Notification(NotificationObject),
    #[serde(rename = "pages")]
    Page(PageObject),
    #[serde(rename = "posts")]
    Post(PostObject),
    #[serde(rename = "projects")]
    Project(ProjectObject),
    #[serde(rename = "project-sessions")]
    ProjectSession(ProjectSessionObject),
    #[serde(rename = "stopwatches")]
    StopWatch(StopWatchObject),
    #[serde(rename = "timers")]
    Timer(TimerObject),
    #[serde(rename = "users")]
    User(UserObject),
    #[serde(rename = "writing-locations")]
    WritingLocation(WritingLocationObject),
    #[serde(rename = "writing-methods")]
    WritingMethod(WritingMethodObject),

    #[serde(rename = "group-users")]
    GroupUser(GroupUserObject),
    #[serde(rename = "location-groups")]
    LocationGroup(LocationGroupObject),
    #[serde(rename = "project-challenges")]
    ProjectChallenge(ProjectChallengeObject),
    #[serde(rename = "user-badges")]
    UserBadge(UserBadgeObject),
}

impl Object {
    fn inner(&self) -> &dyn ObjectInfo {
        match self {
            Object::Badge(data) => data,
            Object::Challenge(data) => data,
            Object::DailyAggregate(data) => data,
            Object::FavoriteAuthor(data) => data,
            Object::FavoriteBook(data) => data,
            Object::Genre(data) => data,
            Object::Group(data) => data,
            Object::GroupExternalLink(data) => data,
            Object::Location(data) => data,
            Object::NanoMessage(data) => data,
            Object::Notification(data) => data,
            Object::Page(data) => data,
            Object::Post(data) => data,
            Object::Project(data) => data,
            Object::ProjectSession(data) => data,
            Object::StopWatch(data) => data,
            Object::Timer(data) => data,
            Object::User(data) => data,
            Object::WritingLocation(data) => data,
            Object::WritingMethod(data) => data,

            Object::GroupUser(data) => data,
            Object::LocationGroup(data) => data,
            Object::ProjectChallenge(data) => data,
            Object::UserBadge(data) => data,
        }
    }
}

impl ObjectInfo for Object {
    fn kind(&self) -> NanoKind {
        self.inner().kind()
    }

    fn id(&self) -> u64 {
        self.inner().id()
    }

    fn relationships(&self) -> &Option<RelationInfo> {
        self.inner().relationships()
    }

    fn links(&self) -> &LinkInfo {
        self.inner().links()
    }
}

macro_rules! obj_ty {
    ($( $name:ident )+) => {
        paste! {
            $(

            #[doc = "A struct representing an object of kind " $name]
            #[derive(Serialize, Deserialize, Debug)]
            pub struct [<$name Object>] {
                #[serde(deserialize_with = "de_str_num")]
                id: u64,
                relationships: Option<RelationInfo>,
                links: LinkInfo,

                /// The attributes unique to this object
                #[serde(rename = "attributes")]
                pub data: [<$name Data>]
            }

            impl ObjectInfo for [<$name Object>] {
                fn kind(&self) -> NanoKind {
                    NanoKind::$name
                }

                fn id(&self) -> u64 {
                    self.id
                }

                fn relationships(&self) -> &Option<RelationInfo> {
                    &self.relationships
                }

                fn links(&self) -> &LinkInfo {
                    &self.links
                }
            }

            impl Object {
                #[doc = "Get this Object as kind " $name ", or panic"]
                #[track_caller]
                pub fn [<unwrap_ $name:snake>](&self) -> &[<$name Object>] {
                    if let Object::$name(inner) = self {
                        inner
                    } else {
                        panic!(concat!("Expected object type ", stringify!($name), " while unwrapping Object"))
                    }
                }
            }
            )+
        }
    }
}

obj_ty!(
    Badge
    Challenge
    DailyAggregate
    FavoriteAuthor
    FavoriteBook
    Genre
    Group
    GroupExternalLink
    Location
    NanoMessage
    Notification
    Page
    Post
    Project
    ProjectSession
    StopWatch
    Timer
    User
    WritingLocation
    WritingMethod

    GroupUser
    LocationGroup
    ProjectChallenge
    UserBadge
);

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct BadgeData {
    pub active: bool,
    pub adheres_to: AdheresTo,
    pub awarded: String,
    pub awarded_description: String,
    pub badge_type: BadgeType,
    pub description: String,
    pub generic_description: String,
    pub list_order: u64,
    pub suborder: Option<u64>,
    pub title: String,
    pub unawarded: String,
    pub winner: bool
}

/// A challenge (Nano, Camp Nano, or custom).
/// The Optional fields will generally be populated for Nanos or Camps,
/// but null for custom challenges. (Warning: This is only mostly, not absolutely, true)
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct ChallengeData {
    pub default_goal: u64,
    pub ends_at: NaiveDate,
    pub event_type: Option<EventType>,
    pub flexible_goal: Option<bool>,
    pub name : String,
    pub prep_starts_at: Option<NaiveDate>,
    pub starts_at: NaiveDate,
    pub unit_type: UnitType,
    pub user_id: u64,
    pub win_allowed_at: Option<NaiveDate>,
    pub writing_type: WritingType,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct DailyAggregateData {
    pub count: u64,
    pub day: NaiveDate,
    pub project_id: u64,
    pub unit_type: UnitType,
    pub user_id: Option<u64>
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct FavoriteAuthorData {
    pub name: String,
    pub user_id: u64
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct FavoriteBookData {
    pub title: String,
    pub user_id: u64
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct GenreData {
    pub name: String,
    /// The user who created this Genre label
    pub user_id: u64
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct GroupData {
    pub approved_by_id: u64,
    pub avatar: Option<String>,
    pub cancelled_by_id: u64,
    pub created_at: DateTime<Utc>,
    pub description: Option<String>,
    pub end_dt: Option<DateTime<Utc>>,
    pub forum_link: Option<String>,
    pub group_id: Option<u64>,
    pub group_type: GroupType,
    pub joining_rule: Option<JoiningRule>,
    pub latitude: Option<f64>,
    pub longitude: Option<f64>,
    pub max_member_count: Option<u64>,
    pub member_count: Option<u64>,
    pub name: String,
    pub plate: Option<String>,
    pub slug: String,
    pub start_dt: Option<DateTime<Utc>>,
    pub time_zone: Option<String>,
    pub updated_at: DateTime<Utc>,
    pub url: Option<String>,
    pub user_id: Option<u64>
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct GroupExternalLinkData {
    pub group_id: u64,
    pub label: Option<String>,
    pub url: String
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case")]
pub struct LocationData {
    pub city: String,
    pub country: String,
    pub county: Option<String>,
    pub formatted_address: Option<String>,
    pub latitude: f64,
    pub longitude: f64,
    pub map_url: Option<String>,
    pub municipality: Option<String>,
    pub name: String,
    pub neighborhood: Option<String>,
    // TODO: Make this like deserialize_with = "de_str_num" but optional
    pub postal_code: String,
    pub state: String,
    #[serde(rename = "street1")]
    pub street1: Option<String>,
    #[serde(rename = "street2")]
    pub street2: Option<String>,
    pub utc_offset: Option<i64>
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct NanoMessageData {
    pub content: String,
    pub created_at: DateTime<Utc>,
    pub group_id: u64,
    pub official: bool,
    pub send_email: Option<bool>,
    pub sender_avatar_url: Option<String>,
    pub sender_name: Option<String>,
    pub sender_slug: Option<String>,
    pub updated_at: DateTime<Utc>,
    pub user_id: u64,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct NotificationData {
    pub action_id: Option<u64>,
    pub action_type: ActionType,
    pub content: String,
    pub created_at: DateTime<Utc>,
    pub data_count: Option<u64>,
    pub display_at: DateTime<Utc>,
    pub display_status: DisplayStatus,
    pub headline: String,
    pub image_url: Option<String>,
    pub last_viewed_at: Option<DateTime<Utc>>,
    pub redirect_url: Option<String>,
    pub updated_at: DateTime<Utc>,
    pub user_id: u64
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct PageData {
    pub body: String,
    pub url: String,
    pub headline: String,
    pub content_type: ContentType,
    pub show_after: Option<DateTime<Utc>>,
    pub promotional_card_image: Option<String>
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct PostData {
    pub api_code: Option<String>, // TODO: ???
    pub body: String,
    pub card_image: Option<String>,
    pub content_type: ContentType,
    pub expires_at: Option<NaiveDate>,
    pub external_link: Option<String>,
    pub headline: String,
    pub offer_code: Option<String>,
    pub order: Option<u64>,
    pub published: bool,
    pub subhead: Option<String> // TODO: ???
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct ProjectData {
    pub cover: Option<String>,
    pub created_at: DateTime<Utc>,
    pub excerpt: Option<String>,
    pub pinterest_url: Option<String>,
    pub playlist_url: Option<String>,
    pub primary: Option<u8>, // TODO: Figure out what this means. It's not an enum
    pub privacy: PrivacySetting,
    pub slug: String,
    pub status: ProjectStatus,
    pub summary: Option<String>,
    pub title: String,
    pub unit_count: Option<u64>,
    pub unit_type: UnitType,
    pub user_id: u64,
    pub writing_type: WritingType,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct ProjectSessionData {
    pub count: i64,
    pub created_at: DateTime<Utc>,
    pub end: DateTime<Utc>,
    pub feeling: Option<Feeling>,
    pub how: Option<How>,
    pub project_challenge_id: u64,
    pub project_id: u64,
    pub session_date: NaiveDate,
    pub start: Option<DateTime<Utc>>,
    pub unit_type: UnitType,
    pub r#where: Option<Where>,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct StopWatchData {
    pub start: DateTime<Utc>,
    pub stop: Option<DateTime<Utc>>
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct TimerData {
    pub cancelled: bool,
    #[serde(deserialize_with = "de_duration_mins", serialize_with = "se_duration_mins")]
    pub duration: chrono::Duration,
    pub start: DateTime<Utc>
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case")]
pub struct UserData {
    pub admin_level: AdminLevel,
    pub avatar: Option<String>,
    pub bio: Option<String>,
    pub confirmed_at: DateTime<Utc>,
    pub created_at: DateTime<Utc>,
    pub discourse_username: Option<String>,
    pub email: Option<String>,

    #[serde(flatten)]
    pub email_settings: Option<EmailSettings>,

    pub halo: bool,
    pub laurels: u64,
    pub location: Option<String>,
    pub name: String,

    #[serde(flatten)]
    pub notification_settings: Option<NotificationSettings>,

    pub notifications_viewed_at: DateTime<Utc>,
    pub plate: Option<String>,
    pub postal_code: Option<String>,

    #[serde(flatten)]
    pub privacy_settings: Option<PrivacySettings>,

    pub registration_path: RegistrationPath,
    pub setting_session_count_by_session: u8, // TODO: ???
    pub setting_session_more_info: bool, // TODO: ???
    pub slug: String,

    #[serde(flatten)]
    pub stats: StatsInfo,

    pub time_zone: String
}

#[derive(Serialize, Deserialize, Debug)]
pub struct EmailSettings {
    #[serde(rename = "email-blog-posts")]
    pub blog_posts: bool,
    #[serde(rename = "email-buddy-requests")]
    pub buddy_requests: bool,
    #[serde(rename = "email-events-in-home-region")]
    pub events_in_home_region: bool,
    #[serde(rename = "email-nanomessages-buddies")]
    pub nanomessages_buddies: bool,
    #[serde(rename = "email-nanomessages-hq")]
    pub nanomessages_hq: bool,
    #[serde(rename = "email-nanomessages-mls")]
    pub nanomessages_mls: bool,
    #[serde(rename = "email-nanowrimo-updates")]
    pub nanowrimo_updates: bool,
    #[serde(rename = "email-newsletter")]
    pub newsletter: bool,
    #[serde(rename = "email-writing-reminders")]
    pub writing_reminders: bool,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct NotificationSettings {
    #[serde(rename = "notification-buddy-activities")]
    pub buddy_activities: bool,
    #[serde(rename = "notification-buddy-requests")]
    pub buddy_requests: bool,
    #[serde(rename = "notification-events-in-home-region")]
    pub events_in_home_region: bool,
    #[serde(rename = "notification-goal-milestones")]
    pub goal_milestones: bool,
    #[serde(rename = "notification-nanomessages-buddies")]
    pub nanomessages_buddies: bool,
    #[serde(rename = "notification-nanomessages-hq")]
    pub nanomessages_hq: bool,
    #[serde(rename = "notification-nanomessages-mls")]
    pub nanomessages_mls: bool,
    #[serde(rename = "notification-new-badges")]
    pub new_badges: bool,
    #[serde(rename = "notification-sprint-invitation")]
    pub sprint_invitation: bool,
    #[serde(rename = "notification-sprint-start")]
    pub sprint_start: bool,
    #[serde(rename = "notification-writing-reminders")]
    pub writing_reminders: bool,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct PrivacySettings {
    #[serde(rename = "privacy-send-nanomessages")]
    pub send_nanomessages: PrivacySetting,
    #[serde(rename = "privacy-view-buddies")]
    pub view_buddies: PrivacySetting,
    #[serde(rename = "privacy-view-profile")]
    pub view_profile: PrivacySetting,
    #[serde(rename = "privacy-view-projects")]
    pub view_projects: PrivacySetting,
    #[serde(rename = "privacy-view-search")]
    pub view_search: PrivacySetting,
    #[serde(rename = "privacy-visibility-activity-logs")]
    pub visibility_activity_logs: bool,
    #[serde(rename = "privacy-visibility-buddy-lists")]
    pub visibility_buddy_lists: bool,
    #[serde(rename = "privacy-visibility-regions")]
    pub visibility_regions: bool,
}

// TODO: What do these *mean*, are all of them the right type?
#[derive(Serialize, Deserialize, Debug)]
pub struct StatsInfo {
    #[serde(rename = "stats-projects")]
    pub projects: u64,
    #[serde(rename = "stats-projects-enabled")]
    pub projects_enabled: bool,
    #[serde(rename = "stats-streak")]
    pub streak: u64,
    #[serde(rename = "stats-streak-enabled")]
    pub streak_enabled: bool,
    #[serde(rename = "stats-word-count")]
    pub word_count: u64,
    #[serde(rename = "stats-word-count-enabled")]
    pub word_count_enabled: bool,
    #[serde(rename = "stats-wordiest")]
    pub wordiest: u64,
    #[serde(rename = "stats-wordiest-enabled")]
    pub wordiest_enabled: bool,
    #[serde(rename = "stats-writing-pace")]
    pub writing_pace: Option<u64>,
    #[serde(rename = "stats-writing-pace-enabled")]
    pub writing_pace_enabled: bool,
    #[serde(rename = "stats-years-done")]
    pub years_done: Option<u64>,
    #[serde(rename = "stats-years-enabled")]
    pub years_enabled: bool,
    #[serde(rename = "stats-years-won")]
    pub years_won: Option<u64>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct WritingLocationData {
    pub name: String
}

#[derive(Serialize, Deserialize, Debug)]
pub struct WritingMethodData {
    pub name: String
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct GroupUserData {
    pub created_at: DateTime<Utc>,
    pub entry_at: Option<DateTime<Utc>>,
    pub entry_method: EntryMethod,
    pub exit_at: Option<DateTime<Utc>>,
    pub exit_method: Option<String>, // TODO: Enum
    pub group_code_id: Option<u64>,
    pub group_id: u64,
    pub group_type: GroupType,
    pub invitation_accepted: InvitationStatus,
    pub invited_by_id: Option<u64>,
    pub is_admin: Option<bool>,
    pub latest_message: Option<String>,
    pub num_unread_messages: u64,
    pub primary: u64,
    pub updated_at: DateTime<Utc>,
    pub user_id: u64
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct LocationGroupData {
    pub group_id: u64,
    pub location_id: u64,
    pub primary: bool
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct ProjectChallengeData {
    pub challenge_id: u64,
    pub current_count: u64,
    pub ends_at: NaiveDate,
    pub event_type: EventType,
    pub feeling: Option<Feeling>,
    pub goal: u64,
    pub how: Option<How>,
    pub last_recompute: Option<DateTime<Utc>>,
    pub name: String,
    pub project_id: u64,
    pub speed: Option<u64>, // TODO: ???
    pub start_count: Option<u64>,
    pub starts_at: NaiveDate,
    pub streak: Option<u64>,
    pub unit_type: UnitType,
    pub user_id: u64,
    pub when: Option<u64>, // TODO: ???
    pub writing_location: Option<String>, // TODO: ???
    pub writing_type: Option<WritingType>,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct UserBadgeData {
    pub badge_id: u64,
    pub created_at: DateTime<Utc>,
    pub project_challenge_id: u64,
    pub user_id: u64,
}

// This doesn't like deny_unknown_fields, I think due to the custom serialize/deserialize impls
#[derive(Serialize, Deserialize, Debug)]
pub struct RelationInfo {
    /// If this is Some, all references are included in the response Include array
    #[serde(flatten, deserialize_with = "de_rel_includes", serialize_with = "se_rel_includes")]
    pub included: HashMap<NanoKind, Vec<ObjectRef>>,
    #[serde(flatten, deserialize_with = "de_relation", serialize_with = "se_relation")]
    pub relations: HashMap<NanoKind, RelationLink>,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct RelationLink {
    #[serde(rename = "self")]
    pub this: String,
    pub related: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct LinkInfo {
    #[serde(rename = "self")]
    pub this: String,
    #[serde(flatten)]
    pub others: HashMap<String, String>
}

#[derive(Serialize, Deserialize, Debug)]
pub struct LinkData {
    #[serde(rename = "self")]
    pub this: String,

    #[serde(flatten)]
    pub extra: HashMap<String, String>
}