aboutsummaryrefslogtreecommitdiff
path: root/src/model/id.rs
blob: 97ee6d635bbf323a62fd9893effcb146874bd304 (plain) (blame)
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
use super::*;
use ::client::{STATE, http};
use ::prelude::*;

impl ChannelId {
    /// Search the state for the channel with the Id.
    pub fn find(&self) -> Option<Channel> {
        STATE.lock().unwrap().find_channel(*self)
    }

    /// Search the state for the channel. If it can't be found, the channel is
    /// requested over REST.
    pub fn get(&self) -> Result<Channel> {
        if let Some(channel) = STATE.lock().unwrap().find_channel(*self) {
            return Ok(channel);
        }

        http::get_channel(self.0)
    }

    /// Returns a [Mention](struct.Mention.html) which will link to the
    /// channel.
    pub fn mention(&self) -> Mention {
        Mention {
            id: self.0,
            prefix: "<#",
        }
    }
}

impl From<Channel> for ChannelId {
    fn from(channel: Channel) -> ChannelId {
        match channel {
            Channel::Group(group) => group.channel_id,
            Channel::Private(channel) => channel.id,
            Channel::Public(channel) => channel.id,
        }
    }
}

impl From<PrivateChannel> for ChannelId {
    fn from(private_channel: PrivateChannel) -> ChannelId {
        private_channel.id
    }
}

impl From<PublicChannel> for ChannelId {
    fn from(public_channel: PublicChannel) -> ChannelId {
        public_channel.id
    }
}

impl From<Emoji> for EmojiId {
    fn from(emoji: Emoji) -> EmojiId {
        emoji.id
    }
}

impl GuildId {
    /// Search the state for the guild.
    pub fn find(&self) -> Option<LiveGuild> {
        STATE.lock().unwrap().find_guild(*self).cloned()
    }

    /// Requests the guild over REST.
    ///
    /// Note that this will not be a complete guild, as REST does not send
    /// all data with a guild retrieval.
    pub fn get(&self) -> Result<Guild> {
        http::get_guild(self.0)
    }

    /// Mentions the [Guild](struct.Guild.html)'s default channel.
    pub fn mention(&self) -> Mention {
        Mention {
            id: self.0,
            prefix: "<#",
        }
    }
}

impl From<Guild> for GuildId {
    fn from(guild: Guild) -> GuildId {
        guild.id
    }
}

impl From<GuildInfo> for GuildId {
    fn from(guild_info: GuildInfo) -> GuildId {
        guild_info.id
    }
}

impl From<InviteGuild> for GuildId {
    fn from(invite_guild: InviteGuild) -> GuildId {
        invite_guild.id
    }
}

impl From<LiveGuild> for GuildId {
    fn from(live_guild: LiveGuild) -> GuildId {
        live_guild.id
    }
}

impl From<Integration> for IntegrationId {
    fn from(integration: Integration) -> IntegrationId {
        integration.id
    }
}

impl From<Message> for MessageId {
    fn from(message: Message) -> MessageId {
        message.id
    }
}

impl From<Role> for RoleId {
    fn from(role: Role) -> RoleId {
        role.id
    }
}

impl RoleId {
    /// Search the state for the role.
    pub fn find(&self) -> Option<Role> {
        STATE.lock()
            .unwrap()
            .guilds
            .values()
            .find(|guild| guild.roles.contains_key(self))
            .map(|guild| guild.roles.get(self))
            .and_then(|v| match v {
                Some(v) => Some(v),
                None => None,
            })
            .cloned()
    }

    /// Returns a [Mention](struct.Mention.html) which will ping members of the
    /// role.
    pub fn mention(&self) -> Mention {
        Mention {
            id: self.0,
            prefix: "<@&",
        }
    }
}

impl From<CurrentUser> for UserId {
    fn from(current_user: CurrentUser) -> UserId {
        current_user.id
    }
}

impl From<Member> for UserId {
    fn from(member: Member) -> UserId {
        member.user.id
    }
}

impl From<User> for UserId {
    fn from(user: User) -> UserId {
        user.id
    }
}

impl UserId {
    /// Returns a [Mention](struct.Mention.html) which will ping the user.
    pub fn mention(&self) -> Mention {
        Mention {
            id: self.0,
            prefix: "<@",
        }
    }
}