add mute/unmute channel menu item

This commit is contained in:
ouwou 2021-12-18 01:58:29 -05:00
parent 1d7529e609
commit f580535d35
7 changed files with 108 additions and 9 deletions

View File

@ -1,8 +1,8 @@
#include "channels.hpp"
#include "abaddon.hpp"
#include "channels.hpp"
#include "imgmanager.hpp"
#include "util.hpp"
#include "statusindicator.hpp"
#include "util.hpp"
#include <algorithm>
#include <map>
#include <unordered_map>
@ -117,7 +117,16 @@ ChannelList::ChannelList()
m_menu_channel_mark_as_read.signal_activate().connect([this] {
Abaddon::Get().GetDiscordClient().MarkChannelAsRead(static_cast<Snowflake>((*m_model->get_iter(m_path_for_menu))[m_columns.m_id]), [](...) {});
});
m_menu_channel_toggle_mute.signal_activate().connect([this] {
const auto id = static_cast<Snowflake>((*m_model->get_iter(m_path_for_menu))[m_columns.m_id]);
auto &discord = Abaddon::Get().GetDiscordClient();
if (discord.IsChannelMuted(id))
discord.UnmuteChannel(id, [](...) {});
else
discord.MuteChannel(id, [](...) {});
});
m_menu_channel.append(m_menu_channel_mark_as_read);
m_menu_channel.append(m_menu_channel_toggle_mute);
m_menu_channel.append(m_menu_channel_copy_id);
m_menu_channel.show_all();
@ -158,6 +167,7 @@ ChannelList::ChannelList()
m_menu_thread.append(m_menu_thread_unarchive);
m_menu_thread.show_all();
m_menu_channel.signal_popped_up().connect(sigc::mem_fun(*this, &ChannelList::OnChannelSubmenuPopup));
m_menu_thread.signal_popped_up().connect(sigc::mem_fun(*this, &ChannelList::OnThreadSubmenuPopup));
auto &discord = Abaddon::Get().GetDiscordClient();
@ -805,6 +815,16 @@ void ChannelList::MoveRow(const Gtk::TreeModel::iterator &iter, const Gtk::TreeM
m_model->erase(iter);
}
void ChannelList::OnChannelSubmenuPopup(const Gdk::Rectangle *flipped_rect, const Gdk::Rectangle *final_rect, bool flipped_x, bool flipped_y) {
const auto iter = m_model->get_iter(m_path_for_menu);
if (!iter) return;
const auto id = static_cast<Snowflake>((*iter)[m_columns.m_id]);
if (Abaddon::Get().GetDiscordClient().IsChannelMuted(id))
m_menu_channel_toggle_mute.set_label("Unmute");
else
m_menu_channel_toggle_mute.set_label("Mute");
}
void ChannelList::OnThreadSubmenuPopup(const Gdk::Rectangle *flipped_rect, const Gdk::Rectangle *final_rect, bool flipped_x, bool flipped_y) {
m_menu_thread_archive.set_visible(false);
m_menu_thread_unarchive.set_visible(false);

View File

@ -115,6 +115,7 @@ protected:
Gtk::Menu m_menu_channel;
Gtk::MenuItem m_menu_channel_copy_id;
Gtk::MenuItem m_menu_channel_mark_as_read;
Gtk::MenuItem m_menu_channel_toggle_mute;
Gtk::Menu m_menu_dm;
Gtk::MenuItem m_menu_dm_copy_id;
@ -126,6 +127,7 @@ protected:
Gtk::MenuItem m_menu_thread_archive;
Gtk::MenuItem m_menu_thread_unarchive;
void OnChannelSubmenuPopup(const Gdk::Rectangle *flipped_rect, const Gdk::Rectangle *final_rect, bool flipped_x, bool flipped_y);
void OnThreadSubmenuPopup(const Gdk::Rectangle *flipped_rect, const Gdk::Rectangle *final_rect, bool flipped_x, bool flipped_y);
bool m_updating_listing = false;

View File

@ -1,7 +1,7 @@
#include "channelscellrenderer.hpp"
#include "abaddon.hpp"
#include <gtkmm.h>
#include "unreadrenderer.hpp"
#include <gtkmm.h>
CellRendererChannels::CellRendererChannels()
: Glib::ObjectBase(typeid(CellRendererChannels))

View File

@ -1,9 +1,11 @@
#include "abaddon.hpp"
#include "discord.hpp"
#include "util.hpp"
#include "abaddon.hpp"
#include <cassert>
#include <cinttypes>
using namespace std::string_literals;
DiscordClient::DiscordClient(bool mem_store)
: m_decompress_buf(InflateChunkSize)
, m_store(mem_store) {
@ -909,6 +911,35 @@ void DiscordClient::MarkGuildAsRead(Snowflake guild_id, sigc::slot<void(DiscordE
});
}
void DiscordClient::MuteChannel(Snowflake channel_id, sigc::slot<void(DiscordError code)> callback) {
const auto channel = GetChannel(channel_id);
if (!channel.has_value()) return;
const auto guild_id_path = channel->GuildID.has_value() ? std::to_string(*channel->GuildID) : "@me"s;
nlohmann::json j;
j["channel_overrides"][std::to_string(channel_id)]["mute_config"] = MuteConfigData { std::nullopt, -1 };
j["channel_overrides"][std::to_string(channel_id)]["muted"] = true;
m_http.MakePATCH("/users/@me/guilds/" + guild_id_path + "/settings", j.dump(), [this, callback](const http::response_type &response) {
if (CheckCode(response))
callback(DiscordError::NONE);
else
callback(GetCodeFromResponse(response));
});
}
void DiscordClient::UnmuteChannel(Snowflake channel_id, sigc::slot<void(DiscordError code)> callback) {
const auto channel = GetChannel(channel_id);
if (!channel.has_value()) return;
const auto guild_id_path = channel->GuildID.has_value() ? std::to_string(*channel->GuildID) : "@me"s;
nlohmann::json j;
j["channel_overrides"][std::to_string(channel_id)]["muted"] = false;
m_http.MakePATCH("/users/@me/guilds/" + guild_id_path + "/settings", j.dump(), [this, callback](const http::response_type &response) {
if (CheckCode(response))
callback(DiscordError::NONE);
else
callback(GetCodeFromResponse(response));
});
}
void DiscordClient::FetchPinned(Snowflake id, sigc::slot<void(std::vector<Message>, DiscordError code)> callback) {
// return from db if we know the pins have already been requested
if (m_channels_pinned_requested.find(id) != m_channels_pinned_requested.end()) {

View File

@ -140,6 +140,8 @@ public:
void UnArchiveThread(Snowflake channel_id, sigc::slot<void(DiscordError code)> callback);
void MarkChannelAsRead(Snowflake channel_id, sigc::slot<void(DiscordError code)> callback);
void MarkGuildAsRead(Snowflake guild_id, sigc::slot<void(DiscordError code)> callback);
void MuteChannel(Snowflake channel_id, sigc::slot<void(DiscordError code)> callback);
void UnmuteChannel(Snowflake channel_id, sigc::slot<void(DiscordError code)> callback);
bool CanModifyRole(Snowflake guild_id, Snowflake role_id) const;
bool CanModifyRole(Snowflake guild_id, Snowflake role_id, Snowflake user_id) const;

View File

@ -143,6 +143,27 @@ void from_json(const nlohmann::json &j, UserGuildSettingsChannelOverride &m) {
JS_D("channel_id", m.ChannelID);
}
void to_json(nlohmann::json &j, const UserGuildSettingsChannelOverride &m) {
j["channel_id"] = m.ChannelID;
j["collapsed"] = m.Collapsed;
j["message_notifications"] = m.MessageNotifications;
j["mute_config"] = m.MuteConfig;
j["muted"] = m.Muted;
}
void from_json(const nlohmann::json &j, MuteConfigData &m) {
JS_ON("end_time", m.EndTime);
JS_D("selected_time_window", m.SelectedTimeWindow);
}
void to_json(nlohmann::json &j, const MuteConfigData &m) {
if (m.EndTime.has_value())
j["end_time"] = *m.EndTime;
else
j["end_time"] = nullptr;
j["selected_time_window"] = m.SelectedTimeWindow;
}
void from_json(const nlohmann::json &j, UserGuildSettingsEntry &m) {
JS_D("version", m.Version);
JS_D("suppress_roles", m.SuppressRoles);
@ -151,13 +172,26 @@ void from_json(const nlohmann::json &j, UserGuildSettingsEntry &m) {
JS_D("mobile_push", m.MobilePush);
JS_D("message_notifications", m.MessageNotifications);
JS_D("hide_muted_channels", m.HideMutedChannels);
JS_D("guild_id", m.GuildID);
JS_N("guild_id", m.GuildID);
JS_D("channel_overrides", m.ChannelOverrides);
}
void to_json(nlohmann::json &j, const UserGuildSettingsEntry &m) {
j["channel_overrides"] = m.ChannelOverrides;
j["guild_id"] = m.GuildID;
j["hide_muted_channels"] = m.HideMutedChannels;
j["message_notifications"] = m.MessageNotifications;
j["mobile_push"] = m.MobilePush;
j["mute_config"] = m.MuteConfig;
j["muted"] = m.Muted;
j["suppress_everyone"] = m.SuppressEveryone;
j["suppress_roles"] = m.SuppressRoles;
j["version"] = m.Version;
}
void from_json(const nlohmann::json &j, UserGuildSettingsData &m) {
JS_D("version", m.Version);
JS_D("partial", m.IsParital);
JS_D("partial", m.IsPartial);
JS_D("entries", m.Entries);
}

View File

@ -244,14 +244,23 @@ struct ReadStateData {
friend void from_json(const nlohmann::json &j, ReadStateData &m);
};
struct MuteConfigData {
std::optional<std::string> EndTime; // nullopt is encoded as null
int SelectedTimeWindow;
friend void from_json(const nlohmann::json &j, MuteConfigData &m);
friend void to_json(nlohmann::json &j, const MuteConfigData &m);
};
struct UserGuildSettingsChannelOverride {
bool Muted;
// MuteConfig
MuteConfigData MuteConfig;
int MessageNotifications;
bool Collapsed;
Snowflake ChannelID;
friend void from_json(const nlohmann::json &j, UserGuildSettingsChannelOverride &m);
friend void to_json(nlohmann::json &j, const UserGuildSettingsChannelOverride &m);
};
struct UserGuildSettingsEntry {
@ -259,7 +268,7 @@ struct UserGuildSettingsEntry {
bool SuppressRoles;
bool SuppressEveryone;
bool Muted;
// MuteConfig
MuteConfigData MuteConfig;
bool MobilePush;
int MessageNotifications;
bool HideMutedChannels;
@ -267,11 +276,12 @@ struct UserGuildSettingsEntry {
std::vector<UserGuildSettingsChannelOverride> ChannelOverrides;
friend void from_json(const nlohmann::json &j, UserGuildSettingsEntry &m);
friend void to_json(nlohmann::json &j, const UserGuildSettingsEntry &m);
};
struct UserGuildSettingsData {
int Version;
bool IsParital;
bool IsPartial;
std::vector<UserGuildSettingsEntry> Entries;
friend void from_json(const nlohmann::json &j, UserGuildSettingsData &m);