feat: show BTTV Pro badges (#6625)
Reviewed-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
#include "providers/bttv/BttvBadges.hpp"
|
||||
|
||||
#include "messages/Emote.hpp"
|
||||
#include "messages/Image.hpp"
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
QString BttvBadges::idForBadge(const QJsonObject &badgeJson) const
|
||||
{
|
||||
return badgeJson["url"].toString();
|
||||
}
|
||||
|
||||
EmotePtr BttvBadges::createBadge(const QString &id,
|
||||
const QJsonObject & /* badgeJson */) const
|
||||
{
|
||||
auto emote = Emote{
|
||||
.name = EmoteName{},
|
||||
.images = ImageSet(Image::fromUrl(Url{id}, 18.0 / 72.0)),
|
||||
.tooltip = Tooltip{"BTTV Pro"},
|
||||
.homePage = Url{},
|
||||
.id = EmoteId{id},
|
||||
};
|
||||
|
||||
return std::make_shared<const Emote>(std::move(emote));
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include "util/BadgeRegistry.hpp"
|
||||
|
||||
#include <QJsonObject>
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
struct Emote;
|
||||
using EmotePtr = std::shared_ptr<const Emote>;
|
||||
|
||||
class BttvBadges : public BadgeRegistry
|
||||
{
|
||||
public:
|
||||
BttvBadges() = default;
|
||||
|
||||
protected:
|
||||
QString idForBadge(const QJsonObject &badgeJson) const override;
|
||||
EmotePtr createBadge(const QString &id,
|
||||
const QJsonObject &badgeJson) const override;
|
||||
};
|
||||
|
||||
} // namespace chatterino
|
||||
@@ -24,6 +24,7 @@ public:
|
||||
BttvLiveUpdatesPrivate &operator=(const BttvLiveUpdatesPrivate &&) = delete;
|
||||
|
||||
std::shared_ptr<BttvLiveUpdateClient> makeClient();
|
||||
std::shared_ptr<BttvLiveUpdateClient> anyClient();
|
||||
|
||||
// Contains all joined Twitch channel-ids
|
||||
std::unordered_set<QString> joinedChannels;
|
||||
@@ -50,6 +51,15 @@ std::shared_ptr<BttvLiveUpdateClient> BttvLiveUpdatesPrivate::makeClient()
|
||||
return std::make_shared<BttvLiveUpdateClient>(this->parent);
|
||||
}
|
||||
|
||||
std::shared_ptr<BttvLiveUpdateClient> BttvLiveUpdatesPrivate::anyClient()
|
||||
{
|
||||
if (this->clients().empty())
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
return this->clients().begin()->second;
|
||||
}
|
||||
|
||||
BttvLiveUpdates::BttvLiveUpdates(QString host)
|
||||
: private_(std::make_unique<BttvLiveUpdatesPrivate>(*this, std::move(host)))
|
||||
{
|
||||
@@ -77,6 +87,16 @@ void BttvLiveUpdates::partChannel(const QString &id)
|
||||
}
|
||||
}
|
||||
|
||||
void BttvLiveUpdates::broadcastMe(const QString &channelID,
|
||||
const QString &userID)
|
||||
{
|
||||
auto client = this->private_->anyClient();
|
||||
if (client)
|
||||
{
|
||||
client->broadcastMe(channelID, userID);
|
||||
}
|
||||
}
|
||||
|
||||
void BttvLiveUpdates::stop()
|
||||
{
|
||||
this->private_->stop();
|
||||
|
||||
@@ -39,6 +39,8 @@ public:
|
||||
*/
|
||||
void joinChannel(const QString &channelID, const QString &userID);
|
||||
|
||||
void broadcastMe(const QString &channelID, const QString &userID);
|
||||
|
||||
/**
|
||||
* Parts a twitch channel by its id (without any prefix like 'twitch:')
|
||||
* if it's joined.
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
#include "providers/bttv/liveupdates/BttvLiveUpdateClient.hpp"
|
||||
|
||||
#include "Application.hpp"
|
||||
#include "providers/bttv/BttvBadges.hpp"
|
||||
#include "providers/bttv/BttvLiveUpdates.hpp"
|
||||
#include "providers/bttv/liveupdates/BttvLiveUpdateMessages.hpp"
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonValue>
|
||||
#include <QStringBuilder>
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
@@ -67,7 +70,25 @@ void BttvLiveUpdateClient::onMessage(const QByteArray &msg)
|
||||
}
|
||||
else if (eventType == "lookup_user")
|
||||
{
|
||||
// ignored
|
||||
auto message = BttvLiveUpdateUserUpdateMessage(eventData);
|
||||
if (!message.validate())
|
||||
{
|
||||
qCDebug(chatterinoBttv) << "Invalid user update message" << json;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!message.hasBadge())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
auto *app = tryGetApp();
|
||||
if (app)
|
||||
{
|
||||
auto *bttvBadges = app->getBttvBadges();
|
||||
auto badgeID = bttvBadges->registerBadge(message.badgeObject);
|
||||
bttvBadges->assignBadgeToUser(badgeID, UserId{message.userID});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -75,4 +96,21 @@ void BttvLiveUpdateClient::onMessage(const QByteArray &msg)
|
||||
}
|
||||
}
|
||||
|
||||
void BttvLiveUpdateClient::broadcastMe(const QString &channelID,
|
||||
const QString &userID)
|
||||
{
|
||||
QJsonObject obj{
|
||||
{"name", "broadcast_me"},
|
||||
{"data",
|
||||
QJsonObject{
|
||||
{"provider", "twitch"},
|
||||
{"providerId", userID},
|
||||
{"channel", QString(u"twitch:" % channelID)},
|
||||
}},
|
||||
};
|
||||
QJsonDocument doc(obj);
|
||||
|
||||
this->sendText(doc.toJson(QJsonDocument::Compact));
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
@@ -15,6 +15,8 @@ public:
|
||||
|
||||
void onMessage(const QByteArray &msg) /* override */;
|
||||
|
||||
void broadcastMe(const QString &channelID, const QString &userID);
|
||||
|
||||
private:
|
||||
BttvLiveUpdates &manager;
|
||||
};
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace Qt::Literals;
|
||||
|
||||
bool tryParseChannelId(QString &channelId)
|
||||
{
|
||||
if (!channelId.startsWith("twitch:"))
|
||||
@@ -49,4 +51,21 @@ bool BttvLiveUpdateEmoteRemoveMessage::validate() const
|
||||
!this->channelID.isEmpty();
|
||||
}
|
||||
|
||||
BttvLiveUpdateUserUpdateMessage::BttvLiveUpdateUserUpdateMessage(
|
||||
const QJsonObject &json)
|
||||
: userID(json["providerId"_L1].toString())
|
||||
, badgeObject(json["badge"_L1].toObject())
|
||||
{
|
||||
}
|
||||
|
||||
bool BttvLiveUpdateUserUpdateMessage::validate() const
|
||||
{
|
||||
return !this->userID.isEmpty();
|
||||
}
|
||||
|
||||
bool BttvLiveUpdateUserUpdateMessage::hasBadge() const
|
||||
{
|
||||
return !this->badgeObject.isEmpty();
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
@@ -35,4 +35,14 @@ private:
|
||||
bool badChannelID_;
|
||||
};
|
||||
|
||||
struct BttvLiveUpdateUserUpdateMessage {
|
||||
BttvLiveUpdateUserUpdateMessage(const QJsonObject &json);
|
||||
|
||||
QString userID;
|
||||
QJsonObject badgeObject;
|
||||
|
||||
bool validate() const;
|
||||
bool hasBadge() const;
|
||||
};
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
Reference in New Issue
Block a user