2022-11-13 12:07:41 +01:00
|
|
|
#include "providers/seventv/SeventvEventAPI.hpp"
|
|
|
|
|
2023-07-29 11:49:44 +02:00
|
|
|
#include "Application.hpp"
|
2023-02-04 13:42:52 +01:00
|
|
|
#include "providers/seventv/eventapi/Client.hpp"
|
|
|
|
#include "providers/seventv/eventapi/Dispatch.hpp"
|
|
|
|
#include "providers/seventv/eventapi/Message.hpp"
|
2023-07-29 11:49:44 +02:00
|
|
|
#include "providers/seventv/SeventvBadges.hpp"
|
|
|
|
#include "providers/seventv/SeventvCosmetics.hpp"
|
2022-11-13 12:07:41 +01:00
|
|
|
|
|
|
|
#include <QJsonArray>
|
Sort and force grouping of includes (#4172)
This change enforces strict include grouping using IncludeCategories
In addition to adding this to the .clang-format file and applying it in the tests/src and src directories, I also did the following small changes:
In ChatterSet.hpp, I changed lrucache to a <>include
In Irc2.hpp, I change common/SignalVector.hpp to a "project-include"
In AttachedWindow.cpp, NativeMessaging.cpp, WindowsHelper.hpp, BaseWindow.cpp, and StreamerMode.cpp, I disabled clang-format for the windows-includes
In WindowDescriptors.hpp, I added the missing vector include. It was previously not needed because the include was handled by another file that was previously included first.
clang-format minimum version has been bumped, so Ubuntu version used in the check-formatting job has been bumped to 22.04 (which is the latest LTS)
2022-11-27 19:32:53 +01:00
|
|
|
|
2022-11-13 12:07:41 +01:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
2023-07-29 11:49:44 +02:00
|
|
|
using namespace seventv;
|
2023-02-04 13:42:52 +01:00
|
|
|
using namespace seventv::eventapi;
|
|
|
|
|
2022-11-13 12:07:41 +01:00
|
|
|
SeventvEventAPI::SeventvEventAPI(
|
|
|
|
QString host, std::chrono::milliseconds defaultHeartbeatInterval)
|
|
|
|
: BasicPubSubManager(std::move(host))
|
|
|
|
, heartbeatInterval_(defaultHeartbeatInterval)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SeventvEventAPI::subscribeUser(const QString &userID,
|
|
|
|
const QString &emoteSetID)
|
|
|
|
{
|
|
|
|
if (!userID.isEmpty() && this->subscribedUsers_.insert(userID).second)
|
|
|
|
{
|
2023-02-04 13:42:52 +01:00
|
|
|
this->subscribe(
|
|
|
|
{ObjectIDCondition{userID}, SubscriptionType::UpdateUser});
|
2022-11-13 12:07:41 +01:00
|
|
|
}
|
|
|
|
if (!emoteSetID.isEmpty() &&
|
|
|
|
this->subscribedEmoteSets_.insert(emoteSetID).second)
|
|
|
|
{
|
|
|
|
this->subscribe(
|
2023-02-04 13:42:52 +01:00
|
|
|
{ObjectIDCondition{emoteSetID}, SubscriptionType::UpdateEmoteSet});
|
2022-11-13 12:07:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-29 11:49:44 +02:00
|
|
|
void SeventvEventAPI::subscribeTwitchChannel(const QString &id)
|
|
|
|
{
|
|
|
|
if (this->subscribedTwitchChannels_.insert(id).second)
|
|
|
|
{
|
|
|
|
this->subscribe({
|
|
|
|
ChannelCondition{id},
|
|
|
|
SubscriptionType::CreateCosmetic,
|
|
|
|
});
|
|
|
|
this->subscribe({
|
|
|
|
ChannelCondition{id},
|
|
|
|
SubscriptionType::CreateEntitlement,
|
|
|
|
});
|
|
|
|
this->subscribe({
|
|
|
|
ChannelCondition{id},
|
|
|
|
SubscriptionType::DeleteEntitlement,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-13 12:07:41 +01:00
|
|
|
void SeventvEventAPI::unsubscribeEmoteSet(const QString &id)
|
|
|
|
{
|
|
|
|
if (this->subscribedEmoteSets_.erase(id) > 0)
|
|
|
|
{
|
|
|
|
this->unsubscribe(
|
2023-02-04 13:42:52 +01:00
|
|
|
{ObjectIDCondition{id}, SubscriptionType::UpdateEmoteSet});
|
2022-11-13 12:07:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SeventvEventAPI::unsubscribeUser(const QString &id)
|
|
|
|
{
|
|
|
|
if (this->subscribedUsers_.erase(id) > 0)
|
|
|
|
{
|
2023-02-04 13:42:52 +01:00
|
|
|
this->unsubscribe(
|
|
|
|
{ObjectIDCondition{id}, SubscriptionType::UpdateUser});
|
2022-11-13 12:07:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-29 11:49:44 +02:00
|
|
|
void SeventvEventAPI::unsubscribeTwitchChannel(const QString &id)
|
|
|
|
{
|
|
|
|
if (this->subscribedTwitchChannels_.erase(id) > 0)
|
|
|
|
{
|
|
|
|
this->unsubscribe({
|
|
|
|
ChannelCondition{id},
|
|
|
|
SubscriptionType::CreateCosmetic,
|
|
|
|
});
|
|
|
|
this->unsubscribe({
|
|
|
|
ChannelCondition{id},
|
|
|
|
SubscriptionType::CreateEntitlement,
|
|
|
|
});
|
|
|
|
this->unsubscribe({
|
|
|
|
ChannelCondition{id},
|
|
|
|
SubscriptionType::DeleteEntitlement,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-04 13:42:52 +01:00
|
|
|
std::shared_ptr<BasicPubSubClient<Subscription>> SeventvEventAPI::createClient(
|
|
|
|
liveupdates::WebsocketClient &client, websocketpp::connection_hdl hdl)
|
2022-11-13 12:07:41 +01:00
|
|
|
{
|
2023-02-04 13:42:52 +01:00
|
|
|
auto shared =
|
|
|
|
std::make_shared<Client>(client, hdl, this->heartbeatInterval_);
|
|
|
|
return std::static_pointer_cast<BasicPubSubClient<Subscription>>(
|
|
|
|
std::move(shared));
|
2022-11-13 12:07:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SeventvEventAPI::onMessage(
|
|
|
|
websocketpp::connection_hdl hdl,
|
2023-02-04 13:42:52 +01:00
|
|
|
BasicPubSubManager<Subscription>::WebsocketMessagePtr msg)
|
2022-11-13 12:07:41 +01:00
|
|
|
{
|
|
|
|
const auto &payload = QString::fromStdString(msg->get_payload());
|
|
|
|
|
2023-02-04 13:42:52 +01:00
|
|
|
auto pMessage = parseBaseMessage(payload);
|
2022-11-13 12:07:41 +01:00
|
|
|
|
|
|
|
if (!pMessage)
|
|
|
|
{
|
|
|
|
qCDebug(chatterinoSeventvEventAPI)
|
|
|
|
<< "Unable to parse incoming event-api message: " << payload;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto message = *pMessage;
|
|
|
|
switch (message.op)
|
|
|
|
{
|
2023-02-04 13:42:52 +01:00
|
|
|
case Opcode::Hello: {
|
2022-11-13 12:07:41 +01:00
|
|
|
if (auto client = this->findClient(hdl))
|
|
|
|
{
|
2023-02-04 13:42:52 +01:00
|
|
|
if (auto *stvClient = dynamic_cast<Client *>(client.get()))
|
2022-11-13 12:07:41 +01:00
|
|
|
{
|
|
|
|
stvClient->setHeartbeatInterval(
|
|
|
|
message.data["heartbeat_interval"].toInt());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2023-02-04 13:42:52 +01:00
|
|
|
case Opcode::Heartbeat: {
|
2022-11-13 12:07:41 +01:00
|
|
|
if (auto client = this->findClient(hdl))
|
|
|
|
{
|
2023-02-04 13:42:52 +01:00
|
|
|
if (auto *stvClient = dynamic_cast<Client *>(client.get()))
|
2022-11-13 12:07:41 +01:00
|
|
|
{
|
|
|
|
stvClient->handleHeartbeat();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2023-02-04 13:42:52 +01:00
|
|
|
case Opcode::Dispatch: {
|
|
|
|
auto dispatch = message.toInner<Dispatch>();
|
2022-11-13 12:07:41 +01:00
|
|
|
if (!dispatch)
|
|
|
|
{
|
|
|
|
qCDebug(chatterinoSeventvEventAPI)
|
|
|
|
<< "Malformed dispatch" << payload;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->handleDispatch(*dispatch);
|
|
|
|
}
|
|
|
|
break;
|
2023-02-04 13:42:52 +01:00
|
|
|
case Opcode::Reconnect: {
|
2022-11-13 12:07:41 +01:00
|
|
|
if (auto client = this->findClient(hdl))
|
|
|
|
{
|
2023-02-04 13:42:52 +01:00
|
|
|
if (auto *stvClient = dynamic_cast<Client *>(client.get()))
|
2022-11-13 12:07:41 +01:00
|
|
|
{
|
|
|
|
stvClient->close("Reconnecting");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2023-02-04 13:42:52 +01:00
|
|
|
case Opcode::Ack: {
|
|
|
|
// unhandled
|
|
|
|
}
|
|
|
|
break;
|
2022-11-13 12:07:41 +01:00
|
|
|
default: {
|
2023-02-04 13:42:52 +01:00
|
|
|
qCDebug(chatterinoSeventvEventAPI) << "Unhandled op:" << payload;
|
2022-11-13 12:07:41 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-04 13:42:52 +01:00
|
|
|
void SeventvEventAPI::handleDispatch(const Dispatch &dispatch)
|
2022-11-13 12:07:41 +01:00
|
|
|
{
|
|
|
|
switch (dispatch.type)
|
|
|
|
{
|
2023-02-04 13:42:52 +01:00
|
|
|
case SubscriptionType::UpdateEmoteSet: {
|
|
|
|
this->onEmoteSetUpdate(dispatch);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SubscriptionType::UpdateUser: {
|
|
|
|
this->onUserUpdate(dispatch);
|
|
|
|
}
|
|
|
|
break;
|
2023-07-29 11:49:44 +02:00
|
|
|
case SubscriptionType::CreateCosmetic: {
|
|
|
|
const CosmeticCreateDispatch cosmetic(dispatch);
|
|
|
|
if (cosmetic.validate())
|
|
|
|
{
|
|
|
|
this->onCosmeticCreate(cosmetic);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qCDebug(chatterinoSeventvEventAPI)
|
|
|
|
<< "Invalid cosmetic dispatch" << dispatch.body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SubscriptionType::CreateEntitlement: {
|
|
|
|
const EntitlementCreateDeleteDispatch entitlement(dispatch);
|
|
|
|
if (entitlement.validate())
|
|
|
|
{
|
|
|
|
this->onEntitlementCreate(entitlement);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qCDebug(chatterinoSeventvEventAPI)
|
|
|
|
<< "Invalid entitlement create dispatch" << dispatch.body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SubscriptionType::DeleteEntitlement: {
|
|
|
|
const EntitlementCreateDeleteDispatch entitlement(dispatch);
|
|
|
|
if (entitlement.validate())
|
|
|
|
{
|
|
|
|
this->onEntitlementDelete(entitlement);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qCDebug(chatterinoSeventvEventAPI)
|
|
|
|
<< "Invalid entitlement delete dispatch" << dispatch.body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2023-02-04 13:42:52 +01:00
|
|
|
default: {
|
|
|
|
qCDebug(chatterinoSeventvEventAPI)
|
2023-07-29 11:49:44 +02:00
|
|
|
<< "Unknown subscription type:"
|
|
|
|
<< magic_enum::enum_name(dispatch.type).data()
|
2023-02-04 13:42:52 +01:00
|
|
|
<< "body:" << dispatch.body;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-11-13 12:07:41 +01:00
|
|
|
|
2023-02-04 13:42:52 +01:00
|
|
|
void SeventvEventAPI::onEmoteSetUpdate(const Dispatch &dispatch)
|
|
|
|
{
|
|
|
|
// dispatchBody: {
|
|
|
|
// pushed: Array<{ key, value }>,
|
|
|
|
// pulled: Array<{ key, old_value }>,
|
|
|
|
// updated: Array<{ key, value, old_value }>,
|
|
|
|
// }
|
|
|
|
for (const auto pushedRef : dispatch.body["pushed"].toArray())
|
|
|
|
{
|
|
|
|
auto pushed = pushedRef.toObject();
|
|
|
|
if (pushed["key"].toString() != "emotes")
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2022-11-13 12:07:41 +01:00
|
|
|
|
2023-02-04 13:42:52 +01:00
|
|
|
const EmoteAddDispatch added(dispatch, pushed["value"].toObject());
|
2022-11-13 12:07:41 +01:00
|
|
|
|
2023-02-04 13:42:52 +01:00
|
|
|
if (added.validate())
|
|
|
|
{
|
|
|
|
this->signals_.emoteAdded.invoke(added);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qCDebug(chatterinoSeventvEventAPI)
|
|
|
|
<< "Invalid dispatch" << dispatch.body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const auto updatedRef : dispatch.body["updated"].toArray())
|
|
|
|
{
|
|
|
|
auto updated = updatedRef.toObject();
|
|
|
|
if (updated["key"].toString() != "emotes")
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2022-11-13 12:07:41 +01:00
|
|
|
|
2023-02-04 13:42:52 +01:00
|
|
|
const EmoteUpdateDispatch update(dispatch,
|
|
|
|
updated["old_value"].toObject(),
|
|
|
|
updated["value"].toObject());
|
|
|
|
|
|
|
|
if (update.validate())
|
|
|
|
{
|
|
|
|
this->signals_.emoteUpdated.invoke(update);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qCDebug(chatterinoSeventvEventAPI)
|
|
|
|
<< "Invalid dispatch" << dispatch.body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const auto pulledRef : dispatch.body["pulled"].toArray())
|
|
|
|
{
|
|
|
|
auto pulled = pulledRef.toObject();
|
|
|
|
if (pulled["key"].toString() != "emotes")
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const EmoteRemoveDispatch removed(dispatch,
|
|
|
|
pulled["old_value"].toObject());
|
|
|
|
|
|
|
|
if (removed.validate())
|
|
|
|
{
|
|
|
|
this->signals_.emoteRemoved.invoke(removed);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qCDebug(chatterinoSeventvEventAPI)
|
|
|
|
<< "Invalid dispatch" << dispatch.body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SeventvEventAPI::onUserUpdate(const Dispatch &dispatch)
|
|
|
|
{
|
|
|
|
// dispatchBody: {
|
|
|
|
// updated: Array<{ key, value: Array<{key, value}> }>
|
|
|
|
// }
|
|
|
|
for (const auto updatedRef : dispatch.body["updated"].toArray())
|
|
|
|
{
|
|
|
|
auto updated = updatedRef.toObject();
|
|
|
|
if (updated["key"].toString() != "connections")
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (const auto valueRef : updated["value"].toArray())
|
|
|
|
{
|
|
|
|
auto value = valueRef.toObject();
|
|
|
|
if (value["key"].toString() != "emote_set")
|
2022-11-13 12:07:41 +01:00
|
|
|
{
|
2023-02-04 13:42:52 +01:00
|
|
|
continue;
|
|
|
|
}
|
2022-11-13 12:07:41 +01:00
|
|
|
|
2023-02-04 13:42:52 +01:00
|
|
|
const UserConnectionUpdateDispatch update(
|
|
|
|
dispatch, value, (size_t)updated["index"].toInt());
|
2022-11-13 12:07:41 +01:00
|
|
|
|
2023-02-04 13:42:52 +01:00
|
|
|
if (update.validate())
|
|
|
|
{
|
|
|
|
this->signals_.userUpdated.invoke(update);
|
2022-11-13 12:07:41 +01:00
|
|
|
}
|
2023-02-04 13:42:52 +01:00
|
|
|
else
|
2022-11-13 12:07:41 +01:00
|
|
|
{
|
2023-02-04 13:42:52 +01:00
|
|
|
qCDebug(chatterinoSeventvEventAPI)
|
|
|
|
<< "Invalid dispatch" << dispatch.body;
|
2022-11-13 12:07:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-29 11:49:44 +02:00
|
|
|
// NOLINTBEGIN(readability-convert-member-functions-to-static)
|
|
|
|
|
|
|
|
void SeventvEventAPI::onCosmeticCreate(const CosmeticCreateDispatch &cosmetic)
|
|
|
|
{
|
2024-01-19 17:59:55 +01:00
|
|
|
auto *badges = getIApp()->getSeventvBadges();
|
2023-07-29 11:49:44 +02:00
|
|
|
switch (cosmetic.kind)
|
|
|
|
{
|
|
|
|
case CosmeticKind::Badge: {
|
|
|
|
badges->registerBadge(cosmetic.data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SeventvEventAPI::onEntitlementCreate(
|
|
|
|
const EntitlementCreateDeleteDispatch &entitlement)
|
|
|
|
{
|
2024-01-19 17:59:55 +01:00
|
|
|
auto *badges = getIApp()->getSeventvBadges();
|
2023-07-29 11:49:44 +02:00
|
|
|
switch (entitlement.kind)
|
|
|
|
{
|
|
|
|
case CosmeticKind::Badge: {
|
|
|
|
badges->assignBadgeToUser(entitlement.refID,
|
|
|
|
UserId{entitlement.userID});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SeventvEventAPI::onEntitlementDelete(
|
|
|
|
const EntitlementCreateDeleteDispatch &entitlement)
|
|
|
|
{
|
2024-01-19 17:59:55 +01:00
|
|
|
auto *badges = getIApp()->getSeventvBadges();
|
2023-07-29 11:49:44 +02:00
|
|
|
switch (entitlement.kind)
|
|
|
|
{
|
|
|
|
case CosmeticKind::Badge: {
|
|
|
|
badges->clearBadgeFromUser(entitlement.refID,
|
|
|
|
UserId{entitlement.userID});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// NOLINTEND(readability-convert-member-functions-to-static)
|
|
|
|
|
2022-11-13 12:07:41 +01:00
|
|
|
} // namespace chatterino
|