aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.toml2
-rw-r--r--src/model/channel.rs88
-rw-r--r--src/model/guild.rs38
-rw-r--r--src/model/id.rs18
-rw-r--r--src/model/invite.rs10
-rw-r--r--src/model/user.rs10
-rw-r--r--src/model/utils.rs7
7 files changed, 104 insertions, 69 deletions
diff --git a/Cargo.toml b/Cargo.toml
index c49f977..84fba9f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -45,7 +45,7 @@ version = "0.0.12"
yaml-rust = "0.3"
[features]
-default = ["extras", "framework", "methods", "cache"]
+default = ["cache", "extras", "framework", "methods"]
cache = []
debug = []
extras = []
diff --git a/src/model/channel.rs b/src/model/channel.rs
index f34eea0..3be8836 100644
--- a/src/model/channel.rs
+++ b/src/model/channel.rs
@@ -30,8 +30,10 @@ use super::utils;
#[cfg(feature = "methods")]
use ::utils::builder::{CreateEmbed, CreateInvite, EditChannel};
-#[cfg(feature = "methods")]
-use ::client::{CACHE, http};
+#[cfg(all(feature = "cache", feature = "methods"))]
+use ::client::CACHE;
+#[cfg(all(feature = "methods"))]
+use ::client::http;
impl Attachment {
/// If this attachment is an image, then a tuple of the width and height
@@ -433,7 +435,7 @@ impl Message {
/// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions
/// [`ClientError::InvalidUser`]: ../client/enum.ClientError.html#variant.InvalidUser
/// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html
- #[cfg(feature = "methods")]
+ #[cfg(all(feature = "cache", feature = "methods"))]
pub fn delete(&self) -> Result<()> {
let req = permissions::MANAGE_MESSAGES;
let is_author = self.author.id != CACHE.read().unwrap().user.id;
@@ -461,7 +463,7 @@ impl Message {
/// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions
/// [`Reaction`]: struct.Reaction.html
/// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html
- #[cfg(feature = "methods")]
+ #[cfg(all(feature = "cache", feature = "methods"))]
pub fn delete_reactions(&self) -> Result<()> {
let req = permissions::MANAGE_MESSAGES;
@@ -487,8 +489,8 @@ impl Message {
///
/// # Errors
///
- /// Returns a [`ClientError::InvalidUser`] if the current user is not the
- /// author.
+ /// If the `cache` is enabled, returns a [`ClientError::InvalidUser`] if the
+ /// current user is not the author.
///
/// Returns a [`ClientError::MessageTooLong`] if the content of the message
/// is over the above limit, containing the number of unicode code points
@@ -503,9 +505,11 @@ impl Message {
return Err(Error::Client(ClientError::MessageTooLong(length_over)));
}
- if self.author.id != CACHE.read().unwrap().user.id {
- return Err(Error::Client(ClientError::InvalidUser));
- }
+ feature_cache_enabled! {{
+ if self.author.id != CACHE.read().unwrap().user.id {
+ return Err(Error::Client(ClientError::InvalidUser));
+ }
+ }}
let mut map = ObjectBuilder::new().insert("content", new_content);
@@ -714,15 +718,17 @@ impl PrivateChannel {
///
/// # Errors
///
- /// Returns a
+ /// If the `cache` is enabled, then returns a
/// [`ClientError::InvalidUser`] if the current user is not a bot user.
///
/// [`ClientError::InvalidUser`]: ../client/enum.ClientError.html#variant.InvalidOperationAsUser
#[cfg(feature = "methods")]
pub fn delete_messages(&self, message_ids: &[MessageId]) -> Result<()> {
- if !CACHE.read().unwrap().user.bot {
- return Err(Error::Client(ClientError::InvalidOperationAsUser));
- }
+ feature_cache_enabled! {{
+ if !CACHE.read().unwrap().user.bot {
+ return Err(Error::Client(ClientError::InvalidOperationAsUser));
+ }
+ }}
let ids: Vec<u64> = message_ids.into_iter()
.map(|message_id| message_id.0)
@@ -970,38 +976,46 @@ impl Reaction {
///
/// # Errors
///
- /// Returns a [`ClientError::InvalidPermissions`] if the current user does
- /// not have the required [permissions].
+ /// If the `cache` is enabled, then returns a
+ /// [`ClientError::InvalidPermissions`] if the current user does not have
+ /// the required [permissions].
///
/// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions
/// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html
/// [permissions]: permissions
#[cfg(feature = "methods")]
pub fn delete(&self) -> Result<()> {
- let user = if self.user_id == CACHE.read().unwrap().user.id {
- None
- } else {
- Some(self.user_id.0)
- };
-
- // If the reaction is one _not_ made by the current user, then ensure
- // that the current user has permission* to delete the reaction.
- //
- // Normally, users can only delete their own reactions.
- //
- // * The `Manage Messages` permission.
- if user.is_some() {
- let req = permissions::MANAGE_MESSAGES;
-
- if !utils::user_has_perms(self.channel_id, req).unwrap_or(true) {
- return Err(Error::Client(ClientError::InvalidPermissions(req)));
+ feature_cache! {{
+ let user = if self.user_id == CACHE.read().unwrap().user.id {
+ None
+ } else {
+ Some(self.user_id.0)
+ };
+
+ // If the reaction is one _not_ made by the current user, then ensure
+ // that the current user has permission* to delete the reaction.
+ //
+ // Normally, users can only delete their own reactions.
+ //
+ // * The `Manage Messages` permission.
+ if user.is_some() {
+ let req = permissions::MANAGE_MESSAGES;
+
+ if !utils::user_has_perms(self.channel_id, req).unwrap_or(true) {
+ return Err(Error::Client(ClientError::InvalidPermissions(req)));
+ }
}
- }
- http::delete_reaction(self.channel_id.0,
- self.message_id.0,
- user,
- self.emoji.clone())
+ http::delete_reaction(self.channel_id.0,
+ self.message_id.0,
+ user,
+ self.emoji.clone())
+ } else {
+ http::delete_reaction(self.channel_id.0,
+ self.message_id.0,
+ Some(self.user_id.0),
+ self.emoji.clone())
+ }}
}
/// Retrieves the list of [`User`]s who have reacted to a [`Message`] with a
diff --git a/src/model/guild.rs b/src/model/guild.rs
index 121521c..842333b 100644
--- a/src/model/guild.rs
+++ b/src/model/guild.rs
@@ -50,7 +50,7 @@ impl Emoji {
/// Finds the [`Guild`] that owns the emoji by looking through the Cache.
///
/// [`Guild`]: struct.Guild.html
- #[cfg(feature = "methods")]
+ #[cfg(all(feature = "cache", feature = "methods"))]
pub fn find_guild_id(&self) -> Option<GuildId> {
CACHE.read()
.unwrap()
@@ -67,7 +67,7 @@ impl Emoji {
/// **Note**: Only user accounts may use this method.
///
/// [Manage Emojis]: permissions/constant.MANAGE_EMOJIS.html
- #[cfg(feature = "methods")]
+ #[cfg(all(feature = "cache", feature = "methods"))]
pub fn delete(&self) -> Result<()> {
match self.find_guild_id() {
Some(guild_id) => http::delete_emoji(guild_id.0, self.id.0),
@@ -82,7 +82,7 @@ impl Emoji {
/// **Note**: Only user accounts may use this method.
///
/// [Manage Emojis]: permissions/constant.MANAGE_EMOJIS.html
- #[cfg(feature = "methods")]
+ #[cfg(all(feature = "cache", feature = "methods"))]
pub fn edit(&mut self, name: &str) -> Result<()> {
match self.find_guild_id() {
Some(guild_id) => {
@@ -373,17 +373,19 @@ impl Guild {
///
/// # Errors
///
- /// Returns a [`ClientError::InvalidUser`] if the current user is not the
- /// guild owner.
+ /// If the `cache` is enabled, then returns a [`ClientError::InvalidUser`]
+ /// if the current user is not the guild owner.
///
/// [`ClientError::InvalidUser`]: ../client/enum.ClientError.html#variant.InvalidUser
#[cfg(feature = "methods")]
pub fn delete(&self) -> Result<PartialGuild> {
- if self.owner_id != CACHE.read().unwrap().user.id {
- let req = permissions::MANAGE_GUILD;
+ feature_cache_enabled! {{
+ if self.owner_id != CACHE.read().unwrap().user.id {
+ let req = permissions::MANAGE_GUILD;
- return Err(Error::Client(ClientError::InvalidPermissions(req)));
- }
+ return Err(Error::Client(ClientError::InvalidPermissions(req)));
+ }
+ }}
http::delete_guild(self.id.0)
}
@@ -768,7 +770,7 @@ impl Member {
///
/// [`Role`]: struct.Role.html
/// [Manage Roles]: permissions/constant.MANAGE_ROLES.html
- #[cfg(feature = "methods")]
+ #[cfg(all(feature = "cache", feature = "methods"))]
pub fn add_role<R: Into<RoleId>>(&mut self, role_id: R) -> Result<()> {
let role_id = role_id.into();
@@ -795,7 +797,7 @@ impl Member {
///
/// [`Role`]: struct.Role.html
/// [Manage Roles]: permissions/constant.MANAGE_ROLES.html
- #[cfg(feature = "methods")]
+ #[cfg(all(feature = "cache", feature = "methods"))]
pub fn add_roles(&mut self, role_ids: &[RoleId]) -> Result<()> {
let guild_id = try!(self.find_guild());
self.roles.extend_from_slice(role_ids);
@@ -818,7 +820,7 @@ impl Member {
/// **Note**: Requires the [Ban Members] role.
///
/// [Ban Members]: permissions/constant.BAN_MEMBERS.html
- #[cfg(feature = "methods")]
+ #[cfg(all(feature = "cache", feature = "methods"))]
pub fn ban(&self, delete_message_days: u8) -> Result<()> {
let guild_id = try!(self.find_guild());
@@ -842,7 +844,7 @@ impl Member {
///
/// [`Context::edit_member`]: ../client/struct.Context.html#method.edit_member
/// [`EditMember`]: ../builder/struct.EditMember.html
- #[cfg(feature = "methods")]
+ #[cfg(all(feature = "cache", feature = "methods"))]
pub fn edit<F>(&self, f: F) -> Result<()>
where F: FnOnce(EditMember) -> EditMember {
let guild_id = try!(self.find_guild());
@@ -854,7 +856,7 @@ impl Member {
/// Finds the Id of the [`Guild`] that the member is in.
///
/// [`Guild`]: struct.Guild.html
- #[cfg(feature = "methods")]
+ #[cfg(all(feature = "cache", feature = "methods"))]
pub fn find_guild(&self) -> Result<GuildId> {
CACHE.read()
.unwrap()
@@ -881,7 +883,7 @@ impl Member {
///
/// [`Role`]: struct.Role.html
/// [Manage Roles]: permissions/constant.MANAGE_ROLES.html
- #[cfg(feature = "methods")]
+ #[cfg(all(feature = "cache", feature = "methods"))]
pub fn remove_role<R: Into<RoleId>>(&mut self, role_id: R) -> Result<()> {
let role_id = role_id.into();
@@ -907,7 +909,7 @@ impl Member {
///
/// [`Role`]: struct.Role.html
/// [Manage Roles]: permissions/constant.MANAGE_ROLES.html
- #[cfg(feature = "methods")]
+ #[cfg(all(feature = "cache", feature = "methods"))]
pub fn remove_roles(&mut self, role_ids: &[RoleId]) -> Result<()> {
let guild_id = try!(self.find_guild());
self.roles.retain(|r| !role_ids.contains(r));
@@ -1011,7 +1013,7 @@ impl Role {
/// **Note** Requires the [Manage Roles] permission.
///
/// [Manage Roles]: permissions/constant.MANAGE_ROLES.html
- #[cfg(feature = "methods")]
+ #[cfg(all(feature = "cache", feature = "methods"))]
pub fn delete(&self) -> Result<()> {
let guild_id = try!(self.find_guild());
@@ -1026,7 +1028,7 @@ impl Role {
/// that contains the role.
///
/// [`ClientError::GuildNotFound`]: ../client/enum.ClientError.html#variant.GuildNotFound
- #[cfg(feature = "methods")]
+ #[cfg(all(feature = "cache", feature = "methods"))]
pub fn find_guild(&self) -> Result<GuildId> {
CACHE.read()
.unwrap()
diff --git a/src/model/id.rs b/src/model/id.rs
index 9c9cb4d..d29fa2a 100644
--- a/src/model/id.rs
+++ b/src/model/id.rs
@@ -1,13 +1,15 @@
use super::*;
+#[cfg(all(feature = "cache", feature = "methods"))]
+use ::client::CACHE;
#[cfg(feature = "methods")]
-use ::client::{CACHE, http};
+use ::client::http;
#[cfg(feature = "methods")]
use ::internal::prelude::*;
impl ChannelId {
/// Search the cache for the channel with the Id.
- #[cfg(feature="methods")]
+ #[cfg(all(feature = "cache", feature = "methods"))]
pub fn find(&self) -> Option<Channel> {
CACHE.read().unwrap().get_channel(*self)
}
@@ -16,9 +18,11 @@ impl ChannelId {
/// requested over REST.
#[cfg(feature="methods")]
pub fn get(&self) -> Result<Channel> {
- if let Some(channel) = CACHE.read().unwrap().get_channel(*self) {
- return Ok(channel.clone());
- }
+ feature_cache_enabled! {{
+ if let Some(channel) = CACHE.read().unwrap().get_channel(*self) {
+ return Ok(channel.clone());
+ }
+ }}
http::get_channel(self.0)
}
@@ -75,7 +79,7 @@ impl From<Emoji> for EmojiId {
impl GuildId {
/// Search the cache for the guild.
- #[cfg(feature="methods")]
+ #[cfg(all(feature = "cache", feature="methods"))]
pub fn find(&self) -> Option<Guild> {
CACHE.read().unwrap().get_guild(*self).cloned()
}
@@ -161,7 +165,7 @@ impl From<Role> for RoleId {
impl RoleId {
/// Search the cache for the role.
- #[cfg(feature="methods")]
+ #[cfg(all(feature = "cache", feature = "methods"))]
pub fn find(&self) -> Option<Role> {
CACHE.read()
.unwrap()
diff --git a/src/model/invite.rs b/src/model/invite.rs
index 5216509..2f65fd1 100644
--- a/src/model/invite.rs
+++ b/src/model/invite.rs
@@ -109,13 +109,15 @@ impl RichInvite {
/// [`http::delete_invite`]: ../client/http/fn.delete_invite.html
/// [Manage Guild]: permissions/constant.MANAGE_GUILD.html
/// [permission]: permissions/index.html
- #[cfg(feature="methods")]
+ #[cfg(feature = "methods")]
pub fn delete(&self) -> Result<Invite> {
let req = permissions::MANAGE_GUILD;
- if !try!(utils::user_has_perms(self.channel.id, req)) {
- return Err(Error::Client(ClientError::InvalidPermissions(req)));
- }
+ feature_cache_enabled! {{
+ if !try!(utils::user_has_perms(self.channel.id, req)) {
+ return Err(Error::Client(ClientError::InvalidPermissions(req)));
+ }
+ }}
http::delete_invite(&self.code)
}
diff --git a/src/model/user.rs b/src/model/user.rs
index 4030644..bedf696 100644
--- a/src/model/user.rs
+++ b/src/model/user.rs
@@ -61,7 +61,7 @@ impl User {
#[cfg(feature="methods")]
pub fn direct_message(&self, content: &str)
-> Result<Message> {
- let private_channel_id = {
+ let private_channel_id = feature_cache! {{
let finding = CACHE.read()
.unwrap()
.private_channels
@@ -78,7 +78,13 @@ impl User {
try!(http::create_private_channel(map)).id
}
- };
+ } else {
+ let map = ObjectBuilder::new()
+ .insert("recipient_id", self.id.0)
+ .build();
+
+ try!(http::create_private_channel(map)).id
+ }};
let map = ObjectBuilder::new()
.insert("content", content)
diff --git a/src/model/utils.rs b/src/model/utils.rs
index d6a4cc8..a995758 100644
--- a/src/model/utils.rs
+++ b/src/model/utils.rs
@@ -302,6 +302,13 @@ pub fn user_has_perms(channel_id: ChannelId,
Ok(permissions.is_empty())
}
+#[doc(hidden)]
+#[cfg(all(not(feature = "cache"), feature = "methods"))]
+pub fn user_has_perms(channel_id: ChannelId, mut permissions: Permissions)
+ -> Result<bool> {
+ Ok(true)
+}
+
pub fn warn_field(name: &str, map: BTreeMap<String, Value>) {
if !map.is_empty() {
debug!("Unhandled keys: {} has {:?}", name, Value::Object(map))