add eventsub over chat shitty code & proper submodule

This commit is contained in:
Rasmus Karlsson 2024-01-27 00:39:35 +01:00
parent 9bf5cfe3b7
commit 2db096d59e
8 changed files with 1515 additions and 10 deletions

3
.gitmodules vendored
View file

@ -41,3 +41,6 @@
[submodule "tools/crash-handler"]
path = tools/crash-handler
url = https://github.com/Chatterino/crash-handler
[submodule "lib/twitch-eventsub-ws"]
path = lib/twitch-eventsub-ws
url = https://github.com/Chatterino/twitch-eventsub-ws

View file

@ -204,6 +204,8 @@ else()
add_subdirectory("${CMAKE_SOURCE_DIR}/lib/settings" EXCLUDE_FROM_ALL)
endif()
add_subdirectory("${CMAKE_SOURCE_DIR}/lib/twitch-eventsub-ws" EXCLUDE_FROM_ALL)
if (CHATTERINO_PLUGINS)
set(LUA_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/lib/lua/src")
add_subdirectory(lib/lua)
@ -213,8 +215,6 @@ if (BUILD_WITH_CRASHPAD)
add_subdirectory("${CMAKE_SOURCE_DIR}/tools/crash-handler")
endif()
add_subdirectory("${CMAKE_SOURCE_DIR}/../beast-websocket-client" eventsub EXCLUDE_FROM_ALL)
# Used to provide a date of build in the About page (for nightly builds). Getting the actual time of
# compilation in CMake is a more involved, as documented in https://stackoverflow.com/q/24292898.
# For CI runs, however, the date of build file generation should be consistent with the date of

@ -0,0 +1 @@
Subproject commit 091301e15d2aa86b5218c1917e351026aaf1d544

View file

@ -374,6 +374,8 @@ set(SOURCE_FILES
providers/twitch/ChannelPointReward.hpp
providers/twitch/EventSub.cpp
providers/twitch/EventSub.hpp
providers/twitch/EventSubMessageBuilder.cpp
providers/twitch/EventSubMessageBuilder.hpp
providers/twitch/IrcMessageHandler.cpp
providers/twitch/IrcMessageHandler.hpp
providers/twitch/PubSubActions.cpp
@ -766,11 +768,7 @@ target_link_libraries(${LIBRARY_PROJECT}
RapidJSON::RapidJSON
LRUCache
MagicEnum
)
target_link_libraries(${LIBRARY_PROJECT}
PUBLIC
eventsub
twitch-eventsub-ws
)
if (CHATTERINO_PLUGINS)
@ -824,7 +822,7 @@ if (BUILD_APP)
target_link_libraries(${EXECUTABLE_PROJECT}
PUBLIC
eventsub
twitch-eventsub-ws
)
set_target_directory_hierarchy(${EXECUTABLE_PROJECT})

View file

@ -7,6 +7,7 @@
#include "messages/Message.hpp"
#include "messages/MessageBuilder.hpp"
#include "providers/twitch/api/Helix.hpp"
#include "providers/twitch/EventSubMessageBuilder.hpp"
#include "providers/twitch/PubSubActions.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchChannel.hpp"
@ -16,8 +17,8 @@
#include <boost/asio/io_context.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/json.hpp>
#include <eventsub/listener.hpp>
#include <eventsub/session.hpp>
#include <twitch-eventsub-ws/listener.hpp>
#include <twitch-eventsub-ws/session.hpp>
#include <chrono>
#include <memory>
@ -103,6 +104,26 @@ public:
<< roomID << ":" << message;
});
}
{
QJsonObject condition;
condition.insert("broadcaster_user_id", roomID);
condition.insert("user_id", sourceUserID);
getHelix()->createEventSubSubscription(
"channel.chat.message", "v1", sessionID, condition,
[roomID](const auto &response) {
qDebug() << "Successfully subscribed to "
"channel.chat.message in "
<< roomID << ":" << response;
},
[roomID](auto error, const auto &message) {
(void)error;
qDebug() << "Failed subscription to "
"channel.chat.message in"
<< roomID << ":" << message;
});
}
}
});
}
@ -198,6 +219,27 @@ public:
qCDebug(LOG) << "On channel update for"
<< payload.event.broadcasterUserLogin.c_str();
}
void onChannelChatMessage(
eventsub::messages::Metadata metadata,
eventsub::payload::channel_chat_message::v1::Payload payload) override
{
(void)metadata;
std::cout << "Channel chat message event!\n";
runInGuiThread([payload{std::move(payload)}]() {
MessageParseArgs args;
EventSubMessageBuilder builder(payload, args);
auto message = builder.build();
auto channel = getApp()->twitch->getChannelOrEmptyByID(
QString::fromStdString(payload.event.broadcasterUserID));
channel->addMessage(message);
});
}
};
void EventSub::start()

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,107 @@
#pragma once
#include "common/Aliases.hpp"
#include "common/Outcome.hpp"
#include "messages/SharedMessageBuilder.hpp"
#include "providers/twitch/TwitchMessageBuilder.hpp" // TODO: REMOVE
#include <QString>
#include <QVariant>
#include <twitch-eventsub-ws/payloads/channel-chat-message-v1.hpp>
#include <optional>
#include <unordered_map>
namespace chatterino {
struct Emote;
using EmotePtr = std::shared_ptr<const Emote>;
class Channel;
class TwitchChannel;
class MessageThread;
struct HelixVip;
using HelixModerator = HelixVip;
struct ChannelPointReward;
struct DeleteAction;
class EventSubMessageBuilder : MessageBuilder
{
const eventsub::payload::channel_chat_message::v1::Payload &payload;
const std::shared_ptr<Channel> channel;
const MessageParseArgs &args;
QString originalMessage;
const TwitchChannel *twitchChannel;
QColor usernameColor = {153, 153, 153};
public:
EventSubMessageBuilder() = delete;
/**
* NOTE: The builder MUST NOT survive longer than the payload
**/
explicit EventSubMessageBuilder(
const eventsub::payload::channel_chat_message::v1::Payload &_payload,
const MessageParseArgs &_args);
~EventSubMessageBuilder() override = default;
MessagePtr build();
void setThread(std::shared_ptr<MessageThread> thread);
void setParent(MessagePtr parent);
void setMessageOffset(int offset);
private:
void parseUsernameColor();
// Parse & build thread information into the message
// Will read information from thread_ or from IRC tags
void parseThread();
void runIgnoreReplaces(std::vector<TwitchEmoteOccurrence> &twitchEmotes);
std::optional<EmotePtr> getTwitchBadge(const Badge &badge) const;
Outcome tryAppendEmote(const EmoteName &name);
void addWords();
void addTextOrEmoji(EmotePtr emote) override;
void addTextOrEmoji(const QString &value) override;
void appendTwitchBadges();
void appendChatterinoBadges();
void appendFfzBadges();
void appendSeventvBadges();
Outcome tryParseCheermote(const QString &string);
bool shouldAddModerationElements() const;
QString roomID_;
bool hasBits_ = false;
QString bits;
int bitsLeft{};
bool bitsStacked = false;
std::shared_ptr<MessageThread> thread_;
MessagePtr parent_;
/**
* Starting offset to be used on index-based operations on `originalMessage_`.
*
* For example:
* originalMessage_ = "there"
* messageOffset_ = 4
* (the irc message is "hey there")
*
* then the index 6 would resolve to 6 - 4 = 2 => 'e'
*/
int messageOffset_ = 0;
// User ID of the sender of this message
QString userId_;
QString userName; // TODO: Rename to userLogin
bool senderIsBroadcaster{};
const QVariantMap tags;
};
} // namespace chatterino

View file

@ -451,6 +451,7 @@ private:
friend class TwitchIrcServer;
friend class TwitchMessageBuilder;
friend class EventSubMessageBuilder; // TODO: Remove this
friend class IrcMessageHandler;
};