aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoracdenisSK <[email protected]>2017-11-01 13:55:29 +0100
committeracdenisSK <[email protected]>2017-11-01 13:55:29 +0100
commitf4918098fcbb6df6129504428e44ccb363ef4ff8 (patch)
tree039d68278b2fb038bb6eaa3f2e258654d0a51305 /src
parentAdd a fallback to `RoleId::from_str` as well (diff)
downloadserenity-f4918098fcbb6df6129504428e44ccb363ef4ff8.tar.xz
serenity-f4918098fcbb6df6129504428e44ccb363ef4ff8.zip
Fix audit logs a bit
Diffstat (limited to 'src')
-rw-r--r--src/http/mod.rs14
-rw-r--r--src/model/guild/audit_log.rs107
-rw-r--r--src/model/guild/guild_id.rs7
-rw-r--r--src/model/guild/mod.rs7
4 files changed, 93 insertions, 42 deletions
diff --git a/src/http/mod.rs b/src/http/mod.rs
index 3fb5b7b..239c777 100644
--- a/src/http/mod.rs
+++ b/src/http/mod.rs
@@ -1055,12 +1055,20 @@ pub fn get_bans(guild_id: u64) -> Result<Vec<Ban>> {
}
/// Gets all audit logs in a specific guild.
-pub fn get_audit_logs(guild_id: u64) -> Result<AuditLogs> {
+pub fn get_audit_logs(guild_id: u64,
+ action_type: Option<u8>,
+ user_id: Option<u64>,
+ before: Option<u64>,
+ limit: Option<u8>) -> Result<AuditLogs> {
let response = request!(
Route::GuildsIdAuditLogs(guild_id),
get,
- "/guilds/{}/audit-logs",
- guild_id
+ "/guilds/{}/audit-logs?user_id={}&action_type={}&before={}&limit={}",
+ guild_id,
+ user_id.unwrap_or(0),
+ action_type.unwrap_or(0),
+ before.unwrap_or(0),
+ limit.unwrap_or(50),
);
serde_json::from_reader::<HyperResponse, AuditLogs>(response)
diff --git a/src/model/guild/audit_log.rs b/src/model/guild/audit_log.rs
index d019b61..08dc818 100644
--- a/src/model/guild/audit_log.rs
+++ b/src/model/guild/audit_log.rs
@@ -1,4 +1,4 @@
-use super::super::{AuditLogEntryId, UserId};
+use super::super::{AuditLogEntryId, User, UserId, ChannelId, Webhook};
use serde::de::{self, Deserialize, Deserializer, MapAccess, Visitor};
use std::fmt;
use std::collections::HashMap;
@@ -6,7 +6,7 @@ use std::mem::transmute;
/// Determines to what entity an action was used on.
#[derive(Debug)]
-#[repr(i32)]
+#[repr(u8)]
pub enum Target {
Guild = 10,
Channel = 20,
@@ -28,10 +28,11 @@ pub enum Action {
Invite(ActionInvite),
Webhook(ActionWebhook),
Emoji(ActionEmoji),
+ MessageDelete,
}
#[derive(Debug)]
-#[repr(i32)]
+#[repr(u8)]
pub enum ActionChannel {
Create = 10,
Update = 11,
@@ -39,7 +40,7 @@ pub enum ActionChannel {
}
#[derive(Debug)]
-#[repr(i32)]
+#[repr(u8)]
pub enum ActionChannelOverwrite {
Create = 13,
Update = 14,
@@ -47,7 +48,7 @@ pub enum ActionChannelOverwrite {
}
#[derive(Debug)]
-#[repr(i32)]
+#[repr(u8)]
pub enum ActionMember {
Kick = 20,
Prune = 21,
@@ -58,7 +59,7 @@ pub enum ActionMember {
}
#[derive(Debug)]
-#[repr(i32)]
+#[repr(u8)]
pub enum ActionRole {
Create = 30,
Update = 31,
@@ -66,7 +67,7 @@ pub enum ActionRole {
}
#[derive(Debug)]
-#[repr(i32)]
+#[repr(u8)]
pub enum ActionInvite {
Create = 40,
Update = 41,
@@ -74,7 +75,7 @@ pub enum ActionInvite {
}
#[derive(Debug)]
-#[repr(i32)]
+#[repr(u8)]
pub enum ActionWebhook {
Create = 50,
Update = 51,
@@ -82,7 +83,7 @@ pub enum ActionWebhook {
}
#[derive(Debug)]
-#[repr(i32)]
+#[repr(u8)]
pub enum ActionEmoji {
Create = 60,
Delete = 61,
@@ -92,6 +93,7 @@ pub enum ActionEmoji {
#[derive(Debug, Deserialize)]
pub struct Change {
#[serde(rename = "key")] pub name: String,
+ // TODO: Change these to an actual type.
#[serde(rename = "old_value")] pub old: String,
#[serde(rename = "new_value")] pub new: String,
}
@@ -99,6 +101,8 @@ pub struct Change {
#[derive(Debug)]
pub struct AuditLogs {
pub entries: HashMap<AuditLogEntryId, AuditLogEntry>,
+ pub webhooks: Vec<Webhook>,
+ pub users: Vec<User>,
}
#[derive(Debug, Deserialize)]
@@ -106,8 +110,7 @@ pub struct AuditLogEntry {
/// Determines to what entity an [`action`] was used on.
///
/// [`action`]: #structfield.action
- #[serde(deserialize_with = "deserialize_target", rename = "target_type")]
- pub target: Target,
+ pub target_id: u64,
/// Determines what action was done on a [`target`]
///
/// [`target`]: #structfield.target
@@ -118,30 +121,30 @@ pub struct AuditLogEntry {
/// The user that did this action on a target.
pub user_id: UserId,
/// What changes were made.
- pub changes: Vec<Change>,
+ pub changes: Option<Vec<Change>>,
/// The id of this entry.
pub id: AuditLogEntryId,
+ /// Some optional data assosiated with this entry.
+ pub options: Option<Options>,
}
-fn deserialize_target<'de, D: Deserializer<'de>>(de: D) -> Result<Target, D::Error> {
- struct TargetVisitor;
-
- impl<'de> Visitor<'de> for TargetVisitor {
- type Value = Target;
-
- fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
- formatter.write_str("an integer between 0 to 70")
- }
-
- fn visit_i32<E: de::Error>(self, value: i32) -> Result<Target, E> {
- Ok(match value {
- 10...70 => unsafe { transmute(value) },
- _ => return Err(E::custom(format!("unexpected target number: {}", value))),
- })
- }
- }
+#[derive(Debug, Deserialize)]
+pub struct Options {
+ /// Number of days after which inactive members were kicked.
+ pub delete_member_days: String,
+ /// Number of members removed by the prune
+ pub members_removed: String,
+ /// Channel in which the messages were deleted
+ pub channel_id: ChannelId,
+ /// Number of deleted messages.
+ pub count: u32,
+ /// Id of the overwritten entity
+ pub id: u64,
+ /// Type of overwritten entity ("member" or "role").
+ #[serde(rename = "type")] pub kind: String,
+ /// Name of the role if type is "role"
+ pub role_name: String,
- de.deserialize_i32(TargetVisitor)
}
fn deserialize_action<'de, D: Deserializer<'de>>(de: D) -> Result<Action, D::Error> {
@@ -151,10 +154,10 @@ fn deserialize_action<'de, D: Deserializer<'de>>(de: D) -> Result<Action, D::Err
type Value = Action;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
- formatter.write_str("an integer between 1 to 62")
+ formatter.write_str("an integer between 1 to 72")
}
- fn visit_i32<E: de::Error>(self, value: i32) -> Result<Action, E> {
+ fn visit_u8<E: de::Error>(self, value: u8) -> Result<Action, E> {
Ok(match value {
1 => Action::GuildUpdate,
10...12 => Action::Channel(unsafe { transmute(value) }),
@@ -164,6 +167,7 @@ fn deserialize_action<'de, D: Deserializer<'de>>(de: D) -> Result<Action, D::Err
40...42 => Action::Invite(unsafe { transmute(value) }),
50...52 => Action::Webhook(unsafe { transmute(value) }),
60...62 => Action::Emoji(unsafe { transmute(value) }),
+ 72 => Action::MessageDelete,
_ => return Err(E::custom(format!("Unexpected action number: {}", value))),
})
}
@@ -178,6 +182,8 @@ impl<'de> Deserialize<'de> for AuditLogs {
#[serde(field_identifier)]
enum Field {
#[serde(rename = "audit_log_entries")] Entries,
+ #[serde(rename = "webhooks")] Webhooks,
+ #[serde(rename = "users")] Users,
}
struct EntriesVisitor;
@@ -190,22 +196,49 @@ impl<'de> Deserialize<'de> for AuditLogs {
}
fn visit_map<V: MapAccess<'de>>(self, mut map: V) -> Result<AuditLogs, V::Error> {
- let audit_log_entries = loop {
- if let Some(Field::Entries) = map.next_key()? {
- break map.next_value::<Vec<AuditLogEntry>>()?;
+ let mut audit_log_entries = None;
+ let mut users = None;
+ let mut webhooks = None;
+
+ while let Some(field) = map.next_key()? {
+ match field {
+ Field::Entries => {
+ if audit_log_entries.is_some() {
+ return Err(de::Error::duplicate_field("entries"));
+ }
+
+ audit_log_entries = Some(map.next_value::<Vec<AuditLogEntry>>()?);
+ },
+ Field::Webhooks => {
+ if webhooks.is_some() {
+ return Err(de::Error::duplicate_field("webhooks"));
+ }
+
+ webhooks = Some(map.next_value::<Vec<Webhook>>()?);
+ },
+ Field::Users => {
+ if users.is_some() {
+ return Err(de::Error::duplicate_field("users"));
+ }
+
+ users = Some(map.next_value::<Vec<User>>()?);
+ },
}
- };
+ }
Ok(AuditLogs {
entries: audit_log_entries
+ .unwrap()
.into_iter()
.map(|entry| (entry.id, entry))
.collect(),
+ webhooks: webhooks.unwrap(),
+ users: users.unwrap(),
})
}
}
- const FIELD: &'static [&'static str] = &["audit_log_entries"];
+ const FIELD: &[&str] = &["audit_log_entries"];
de.deserialize_struct("AuditLogs", FIELD, EntriesVisitor)
}
}
diff --git a/src/model/guild/guild_id.rs b/src/model/guild/guild_id.rs
index 7a851bb..37b3886 100644
--- a/src/model/guild/guild_id.rs
+++ b/src/model/guild/guild_id.rs
@@ -73,7 +73,12 @@ impl GuildId {
/// Gets a list of the guild's audit log entries
#[inline]
- pub fn audit_logs(&self) -> Result<AuditLogs> { http::get_audit_logs(self.0) }
+ pub fn audit_logs(&self, action_type: Option<u8>,
+ user_id: Option<UserId>,
+ before: Option<AuditLogEntryId>,
+ limit: Option<u8>) -> Result<AuditLogs> {
+ http::get_audit_logs(self.0, action_type, user_id.map(|u| u.0), before.map(|a| a.0), limit)
+ }
/// Gets all of the guild's channels over the REST API.
///
diff --git a/src/model/guild/mod.rs b/src/model/guild/mod.rs
index 7aeb98f..7582180 100644
--- a/src/model/guild/mod.rs
+++ b/src/model/guild/mod.rs
@@ -250,7 +250,12 @@ impl Guild {
///
/// [`AuditLogs`]: audit_log/struct.AuditLogs.html
#[inline]
- pub fn audit_logs(&self) -> Result<AuditLogs> { self.id.audit_logs() }
+ pub fn audit_logs(&self, action_type: Option<u8>,
+ user_id: Option<UserId>,
+ before: Option<AuditLogEntryId>,
+ limit: Option<u8>) -> Result<AuditLogs> {
+ self.id.audit_logs(action_type, user_id, before, limit)
+ }
/// Gets all of the guild's channels over the REST API.
///