From c7dea40b6baa9f401cc5559723b103edfb75741e Mon Sep 17 00:00:00 2001 From: nerix Date: Sun, 14 Sep 2025 13:28:39 +0200 Subject: [PATCH] fix(plugins): Implement `__eq` for `Channel` (#6456) --- CHANGELOG.md | 1 + src/controllers/plugins/api/ChannelRef.cpp | 6 ++++++ src/controllers/plugins/api/ChannelRef.hpp | 2 ++ src/util/WeakPtrHelpers.hpp | 19 +++++++++++++++++++ tests/src/Plugins.cpp | 9 +++++++++ 5 files changed, 37 insertions(+) create mode 100644 src/util/WeakPtrHelpers.hpp diff --git a/CHANGELOG.md b/CHANGELOG.md index dc69896f..679de3b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Bugfix: Fixed zero-width global BTTV emotes not showing in the `:~` completions. (#6440) - Bugfix: Fixed an issue where the update button would be unclickable on macOS and Linux. (#6447, #6453) - Bugfix: Fixed flickering tooltips on Wayland when the mouse cursor is over them. (#6451) +- Bugfix: Fixed `c2.Channel` comparing `false` to the same channel. (#6456) - Bugfix: Fixed an issue where the moderation icon was missing from the Moderation tab. (#6457) - Dev: Added documentation for WebSockets to `wip-plugins.md`. (#6432) diff --git a/src/controllers/plugins/api/ChannelRef.cpp b/src/controllers/plugins/api/ChannelRef.cpp index 254b01b7..97b0bbe2 100644 --- a/src/controllers/plugins/api/ChannelRef.cpp +++ b/src/controllers/plugins/api/ChannelRef.cpp @@ -7,6 +7,7 @@ # include "controllers/plugins/SolTypes.hpp" # include "providers/twitch/TwitchChannel.hpp" # include "providers/twitch/TwitchIrcServer.hpp" +# include "util/WeakPtrHelpers.hpp" # include @@ -159,6 +160,11 @@ QString ChannelRef::to_string() return QStringView(u"").arg(chan->getName()); } +bool ChannelRef::operator==(const ChannelRef &other) const noexcept +{ + return weakOwnerEquals(this->weak, other.weak); +} + std::optional ChannelRef::get_by_name(const QString &name) { auto chan = getApp()->getTwitch()->getChannelOrEmpty(name); diff --git a/src/controllers/plugins/api/ChannelRef.hpp b/src/controllers/plugins/api/ChannelRef.hpp index a174ef04..8057d002 100644 --- a/src/controllers/plugins/api/ChannelRef.hpp +++ b/src/controllers/plugins/api/ChannelRef.hpp @@ -155,6 +155,8 @@ public: */ QString to_string(); + bool operator==(const ChannelRef &other) const noexcept; + /** * Static functions */ diff --git a/src/util/WeakPtrHelpers.hpp b/src/util/WeakPtrHelpers.hpp new file mode 100644 index 00000000..9b4b4f76 --- /dev/null +++ b/src/util/WeakPtrHelpers.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include + +namespace chatterino { + +/// Check if the owner of two weak pointers is equal. +/// +/// Like `std::weak_ptr::owner_before`, this compares the control blocks, +/// not the objects themselves. +/// Equivalent to the C++ 26 `std::weak_ptr::owner_equal`. +template +bool weakOwnerEquals(const std::weak_ptr &a, + const std::weak_ptr &b) noexcept +{ + return !a.owner_before(b) && !b.owner_before(a); +} + +} // namespace chatterino diff --git a/tests/src/Plugins.cpp b/tests/src/Plugins.cpp index 38390361..156bd0b3 100644 --- a/tests/src/Plugins.cpp +++ b/tests/src/Plugins.cpp @@ -309,6 +309,15 @@ TEST_F(PluginTest, testChannel) lua->script(R"lua( return chn:is_twitch_channel() )lua").get(0), true); + ASSERT_TRUE(lua->script(R"lua( + assert(c2.Channel.by_name("mm2pl") == c2.Channel.by_name("mm2pl")) + assert(not c2.Channel.by_name("mm2pl") ~= c2.Channel.by_name("mm2pl")) + assert(c2.Channel.by_name("mm2pl") ~= c2.Channel.by_name("twitchdev")) + assert(c2.Channel.by_name("twitchdev") == c2.Channel.by_name("twitchdev")) + assert(c2.Channel.by_name("twitchdev") == c2.Channel.by_name("other")) -- empty channel + )lua") + .valid()); + // this is not a TwitchChannel const auto *shouldThrow1 = R"lua( return chn:is_broadcaster()