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
/// Raw Hangouts.json models.
#[cfg(feature = "raw")]
pub mod raw;

mod event;

use std::collections::HashMap;

pub use crate::event::*;
pub use chrono;

use chrono::{DateTime, Utc};

/// Top-level struct for Hangouts data.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde-impl", derive(serde::Deserialize, serde::Serialize))]
pub struct Hangouts {
    /// List of conversations.
    pub conversations: Vec<Conversation>,
}

/// A single conversation.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde-impl", derive(serde::Deserialize, serde::Serialize))]
pub struct Conversation {
    pub conversation_id: String,
    pub id: String,

    /// Name of the conversation. Typically [`None`] if the conversation is one-on-one.
    pub name: Option<String>,

    /// List of current participants in the conversation.
    pub current_participants: Vec<ParticipantId>,
    /// List of all past and present participants in the conversation.
    pub participants: HashMap<ParticipantId, Participant>,
    /// List of conversation events.
    pub events: Vec<Event>,

    /// User state with regards to the conversation.
    pub self_state: SelfState,
    /// Timestamp used to sort conversations.
    pub sort_timestamp: DateTime<Utc>,

    /// Currently set group link sharing mode.
    pub group_link_sharing_status: LinkSharingStatus,
}

impl Conversation {
    /// Get the data for all the current participants in the conversation.
    #[inline]
    pub fn current_participants(&self) -> Vec<&Participant> {
        self.current_participants
            .iter()
            .map(|id| self.participants.get(id).unwrap())
            .collect()
    }

    /// Sort the events by timestamp, from oldest to newest.
    #[inline]
    pub fn sort_events_by_time(&mut self) {
        self.events.sort_by(|a, b| a.timestamp.cmp(&b.timestamp));
    }
}

/// A participant in a conversation.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde-impl", derive(serde::Deserialize, serde::Serialize))]
pub struct Participant {
    /// Fallback name.
    pub fallback_name: Option<String>,
    /// Type of the participant.
    pub typ: Option<ParticipantType>,

    /// Invitation status for the participant.
    pub invitation_status: Option<InvitationStatus>,
    pub new_invitation_status: Option<InvitationStatus>,

    /// Read state for the participant.
    pub read_state: ReadState,
}

impl Participant {
    /// Get the name of the participant, if present.
    #[inline]
    pub fn name(&self) -> Option<&String> {
        self.fallback_name.as_ref()
    }
}

/// Composite ID for a participant user.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde-impl", derive(serde::Deserialize, serde::Serialize))]
pub struct ParticipantId {
    /// Gaia ID component.
    pub gaia_id: String,
    /// Chat ID component.
    pub chat_id: String,
}

/// Type of a given participant.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde-impl", derive(serde::Deserialize, serde::Serialize))]
pub enum ParticipantType {
    Gaia,
    OffNetworkPhone,
}

impl ParticipantType {
    /// Returns `true` if the participant type is [`Self::Gaia`].
    #[inline]
    pub fn is_gaia(&self) -> bool {
        matches!(self, Self::Gaia)
    }

    /// Returns `true` if the participant type is [`Self::OffNetworkPhone`].
    #[inline]
    pub fn is_off_network_phone(&self) -> bool {
        matches!(self, Self::OffNetworkPhone)
    }
}

/// Metadata regarding the user's status in a conversation.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde-impl", derive(serde::Deserialize, serde::Serialize))]
pub struct SelfState {
    /// The user's involvement status.
    pub status: ConversationStatus,
    /// Currently set notification level.
    pub notification_level: NotificationLevel,
    /// Data about the invitation to the user for the conversation.
    pub invitation: InvitationData,
    /// Views the conversation are present in.
    //  TODO: Better name for this property
    pub views: Vec<View>,
}

/// Status of a participant's involvement in a conversation.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde-impl", derive(serde::Deserialize, serde::Serialize))]
pub enum ConversationStatus {
    /// The participant is active in the conversation.
    Active,
    /// The participant has been invited, but is not active in the conversation.
    Invited,
}

impl ConversationStatus {
    /// Returns `true` if the status is [`Self::Active`].
    #[inline]
    pub fn is_active(&self) -> bool {
        matches!(self, Self::Active)
    }

    /// Returns `true` if the status is [`Self::Invited`].
    #[inline]
    pub fn is_invited(&self) -> bool {
        matches!(self, Self::Invited)
    }
}

/// Invitation status of a participant.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde-impl", derive(serde::Deserialize, serde::Serialize))]
pub enum InvitationStatus {
    /// The invitation is pending.
    Pending,
    /// The invitation has been accepted.
    Accepted,
}

impl InvitationStatus {
    /// Returns `true` if the invitation status is [`Self::Pending`].
    #[inline]
    pub fn is_pending(&self) -> bool {
        matches!(self, Self::Pending)
    }

    /// Returns `true` if the invitation status is [`Self::Accepted`].
    #[inline]
    pub fn is_accepted(&self) -> bool {
        matches!(self, Self::Accepted)
    }
}

/// Metadata for the invitation to the user to join a conversation.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde-impl", derive(serde::Deserialize, serde::Serialize))]
pub struct InvitationData {
    /// ID of the participant who created the invitation.
    pub inviter: ParticipantId,
    /// Time of invitation.
    pub timestamp: DateTime<Utc>,
    pub affinity: InvitationAffinity,
}

/// Affinity of the invitation.
//  TODO: not really sure what this does.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde-impl", derive(serde::Deserialize, serde::Serialize))]
pub enum InvitationAffinity {
    /// No invitation affinity specified.
    None,
    /// Low invitation affinity.
    Low,
    /// High invitation affinity.
    High,
}

impl InvitationAffinity {
    /// Returns `true` if the affinity is [`Self::None`].
    #[inline]
    pub fn is_none(&self) -> bool {
        matches!(self, Self::None)
    }

    /// Returns `true` if the affinity is [`Self::Low`].
    #[inline]
    pub fn is_low(&self) -> bool {
        matches!(self, Self::Low)
    }

    /// Returns `true` if the affinity is [`Self::High`].
    #[inline]
    pub fn is_high(&self) -> bool {
        matches!(self, Self::High)
    }
}

/// Notification ring level for a participant in a conversation.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde-impl", derive(serde::Deserialize, serde::Serialize))]
pub enum NotificationLevel {
    /// Notifications have been set to quiet.
    Quiet,
    /// Notifications have been set to ring.
    Ring,
}

impl NotificationLevel {
    /// Returns `true` if the notification level is [`Self::Quiet`].
    #[inline]
    pub fn is_quiet(&self) -> bool {
        matches!(self, Self::Quiet)
    }

    /// Returns `true` if the notification level is [`Self::Ring`].
    #[inline]
    pub fn is_ring(&self) -> bool {
        matches!(self, Self::Ring)
    }
}

/// Last-read information in a conversation for a participant.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde-impl", derive(serde::Deserialize, serde::Serialize))]
pub struct ReadState {
    /// Last time of read.
    pub timestamp: DateTime<Utc>,
}

/// A Hangouts view collection.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde-impl", derive(serde::Deserialize, serde::Serialize))]
pub enum View {
    /// The hangout is visible in the inbox.
    Inbox,
    /// The hangout has been archived.
    Archived,
}

impl View {
    /// Returns `true` if the view is [`Self::Inbox`].
    #[inline]
    pub fn is_inbox(&self) -> bool {
        matches!(self, Self::Inbox)
    }

    /// Returns `true` if the view is [`Self::Archived`].
    #[inline]
    pub fn is_archived(&self) -> bool {
        matches!(self, Self::Archived)
    }
}

/// Link sharing enabled/disabled setting for a conversation.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde-impl", derive(serde::Deserialize, serde::Serialize))]
pub enum LinkSharingStatus {
    /// Link sharing is disabled.
    Off,
    /// Link sharing is enabled.
    On,
}

impl LinkSharingStatus {
    /// Returns `true` if the link_sharing_status is [`Self::Off`].
    #[inline]
    pub fn is_off(&self) -> bool {
        matches!(self, Self::Off)
    }

    /// Returns `true` if the link_sharing_status is [`Self::On`].
    #[inline]
    pub fn is_on(&self) -> bool {
        matches!(self, Self::On)
    }
}