diff options
| author | Zeyla Hellyer <[email protected]> | 2017-04-11 08:15:37 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-04-11 10:52:43 -0700 |
| commit | f6b27eb39c042e6779edc2d5d4b6e6c27d133eaf (patch) | |
| tree | a6169fee3bf9ea75391101577dcb2982e3daa388 /src/model/channel/message.rs | |
| parent | Clippy lints + permission byte literals (diff) | |
| download | serenity-f6b27eb39c042e6779edc2d5d4b6e6c27d133eaf.tar.xz serenity-f6b27eb39c042e6779edc2d5d4b6e6c27d133eaf.zip | |
Switch to using serde for deserialization
The current build system is rudimentary, incomplete, and rigid, offering
little in the way of customizing decoding options.
To solve this, switch to using serde-derive with custom Deserialization
implementations. This allows very simple deserialization when special
logic does not need to be applied, yet allows us to implement our own
deserialization logic when required.
The problem with the build system was that it built enums and structs
from YAML files. This is not so good, because it requires creating a
custom build system (which was rudimentary), creating "special struct
configs" when logic needed to be ever so slightly extended (rigid), and
if special logic needed to be applied, a custom deserialization method
would have been needed to be made anyway (incomplete).
To solve this, switch to serde-derive and implementing Deserialize
ourselves where required. This reduces YAML definitions that might
look like:
```yaml
---
name: Group
description: >
A group channel, potentially including other users, separate from a [`Guild`].
[`Guild`]: struct.Guild.html
fields:
- name: channel_id
description: The Id of the group channel.
from: id
type: ChannelId
- name: icon
description: The optional icon of the group channel.
optional: true
type: string
- name: last_message_id
description: The Id of the last message sent.
optional: true
type: MessageId
- name: last_pin_timestamp
description: Timestamp of the latest pinned message.
optional: true
type: string
- name: name
description: The name of the group channel.
optional: true
type: string
- name: owner_id
description: The Id of the group channel creator.
type: UserId
- name: recipients
description: Group channel's members.
custom: decode_users
t: UserId, Arc<RwLock<User>>
type: hashmap
```
to:
```rs
/// A group channel - potentially including other [`User`]s - separate from a
/// [`Guild`].
///
/// [`Guild`]: struct.Guild.html
/// [`User`]: struct.User.html
pub struct Group {
/// The Id of the group channel.
#[serde(rename="id")]
pub channel_id: ChannelId,
/// The optional icon of the group channel.
pub icon: Option<String>,
/// 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>,
/// The name of the group channel.
pub name: Option<String>,
/// The Id of the group owner.
pub owner_id: UserId,
/// A map of the group's recipients.
#[serde(deserialize_with="deserialize_users")]
pub recipients: HashMap<UserId, Arc<RwLock<User>>>,
}
```
This is much simpler and does not have as much boilerplate.
There should not be any backwards incompatible changes other than the
old, public - yet undocumented (and hidden from documentation) - decode
methods being removed. Due to the nature of this commit, field names may
be incorrect, and will need to be corrected as deserialization errors
are found.
Diffstat (limited to 'src/model/channel/message.rs')
| -rw-r--r-- | src/model/channel/message.rs | 125 |
1 files changed, 120 insertions, 5 deletions
diff --git a/src/model/channel/message.rs b/src/model/channel/message.rs index 709ce9a..029914d 100644 --- a/src/model/channel/message.rs +++ b/src/model/channel/message.rs @@ -1,4 +1,3 @@ -use serde_json::builder::ObjectBuilder; use std::mem; use ::constants; use ::client::rest; @@ -8,6 +7,63 @@ use ::utils::builder::{CreateEmbed, CreateMessage}; #[cfg(feature="cache")] use ::client::CACHE; +/// A representation of a message over a guild's text channel, a group, or a +/// private channel. +#[derive(Clone, Debug, Deserialize)] +pub struct Message { + /// The unique Id of the message. Can be used to calculate the creation date + /// of the message. + pub id: MessageId, + /// An array of the files attached to a message. + pub attachments: Vec<Attachment>, + /// The user that sent the message. + pub author: User, + /// The Id of the [`Channel`] that the message was sent to. + /// + /// [`Channel`]: enum.Channel.html + pub channel_id: ChannelId, + /// 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>, + /// Array of embeds sent with the message. + pub embeds: Vec<Embed>, + /// Whether the message is the "found" message in a search. + /// + /// Note that this is only relevant in the context of searches, and will + /// otherwise always be `false`. + #[serde(default)] + pub hit: bool, + /// Indicator of the type of message this is, i.e. whether it is a regular + /// message or a system message. + #[serde(rename="type")] + pub kind: MessageType, + /// Indicator of whether the message mentions everyone. + pub mention_everyone: bool, + /// Array of [`Role`]s' Ids mentioned in the message. + /// + /// [`Role`]: struct.Role.html + pub mention_roles: Vec<RoleId>, + /// Array of users mentioned in the message. + pub mentions: Vec<User>, + /// Non-repeating number used for ensuring message order. + pub nonce: Option<String>, + /// Indicator of whether the message is pinned. + pub pinned: bool, + /// Array of reactions performed on the message. + #[serde(default)] + pub reactions: Vec<MessageReaction>, + /// Initial message creation timestamp, calculated from its Id. + pub timestamp: String, + /// Indicator of whether the command is to be played back via + /// text-to-speech. + /// + /// In the client, this is done via the `/tts` slash command. + pub tts: bool, + /// The Id of the webhook that sent this message, if one did. + pub webhook_id: Option<WebhookId>, +} + impl Message { /// Deletes the message. /// @@ -328,10 +384,10 @@ impl Message { gen.push_str(": "); gen.push_str(content); - let map = ObjectBuilder::new() - .insert("content", gen) - .insert("tts", false) - .build(); + let map = json!({ + "content": gen, + "tts": false, + }); rest::send_message(self.channel_id.0, &map) } @@ -368,3 +424,62 @@ impl From<Message> for MessageId { message.id } } + +/// A representation of a reaction to a message. +/// +/// Multiple of the same [reaction type] are sent into one `MessageReaction`, +/// with an associated [`count`]. +/// +/// [`count`]: #structfield.count +/// [reaction type]: enum.ReactionType.html +#[derive(Clone, Debug, Deserialize)] +pub struct MessageReaction { + /// The amount of the type of reaction that have been sent for the + /// associated message. + pub count: u64, + /// Indicator of whether the current user has sent the type of reaction. + pub me: bool, + /// The type of reaction. + #[serde(rename="emoji")] + pub reaction_type: ReactionType, +} + +enum_number!( + /// Differentiates between regular and different types of system messages. + MessageType { + /// A regular message. + Regular = 0, + /// An indicator that a recipient was added by the author. + GroupRecipientAddition = 1, + /// An indicator that a recipient was removed by the author. + GroupRecipientRemoval = 2, + /// An indicator that a call was started by the author. + GroupCallCreation = 3, + /// An indicator that the group name was modified by the author. + GroupNameUpdate = 4, + /// An indicator that the group icon was modified by the author. + GroupIconUpdate = 5, + /// An indicator that a message was pinned by the author. + PinsAdd = 6, + } +); + +/// An emoji reaction to a message. +#[derive(Clone, Debug, Deserialize)] +pub struct Reaction { + /// The [`Channel`] of the associated [`Message`]. + /// + /// [`Channel`]: enum.Channel.html + /// [`Message`]: struct.Message.html + pub channel_id: ChannelId, + /// The reactive emoji used. + pub emoji: ReactionType, + /// The Id of the [`Message`] that was reacted to. + /// + /// [`Message`]: struct.Message.html + pub message_id: MessageId, + /// The Id of the [`User`] that sent the reaction. + /// + /// [`User`]: struct.User.html + pub user_id: UserId, +} |