chore(eventsub): simplify building for system messages (#5980)

This commit is contained in:
nerix
2025-02-23 17:23:29 +01:00
committed by GitHub
parent 7fee3f7e37
commit 0d7a05e62f
4 changed files with 89 additions and 111 deletions
+1 -1
View File
@@ -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, #5974)
- Dev: Add initial experimental EventSub support. (#5837, #5895, #5897, #5904, #5910, #5903, #5915, #5916, #5930, #5935, #5932, #5943, #5952, #5953, #5968, #5973, #5974, #5980)
- 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)
+16 -22
View File
@@ -20,9 +20,20 @@
namespace {
using namespace chatterino;
using namespace chatterino::eventsub;
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
const auto &LOG = chatterinoTwitchEventSub;
template <typename Action>
concept CanMakeModMessage =
requires(EventSubMessageBuilder &builder,
const lib::payload::channel_moderate::v2::Event &event,
const std::remove_cvref_t<Action> &action) {
makeModerateMessage(builder, event, action);
};
} // namespace
namespace chatterino::eventsub {
@@ -178,29 +189,12 @@ void Connection::onChannelModerate(
std::visit(
[&](auto &&action) {
using Action = std::remove_cvref_t<decltype(action)>;
if constexpr (std::is_same_v<
Action, lib::payload::channel_moderate::v2::Vip>)
if constexpr (CanMakeModMessage<Action>)
{
auto msg = makeVipMessage(channel, now, payload.event, action);
runInGuiThread([channel, msg] {
channel->addMessage(msg, MessageContext::Original);
});
}
else if constexpr (std::is_same_v<
Action,
lib::payload::channel_moderate::v2::Unvip>)
{
auto msg =
makeUnvipMessage(channel, now, payload.event, action);
runInGuiThread([channel, msg] {
channel->addMessage(msg, MessageContext::Original);
});
}
else if constexpr (std::is_same_v<
Action,
lib::payload::channel_moderate::v2::Warn>)
{
auto msg = makeWarnMessage(channel, now, payload.event, action);
EventSubMessageBuilder builder(channel, now);
builder->loginName = payload.event.moderatorUserLogin.qt();
makeModerateMessage(builder, payload.event, action);
auto msg = builder.release();
runInGuiThread([channel, msg] {
channel->addMessage(msg, MessageContext::Original);
});
@@ -1,111 +1,82 @@
#include "providers/twitch/eventsub/MessageBuilder.hpp"
#include "messages/Message.hpp"
#include "messages/MessageBuilder.hpp"
namespace chatterino::eventsub {
MessagePtr makeVipMessage(
TwitchChannel *channel, const QDateTime &time,
const lib::payload::channel_moderate::v2::Event &event,
const lib::payload::channel_moderate::v2::Vip &action)
EventSubMessageBuilder::EventSubMessageBuilder(TwitchChannel *channel,
const QDateTime &time)
: channel(channel)
{
MessageBuilder builder;
this->emplace<TimestampElement>();
this->message().flags.set(MessageFlag::System);
this->message().flags.set(MessageFlag::Timeout); // do we need this?
this->message().serverReceivedTime = time;
}
EventSubMessageBuilder::~EventSubMessageBuilder() = default;
void EventSubMessageBuilder::appendUser(const lib::String &userName,
const lib::String &userLogin,
QString &text, bool trailingSpace)
{
auto login = userLogin.qt();
auto *el = this->emplace<MentionElement>(userName.qt(), login,
MessageColor::System,
channel->getUserColor(login));
text.append(login);
if (trailingSpace)
{
text.append(u' ');
}
else
{
el->setTrailingSpace(false);
}
}
void makeModerateMessage(EventSubMessageBuilder &builder,
const lib::payload::channel_moderate::v2::Event &event,
const lib::payload::channel_moderate::v2::Vip &action)
{
QString text;
builder.emplace<TimestampElement>();
builder->flags.set(MessageFlag::System);
builder->flags.set(MessageFlag::Timeout);
builder->loginName = event.moderatorUserLogin.qt();
builder.emplace<MentionElement>(
event.moderatorUserName.qt(), event.moderatorUserLogin.qt(),
MessageColor::System,
channel->getUserColor(event.moderatorUserLogin.qt()));
text.append(event.moderatorUserLogin.qt() + " ");
builder.appendUser(event.moderatorUserName, event.moderatorUserLogin, text);
builder.emplaceSystemTextAndUpdate("has added", text);
builder.emplace<MentionElement>(
action.userName.qt(), action.userLogin.qt(), MessageColor::System,
channel->getUserColor(action.userLogin.qt()));
text.append(action.userLogin.qt() + " ");
builder.appendUser(action.userName, action.userLogin, text);
builder.emplaceSystemTextAndUpdate("as a VIP of this channel.", text);
builder.message().messageText = text;
builder.message().searchText = text;
builder.message().serverReceivedTime = time;
return builder.release();
}
MessagePtr makeUnvipMessage(
TwitchChannel *channel, const QDateTime &time,
void makeModerateMessage(
EventSubMessageBuilder &builder,
const lib::payload::channel_moderate::v2::Event &event,
const lib::payload::channel_moderate::v2::Unvip &action)
{
MessageBuilder builder;
QString text;
builder.emplace<TimestampElement>();
builder->flags.set(MessageFlag::System);
builder->flags.set(MessageFlag::Timeout);
builder->loginName = event.moderatorUserLogin.qt();
builder.emplace<MentionElement>(
event.moderatorUserName.qt(), event.moderatorUserLogin.qt(),
MessageColor::System,
channel->getUserColor(event.moderatorUserLogin.qt()));
text.append(event.moderatorUserLogin.qt() + " ");
builder.appendUser(event.moderatorUserName, event.moderatorUserLogin, text);
builder.emplaceSystemTextAndUpdate("has removed", text);
builder.emplace<MentionElement>(
action.userName.qt(), action.userLogin.qt(), MessageColor::System,
channel->getUserColor(action.userLogin.qt()));
text.append(action.userLogin.qt() + " ");
builder.appendUser(action.userName, action.userLogin, text);
builder.emplaceSystemTextAndUpdate("as a VIP of this channel.", text);
builder.message().messageText = text;
builder.message().searchText = text;
builder.message().serverReceivedTime = time;
return builder.release();
}
MessagePtr makeWarnMessage(
TwitchChannel *channel, const QDateTime &time,
const lib::payload::channel_moderate::v2::Event &event,
const lib::payload::channel_moderate::v2::Warn &action)
void makeModerateMessage(EventSubMessageBuilder &builder,
const lib::payload::channel_moderate::v2::Event &event,
const lib::payload::channel_moderate::v2::Warn &action)
{
MessageBuilder builder;
QString text;
builder.emplace<TimestampElement>();
builder->flags.set(MessageFlag::System);
builder->flags.set(MessageFlag::Timeout);
builder->loginName = event.moderatorUserLogin.qt();
builder.emplace<MentionElement>(
event.moderatorUserName.qt(), event.moderatorUserLogin.qt(),
MessageColor::System,
channel->getUserColor(event.moderatorUserLogin.qt()));
text.append(event.moderatorUserLogin.qt() + " ");
builder.appendUser(event.moderatorUserName, event.moderatorUserLogin, text);
builder.emplaceSystemTextAndUpdate("has warned", text);
builder
.emplace<MentionElement>(action.userName.qt(), action.userLogin.qt(),
MessageColor::System,
channel->getUserColor(action.userLogin.qt()))
->setTrailingSpace(false);
text.append(action.userLogin.qt());
builder.appendUser(action.userName, action.userLogin, text, false);
QStringList reasons;
@@ -134,10 +105,6 @@ MessagePtr makeWarnMessage(
builder.message().messageText = text;
builder.message().searchText = text;
builder.message().serverReceivedTime = time;
return builder.release();
}
} // namespace chatterino::eventsub
@@ -1,6 +1,6 @@
#pragma once
#include "messages/Message.hpp"
#include "messages/MessageBuilder.hpp"
#include "providers/twitch/TwitchChannel.hpp"
#include "twitch-eventsub-ws/payloads/channel-moderate-v2.hpp"
@@ -8,21 +8,38 @@
namespace chatterino::eventsub {
class EventSubMessageBuilder : public MessageBuilder
{
public:
EventSubMessageBuilder(TwitchChannel *channel, const QDateTime &time);
~EventSubMessageBuilder();
EventSubMessageBuilder(const EventSubMessageBuilder &) = delete;
EventSubMessageBuilder(EventSubMessageBuilder &&) = delete;
EventSubMessageBuilder &operator=(const EventSubMessageBuilder &) = delete;
EventSubMessageBuilder &operator=(EventSubMessageBuilder &&) = delete;
void appendUser(const lib::String &userName, const lib::String &userLogin,
QString &text, bool trailingSpace = true);
private:
TwitchChannel *channel;
};
/// <BROADCASTER> has added <USER> as a VIP of this channel.
MessagePtr makeVipMessage(
TwitchChannel *channel, const QDateTime &time,
const lib::payload::channel_moderate::v2::Event &event,
const lib::payload::channel_moderate::v2::Vip &action);
void makeModerateMessage(EventSubMessageBuilder &builder,
const lib::payload::channel_moderate::v2::Event &event,
const lib::payload::channel_moderate::v2::Vip &action);
/// <BROADCASTER> has removed <USER> as a VIP of this channel.
MessagePtr makeUnvipMessage(
TwitchChannel *channel, const QDateTime &time,
void makeModerateMessage(
EventSubMessageBuilder &builder,
const lib::payload::channel_moderate::v2::Event &event,
const lib::payload::channel_moderate::v2::Unvip &action);
/// <MODERATOR> has warned <USER>: <REASON>
MessagePtr makeWarnMessage(
TwitchChannel *channel, const QDateTime &time,
void makeModerateMessage(
EventSubMessageBuilder &builder,
const lib::payload::channel_moderate::v2::Event &event,
const lib::payload::channel_moderate::v2::Warn &action);