aboutsummaryrefslogtreecommitdiff
path: root/src/model/utils.rs
diff options
context:
space:
mode:
authorZeyla Hellyer <[email protected]>2017-03-20 22:05:12 -0700
committerZeyla Hellyer <[email protected]>2017-03-20 22:10:15 -0700
commitcd914f503c8f0ada7473b5b56e4ad7830370ea45 (patch)
tree3f72ca3f878efe2d96a1e64479e6e23dc7c95113 /src/model/utils.rs
parentFix deadlock/panic on channel create event for private messages (diff)
downloadserenity-cd914f503c8f0ada7473b5b56e4ad7830370ea45.tar.xz
serenity-cd914f503c8f0ada7473b5b56e4ad7830370ea45.zip
Fix Member methods due to variant joined_at values
Fix an issue where the library would check the Id of the guild that a member is in by checking the Member's ID and joined_at value with those of the members of guilds present in the cache. Performing this check would have the issue of where a joined_at for a member received over websocket would potentially have a varying value than that of the same member retrieved over REST. To fix this, attach the relevant guild's Id to the member on creation, where the Id is available. Fixes #68.
Diffstat (limited to 'src/model/utils.rs')
-rw-r--r--src/model/utils.rs34
1 files changed, 18 insertions, 16 deletions
diff --git a/src/model/utils.rs b/src/model/utils.rs
index 8de7dd2..17f91f1 100644
--- a/src/model/utils.rs
+++ b/src/model/utils.rs
@@ -1,21 +1,6 @@
use std::collections::{BTreeMap, HashMap};
use std::sync::{Arc, RwLock};
-use super::{
- Channel,
- ChannelId,
- Emoji,
- EmojiId,
- Member,
- Message,
- Presence,
- ReadState,
- Relationship,
- Role,
- RoleId,
- User,
- UserId,
- VoiceState,
-};
+use super::*;
use ::internal::prelude::*;
use ::utils::{decode_array, into_array};
@@ -100,6 +85,23 @@ pub fn decode_members(value: Value) -> Result<HashMap<UserId, Member>> {
Ok(members)
}
+pub fn decode_guild_members(guild_id: GuildId, value: Value) -> Result<HashMap<UserId, Member>> {
+ let mut members = HashMap::new();
+ let member_vec = into_array(value).map(|x| x
+ .into_iter()
+ .map(|v| Member::decode_guild(guild_id, v))
+ .filter_map(Result::ok)
+ .collect::<Vec<_>>())?;
+
+ for member in member_vec {
+ let user_id = member.user.read().unwrap().id;
+
+ members.insert(user_id, member);
+ }
+
+ Ok(members)
+}
+
// Clippy's lint is incorrect here and will result in invalid code.
//
// Bit more detaul: `result_unwrap_or_default` is not yet stable as of rustc