mirror-chatterino2/src/providers/twitch/twitchchannel.cpp

258 lines
7.3 KiB
C++
Raw Normal View History

2018-02-05 15:11:50 +01:00
#include "providers/twitch/twitchchannel.hpp"
#include "common.hpp"
#include "debug/log.hpp"
2018-01-23 23:28:06 +01:00
#include "messages/message.hpp"
2018-02-05 15:11:50 +01:00
#include "providers/twitch/twitchmessagebuilder.hpp"
2018-01-23 23:28:06 +01:00
#include "singletons/channelmanager.hpp"
2017-12-31 00:50:07 +01:00
#include "singletons/emotemanager.hpp"
2018-01-17 18:36:12 +01:00
#include "singletons/ircmanager.hpp"
2018-01-22 20:14:43 +01:00
#include "singletons/settingsmanager.hpp"
#include "util/urlfetch.hpp"
2018-02-05 15:11:50 +01:00
#include <IrcConnection>
#include <QThread>
#include <QTimer>
namespace chatterino {
2018-02-05 15:11:50 +01:00
namespace providers {
namespace twitch {
2018-02-05 15:11:50 +01:00
TwitchChannel::TwitchChannel(const QString &channelName, Communi::IrcConnection *_readConnection)
: Channel(channelName)
2017-12-31 22:58:35 +01:00
, bttvChannelEmotes(new util::EmoteMap)
, ffzChannelEmotes(new util::EmoteMap)
2017-12-28 17:50:28 +01:00
, subscriptionURL("https://www.twitch.tv/subs/" + name)
, channelURL("https://twitch.tv/" + name)
, popoutPlayerURL("https://player.twitch.tv/?channel=" + name)
, isLive(false)
2018-01-17 18:36:12 +01:00
, mod(false)
2018-02-05 15:11:50 +01:00
, readConnecetion(_readConnection)
{
debug::Log("[TwitchChannel:{}] Opened", this->name);
2017-12-31 00:50:07 +01:00
this->reloadChannelEmotes();
this->liveStatusTimer = new QTimer;
QObject::connect(this->liveStatusTimer, &QTimer::timeout, [this]() {
this->refreshLiveStatus(); //
});
this->liveStatusTimer->start(60000);
this->roomIDchanged.connect([this]() {
this->refreshLiveStatus(); //
});
this->fetchMessages.connect([this] {
this->fetchRecentMessages(); //
});
2018-01-17 18:36:12 +01:00
this->messageSuffix.append(' ');
this->messageSuffix.append(QChar(0x206D));
}
TwitchChannel::~TwitchChannel()
{
2018-01-17 18:36:12 +01:00
this->connectedConnection.disconnect();
this->liveStatusTimer->stop();
this->liveStatusTimer->deleteLater();
}
bool TwitchChannel::isEmpty() const
{
return this->name.isEmpty();
}
bool TwitchChannel::canSendMessage() const
{
2017-12-31 00:50:07 +01:00
return !this->isEmpty();
}
void TwitchChannel::setRoomID(const QString &_roomID)
{
this->roomID = _roomID;
this->roomIDchanged();
this->fetchMessages.invoke();
}
void TwitchChannel::reloadChannelEmotes()
{
2017-12-31 22:58:35 +01:00
auto &emoteManager = singletons::EmoteManager::getInstance();
2017-12-17 02:18:13 +01:00
debug::Log("[TwitchChannel:{}] Reloading channel emotes", this->name);
2017-12-17 02:18:13 +01:00
emoteManager.reloadBTTVChannelEmotes(this->name, this->bttvChannelEmotes);
emoteManager.reloadFFZChannelEmotes(this->name, this->ffzChannelEmotes);
}
void TwitchChannel::sendMessage(const QString &message)
{
2017-12-31 22:58:35 +01:00
auto &emoteManager = singletons::EmoteManager::getInstance();
2017-12-17 02:18:13 +01:00
debug::Log("[TwitchChannel:{}] Send message: {}", this->name, message);
// Do last message processing
2017-12-17 02:18:13 +01:00
QString parsedMessage = emoteManager.replaceShortCodes(message);
parsedMessage = parsedMessage.trimmed();
2018-02-05 15:11:50 +01:00
if (parsedMessage.isEmpty()) {
return;
2018-02-05 15:11:50 +01:00
}
if (singletons::SettingManager::getInstance().allowDuplicateMessages) {
if (parsedMessage == this->lastSentMessage) {
parsedMessage.append(this->messageSuffix);
this->lastSentMessage = "";
2018-02-11 21:13:23 +01:00
}
}
2018-02-11 21:13:23 +01:00
this->lastSentMessage = parsedMessage;
2018-02-05 15:11:50 +01:00
this->sendMessageSignal.invoke(this->name, parsedMessage);
}
bool TwitchChannel::isMod() const
2018-01-17 17:17:26 +01:00
{
return this->mod;
}
2018-01-17 18:36:12 +01:00
void TwitchChannel::setMod(bool value)
{
if (this->mod != value) {
this->mod = value;
this->userStateChanged();
}
}
2018-01-17 17:17:26 +01:00
bool TwitchChannel::isBroadcaster()
{
2018-01-17 18:36:12 +01:00
return this->name ==
singletons::AccountManager::getInstance().Twitch.getCurrent()->getUserName();
2018-01-17 17:17:26 +01:00
}
bool TwitchChannel::hasModRights()
{
// fourtf: check if staff
return this->isMod() || this->isBroadcaster();
}
void TwitchChannel::setLive(bool newLiveStatus)
{
if (this->isLive == newLiveStatus) {
return;
}
this->isLive = newLiveStatus;
this->onlineStatusChanged();
}
void TwitchChannel::refreshLiveStatus()
{
if (this->roomID.isEmpty()) {
this->setLive(false);
return;
}
debug::Log("[TwitchChannel:{}] Refreshing live status", this->name);
QString url("https://api.twitch.tv/kraken/streams/" + this->roomID);
std::weak_ptr<Channel> weak = this->shared_from_this();
2018-01-23 21:40:51 +01:00
util::twitch::get2(url, QThread::currentThread(), false, [weak](const rapidjson::Document &d) {
ChannelPtr shared = weak.lock();
if (!shared) {
return;
}
TwitchChannel *channel = dynamic_cast<TwitchChannel *>(shared.get());
if (!d.IsObject()) {
debug::Log("[TwitchChannel:refreshLiveStatus] root is not an object");
return;
}
if (!d.HasMember("stream")) {
debug::Log("[TwitchChannel:refreshLiveStatus] Missing stream in root");
return;
}
const auto &stream = d["stream"];
if (!stream.IsObject()) {
// Stream is offline (stream is most likely null)
channel->setLive(false);
return;
}
if (!stream.HasMember("viewers") || !stream.HasMember("game") ||
!stream.HasMember("channel") || !stream.HasMember("created_at")) {
debug::Log("[TwitchChannel:refreshLiveStatus] Missing members in stream");
channel->setLive(false);
return;
}
const rapidjson::Value &streamChannel = stream["channel"];
if (!streamChannel.IsObject() || !streamChannel.HasMember("status")) {
debug::Log("[TwitchChannel:refreshLiveStatus] Missing member \"status\" in channel");
return;
}
// Stream is live
channel->streamViewerCount = QString::number(stream["viewers"].GetInt());
channel->streamGame = stream["game"].GetString();
channel->streamStatus = streamChannel["status"].GetString();
QDateTime since = QDateTime::fromString(stream["created_at"].GetString(), Qt::ISODate);
auto diff = since.secsTo(QDateTime::currentDateTime());
channel->streamUptime =
QString::number(diff / 3600) + "h " + QString::number(diff % 3600 / 60) + "m";
channel->setLive(true);
});
}
void TwitchChannel::fetchRecentMessages()
{
static QString genericURL =
"https://tmi.twitch.tv/api/rooms/%1/recent_messages?client_id=" + getDefaultClientID();
std::weak_ptr<Channel> weak = this->shared_from_this();
util::twitch::get(genericURL.arg(roomID), QThread::currentThread(), [weak](QJsonObject obj) {
ChannelPtr shared = weak.lock();
if (!shared) {
return;
}
TwitchChannel *channel = dynamic_cast<TwitchChannel *>(shared.get());
2018-02-05 15:11:50 +01:00
static auto readConnection = channel->readConnecetion;
auto msgArray = obj.value("messages").toArray();
if (msgArray.size() > 0) {
std::vector<messages::MessagePtr> messages;
for (int i = 0; i < msgArray.size(); i++) {
QByteArray content = msgArray[i].toString().toUtf8();
auto msg = Communi::IrcMessage::fromData(content, readConnection);
auto privMsg = static_cast<Communi::IrcPrivateMessage *>(msg);
messages::MessageParseArgs args;
twitch::TwitchMessageBuilder builder(channel, privMsg, args);
2018-01-23 21:33:49 +01:00
if (!builder.isIgnored()) {
2018-01-23 23:28:06 +01:00
messages.push_back(builder.build());
2018-01-23 21:33:49 +01:00
}
}
channel->addMessagesAtStart(messages);
}
});
}
} // namespace twitch
2018-02-05 15:11:50 +01:00
} // namespace providers
} // namespace chatterino