feat(eventsub): implement (shared-chat-)ban (#5985)

This commit is contained in:
nerix
2025-02-25 21:13:05 +01:00
committed by GitHub
parent aee5f1a54d
commit 044dc69aa6
18 changed files with 1060 additions and 331 deletions
+2 -46
View File
@@ -73,52 +73,8 @@ void Connection::onChannelBan(
const lib::payload::channel_ban::v1::Payload &payload)
{
(void)metadata;
auto roomID = QString::fromStdString(payload.event.broadcasterUserID);
BanAction action{};
if (!getApp()->isTest())
{
action.timestamp = std::chrono::steady_clock::now();
}
action.roomID = roomID;
action.source = ActionUser{
.id = QString::fromStdString(payload.event.moderatorUserID),
.login = QString::fromStdString(payload.event.moderatorUserLogin),
.displayName = QString::fromStdString(payload.event.moderatorUserName),
};
action.target = ActionUser{
.id = QString::fromStdString(payload.event.userID),
.login = QString::fromStdString(payload.event.userLogin),
.displayName = QString::fromStdString(payload.event.userName),
};
action.reason = QString::fromStdString(payload.event.reason);
if (payload.event.isPermanent)
{
action.duration = 0;
}
else
{
auto timeoutDuration = payload.event.timeoutDuration();
auto timeoutDurationInSeconds =
std::chrono::duration_cast<std::chrono::seconds>(timeoutDuration)
.count();
action.duration = timeoutDurationInSeconds;
}
auto chan = getApp()->getTwitch()->getChannelOrEmptyByID(roomID);
runInGuiThread([action{std::move(action)}, chan{std::move(chan)}] {
auto time = QDateTime::currentDateTime();
if (getApp()->isTest())
{
time = QDateTime::fromSecsSinceEpoch(0).toUTC();
}
MessageBuilder msg(action, time);
msg->flags.set(MessageFlag::PubSub);
chan->addOrReplaceTimeout(msg.release(), QDateTime::currentDateTime());
});
qCDebug(LOG) << "On channel ban event for channel"
<< payload.event.broadcasterUserLogin.c_str();
}
void Connection::onStreamOnline(
@@ -107,4 +107,37 @@ void makeModerateMessage(EventSubMessageBuilder &builder,
builder.message().searchText = text;
}
void makeModerateMessage(EventSubMessageBuilder &builder,
const lib::payload::channel_moderate::v2::Event &event,
const lib::payload::channel_moderate::v2::Ban &action)
{
QString text;
bool isShared = event.isFromSharedChat();
builder.appendUser(event.moderatorUserName, event.moderatorUserLogin, text);
builder.emplaceSystemTextAndUpdate("banned", text);
builder.appendUser(action.userName, action.userLogin, text, isShared);
if (isShared)
{
builder.emplaceSystemTextAndUpdate("in", text);
builder.appendUser(*event.sourceBroadcasterUserName,
*event.sourceBroadcasterUserLogin, text, false);
}
if (action.reason.view().empty())
{
builder.emplaceSystemTextAndUpdate(".", text);
}
else
{
builder.emplaceSystemTextAndUpdate(":", text);
builder.emplaceSystemTextAndUpdate(action.reason.qt(), text);
}
builder->messageText = text;
builder->searchText = text;
builder->timeoutUser = action.userLogin.qt();
}
} // namespace chatterino::eventsub
@@ -43,4 +43,9 @@ void makeModerateMessage(
const lib::payload::channel_moderate::v2::Event &event,
const lib::payload::channel_moderate::v2::Warn &action);
/// <MODERATOR> banned <USER>[ in <CHANNEL>]: <REASON>
void makeModerateMessage(EventSubMessageBuilder &builder,
const lib::payload::channel_moderate::v2::Event &event,
const lib::payload::channel_moderate::v2::Ban &action);
} // namespace chatterino::eventsub
@@ -1,6 +1,7 @@
#include "providers/twitch/eventsub/MessageHandlers.hpp"
#include "Application.hpp"
#include "messages/Message.hpp"
#include "messages/MessageBuilder.hpp"
#include "providers/twitch/TwitchChannel.hpp"
#include "singletons/Settings.hpp"
@@ -26,4 +27,19 @@ void handleModerateMessage(
});
}
void addModerateMessage(
TwitchChannel *channel, MessagePtr message, const QDateTime &time,
const lib::payload::channel_moderate::v2::Ban & /*action*/)
{
runInGuiThread([channel, msg{std::move(message)}, time] {
channel->addOrReplaceTimeout(msg, time);
if (getSettings()->hideModerated)
{
// XXX: This is expensive. We could use a layout request if the layout
// would store the previous message flags.
getApp()->getWindows()->forceLayoutChannelViews();
}
});
}
} // namespace chatterino::eventsub
@@ -2,10 +2,17 @@
#include "twitch-eventsub-ws/payloads/channel-moderate-v2.hpp"
#include <memory>
class QDateTime;
namespace chatterino {
class TwitchChannel;
struct Message;
using MessagePtr = std::shared_ptr<const Message>;
} // namespace chatterino
namespace chatterino::eventsub {