Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent channelPointRewardAdded invocation while connecting #4943

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
- Bugfix: Fixed headers of tables in the settings switching to bold text when selected. (#4913)
- Bugfix: Fixed tooltips appearing too large and/or away from the cursor. (#4920)
- Bugfix: Fixed a crash when clicking `More messages below` button in a usercard and closing it quickly. (#4933)
- Bugfix: Fixed occasional crash for channel point redemptions with text input. (#4942)
- Bugfix: Fixed first redemption of a channel points reward not appearing in rare cases. (#4943)
- Dev: Change clang-format from v14 to v16. (#4929)
- Dev: Fixed UTF16 encoding of `modes` file for the installer. (#4791)
- Dev: Temporarily disable High DPI scaling on Qt6 builds on Windows. (#4767)
Expand Down
55 changes: 34 additions & 21 deletions src/providers/twitch/IrcMessageHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1251,28 +1251,41 @@ void IrcMessageHandler::addMessage(Communi::IrcMessage *message,
if (const auto it = tags.find("custom-reward-id"); it != tags.end())
{
const auto rewardId = it.value().toString();
if (!channel->isChannelPointRewardKnown(rewardId))
if (!rewardId.isEmpty() &&
!channel->isChannelPointRewardKnown(rewardId))
{
// Need to wait for pubsub reward notification
auto *clone = message->clone();
qCDebug(chatterinoTwitch) << "TwitchChannel reward added ADD "
"callback since reward is not known:"
<< rewardId;
channel->channelPointRewardAdded.connect(
[=, this, &server](ChannelPointReward reward) {
qCDebug(chatterinoTwitch)
<< "TwitchChannel reward added callback:" << reward.id
<< "-" << rewardId;
if (reward.id == rewardId)
{
this->addMessage(clone, target, originalContent, server,
isSub, isAction);
clone->deleteLater();
return true;
}
return false;
});
return;
std::shared_lock lock(channel->rewardAddedMutex);
if (!channel->isChannelPointRewardKnown(rewardId))
{
// Need to wait for pubsub reward notification
auto *rewardClone = new QString(rewardId);
auto *targetClone = new QString(target);
auto *contentClone = new QString(originalContent);
auto *clone = message->clone();
qCDebug(chatterinoTwitch)
<< "TwitchChannel reward added ADD "
"callback since reward is not known:"
<< rewardId;
channel->channelPointRewardAdded.connect(
[&, rewardClone, clone, targetClone,
contentClone](const auto reward) {
qCDebug(chatterinoTwitch)
<< "TwitchChannel reward added callback:"
<< reward.id << "-" << *rewardClone;
if (reward.id == *rewardClone)
{
this->addMessage(clone, *targetClone, *contentClone,
server, false, false);
clone->deleteLater();
delete rewardClone;
delete targetClone;
delete contentClone;
return true;
}
return false;
});
return;
}
}
args.channelPointRewardId = rewardId;
}
Expand Down
1 change: 1 addition & 0 deletions src/providers/twitch/TwitchChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ void TwitchChannel::addChannelPointReward(const ChannelPointReward &reward)
// This only attempts to prevent a crash when invoking the signal.
try
{
std::unique_lock lock(this->rewardAddedMutex);
this->channelPointRewardAdded.invoke(reward);
}
catch (const std::bad_function_call &)
Expand Down
2 changes: 2 additions & 0 deletions src/providers/twitch/TwitchChannel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <atomic>
#include <mutex>
#include <optional>
#include <shared_mutex>
#include <unordered_map>

namespace chatterino {
Expand Down Expand Up @@ -218,6 +219,7 @@ class TwitchChannel final : public Channel, public ChannelChatters
pajlada::Signals::NoArgSignal roomModesChanged;

// Channel point rewards
std::shared_mutex rewardAddedMutex;
pajlada::Signals::SelfDisconnectingSignal<ChannelPointReward>
channelPointRewardAdded;
void addChannelPointReward(const ChannelPointReward &reward);
Expand Down
Loading