populate channel list from READY message and other shit

This commit is contained in:
ouwou 2020-08-19 01:13:36 -04:00
parent 3c3fe3b9f7
commit 69404a97cd
9 changed files with 746 additions and 14 deletions

View File

@ -64,6 +64,15 @@ std::string Abaddon::GetDiscordToken() const {
return m_discord_token;
}
const DiscordClient &Abaddon::GetDiscordClient() const {
std::scoped_lock<std::mutex> guard(m_mutex);
return m_discord;
}
void Abaddon::DiscordNotifyReady() {
m_main_window->UpdateChannelListing();
}
void Abaddon::ActionConnect() {
if (!m_discord.IsStarted())
StartDiscord();

View File

@ -1,5 +1,6 @@
#include <gtkmm.h>
#include <memory>
#include <mutex>
#include <string>
#include "discord/discord.hpp"
#include "windows/mainwindow.hpp"
@ -23,11 +24,14 @@ public:
std::string GetDiscordToken() const;
bool IsDiscordActive() const;
const DiscordClient &GetDiscordClient() const;
void DiscordNotifyReady();
private:
std::string m_discord_token;
mutable std::mutex m_mutex;
Glib::RefPtr<Gtk::Application> m_gtk_app;
DiscordClient m_discord;
SettingsManager m_settings;
std::unique_ptr<MainWindow> m_main_window; // wah wah cant create a gtkstylecontext fuck you
};
};

View File

@ -1,12 +1,255 @@
#include "channels.hpp"
#include <algorithm>
#include <map>
#include <unordered_map>
#include "../abaddon.hpp"
ChannelList::ChannelList() {
m_main = Gtk::manage(new Gtk::ScrolledWindow);
m_list = Gtk::manage(new Gtk::ListBox);
m_list->set_activate_on_single_click(true);
m_list->signal_row_activated().connect(sigc::mem_fun(*this, &ChannelList::on_row_activated));
m_main->add(*m_list);
m_main->show_all();
m_update_dispatcher.connect(sigc::mem_fun(*this, &ChannelList::SetListingFromGuildsInternal));
}
Gtk::Widget* ChannelList::GetRoot() const {
void ChannelList::SetAbaddon(Abaddon *ptr) {
m_abaddon = ptr;
}
Gtk::Widget *ChannelList::GetRoot() const {
return m_main;
}
void ChannelList::SetListingFromGuilds(const DiscordClient::Guilds_t &guilds) {
std::scoped_lock<std::mutex> guard(m_update_mutex);
m_update_queue.push(guilds);
m_update_dispatcher.emit();
}
void ChannelList::on_row_activated(Gtk::ListBoxRow *row) {
auto &info = m_infos[row];
bool new_collapsed = !info.IsUserCollapsed;
info.IsUserCollapsed = new_collapsed;
if (info.CatArrow != nullptr)
info.CatArrow->set(new_collapsed ? Gtk::ARROW_RIGHT : Gtk::ARROW_DOWN, Gtk::SHADOW_NONE);
if (new_collapsed) {
std::function<void(Gtk::ListBoxRow *)> collapse_children = [&](Gtk::ListBoxRow *row) {
for (auto child : m_infos[row].Children) {
auto &row_info = m_infos[child];
row_info.IsHidden = true;
child->hide();
collapse_children(child);
}
};
collapse_children(row);
} else {
std::function<void(Gtk::ListBoxRow *)> restore_children = [&](Gtk::ListBoxRow *row) {
auto &row_info = m_infos[row];
row->show();
row_info.IsHidden = false;
if (!row_info.IsUserCollapsed)
for (auto row : row_info.Children)
restore_children(row);
};
restore_children(row);
}
}
void ChannelList::SetListingFromGuildsInternal() {
DiscordClient::Guilds_t *guilds;
{
std::scoped_lock<std::mutex> guard(m_update_mutex);
guilds = &m_update_queue.front();
}
auto children = m_list->get_children();
auto it = children.begin();
while (it != children.end()) {
delete *it;
it++;
}
auto &settings = m_abaddon->GetDiscordClient().GetUserSettings();
std::vector<std::pair<Snowflake, GuildData>> sorted_guilds;
if (settings.GuildPositions.size()) {
for (const auto &id : settings.GuildPositions) {
auto &guild = (*guilds)[id];
sorted_guilds.push_back(std::make_pair(id, guild));
}
} else { // default sort is alphabetic
for (auto &it : *guilds)
sorted_guilds.push_back(it);
std::sort(sorted_guilds.begin(), sorted_guilds.end(), [&](auto &a, auto &b) -> bool {
std::string &s1 = a.second.Name;
std::string &s2 = b.second.Name;
if (s1.empty() || s2.empty())
return s1 < s2;
bool ac[] = {
!isalnum(s1[0]),
!isalnum(s2[0]),
isdigit(s1[0]),
isdigit(s2[0]),
isalpha(s1[0]),
isalpha(s2[0]),
};
if ((ac[0] && ac[1]) || (ac[2] && ac[3]) || (ac[4] && ac[5]))
return s1 < s2;
return ac[0] || ac[5];
});
}
// map each category to its channels
std::unordered_map<Snowflake, std::vector<const ChannelData *>> cat_to_channels;
std::unordered_map<Snowflake, std::vector<const ChannelData *>> orphan_channels;
for (const auto &[gid, guild] : *guilds) {
for (const auto &chan : guild.Channels) {
switch (chan.Type) {
case ChannelType::GUILD_TEXT: {
if (chan.ParentID.IsValid()) {
auto it = cat_to_channels.find(chan.ParentID);
if (it == cat_to_channels.end())
cat_to_channels[chan.ParentID] = std::vector<const ChannelData *>();
cat_to_channels[chan.ParentID].push_back(&chan);
} else {
auto it = orphan_channels.find(gid);
if (it == orphan_channels.end())
orphan_channels[gid] = std::vector<const ChannelData *>();
orphan_channels[gid].push_back(&chan);
}
} break;
default:
break;
}
}
}
auto add_channel = [&](const Snowflake &id, const ChannelData &channel) -> Gtk::ListBoxRow * {
auto *channel_row = Gtk::manage(new Gtk::ListBoxRow);
auto *channel_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
auto *channel_label = Gtk::manage(new Gtk::Label);
channel_label->set_text("#" + channel.Name);
channel_box->set_halign(Gtk::ALIGN_START);
channel_box->pack_start(*channel_label);
channel_row->add(*channel_box);
channel_row->show_all_children();
m_list->add(*channel_row);
ListItemInfo info;
info.ID = id;
info.IsUserCollapsed = false;
info.IsHidden = false;
m_infos[channel_row] = std::move(info);
return channel_row;
};
auto add_category = [&](const Snowflake &id, const ChannelData &channel) -> Gtk::ListBoxRow * {
auto *category_row = Gtk::manage(new Gtk::ListBoxRow);
auto *category_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
auto *category_label = Gtk::manage(new Gtk::Label);
auto *category_arrow = Gtk::manage(new Gtk::Arrow(Gtk::ARROW_DOWN, Gtk::SHADOW_NONE));
category_label->set_text(channel.Name);
category_box->set_halign(Gtk::ALIGN_START);
category_box->pack_start(*category_arrow);
category_box->pack_start(*category_label);
category_row->add(*category_box);
category_row->show_all_children();
m_list->add(*category_row);
ListItemInfo info;
info.ID = id;
info.IsUserCollapsed = false;
info.IsHidden = true;
info.CatArrow = category_arrow;
if (cat_to_channels.find(id) != cat_to_channels.end()) {
std::map<int, const ChannelData *> sorted_channels;
for (const auto channel : cat_to_channels[id]) {
assert(channel->Position != -1);
sorted_channels[channel->Position] = channel;
}
for (const auto &[pos, channel] : sorted_channels) {
auto channel_row = add_channel(channel->ID, *channel);
info.Children.insert(channel_row);
}
}
m_infos[category_row] = std::move(info);
return category_row;
};
auto add_guild = [&](const Snowflake &id, const GuildData &guild) {
auto *guild_row = Gtk::manage(new Gtk::ListBoxRow);
auto *guild_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
auto *guild_label = Gtk::manage(new Gtk::Label);
guild_label->set_markup("<b>" + Glib::Markup::escape_text(guild.Name) + "</b>");
guild_box->set_halign(Gtk::ALIGN_START);
guild_box->pack_start(*guild_label);
guild_row->add(*guild_box);
guild_row->show_all();
m_list->add(*guild_row);
ListItemInfo info;
info.ID = id;
info.IsUserCollapsed = true;
info.IsHidden = false;
if (orphan_channels.find(id) != orphan_channels.end()) {
std::map<int, const ChannelData *> sorted_orphans;
for (const auto channel : orphan_channels[id]) {
assert(channel->Position != -1); // can this trigger?
sorted_orphans[channel->Position] = channel;
}
for (const auto &[pos, channel] : sorted_orphans) {
auto channel_row = add_channel(channel->ID, *channel);
m_infos[channel_row].IsHidden = true;
info.Children.insert(channel_row);
}
}
std::map<int, const ChannelData *> sorted_categories;
for (const auto &channel : guild.Channels) {
if (channel.Type == ChannelType::GUILD_CATEGORY) {
assert(channel.Position != -1);
sorted_categories[channel.Position] = &channel;
}
}
for (const auto &[pos, channel] : sorted_categories) {
if (channel->Type == ChannelType::GUILD_CATEGORY) {
auto category_row = add_category(channel->ID, *channel);
info.Children.insert(category_row);
}
}
m_infos[guild_row] = std::move(info);
};
for (const auto &[id, guild] : sorted_guilds) {
add_guild(id, guild);
}
{
std::scoped_lock<std::mutex> guard(m_update_mutex);
m_update_queue.pop();
}
}

View File

@ -1,12 +1,40 @@
#pragma once
#include <gtkmm.h>
#include <string>
#include <queue>
#include <mutex>
#include <unordered_set>
#include "../discord/discord.hpp"
class Abaddon;
class ChannelList {
public:
ChannelList();
Gtk::Widget *GetRoot() const;
void SetListingFromGuilds(const DiscordClient::Guilds_t &guilds);
void SetAbaddon(Abaddon *ptr);
protected:
Gtk::ListBox *m_list;
Gtk::ScrolledWindow *m_main;
struct ListItemInfo {
Snowflake ID;
std::unordered_set<Gtk::ListBoxRow *> Children;
bool IsUserCollapsed;
bool IsHidden;
// for categories
Gtk::Arrow *CatArrow = nullptr;
};
std::unordered_map<Gtk::ListBoxRow *, ListItemInfo> m_infos;
void on_row_activated(Gtk::ListBoxRow *row);
Glib::Dispatcher m_update_dispatcher;
mutable std::mutex m_update_mutex;
std::queue<DiscordClient::Guilds_t> m_update_queue;
void SetListingFromGuildsInternal();
Abaddon *m_abaddon = nullptr;
};

View File

@ -20,6 +20,7 @@ void DiscordClient::Start() {
}
void DiscordClient::Stop() {
std::scoped_lock<std::mutex> guard(m_mutex);
if (!m_client_connected) return;
m_heartbeat_waiter.kill();
@ -32,6 +33,17 @@ bool DiscordClient::IsStarted() const {
return m_client_connected;
}
const DiscordClient::Guilds_t &DiscordClient::GetGuilds() const {
std::scoped_lock<std::mutex> guard(m_mutex);
return m_guilds;
}
const UserSettingsData &DiscordClient::GetUserSettings() const {
std::scoped_lock<std::mutex> guard(m_mutex);
assert(m_ready_received);
return m_user_settings;
}
void DiscordClient::HandleGatewayMessage(nlohmann::json j) {
GatewayMessage m;
try {
@ -70,7 +82,19 @@ void DiscordClient::HandleGatewayMessage(nlohmann::json j) {
}
void DiscordClient::HandleGatewayReady(const GatewayMessage &msg) {
m_ready_received = true;
ReadyEventData data = msg.Data;
for (const auto &g : data.Guilds) {
StoreGuild(g.ID, g);
}
m_abaddon->DiscordNotifyReady();
m_user_settings = data.UserSettings;
return;
}
void DiscordClient::StoreGuild(Snowflake id, const GuildData &g) {
assert(id.IsValid() && id == g.ID);
m_guilds[id] = g;
}
void DiscordClient::HeartbeatThread() {
@ -106,16 +130,169 @@ void DiscordClient::LoadEventMap() {
m_event_map["READY"] = GatewayEvent::READY;
}
#define JS_D(k, t) \
do { \
j.at(k).get_to(t); \
} while (0)
#define JS_O(k, t) \
do { \
if (j.contains(k)) j.at(k).get_to(t); \
} while (0)
#define JS_N(k, t) \
do { \
if (!j.at(k).is_null()) j.at(k).get_to(t); \
} while (0)
#define JS_ON(k, t) \
do { \
if (j.contains(k) && !j.at(k).is_null()) \
j.at(k).get_to(t); \
} while (0)
void from_json(const nlohmann::json &j, GatewayMessage &m) {
j.at("op").get_to(m.Opcode);
JS_D("op", m.Opcode);
m.Data = j.at("d");
if (j.contains("t") && !j.at("t").is_null())
j.at("t").get_to(m.Type);
JS_ON("t", m.Type);
}
void from_json(const nlohmann::json &j, HelloMessageData &m) {
j.at("heartbeat_interval").get_to(m.HeartbeatInterval);
JS_D("heartbeat_interval", m.HeartbeatInterval);
}
void from_json(const nlohmann::json &j, UserData &m) {
JS_D("id", m.ID);
JS_D("username", m.Username);
JS_D("discriminator", m.Discriminator);
JS_N("avatar", m.Avatar);
JS_O("bot", m.IsBot);
JS_O("system", m.IsSystem);
JS_O("mfa_enabled", m.IsMFAEnabled);
JS_O("locale", m.Locale);
JS_O("verified", m.IsVerified);
JS_O("email", m.Email);
JS_O("flags", m.Flags);
JS_O("premium_type", m.PremiumType);
JS_O("public_flags", m.PublicFlags);
JS_O("desktop", m.IsDesktop);
JS_O("mobile", m.IsMobile);
JS_ON("nsfw_allowed", m.IsNSFWAllowed);
JS_ON("phone", m.Phone);
}
void from_json(const nlohmann::json &j, GuildData &m) {
JS_D("id", m.ID);
JS_D("name", m.Name);
JS_N("icon", m.Icon);
JS_N("splash", m.Splash);
JS_N("discovery_splash", m.DiscoverySplash);
JS_O("owner", m.IsOwner);
JS_D("owner_id", m.OwnerID);
JS_O("permissions", m.Permissions);
JS_O("permissions_new", m.PermissionsNew);
JS_D("region", m.VoiceRegion);
JS_N("afk_channel_id", m.AFKChannelID);
JS_D("afk_timeout", m.AFKTimeout);
JS_O("embed_enabled", m.IsEmbedEnabled);
JS_ON("embed_channel_id", m.EmbedChannelID);
JS_D("verification_level", m.VerificationLevel);
JS_D("default_message_notifications", m.DefaultMessageNotifications);
JS_D("explicit_content_filter", m.ExplicitContentFilter);
// JS_D("roles", m.Roles);
// JS_D("emojis", m.Emojis);
JS_D("features", m.Features);
JS_D("mfa_level", m.MFALevel);
JS_N("application_id", m.ApplicationID);
JS_O("widget_enabled", m.IsWidgetEnabled);
JS_ON("widget_channel_id", m.WidgetChannelID);
JS_N("system_channel_id", m.SystemChannelID);
JS_D("system_channel_flags", m.SystemChannelFlags);
JS_N("rules_channel_id", m.RulesChannelID);
JS_O("joined_at", m.JoinedAt);
JS_O("large", m.IsLarge);
JS_O("unavailable", m.IsUnavailable);
JS_O("member_count", m.MemberCount);
// JS_O("voice_states", m.VoiceStates);
// JS_O("members", m.Members);
JS_O("channels", m.Channels);
// JS_O("presences", m.Presences);
JS_ON("max_presences", m.MaxPresences);
JS_O("max_members", m.MaxMembers);
JS_N("vanity_url_code", m.VanityURL);
JS_N("description", m.Description);
JS_N("banner", m.BannerHash);
JS_D("premium_tier", m.PremiumTier);
JS_O("premium_subscription_count", m.PremiumSubscriptionCount);
JS_D("preferred_locale", m.PreferredLocale);
JS_N("public_updates_channel_id", m.PublicUpdatesChannelID);
JS_O("max_video_channel_users", m.MaxVideoChannelUsers);
JS_O("approximate_member_count", m.ApproximateMemberCount);
JS_O("approximate_presence_count", m.ApproximatePresenceCount);
}
void from_json(const nlohmann::json &j, ChannelData &m) {
JS_D("id", m.ID);
JS_D("type", m.Type);
JS_O("guild_id", m.GuildID);
JS_O("position", m.Position);
// JS_O("permission_overwrites", m.PermissionOverwrites);
JS_O("name", m.Name);
JS_ON("topic", m.Topic);
JS_O("nsfw", m.IsNSFW);
JS_ON("last_message_id", m.LastMessageID);
JS_O("bitrate", m.Bitrate);
JS_O("user_limit", m.UserLimit);
JS_O("rate_limit_per_user", m.RateLimitPerUser);
JS_O("recipients", m.Recipients);
JS_ON("icon", m.Icon);
JS_O("owner_id", m.OwnerID);
JS_O("application_id", m.ApplicationID);
JS_ON("parent_id", m.ParentID);
JS_ON("last_pin_timestamp", m.LastPinTimestamp);
}
void from_json(const nlohmann::json &j, ReadyEventData &m) {
JS_D("v", m.GatewayVersion);
JS_D("user", m.User);
JS_D("guilds", m.Guilds);
JS_D("session_id", m.SessionID);
JS_D("analytics_token", m.AnalyticsToken);
JS_D("friend_suggestion_count", m.FriendSuggestionCount);
JS_D("user_settings", m.UserSettings);
}
void from_json(const nlohmann::json &j, UserSettingsData &m) {
JS_D("timezone_offset", m.TimezoneOffset);
JS_D("theme", m.Theme);
JS_D("stream_notifications_enabled", m.AreStreamNotificationsEnabled);
JS_D("status", m.Status);
JS_D("show_current_game", m.ShouldShowCurrentGame);
// JS_D("restricted_guilds", m.RestrictedGuilds);
JS_D("render_reactions", m.ShouldRenderReactions);
JS_D("render_embeds", m.ShouldRenderEmbeds);
JS_D("native_phone_integration_enabled", m.IsNativePhoneIntegrationEnabled);
JS_D("message_display_compact", m.ShouldMessageDisplayCompact);
JS_D("locale", m.Locale);
JS_D("inline_embed_media", m.ShouldInlineEmbedMedia);
JS_D("inline_attachment_media", m.ShouldInlineAttachmentMedia);
JS_D("guild_positions", m.GuildPositions);
// JS_D("guild_folders", m.GuildFolders);
JS_D("gif_auto_play", m.ShouldGIFAutoplay);
// JS_D("friend_source_flags", m.FriendSourceFlags);
JS_D("explicit_content_filter", m.ExplicitContentFilter);
JS_D("enable_tts_command", m.IsTTSCommandEnabled);
JS_D("disable_games_tab", m.ShouldDisableGamesTab);
JS_D("developer_mode", m.DeveloperMode);
JS_D("detect_platform_accounts", m.ShouldDetectPlatformAccounts);
JS_D("default_guilds_restricted", m.AreDefaultGuildsRestricted);
// JS_N("custom_status", m.CustomStatus);
JS_D("convert_emoticons", m.ShouldConvertEmoticons);
JS_D("contact_sync_enabled", m.IsContactSyncEnabled);
JS_D("animate_emoji", m.ShouldAnimateEmojis);
JS_D("allow_accessibility_detection", m.IsAccessibilityDetectionAllowed);
JS_D("afk_timeout", m.AFKTimeout);
}
void to_json(nlohmann::json &j, const IdentifyProperties &m) {
@ -141,3 +318,34 @@ void to_json(nlohmann::json &j, const HeartbeatMessage &m) {
else
j["d"] = m.Sequence;
}
Snowflake::Snowflake()
: m_num(Invalid) {}
Snowflake::Snowflake(const Snowflake &s)
: m_num(s.m_num) {}
Snowflake::Snowflake(uint64_t n)
: m_num(n) {}
Snowflake::Snowflake(const std::string &str) {
if (str.size())
m_num = std::stoull(str);
else
m_num = Invalid;
};
bool Snowflake::IsValid() const {
return m_num != Invalid;
}
void from_json(const nlohmann::json &j, Snowflake &s) {
std::string tmp;
j.get_to(tmp);
s.m_num = std::stoull(tmp);
}
#undef JS_O
#undef JS_D
#undef JS_N
#undef JS_ON

View File

@ -3,6 +3,41 @@
#include <nlohmann/json.hpp>
#include <thread>
#include <unordered_map>
#include <mutex>
struct Snowflake {
Snowflake();
Snowflake(const Snowflake &s);
Snowflake(uint64_t n);
Snowflake(const std::string &str);
bool IsValid() const;
bool operator==(const Snowflake &s) const noexcept {
return m_num == s.m_num;
}
bool operator<(const Snowflake &s) const noexcept {
return m_num < s.m_num;
}
const static int Invalid = -1;
friend void from_json(const nlohmann::json &j, Snowflake &s);
private:
friend struct std::hash<Snowflake>;
unsigned long long m_num;
};
namespace std {
template<>
struct hash<Snowflake> {
std::size_t operator()(const Snowflake &k) const {
return k.m_num;
}
};
} // namespace std
enum class GatewayOp : int {
Event = 0,
@ -30,9 +65,180 @@ struct HelloMessageData {
friend void from_json(const nlohmann::json &j, HelloMessageData &m);
};
struct ReadyEventData {
std::string AnalyticsToken; // opt
enum class ChannelType : int {
GUILD_TEXT = 0,
DM = 1,
GUILD_VOICE = 2,
GROUP_DM = 3,
GUILD_CATEGORY = 4,
GUILD_NEWS = 5,
GUILD_STORE = 6,
};
struct UserData {
Snowflake ID; //
std::string Username; //
std::string Discriminator; //
std::string Avatar; // null
bool IsBot = false; // opt
bool IsSystem = false; // opt
bool IsMFAEnabled = false; // opt
std::string Locale; // opt
bool IsVerified = false; // opt
std::string Email; // opt, null
int Flags = 0; // opt
int PremiumType = 0; // opt
int PublicFlags = 0; // opt
// undocumented (opt)
bool IsDesktop = false; //
bool IsMobile = false; //
bool IsNSFWAllowed = false; // null
std::string Phone; // null?
friend void from_json(const nlohmann::json &j, UserData &m);
};
struct ChannelData {
Snowflake ID; //
ChannelType Type; //
Snowflake GuildID; // opt
int Position = -1; // opt
// std::vector<PermissionOverwriteData> PermissionOverwrites; // opt
std::string Name; // opt
std::string Topic; // opt, null
bool IsNSFW = false; // opt
Snowflake LastMessageID; // opt, null
int Bitrate = 0; // opt
int UserLimit = 0; // opt
int RateLimitPerUser = 0; // opt
std::vector<UserData> Recipients; // opt
std::string Icon; // opt, null
Snowflake OwnerID; // opt
Snowflake ApplicationID; // opt
Snowflake ParentID; // opt, null
std::string LastPinTimestamp; // opt, can be null even tho docs say otherwise
friend void from_json(const nlohmann::json &j, ChannelData &m);
};
// a bot is apparently only supposed to receive the `id` and `unavailable` as false
// but user tokens seem to get the full objects (minus users)
struct GuildData {
Snowflake ID; //
std::string Name; //
std::string Icon; // null
std::string Splash; // null
std::string DiscoverySplash; // null
bool IsOwner = false; // opt
Snowflake OwnerID; //
int Permissions = 0; // opt
std::string PermissionsNew; // opt
std::string VoiceRegion; // opt
Snowflake AFKChannelID; // null
int AFKTimeout; //
bool IsEmbedEnabled = false; // opt, deprecated
Snowflake EmbedChannelID; // opt, null, deprecated
int VerificationLevel; //
int DefaultMessageNotifications; //
int ExplicitContentFilter; //
// std::vector<RoleData> Roles; //
// std::vector<EmojiData> Emojis; //
std::vector<std::string> Features; //
int MFALevel; //
Snowflake ApplicationID; // null
bool IsWidgetEnabled = false; // opt
Snowflake WidgetChannelID; // opt, null
Snowflake SystemChannelID; // null
int SystemChannelFlags; //
Snowflake RulesChannelID; // null
std::string JoinedAt; // opt*
bool IsLarge = false; // opt*
bool IsUnavailable = false; // opt*
int MemberCount = 0; // opt*
// std::vector<VoiceStateData> VoiceStates; // opt*
// std::vector<MemberData> Members; // opt* - incomplete anyways
std::vector<ChannelData> Channels; // opt*
// std::vector<PresenceUpdateData> Presences; // opt*
int MaxPresences = 0; // opt, null
int MaxMembers = 0; // opt
std::string VanityURL; // null
std::string Description; // null
std::string BannerHash; // null
int PremiumTier; //
int PremiumSubscriptionCount = 0; // opt
std::string PreferredLocale; //
Snowflake PublicUpdatesChannelID; // null
int MaxVideoChannelUsers = 0; // opt
int ApproximateMemberCount = 0; // opt
int ApproximatePresenceCount = 0; // opt
// undocumented
// std::map<std::string, Unknown> GuildHashes;
bool IsLazy = false;
// * - documentation says only sent in GUILD_CREATE, but these can be sent anyways in the READY event
friend void from_json(const nlohmann::json &j, GuildData &m);
};
struct UserSettingsData {
int TimezoneOffset; //
std::string Theme; //
bool AreStreamNotificationsEnabled; //
std::string Status; //
bool ShouldShowCurrentGame; //
// std::vector<Unknown> RestrictedGuilds; //
bool ShouldRenderReactions; //
bool ShouldRenderEmbeds; //
bool IsNativePhoneIntegrationEnabled; //
bool ShouldMessageDisplayCompact; //
std::string Locale; //
bool ShouldInlineEmbedMedia; //
bool ShouldInlineAttachmentMedia; //
std::vector<Snowflake> GuildPositions; //
// std::vector<GuildFolderEntryData> GuildFolders; //
bool ShouldGIFAutoplay; //
// Unknown FriendSourceFlags; //
int ExplicitContentFilter; //
bool IsTTSCommandEnabled; //
bool ShouldDisableGamesTab; //
bool DeveloperMode; //
bool ShouldDetectPlatformAccounts; //
bool AreDefaultGuildsRestricted; //
// Unknown CustomStatus; // null
bool ShouldConvertEmoticons; //
bool IsContactSyncEnabled; //
bool ShouldAnimateEmojis; //
bool IsAccessibilityDetectionAllowed; //
int AFKTimeout;
friend void from_json(const nlohmann::json &j, UserSettingsData &m);
};
struct ReadyEventData {
int GatewayVersion; //
UserData User; //
std::vector<GuildData> Guilds; //
std::string SessionID; //
// std::vector<ChannelData?/PrivateChannelData?> PrivateChannels;
// undocumented
std::string AnalyticsToken; // opt
int FriendSuggestionCount; // opt
UserSettingsData UserSettings; // opt
// std::vector<Unknown> ConnectedAccounts; // opt
// std::map<std::string, Unknown> Consents; // opt
// std::vector<Unknown> Experiments; // opt
// std::vector<Unknown> GuildExperiments; // opt
// std::map<Unknown, Unknown> Notes; // opt
// std::vector<PresenceData> Presences; // opt
// std::vector<ReadStateData> ReadStates; // opt
// std::vector<RelationshipData> Relationships; // opt
// Unknown Tutorial; // opt, null
// std::vector<GuildSettingData> UserGuildSettings; // opt
friend void from_json(const nlohmann::json &j, ReadyEventData &m);
};
struct IdentifyProperties {
@ -92,6 +298,10 @@ public:
void Stop();
bool IsStarted() const;
using Guilds_t = std::unordered_map<Snowflake, GuildData>;
const Guilds_t &GetGuilds() const;
const UserSettingsData &GetUserSettings() const;
private:
void HandleGatewayMessage(nlohmann::json msg);
void HandleGatewayReady(const GatewayMessage &msg);
@ -99,9 +309,16 @@ private:
void SendIdentify();
Abaddon *m_abaddon = nullptr;
mutable std::mutex m_mutex;
void StoreGuild(Snowflake id, const GuildData &g);
Guilds_t m_guilds;
UserSettingsData m_user_settings;
Websocket m_websocket;
bool m_client_connected = false;
std::atomic<bool> m_client_connected = false;
std::atomic<bool> m_ready_received = false;
std::unordered_map<std::string, GatewayEvent> m_event_map;
void LoadEventMap();

View File

@ -1,7 +1,15 @@
#include "settings.hpp"
#include <filesystem>
#include <fstream>
SettingsManager::SettingsManager(std::string filename)
: m_filename(filename) {
if (!std::filesystem::exists(filename)) {
std::fstream fs;
fs.open(filename, std::ios::out);
fs.close();
}
auto rc = m_ini.LoadFile(filename.c_str());
m_ok = rc == SI_OK;
}

View File

@ -1,11 +1,11 @@
#include "mainwindow.hpp"
#include "../abaddon.hpp"
MainWindow::MainWindow() {
MainWindow::MainWindow()
: m_main_box(Gtk::ORIENTATION_VERTICAL)
, m_content_box(Gtk::ORIENTATION_HORIZONTAL) {
set_default_size(800, 600);
m_main_box.set_orientation(Gtk::ORIENTATION_VERTICAL);
m_menu_discord.set_label("Discord");
m_menu_discord.set_submenu(m_menu_discord_sub);
m_menu_discord_connect.set_label("Connect");
@ -31,10 +31,16 @@ MainWindow::MainWindow() {
m_abaddon->ActionSetToken();
});
m_content_box.set_hexpand(true);
m_content_box.set_vexpand(true);
m_main_box.add(m_menu_bar);
m_main_box.add(m_content_box);
auto *channel_list = m_channel_list.GetRoot();
m_main_box.add(*channel_list);
channel_list->set_hexpand(true);
channel_list->set_vexpand(true);
m_content_box.add(*channel_list);
add(m_main_box);
@ -51,6 +57,12 @@ void MainWindow::UpdateMenuStatus() {
m_menu_discord_disconnect.set_sensitive(discord_active);
}
void MainWindow::UpdateChannelListing() {
auto &discord = m_abaddon->GetDiscordClient();
m_channel_list.SetListingFromGuilds(discord.GetGuilds());
}
void MainWindow::SetAbaddon(Abaddon *ptr) {
m_abaddon = ptr;
m_channel_list.SetAbaddon(ptr);
}

View File

@ -7,10 +7,13 @@ class MainWindow : public Gtk::Window {
public:
MainWindow();
void SetAbaddon(Abaddon *ptr);
void UpdateMenuStatus();
void UpdateChannelListing();
protected:
Gtk::Box m_main_box;
Gtk::Box m_content_box;
ChannelList m_channel_list;