Skip to content

Commit

Permalink
request/speak/leave stage and stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
ouwou committed Jun 30, 2024
1 parent cf5c949 commit 7f709ce
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 10 deletions.
28 changes: 28 additions & 0 deletions src/discord/discord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,10 @@ bool DiscordClient::CanManageMember(Snowflake guild_id, Snowflake actor, Snowfla
return actor_highest->Position > target_highest->Position;
}

bool DiscordClient::IsStageModerator(Snowflake user_id, Snowflake channel_id) const {
return HasChannelPermission(user_id, channel_id, Permission::MANAGE_CHANNELS | Permission::MOVE_MEMBERS | Permission::MUTE_MEMBERS);
}

void DiscordClient::ChatMessageCallback(const std::string &nonce, const http::response_type &response, const sigc::slot<void(DiscordError)> &callback) {
if (!CheckCode(response)) {
if (response.status_code == http::TooManyRequests) {
Expand Down Expand Up @@ -1306,6 +1310,11 @@ bool DiscordClient::HasUserRequestedToSpeak(Snowflake user_id) const {
return state.has_value() && state->second.RequestToSpeakTimestamp.has_value() && util::FlagSet(state->second.Flags, VoiceStateFlags::Suppressed);
}

bool DiscordClient::IsUserInvitedToSpeak(Snowflake user_id) const {
const auto state = GetVoiceState(user_id);
return state.has_value() && state->second.RequestToSpeakTimestamp.has_value() && !util::FlagSet(state->second.Flags, VoiceStateFlags::Suppressed);
}

void DiscordClient::RequestToSpeak(Snowflake channel_id, bool want, const sigc::slot<void(DiscordError code)> &callback) {
if (want && !HasSelfChannelPermission(channel_id, Permission::REQUEST_TO_SPEAK)) return;
const auto channel = GetChannel(channel_id);
Expand All @@ -1327,6 +1336,25 @@ void DiscordClient::RequestToSpeak(Snowflake channel_id, bool want, const sigc::
});
}

void DiscordClient::SetStageSpeaking(Snowflake channel_id, bool want, const sigc::slot<void(DiscordError code)> &callback) {
const auto channel = GetChannel(channel_id);
if (!channel.has_value() || !channel->GuildID.has_value()) return;

ModifyCurrentUserVoiceStateObject d;
d.ChannelID = channel_id;
d.Suppress = !want;
if (want) {
d.RequestToSpeakTimestamp = "";
}
m_http.MakePATCH("/guilds/" + std::to_string(*channel->GuildID) + "/voice-states/@me", nlohmann::json(d).dump(), [callback](const http::response_type &response) {
if (CheckCode(response, 204)) {
callback(DiscordError::NONE);
} else {
callback(GetCodeFromResponse(response));
}
});
}

DiscordVoiceClient &DiscordClient::GetVoiceClient() {
return m_voice;
}
Expand Down
3 changes: 3 additions & 0 deletions src/discord/discord.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class DiscordClient {
Permission ComputePermissions(Snowflake member_id, Snowflake guild_id) const;
Permission ComputeOverwrites(Permission base, Snowflake member_id, Snowflake channel_id) const;
bool CanManageMember(Snowflake guild_id, Snowflake actor, Snowflake target) const; // kick, ban, edit nickname (cant think of a better name)
bool IsStageModerator(Snowflake user_id, Snowflake channel_id) const;

void ChatMessageCallback(const std::string &nonce, const http::response_type &response, const sigc::slot<void(DiscordError code)> &callback);
void SendChatMessageNoAttachments(const ChatSubmitParams &params, const sigc::slot<void(DiscordError code)> &callback);
Expand Down Expand Up @@ -204,8 +205,10 @@ class DiscordClient {
[[nodiscard]] std::optional<uint32_t> GetSSRCOfUser(Snowflake id) const;
[[nodiscard]] bool IsUserSpeaker(Snowflake user_id) const;
[[nodiscard]] bool HasUserRequestedToSpeak(Snowflake user_id) const;
[[nodiscard]] bool IsUserInvitedToSpeak(Snowflake user_id) const;

void RequestToSpeak(Snowflake channel_id, bool want, const sigc::slot<void(DiscordError code)> &callback);
void SetStageSpeaking(Snowflake channel_id, bool want, const sigc::slot<void(DiscordError code)> &callback);

DiscordVoiceClient &GetVoiceClient();

Expand Down
48 changes: 41 additions & 7 deletions src/windows/voice/voicewindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ VoiceWindow::VoiceWindow(Snowflake channel_id)
, m_deafen("Deafen")
, m_noise_suppression("Suppress Noise")
, m_mix_mono("Mix Mono")
, m_request_to_speak("Request to Speak")
, m_stage_command("Request to Speak")
, m_disconnect("Disconnect")
, m_channel_id(channel_id)
, m_menu_view("View")
Expand Down Expand Up @@ -213,10 +213,21 @@ VoiceWindow::VoiceWindow(Snowflake channel_id)
},
*this));

m_request_to_speak.signal_clicked().connect([this]() {
m_stage_command.signal_clicked().connect([this]() {
auto &discord = Abaddon::Get().GetDiscordClient();
const bool requested = discord.HasUserRequestedToSpeak(discord.GetUserData().ID);
Abaddon::Get().GetDiscordClient().RequestToSpeak(m_channel_id, !requested, NOOP_CALLBACK);
const auto user_id = discord.GetUserData().ID;
const bool is_moderator = discord.IsStageModerator(user_id, m_channel_id);
const bool is_speaker = discord.IsUserSpeaker(user_id);
const bool is_invited_to_speak = discord.IsUserInvitedToSpeak(user_id);

if (is_speaker) {
discord.SetStageSpeaking(m_channel_id, false, NOOP_CALLBACK);
} else if (is_moderator || is_invited_to_speak) {
discord.SetStageSpeaking(m_channel_id, true, NOOP_CALLBACK);
} else {
const bool requested = discord.HasUserRequestedToSpeak(user_id);
discord.RequestToSpeak(m_channel_id, !requested, NOOP_CALLBACK);
}
});

m_TMP_speakers_label.set_markup("<b>Speakers</b>");
Expand All @@ -231,7 +242,7 @@ VoiceWindow::VoiceWindow(Snowflake channel_id)
m_controls.add(m_noise_suppression);
m_controls.add(m_mix_mono);
m_buttons.set_halign(Gtk::ALIGN_CENTER);
m_buttons.pack_start(m_request_to_speak, false, true);
m_buttons.pack_start(m_stage_command, false, true);
m_buttons.pack_start(m_disconnect, false, true);
m_main.pack_start(m_menu_bar, false, true);
m_main.pack_start(m_TMP_stagelabel, false, true);
Expand All @@ -246,6 +257,8 @@ VoiceWindow::VoiceWindow(Snowflake channel_id)
show_all_children();

Glib::signal_timeout().connect(sigc::mem_fun(*this, &VoiceWindow::UpdateVoiceMeters), 40);

UpdateStageCommand();
}

void VoiceWindow::SetUsers(const std::unordered_set<Snowflake> &user_ids) {
Expand Down Expand Up @@ -335,6 +348,26 @@ void VoiceWindow::UpdateVADParamValue() {
}
}

void VoiceWindow::UpdateStageCommand() {
auto &discord = Abaddon::Get().GetDiscordClient();
const auto user_id = discord.GetUserData().ID;

m_has_requested_to_speak = discord.HasUserRequestedToSpeak(user_id);
const bool is_moderator = discord.IsStageModerator(user_id, m_channel_id);
const bool is_speaker = discord.IsUserSpeaker(user_id);
const bool is_invited_to_speak = discord.IsUserInvitedToSpeak(user_id);

if (is_speaker) {
m_stage_command.set_label("Leave the Stage");
} else if (is_moderator || is_invited_to_speak) {
m_stage_command.set_label("Speak on Stage");
} else if (m_has_requested_to_speak) {
m_stage_command.set_label("Cancel Request");
} else {
m_stage_command.set_label("Request to Speak");
}
}

void VoiceWindow::OnUserConnect(Snowflake user_id, Snowflake to_channel_id) {
if (m_channel_id == to_channel_id) {
if (auto it = m_rows.find(user_id); it == m_rows.end()) {
Expand Down Expand Up @@ -363,8 +396,9 @@ void VoiceWindow::OnSpeakerStateChanged(Snowflake channel_id, Snowflake user_id,

void VoiceWindow::OnVoiceStateUpdate(Snowflake user_id, Snowflake channel_id, VoiceStateFlags flags) {
auto &discord = Abaddon::Get().GetDiscordClient();
m_has_requested_to_speak = discord.HasUserRequestedToSpeak(discord.GetUserData().ID);
m_request_to_speak.set_label(m_has_requested_to_speak ? "Cancel Request" : "Request to Speak");
if (user_id != discord.GetUserData().ID) return;

UpdateStageCommand();
}

VoiceWindow::type_signal_mute VoiceWindow::signal_mute() {
Expand Down
5 changes: 2 additions & 3 deletions src/windows/voice/voicewindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ class VoiceWindow : public Gtk::Window {
void OnDeafenChanged();

void TryDeleteRow(Snowflake id);

bool UpdateVoiceMeters();

void UpdateVADParamValue();
void UpdateStageCommand();

Gtk::Box m_main;
Gtk::Box m_controls;
Expand All @@ -65,7 +64,7 @@ class VoiceWindow : public Gtk::Window {

Gtk::HBox m_buttons;
Gtk::Button m_disconnect;
Gtk::Button m_request_to_speak;
Gtk::Button m_stage_command;

bool m_has_requested_to_speak = false;

Expand Down

0 comments on commit 7f709ce

Please sign in to comment.