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

chore(eventsub): add operator== and indicator for shared chat #5974

Merged
merged 6 commits into from
Feb 22, 2025
Merged
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: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
- Bugfix: Fixed channel point redemptions with messages not showing up if PubSub is disconnected. (#5948)
- Bugfix: Fixed the input font not immediately updating when zooming in/out. (#5960)
- Dev: Subscriptions to PubSub channel points redemption topics now use no auth token, making it continue to work during PubSub shutdown. (#5947)
- Dev: Add initial experimental EventSub support. (#5837, #5895, #5897, #5904, #5910, #5903, #5915, #5916, #5930, #5935, #5932, #5943, #5952, #5953, #5968, #5973)
- Dev: Add initial experimental EventSub support. (#5837, #5895, #5897, #5904, #5910, #5903, #5915, #5916, #5930, #5935, #5932, #5943, #5952, #5953, #5968, #5973, #5974)
- Dev: Remove unneeded platform specifier for toasts. (#5914)
- Dev: Highlight checks now use non-capturing groups for the boundaries. (#5784)
- Dev: Removed unused PubSub whisper code. (#5898)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,11 @@ struct Event {
String broadcasterUserName;

/// For Shared Chat events, the user ID (e.g. 117166826) of the user who's channel the event took place in
std::optional<std::string> sourceBroadcasterUserID;
std::optional<String> sourceBroadcasterUserID;
/// For Shared Chat events, the user Login (e.g. testaccount_420) of the user who's channel the event took place in
std::optional<std::string> sourceBroadcasterUserLogin;
std::optional<String> sourceBroadcasterUserLogin;
/// For Shared Chat events, the user Name (e.g. 테스트계정420) of the user who's channel the event took place in
std::optional<std::string> sourceBroadcasterUserName;
std::optional<String> sourceBroadcasterUserName;

/// User ID (e.g. 117166826) of the user who took the action
String moderatorUserID;
Expand Down Expand Up @@ -439,6 +439,8 @@ struct Event {
SharedChatDelete, //
std::string>
action;

bool isFromSharedChat() const noexcept;
};

struct Payload {
Expand Down
18 changes: 18 additions & 0 deletions lib/twitch-eventsub-ws/include/twitch-eventsub-ws/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,24 @@ struct String {
return (this->flags & (ALLOC_BIT | QT_BIT)) == 0;
}

// note: because we're using C++ 20, the reversed operator
// (e.g. QAnyStringView == String) is automatically "synthesized".
bool operator==(const QAnyStringView &other) const noexcept
{
return this->view() == other;
}

bool operator==(const String &other) const noexcept
{
return this->view() == other.view();
}

template <typename = void> // weak overload
bool operator==(const std::string_view &other) const noexcept
{
return this->view() == other;
}

private:
static constexpr size_t QT_BIT = 1ULL << (sizeof(size_t) * 8 - 1);
static constexpr size_t ALLOC_BIT = 1ULL << (sizeof(size_t) * 8 - 2);
Expand Down
1 change: 1 addition & 0 deletions lib/twitch-eventsub-ws/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set(SOURCE_FILES

# Subscription types (only additional functions)
payloads/channel-ban-v1.cpp
payloads/channel-moderate-v2.cpp
# Add your new subscription type source file above this line

${eventsub_generated_sources}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1215,14 +1215,16 @@ boost::json::result_for<Event, boost::json::value>::type tag_invoke(
return broadcasterUserName.error();
}

std::optional<std::string> sourceBroadcasterUserID = std::nullopt;
std::optional<chatterino::eventsub::lib::String> sourceBroadcasterUserID =
std::nullopt;
const auto *jvsourceBroadcasterUserID =
root.if_contains("source_broadcaster_user_id");
if (jvsourceBroadcasterUserID != nullptr &&
!jvsourceBroadcasterUserID->is_null())
{
auto tsourceBroadcasterUserID =
boost::json::try_value_to<std::string>(*jvsourceBroadcasterUserID);
boost::json::try_value_to<chatterino::eventsub::lib::String>(
*jvsourceBroadcasterUserID);

if (tsourceBroadcasterUserID.has_error())
{
Expand All @@ -1231,14 +1233,15 @@ boost::json::result_for<Event, boost::json::value>::type tag_invoke(
sourceBroadcasterUserID = std::move(tsourceBroadcasterUserID.value());
}

std::optional<std::string> sourceBroadcasterUserLogin = std::nullopt;
std::optional<chatterino::eventsub::lib::String>
sourceBroadcasterUserLogin = std::nullopt;
const auto *jvsourceBroadcasterUserLogin =
root.if_contains("source_broadcaster_user_login");
if (jvsourceBroadcasterUserLogin != nullptr &&
!jvsourceBroadcasterUserLogin->is_null())
{
auto tsourceBroadcasterUserLogin =
boost::json::try_value_to<std::string>(
boost::json::try_value_to<chatterino::eventsub::lib::String>(
*jvsourceBroadcasterUserLogin);

if (tsourceBroadcasterUserLogin.has_error())
Expand All @@ -1249,14 +1252,15 @@ boost::json::result_for<Event, boost::json::value>::type tag_invoke(
std::move(tsourceBroadcasterUserLogin.value());
}

std::optional<std::string> sourceBroadcasterUserName = std::nullopt;
std::optional<chatterino::eventsub::lib::String> sourceBroadcasterUserName =
std::nullopt;
const auto *jvsourceBroadcasterUserName =
root.if_contains("source_broadcaster_user_name");
if (jvsourceBroadcasterUserName != nullptr &&
!jvsourceBroadcasterUserName->is_null())
{
auto tsourceBroadcasterUserName =
boost::json::try_value_to<std::string>(
boost::json::try_value_to<chatterino::eventsub::lib::String>(
*jvsourceBroadcasterUserName);

if (tsourceBroadcasterUserName.has_error())
Expand Down
11 changes: 11 additions & 0 deletions lib/twitch-eventsub-ws/src/payloads/channel-moderate-v2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "twitch-eventsub-ws/payloads/channel-moderate-v2.hpp"

namespace chatterino::eventsub::lib::payload::channel_moderate::v2 {

bool Event::isFromSharedChat() const noexcept
{
return this->sourceBroadcasterUserID && this->sourceBroadcasterUserLogin &&
this->sourceBroadcasterUserName &&
*this->sourceBroadcasterUserID != this->broadcasterUserID;
}
} // namespace chatterino::eventsub::lib::payload::channel_moderate::v2
52 changes: 52 additions & 0 deletions lib/twitch-eventsub-ws/tests/src/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,55 @@ TEST(String, QtLifetime)
}
ASSERT_TRUE(qt.isDetached());
}

TEST(String, Equals)
{
String empty;

// This is using ASSERT_TRUE instead of ASSERT_EQ to be able to view/go to
// the chosen operator== in editors (with clangd for example).
ASSERT_TRUE(empty == empty);
ASSERT_TRUE(empty == "");
ASSERT_TRUE(empty == u"");
ASSERT_TRUE(empty == std::string_view{});
ASSERT_TRUE(empty == QAnyStringView());
ASSERT_TRUE(empty == String());
ASSERT_TRUE("" == empty);
ASSERT_TRUE(u"" == empty);

String longestSso(LONGEST_SSO);
String longestSsoQt(LONGEST_SSO);
ASSERT_EQ(longestSsoQt.qt(), LONGEST_SSO);

ASSERT_TRUE(longestSso == longestSso);
ASSERT_TRUE(longestSsoQt == longestSsoQt);
ASSERT_TRUE(longestSso == longestSsoQt);
ASSERT_TRUE(longestSso == LONGEST_SSO);
ASSERT_TRUE(longestSsoQt == LONGEST_SSO);
ASSERT_TRUE(longestSso == String(LONGEST_SSO));
ASSERT_TRUE(longestSsoQt == String(LONGEST_SSO));

String tooLong(TOO_LONG);
String tooLongQt(TOO_LONG);
ASSERT_EQ(tooLongQt.qt(), TOO_LONG);

ASSERT_TRUE(tooLong == tooLong);
ASSERT_TRUE(tooLongQt == tooLongQt);
ASSERT_TRUE(tooLong == tooLongQt);
ASSERT_TRUE(tooLong == TOO_LONG);
ASSERT_TRUE(tooLongQt == TOO_LONG);
ASSERT_TRUE(tooLong == String(TOO_LONG));
ASSERT_TRUE(tooLongQt == String(TOO_LONG));

String reallyLong(REALLY_LONG);
String reallyLongQt(REALLY_LONG);
ASSERT_EQ(reallyLongQt.qt(), REALLY_LONG);

ASSERT_TRUE(reallyLong == reallyLong);
ASSERT_TRUE(reallyLongQt == reallyLongQt);
ASSERT_TRUE(reallyLong == reallyLongQt);
ASSERT_TRUE(reallyLong == REALLY_LONG);
ASSERT_TRUE(reallyLongQt == REALLY_LONG);
ASSERT_TRUE(reallyLong == String(REALLY_LONG));
ASSERT_TRUE(reallyLongQt == String(REALLY_LONG));
}
Loading