add viewing audit log

This commit is contained in:
ouwou
2021-01-24 22:24:03 -05:00
parent cfa6607fc8
commit 3923acd0dd
14 changed files with 690 additions and 7 deletions

34
discord/auditlog.cpp Normal file
View File

@@ -0,0 +1,34 @@
#include "auditlog.hpp"
void from_json(const nlohmann::json &j, AuditLogChange &m) {
JS_D("key", m.Key);
JS_O("old_value", m.OldValue);
JS_O("new_value", m.NewValue);
}
void from_json(const nlohmann::json &j, AuditLogOptions &m) {
JS_O("delete_member_days", m.DeleteMemberDays);
JS_O("members_removed", m.MembersRemoved);
JS_O("channel_id", m.ChannelID);
JS_O("message_id", m.MessageID);
JS_O("count", m.Count);
JS_O("id", m.ID);
JS_O("type", m.Type);
JS_O("role_name", m.RoleName);
}
void from_json(const nlohmann::json &j, AuditLogEntry &m) {
JS_N("target_id", m.TargetID);
JS_O("changes", m.Changes);
JS_D("user_id", m.UserID);
JS_D("id", m.ID);
JS_D("action_type", m.Type);
JS_O("options", m.Options);
JS_O("reason", m.Reason);
}
void from_json(const nlohmann::json &j, AuditLogData &m) {
JS_D("audit_log_entries", m.Entries);
JS_D("users", m.Users);
JS_D("webhooks", m.Webhooks);
}

111
discord/auditlog.hpp Normal file
View File

@@ -0,0 +1,111 @@
#pragma once
#include "snowflake.hpp"
#include "user.hpp"
#include "json.hpp"
#include "webhook.hpp"
enum class AuditLogActionType {
GUILD_UPDATE = 1,
CHANNEL_CREATE = 10,
CHANNEL_UPDATE = 11,
CHANNEL_DELETE = 12,
CHANNEL_OVERWRITE_CREATE = 13,
CHANNEL_OVERWRITE_UPDATE = 14,
CHANNEL_OVERWRITE_DELETE = 15,
MEMBER_KICK = 20,
MEMBER_PRUNE = 21,
MEMBER_BAN_ADD = 22,
MEMBER_BAN_REMOVE = 23,
MEMBER_UPDATE = 24,
MEMBER_ROLE_UPDATE = 25,
MEMBER_MOVE = 26,
MEMBER_DISCONNECT = 27,
BOT_ADD = 28,
ROLE_CREATE = 30,
ROLE_UPDATE = 31,
ROLE_DELETE = 32,
INVITE_CREATE = 40,
INVITE_UPDATE = 41,
INVITE_DELETE = 42,
WEBHOOK_CREATE = 50,
WEBHOOK_UPDATE = 51,
WEBHOOK_DELETE = 52,
EMOJI_CREATE = 60,
EMOJI_UPDATE = 61,
EMOJI_DELETE = 62,
MESSAGE_DELETE = 72,
MESSAGE_BULK_DELETE = 73,
MESSAGE_PIN = 74,
MESSAGE_UNPIN = 75,
INTEGRATION_CREATE = 80,
INTEGRATION_UPDATE = 81,
INTEGRATION_DELETE = 82,
};
struct AuditLogChange {
std::string Key;
std::optional<nlohmann::json> OldValue;
std::optional<nlohmann::json> NewValue;
friend void from_json(const nlohmann::json &j, AuditLogChange &m);
};
struct AuditLogOptions {
std::optional<std::string> DeleteMemberDays; // MEMBER_PRUNE
std::optional<std::string> MembersRemoved; // MEMBER_PRUNE
std::optional<Snowflake> ChannelID; // MEMBER_MOVE, MESSAGE_PIN, MESSAGE_UNPIN, MESSAGE_DELETE
std::optional<Snowflake> MessageID; // MESSAGE_PIN, MESSAGE_UNPIN,
std::optional<std::string> Count; // MESSAGE_DELETE, MESSAGE_BULK_DELETE, MEMBER_DISCONNECT, MEMBER_MOVE
std::optional<Snowflake> ID; // CHANNEL_OVERWRITE_CREATE, CHANNEL_OVERWRITE_UPDATE, CHANNEL_OVERWRITE_DELETE
std::optional<std::string> Type; // CHANNEL_OVERWRITE_CREATE, CHANNEL_OVERWRITE_UPDATE, CHANNEL_OVERWRITE_DELETE
std::optional<std::string> RoleName; // CHANNEL_OVERWRITE_CREATE, CHANNEL_OVERWRITE_UPDATE, CHANNEL_OVERWRITE_DELETE
friend void from_json(const nlohmann::json &j, AuditLogOptions &m);
};
struct AuditLogEntry {
Snowflake ID;
std::string TargetID; // null
Snowflake UserID;
AuditLogActionType Type;
std::optional<std::string> Reason;
std::optional<std::vector<AuditLogChange>> Changes;
std::optional<AuditLogOptions> Options;
friend void from_json(const nlohmann::json &j, AuditLogEntry &m);
template<typename T>
std::optional<T> GetOldFromKey(const std::string &key) const;
template<typename T>
std::optional<T> GetNewFromKey(const std::string &key) const;
};
struct AuditLogData {
std::vector<AuditLogEntry> Entries;
std::vector<UserData> Users;
std::vector<WebhookData> Webhooks;
// std::vector<IntegrationData> Integrations;
friend void from_json(const nlohmann::json &j, AuditLogData &m);
};
template<typename T>
inline std::optional<T> AuditLogEntry::GetOldFromKey(const std::string &key) const {
if (!Changes.has_value()) return std::nullopt;
for (const auto &change : *Changes)
if (change.Key == key && change.OldValue.has_value())
return change.OldValue->get<T>();
return std::nullopt;
}
template<typename T>
inline std::optional<T> AuditLogEntry::GetNewFromKey(const std::string &key) const {
if (!Changes.has_value()) return std::nullopt;
for (const auto &change : *Changes)
if (change.Key == key && change.NewValue.has_value())
return change.NewValue->get<T>();
return std::nullopt;
}

View File

@@ -553,6 +553,22 @@ void DiscordClient::FetchGuildInvites(Snowflake guild_id, sigc::slot<void(std::v
});
}
void DiscordClient::FetchAuditLog(Snowflake guild_id, sigc::slot<void(AuditLogData)> callback) {
sigc::signal<void, AuditLogData> signal;
signal.connect(callback);
m_http.MakeGET("/guilds/" + std::to_string(guild_id) + "/audit-logs", [this, callback](const http::response &response) {
if (!CheckCode(response)) return;
auto data = nlohmann::json::parse(response.text).get<AuditLogData>();
m_store.BeginTransaction();
for (const auto &user : data.Users)
m_store.SetUser(user.ID, user);
m_store.EndTransaction();
callback(data);
});
}
void DiscordClient::UpdateToken(std::string token) {
if (!IsStarted()) {
m_token = token;

View File

@@ -127,6 +127,8 @@ public:
void FetchInvite(std::string code, sigc::slot<void(std::optional<InviteData>)> callback);
void FetchGuildInvites(Snowflake guild_id, sigc::slot<void(std::vector<InviteData>)> callback);
void FetchAuditLog(Snowflake guild_id, sigc::slot<void(AuditLogData)> callback);
void UpdateToken(std::string token);
void SetUserAgent(std::string agent);

View File

@@ -17,6 +17,7 @@
#include "activity.hpp"
#include "sticker.hpp"
#include "ban.hpp"
#include "auditlog.hpp"
// most stuff below should just be objects that get processed and thrown away immediately

View File

@@ -21,6 +21,10 @@ std::string UserData::GetMention() const {
return "<@" + std::to_string(ID) + ">";
}
std::string UserData::GetEscapedString() const {
return Glib::Markup::escape_text(Username) + "#" + Discriminator;
}
void from_json(const nlohmann::json &j, UserData &m) {
JS_D("id", m.ID);
JS_D("username", m.Username);

View File

@@ -33,4 +33,5 @@ struct UserData {
std::string GetAvatarURL(std::string ext = "png", std::string size = "32") const;
Snowflake GetHoistedRole(Snowflake guild_id, bool with_color = false) const;
std::string GetMention() const;
std::string GetEscapedString() const;
};

13
discord/webhook.cpp Normal file
View File

@@ -0,0 +1,13 @@
#include "webhook.hpp"
void from_json(const nlohmann::json &j, WebhookData &m) {
JS_D("id", m.ID);
JS_D("type", m.Type);
JS_O("guild_id", m.GuildID);
JS_D("channel_id", m.ChannelID);
JS_O("user", m.User);
JS_N("name", m.Name);
JS_N("avatar", m.Avatar);
JS_O("token", m.Token);
JS_N("application_id", m.ApplicationID);
}

24
discord/webhook.hpp Normal file
View File

@@ -0,0 +1,24 @@
#pragma once
#include <optional>
#include "json.hpp"
#include "snowflake.hpp"
#include "user.hpp"
enum class WebhookType {
Incoming = 1,
ChannelFollower = 2,
};
struct WebhookData {
Snowflake ID;
WebhookType Type;
std::optional<Snowflake> GuildID;
Snowflake ChannelID;
std::optional<UserData> User;
std::string Name; // null
std::string Avatar; // null
std::optional<std::string> Token;
Snowflake ApplicationID; // null
friend void from_json(const nlohmann::json &j, WebhookData &m);
};