Merge branch 'master' into channels-list

This commit is contained in:
ouwou
2021-07-12 19:06:00 -04:00
6 changed files with 54 additions and 41 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ on: [push, pull_request]
jobs: jobs:
windows: windows:
name: windows-${{ matrix.buildtype }} name: windows-${{ matrix.buildtype }}
runs-on: windows-2016 runs-on: windows-latest
strategy: strategy:
matrix: matrix:
buildtype: [Debug, RelWithDebInfo, MinSizeRel] buildtype: [Debug, RelWithDebInfo, MinSizeRel]
+1 -2
View File
@@ -432,8 +432,7 @@ void Abaddon::ActionConnect() {
} }
void Abaddon::ActionDisconnect() { void Abaddon::ActionDisconnect() {
if (m_discord.IsStarted()) StopDiscord();
StopDiscord();
} }
void Abaddon::ActionSetToken() { void Abaddon::ActionSetToken() {
+30 -18
View File
@@ -24,6 +24,8 @@ DiscordClient::DiscordClient(bool mem_store)
} }
void DiscordClient::Start() { void DiscordClient::Start() {
if (m_client_started) return;
m_http.SetBase(GetAPIURL()); m_http.SetBase(GetAPIURL());
std::memset(&m_zstream, 0, sizeof(m_zstream)); std::memset(&m_zstream, 0, sizeof(m_zstream));
@@ -32,27 +34,31 @@ void DiscordClient::Start() {
m_last_sequence = -1; m_last_sequence = -1;
m_heartbeat_acked = true; m_heartbeat_acked = true;
m_client_connected = true; m_client_connected = true;
m_client_started = true;
m_websocket.StartConnection(GetGatewayURL()); m_websocket.StartConnection(GetGatewayURL());
} }
void DiscordClient::Stop() { void DiscordClient::Stop() {
if (!m_client_connected) return; if (m_client_started) {
inflateEnd(&m_zstream);
m_compressed_buf.clear();
inflateEnd(&m_zstream); m_heartbeat_waiter.kill();
m_compressed_buf.clear(); if (m_heartbeat_thread.joinable()) m_heartbeat_thread.join();
m_client_connected = false;
m_reconnecting = false;
m_heartbeat_waiter.kill(); m_store.ClearAll();
if (m_heartbeat_thread.joinable()) m_heartbeat_thread.join(); m_guild_to_users.clear();
m_client_connected = false;
m_store.ClearAll(); m_websocket.Stop();
m_guild_to_users.clear(); }
m_websocket.Stop(); m_client_started = false;
} }
bool DiscordClient::IsStarted() const { bool DiscordClient::IsStarted() const {
return m_client_connected; return m_client_started;
} }
bool DiscordClient::IsStoreValid() const { bool DiscordClient::IsStoreValid() const {
@@ -233,19 +239,19 @@ std::optional<RoleData> DiscordClient::GetMemberHighestRole(Snowflake guild_id,
}); });
} }
std::unordered_set<Snowflake> DiscordClient::GetUsersInGuild(Snowflake id) const { std::set<Snowflake> DiscordClient::GetUsersInGuild(Snowflake id) const {
auto it = m_guild_to_users.find(id); auto it = m_guild_to_users.find(id);
if (it != m_guild_to_users.end()) if (it != m_guild_to_users.end())
return it->second; return it->second;
return std::unordered_set<Snowflake>(); return {};
} }
std::unordered_set<Snowflake> DiscordClient::GetChannelsInGuild(Snowflake id) const { std::set<Snowflake> DiscordClient::GetChannelsInGuild(Snowflake id) const {
auto it = m_guild_to_channels.find(id); auto it = m_guild_to_channels.find(id);
if (it != m_guild_to_channels.end()) if (it != m_guild_to_channels.end())
return it->second; return it->second;
return std::unordered_set<Snowflake>(); return {};
} }
bool DiscordClient::HasGuildPermission(Snowflake user_id, Snowflake guild_id, Permission perm) const { bool DiscordClient::HasGuildPermission(Snowflake user_id, Snowflake guild_id, Permission perm) const {
@@ -953,12 +959,12 @@ PresenceStatus DiscordClient::GetUserStatus(Snowflake id) const {
return PresenceStatus::Offline; return PresenceStatus::Offline;
} }
std::unordered_map<Snowflake, RelationshipType> DiscordClient::GetRelationships() const { std::map<Snowflake, RelationshipType> DiscordClient::GetRelationships() const {
return m_user_relationships; return m_user_relationships;
} }
std::unordered_set<Snowflake> DiscordClient::GetRelationships(RelationshipType type) const { std::set<Snowflake> DiscordClient::GetRelationships(RelationshipType type) const {
std::unordered_set<Snowflake> ret; std::set<Snowflake> ret;
for (const auto &[id, rtype] : m_user_relationships) for (const auto &[id, rtype] : m_user_relationships)
if (rtype == type) if (rtype == type)
ret.insert(id); ret.insert(id);
@@ -1682,7 +1688,8 @@ void DiscordClient::HandleGatewayInvalidSession(const GatewayMessage &msg) {
m_websocket.Stop(1000); m_websocket.Stop(1000);
m_websocket.StartConnection(GetGatewayURL()); if (m_client_started)
Glib::signal_timeout().connect_once([this] { if (m_client_started) m_websocket.StartConnection(GetGatewayURL()); }, 1000);
} }
bool IsCompleteMessageObject(const nlohmann::json &j) { bool IsCompleteMessageObject(const nlohmann::json &j) {
@@ -1886,6 +1893,11 @@ void DiscordClient::HandleSocketClose(uint16_t code) {
m_store.ClearAll(); m_store.ClearAll();
m_guild_to_users.clear(); m_guild_to_users.clear();
if (m_client_started && !m_reconnecting && close_code == GatewayCloseCode::Abnormal) {
Glib::signal_timeout().connect_once([this] { if (m_client_started) HandleGatewayReconnect(GatewayMessage()); }, 1000);
m_reconnecting = true;
}
m_signal_disconnected.emit(m_reconnecting, close_code); m_signal_disconnected.emit(m_reconnecting, close_code);
}; };
m_generic_mutex.lock(); m_generic_mutex.lock();
+12 -12
View File
@@ -6,9 +6,8 @@
#include <sigc++/sigc++.h> #include <sigc++/sigc++.h>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <thread> #include <thread>
#include <unordered_map> #include <map>
#include <set> #include <set>
#include <unordered_set>
#include <mutex> #include <mutex>
#include <zlib.h> #include <zlib.h>
#include <glibmm.h> #include <glibmm.h>
@@ -85,8 +84,8 @@ public:
std::optional<BanData> GetBan(Snowflake guild_id, Snowflake user_id) const; std::optional<BanData> GetBan(Snowflake guild_id, Snowflake user_id) const;
Snowflake GetMemberHoistedRole(Snowflake guild_id, Snowflake user_id, bool with_color = false) const; Snowflake GetMemberHoistedRole(Snowflake guild_id, Snowflake user_id, bool with_color = false) const;
std::optional<RoleData> GetMemberHighestRole(Snowflake guild_id, Snowflake user_id) const; std::optional<RoleData> GetMemberHighestRole(Snowflake guild_id, Snowflake user_id) const;
std::unordered_set<Snowflake> GetUsersInGuild(Snowflake id) const; std::set<Snowflake> GetUsersInGuild(Snowflake id) const;
std::unordered_set<Snowflake> GetChannelsInGuild(Snowflake id) const; std::set<Snowflake> GetChannelsInGuild(Snowflake id) const;
bool HasGuildPermission(Snowflake user_id, Snowflake guild_id, Permission perm) const; bool HasGuildPermission(Snowflake user_id, Snowflake guild_id, Permission perm) const;
@@ -184,8 +183,8 @@ public:
PresenceStatus GetUserStatus(Snowflake id) const; PresenceStatus GetUserStatus(Snowflake id) const;
std::unordered_map<Snowflake, RelationshipType> GetRelationships() const; std::map<Snowflake, RelationshipType> GetRelationships() const;
std::unordered_set<Snowflake> GetRelationships(RelationshipType type) const; std::set<Snowflake> GetRelationships(RelationshipType type) const;
std::optional<RelationshipType> GetRelationship(Snowflake id) const; std::optional<RelationshipType> GetRelationship(Snowflake id) const;
private: private:
@@ -255,14 +254,14 @@ private:
std::string m_token; std::string m_token;
void AddUserToGuild(Snowflake user_id, Snowflake guild_id); void AddUserToGuild(Snowflake user_id, Snowflake guild_id);
std::unordered_map<Snowflake, std::unordered_set<Snowflake>> m_guild_to_users; std::map<Snowflake, std::set<Snowflake>> m_guild_to_users;
std::unordered_map<Snowflake, std::unordered_set<Snowflake>> m_guild_to_channels; std::map<Snowflake, std::set<Snowflake>> m_guild_to_channels;
std::unordered_map<Snowflake, GuildApplicationData> m_guild_join_requests; std::map<Snowflake, GuildApplicationData> m_guild_join_requests;
std::unordered_map<Snowflake, PresenceStatus> m_user_to_status; std::map<Snowflake, PresenceStatus> m_user_to_status;
std::unordered_map<Snowflake, RelationshipType> m_user_relationships; std::map<Snowflake, RelationshipType> m_user_relationships;
UserData m_user_data; UserData m_user_data;
UserSettings m_user_settings; UserSettings m_user_settings;
@@ -272,8 +271,9 @@ private:
Websocket m_websocket; Websocket m_websocket;
std::atomic<bool> m_client_connected = false; std::atomic<bool> m_client_connected = false;
std::atomic<bool> m_ready_received = false; std::atomic<bool> m_ready_received = false;
bool m_client_started = false;
std::unordered_map<std::string, GatewayEvent> m_event_map; std::map<std::string, GatewayEvent> m_event_map;
void LoadEventMap(); void LoadEventMap();
std::thread m_heartbeat_thread; std::thread m_heartbeat_thread;
+9 -7
View File
@@ -148,13 +148,6 @@ MainWindow::MainWindow()
void MainWindow::UpdateComponents() { void MainWindow::UpdateComponents() {
bool discord_active = Abaddon::Get().IsDiscordActive(); bool discord_active = Abaddon::Get().IsDiscordActive();
std::string token = Abaddon::Get().GetDiscordToken();
m_menu_discord_connect.set_sensitive(token.size() > 0 && !discord_active);
m_menu_discord_disconnect.set_sensitive(discord_active);
m_menu_discord_join_guild.set_sensitive(discord_active);
m_menu_discord_set_token.set_sensitive(!discord_active);
m_menu_discord_set_status.set_sensitive(discord_active);
if (!discord_active) { if (!discord_active) {
m_chat.Clear(); m_chat.Clear();
m_members.Clear(); m_members.Clear();
@@ -258,6 +251,15 @@ void MainWindow::OnDiscordSubmenuPopup(const Gdk::Rectangle *flipped_rect, const
m_menu_discord_add_recipient.set_visible(false); m_menu_discord_add_recipient.set_visible(false);
if (channel.has_value() && channel->GetDMRecipients().size() + 1 < 10) if (channel.has_value() && channel->GetDMRecipients().size() + 1 < 10)
m_menu_discord_add_recipient.set_visible(channel->Type == ChannelType::GROUP_DM); m_menu_discord_add_recipient.set_visible(channel->Type == ChannelType::GROUP_DM);
const bool discord_active = Abaddon::Get().GetDiscordClient().IsStarted();
std::string token = Abaddon::Get().GetDiscordToken();
m_menu_discord_connect.set_sensitive(token.size() > 0 && !discord_active);
m_menu_discord_disconnect.set_sensitive(discord_active);
m_menu_discord_join_guild.set_sensitive(discord_active);
m_menu_discord_set_token.set_sensitive(!discord_active);
m_menu_discord_set_status.set_sensitive(discord_active);
} }
void MainWindow::OnViewSubmenuPopup(const Gdk::Rectangle *flipped_rect, const Gdk::Rectangle *final_rect, bool flipped_x, bool flipped_y) { void MainWindow::OnViewSubmenuPopup(const Gdk::Rectangle *flipped_rect, const Gdk::Rectangle *final_rect, bool flipped_x, bool flipped_y) {