diff options
| author | Zeyla Hellyer <[email protected]> | 2017-06-04 20:32:25 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-06-06 10:12:13 -0700 |
| commit | 990e611a56f37f64fbce74fbc487c7dcc4aa4e28 (patch) | |
| tree | 399c69583bd256d31668affc227f235386814552 /src/model | |
| parent | Add CurrentUser::face(), User::face() (diff) | |
| download | serenity-990e611a56f37f64fbce74fbc487c7dcc4aa4e28.tar.xz serenity-990e611a56f37f64fbce74fbc487c7dcc4aa4e28.zip | |
Use chrono for struct timestamp fields
Chrono is easier to use than timestamped strings, so they should be
automatically deserialized and available for the user, instead of having
the user deserialize the strings themselves.
These fields have been changed to use a type of `DateTime<FixedOffset>`:
- `ChannelPinsUpdateEvent.last_pin_timestamp`
- `Group.last_pin_timestamp`
- `Guild.joined_at`
- `GuildChannel.last_pin_timestamp`
- `Invite.created_at`
- `Member.joined_at`
- `Message.edited_timestamp
- `Message.timestamp`
- `MessageUpdateEvent.edited_timestamp`
- `MessageUpdateEvent.timestamp`
- `PrivateChannel.last_pin_timestamp`
`Member.joined_at` is now also an `Option`. Previously, if a Guild
Member Update was received for a member not in the cache, a new Member
would be instantiated with a default String value. This is incorrect
behaviour, and has now been replaced with being set to `None` in that
case.
Id methods' `created_at()` method now return a `chrono::NaiveDateTime`
instead of a `time::Timespec`, and `User::created_at` has been updated
to reflect that.
Additionally, drop `time` as a direct dependency and use chrono for
internals.
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/channel/group.rs | 3 | ||||
| -rw-r--r-- | src/model/channel/guild_channel.rs | 3 | ||||
| -rw-r--r-- | src/model/channel/message.rs | 25 | ||||
| -rw-r--r-- | src/model/channel/private_channel.rs | 3 | ||||
| -rw-r--r-- | src/model/event.rs | 7 | ||||
| -rw-r--r-- | src/model/guild/member.rs | 5 | ||||
| -rw-r--r-- | src/model/guild/mod.rs | 5 | ||||
| -rw-r--r-- | src/model/invite.rs | 5 | ||||
| -rw-r--r-- | src/model/mod.rs | 6 | ||||
| -rw-r--r-- | src/model/user.rs | 6 |
10 files changed, 36 insertions, 32 deletions
diff --git a/src/model/channel/group.rs b/src/model/channel/group.rs index 25c4690..63efb90 100644 --- a/src/model/channel/group.rs +++ b/src/model/channel/group.rs @@ -1,3 +1,4 @@ +use chrono::{DateTime, FixedOffset}; use ::model::*; #[cfg(feature="model")] @@ -26,7 +27,7 @@ pub struct Group { /// The Id of the last message sent. pub last_message_id: Option<MessageId>, /// Timestamp of the latest pinned message. - pub last_pin_timestamp: Option<String>, + pub last_pin_timestamp: Option<DateTime<FixedOffset>>, /// The name of the group channel. pub name: Option<String>, /// The Id of the group owner. diff --git a/src/model/channel/guild_channel.rs b/src/model/channel/guild_channel.rs index 8b55d04..5ce7031 100644 --- a/src/model/channel/guild_channel.rs +++ b/src/model/channel/guild_channel.rs @@ -1,3 +1,4 @@ +use chrono::{DateTime, FixedOffset}; use ::model::*; #[cfg(feature="model")] @@ -46,7 +47,7 @@ pub struct GuildChannel { /// The timestamp of the time a pin was most recently made. /// /// **Note**: This is only available for text channels. - pub last_pin_timestamp: Option<String>, + pub last_pin_timestamp: Option<DateTime<FixedOffset>>, /// The name of the channel. pub name: String, /// Permission overwrites for [`Member`]s and for [`Role`]s. diff --git a/src/model/channel/message.rs b/src/model/channel/message.rs index 8b9f0af..612113d 100644 --- a/src/model/channel/message.rs +++ b/src/model/channel/message.rs @@ -1,3 +1,4 @@ +use chrono::{DateTime, FixedOffset}; use ::model::*; #[cfg(feature="cache")] @@ -5,8 +6,6 @@ use std::fmt::Write; #[cfg(feature="model")] use std::mem; #[cfg(feature="model")] -use time; -#[cfg(feature="model")] use ::builder::{CreateEmbed, CreateMessage}; #[cfg(feature="model")] use ::constants; @@ -33,7 +32,7 @@ pub struct Message { /// The content of the message. pub content: String, /// The timestamp of the last time the message was updated, if it was. - pub edited_timestamp: Option<String>, + pub edited_timestamp: Option<DateTime<FixedOffset>>, /// Array of embeds sent with the message. pub embeds: Vec<Embed>, /// Indicator of the type of message this is, i.e. whether it is a regular @@ -56,7 +55,7 @@ pub struct Message { #[serde(default)] pub reactions: Vec<MessageReaction>, /// Initial message creation timestamp, calculated from its Id. - pub timestamp: String, + pub timestamp: DateTime<FixedOffset>, /// Indicator of whether the command is to be played back via /// text-to-speech. /// @@ -240,16 +239,14 @@ impl Message { self.content = format!("{} pinned a message to this channel. See all the pins.", self.author); }, MessageType::MemberJoin => { - if let Ok(tm) = time::strptime(&self.timestamp, "%Y-%m-%dT%H:%M:%S") { - let sec = tm.to_timespec().sec as usize; - let chosen = constants::JOIN_MESSAGES[sec % constants::JOIN_MESSAGES.len()]; - - self.content = if chosen.contains("$user") { - chosen.replace("$user", &self.author.mention()) - } else { - chosen.to_owned() - }; - } + let sec = self.timestamp.timestamp() as usize; + let chosen = constants::JOIN_MESSAGES[sec % constants::JOIN_MESSAGES.len()]; + + self.content = if chosen.contains("$user") { + chosen.replace("$user", &self.author.mention()) + } else { + chosen.to_owned() + }; }, _ => {}, } diff --git a/src/model/channel/private_channel.rs b/src/model/channel/private_channel.rs index 7a0ed54..797ba94 100644 --- a/src/model/channel/private_channel.rs +++ b/src/model/channel/private_channel.rs @@ -1,3 +1,4 @@ +use chrono::{DateTime, FixedOffset}; use std::fmt::{Display, Formatter, Result as FmtResult}; use super::deserialize_single_recipient; use ::model::*; @@ -21,7 +22,7 @@ pub struct PrivateChannel { /// Timestamp of the last time a [`Message`] was pinned. /// /// [`Message`]: struct.Message.html - pub last_pin_timestamp: Option<String>, + pub last_pin_timestamp: Option<DateTime<FixedOffset>>, /// Indicator of the type of channel this is. /// /// This should always be [`ChannelType::Private`]. diff --git a/src/model/event.rs b/src/model/event.rs index 6458b77..bbb60c7 100644 --- a/src/model/event.rs +++ b/src/model/event.rs @@ -1,5 +1,6 @@ //! All the events this library handles. +use chrono::{DateTime, FixedOffset}; use serde::de::Error as DeError; use serde_json::{self, Error as JsonError}; use std::collections::HashMap; @@ -55,7 +56,7 @@ impl<'de> Deserialize<'de> for ChannelDeleteEvent { #[derive(Clone, Debug, Deserialize)] pub struct ChannelPinsUpdateEvent { pub channel_id: ChannelId, - pub last_pin_timestamp: Option<String>, + pub last_pin_timestamp: Option<DateTime<FixedOffset>>, } #[derive(Clone, Debug, Deserialize)] @@ -278,8 +279,8 @@ pub struct MessageUpdateEvent { pub nonce: Option<String>, pub tts: Option<bool>, pub pinned: Option<bool>, - pub timestamp: Option<String>, - pub edited_timestamp: Option<String>, + pub timestamp: Option<DateTime<FixedOffset>>, + pub edited_timestamp: Option<DateTime<FixedOffset>>, pub author: Option<User>, pub mention_everyone: Option<bool>, pub mentions: Option<Vec<User>>, diff --git a/src/model/guild/member.rs b/src/model/guild/member.rs index 99bf67b..0082643 100644 --- a/src/model/guild/member.rs +++ b/src/model/guild/member.rs @@ -1,3 +1,4 @@ +use chrono::{DateTime, FixedOffset}; use std::fmt::{Display, Formatter, Result as FmtResult}; use super::deserialize_sync_user; use ::model::*; @@ -23,7 +24,7 @@ pub struct Member { /// The unique Id of the guild that the member is a part of. pub guild_id: Option<GuildId>, /// Timestamp representing the date when the member joined. - pub joined_at: String, + pub joined_at: Option<DateTime<FixedOffset>>, /// Indicator of whether the member can speak in voice channels. pub mute: bool, /// The member's nickname, if present. @@ -326,7 +327,7 @@ impl Member { .unwrap() .members .values() - .any(|m| m.user.read().unwrap().id == self.user.read().unwrap().id && m.joined_at == *self.joined_at)) + .any(|m| m.user.read().unwrap().id == self.user.read().unwrap().id && m.joined_at == self.joined_at)) .map(|guild| guild .read() .unwrap() diff --git a/src/model/guild/mod.rs b/src/model/guild/mod.rs index 24e1881..a744b2c 100644 --- a/src/model/guild/mod.rs +++ b/src/model/guild/mod.rs @@ -14,6 +14,7 @@ pub use self::member::*; pub use self::partial_guild::*; pub use self::role::*; +use chrono::{DateTime, FixedOffset}; use serde::de::Error as DeError; use serde_json; use super::utils::*; @@ -73,7 +74,7 @@ pub struct Guild { /// that of the default channel (typically `#general`). pub id: GuildId, /// The date that the current user joined the guild. - pub joined_at: String, + pub joined_at: DateTime<FixedOffset>, /// Indicator of whether the guild is considered "large" by Discord. pub large: bool, /// The number of members in the guild. @@ -1149,7 +1150,7 @@ impl<'de> Deserialize<'de> for Guild { .map_err(DeError::custom)?; let joined_at = map.remove("joined_at") .ok_or_else(|| DeError::custom("expected guild joined_at")) - .and_then(String::deserialize) + .and_then(DateTime::deserialize) .map_err(DeError::custom)?; let large = map.remove("large") .ok_or_else(|| DeError::custom("expected guild large")) diff --git a/src/model/invite.rs b/src/model/invite.rs index 7b67296..7ff3fd1 100644 --- a/src/model/invite.rs +++ b/src/model/invite.rs @@ -1,3 +1,4 @@ +use chrono::{DateTime, FixedOffset}; use super::*; #[cfg(feature="cache")] @@ -228,7 +229,7 @@ pub struct RichInvite { /// The unique code for the invite. pub code: String, /// When the invite was created. - pub created_at: String, + pub created_at: DateTime<FixedOffset>, /// A representation of the minimal amount of information needed about the /// guild being invited to. pub guild: InviteGuild, @@ -302,7 +303,7 @@ impl RichInvite { /// # name: "foo".to_owned(), /// # kind: ChannelType::Text, /// # }, - /// # created_at: "bar".to_owned(), + /// # created_at: "2017-01-29T15:35:17.136000+00:00".parse().unwrap(), /// # guild: InviteGuild { /// # id: GuildId(2), /// # icon: None, diff --git a/src/model/mod.rs b/src/model/mod.rs index 26c6f42..bc414c7 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -40,12 +40,12 @@ pub use self::user::*; pub use self::voice::*; pub use self::webhook::*; +use chrono::NaiveDateTime; use self::utils::*; use serde::de::Visitor; use std::collections::HashMap; use std::fmt::{Formatter, Result as FmtResult}; use std::sync::{Arc, RwLock}; -use time::Timespec; use ::internal::prelude::*; #[cfg(feature="utils")] @@ -63,10 +63,10 @@ macro_rules! id { impl $name { /// Retrieves the time that the Id was created at. - pub fn created_at(&self) -> Timespec { + pub fn created_at(&self) -> NaiveDateTime { let offset = (self.0 >> 22) / 1000; - Timespec::new(1420070400 + offset as i64, 0) + NaiveDateTime::from_timestamp(1420070400 + offset as i64, 0) } } diff --git a/src/model/user.rs b/src/model/user.rs index e1483d2..ef26ca1 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -6,13 +6,13 @@ use ::internal::prelude::*; use ::model::misc::Mentionable; #[cfg(feature="model")] +use chrono::NaiveDateTime; +#[cfg(feature="model")] use std::fmt::Write; #[cfg(feature="model")] use std::mem; #[cfg(feature="cache")] use std::sync::{Arc, RwLock}; -#[cfg(feature="model")] -use time::Timespec; #[cfg(feature="cache")] use ::CACHE; #[cfg(feature="model")] @@ -377,7 +377,7 @@ impl User { /// Retrieves the time that this user was created at. #[inline] - pub fn created_at(&self) -> Timespec { + pub fn created_at(&self) -> NaiveDateTime { self.id.created_at() } |