2017-09-16 00:05:06 +02:00
|
|
|
#include "twitchchannel.hpp"
|
2017-11-04 14:57:29 +01:00
|
|
|
#include "debug/log.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-01 22:29:21 +01:00
|
|
|
#include "twitch/twitchmessagebuilder.hpp"
|
2017-11-04 14:57:29 +01:00
|
|
|
#include "util/urlfetch.hpp"
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
#include <QThread>
|
|
|
|
#include <QTimer>
|
2017-09-16 00:05:06 +02:00
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace twitch {
|
|
|
|
|
2017-12-31 00:50:07 +01:00
|
|
|
TwitchChannel::TwitchChannel(const QString &channelName)
|
2017-11-04 14:57:29 +01:00
|
|
|
: 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)
|
2017-11-04 14:57:29 +01:00
|
|
|
, channelURL("https://twitch.tv/" + name)
|
|
|
|
, popoutPlayerURL("https://player.twitch.tv/?channel=" + name)
|
2017-09-16 00:05:06 +02:00
|
|
|
, isLive(false)
|
2018-01-17 18:36:12 +01:00
|
|
|
, mod(false)
|
2017-09-16 00:05:06 +02:00
|
|
|
{
|
2017-11-04 14:57:29 +01:00
|
|
|
debug::Log("[TwitchChannel:{}] Opened", this->name);
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2017-12-31 00:50:07 +01:00
|
|
|
this->reloadChannelEmotes();
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
this->liveStatusTimer = new QTimer;
|
|
|
|
QObject::connect(this->liveStatusTimer, &QTimer::timeout, [this]() {
|
|
|
|
this->refreshLiveStatus(); //
|
|
|
|
});
|
|
|
|
this->liveStatusTimer->start(60000);
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
this->roomIDchanged.connect([this]() {
|
|
|
|
this->refreshLiveStatus(); //
|
|
|
|
});
|
2017-12-28 00:03:52 +01:00
|
|
|
|
|
|
|
this->fetchMessages.connect([this] {
|
2017-12-28 17:47:00 +01:00
|
|
|
this->fetchRecentMessages(); //
|
2017-12-28 00:03:52 +01:00
|
|
|
});
|
2018-01-17 18:36:12 +01:00
|
|
|
|
|
|
|
this->connectedConnection = singletons::IrcManager::getInstance().connected.connect(
|
|
|
|
[this] { this->userStateChanged(); });
|
2018-01-22 15:24:39 +01:00
|
|
|
|
|
|
|
this->messageSuffix.append(' ');
|
|
|
|
this->messageSuffix.append(QChar(0x206D));
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
TwitchChannel::~TwitchChannel()
|
2017-09-16 00:05:06 +02:00
|
|
|
{
|
2018-01-17 18:36:12 +01:00
|
|
|
this->connectedConnection.disconnect();
|
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
this->liveStatusTimer->stop();
|
|
|
|
this->liveStatusTimer->deleteLater();
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
bool TwitchChannel::isEmpty() const
|
2017-09-16 00:05:06 +02:00
|
|
|
{
|
2017-11-04 14:57:29 +01:00
|
|
|
return this->name.isEmpty();
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
bool TwitchChannel::canSendMessage() const
|
2017-09-16 00:05:06 +02:00
|
|
|
{
|
2017-12-31 00:50:07 +01:00
|
|
|
return !this->isEmpty();
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
void TwitchChannel::setRoomID(const QString &_roomID)
|
2017-09-16 00:05:06 +02:00
|
|
|
{
|
2017-11-04 14:57:29 +01:00
|
|
|
this->roomID = _roomID;
|
2017-09-16 00:05:06 +02:00
|
|
|
this->roomIDchanged();
|
2017-12-28 00:03:52 +01:00
|
|
|
this->fetchMessages.invoke();
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchChannel::reloadChannelEmotes()
|
|
|
|
{
|
2017-12-31 22:58:35 +01:00
|
|
|
auto &emoteManager = singletons::EmoteManager::getInstance();
|
2017-12-17 02:18:13 +01:00
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
debug::Log("[TwitchChannel:{}] Reloading channel emotes", this->name);
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2017-12-17 02:18:13 +01:00
|
|
|
emoteManager.reloadBTTVChannelEmotes(this->name, this->bttvChannelEmotes);
|
|
|
|
emoteManager.reloadFFZChannelEmotes(this->name, this->ffzChannelEmotes);
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
debug::Log("[TwitchChannel:{}] Send message: {}", this->name, message);
|
2017-09-16 00:05:06 +02:00
|
|
|
|
|
|
|
// Do last message processing
|
2017-12-17 02:18:13 +01:00
|
|
|
QString parsedMessage = emoteManager.replaceShortCodes(message);
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2018-01-22 15:24:39 +01:00
|
|
|
parsedMessage = parsedMessage.trimmed();
|
|
|
|
|
|
|
|
if (parsedMessage.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (singletons::SettingManager::getInstance().allowDuplicateMessages) {
|
|
|
|
if (parsedMessage == this->lastSentMessage) {
|
|
|
|
parsedMessage.append(this->messageSuffix);
|
|
|
|
|
|
|
|
this->lastSentMessage = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
singletons::IrcManager::getInstance().sendMessage(this->name, parsedMessage);
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
2017-11-04 14:57:29 +01:00
|
|
|
|
2018-01-18 18:17:48 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
void TwitchChannel::setLive(bool newLiveStatus)
|
|
|
|
{
|
|
|
|
if (this->isLive == newLiveStatus) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->isLive = newLiveStatus;
|
|
|
|
this->onlineStatusChanged();
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
2017-11-04 14:57:29 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2018-01-05 00:58:25 +01:00
|
|
|
std::weak_ptr<Channel> weak = this->shared_from_this();
|
|
|
|
|
2018-01-19 22:45:33 +01:00
|
|
|
util::twitch::get2(url, QThread::currentThread(), [weak](const rapidjson::Document &d) {
|
2018-01-11 20:16:25 +01:00
|
|
|
SharedChannel shared = weak.lock();
|
2018-01-05 00:58:25 +01:00
|
|
|
|
|
|
|
if (!shared) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TwitchChannel *channel = dynamic_cast<TwitchChannel *>(shared.get());
|
|
|
|
|
2017-12-28 17:47:00 +01:00
|
|
|
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)
|
2018-01-05 00:58:25 +01:00
|
|
|
channel->setLive(false);
|
2017-12-28 17:47:00 +01:00
|
|
|
return;
|
2017-11-04 14:57:29 +01:00
|
|
|
}
|
2017-12-28 17:47:00 +01:00
|
|
|
|
|
|
|
if (!stream.HasMember("viewers") || !stream.HasMember("game") ||
|
|
|
|
!stream.HasMember("channel") || !stream.HasMember("created_at")) {
|
|
|
|
debug::Log("[TwitchChannel:refreshLiveStatus] Missing members in stream");
|
2018-01-05 00:58:25 +01:00
|
|
|
channel->setLive(false);
|
2017-12-28 17:47:00 +01:00
|
|
|
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
|
2018-01-05 00:58:25 +01:00
|
|
|
channel->streamViewerCount = QString::number(stream["viewers"].GetInt());
|
|
|
|
channel->streamGame = stream["game"].GetString();
|
|
|
|
channel->streamStatus = streamChannel["status"].GetString();
|
2017-12-28 17:47:00 +01:00
|
|
|
QDateTime since = QDateTime::fromString(stream["created_at"].GetString(), Qt::ISODate);
|
|
|
|
auto diff = since.secsTo(QDateTime::currentDateTime());
|
2018-01-05 00:58:25 +01:00
|
|
|
channel->streamUptime =
|
2017-12-28 17:47:00 +01:00
|
|
|
QString::number(diff / 3600) + "h " + QString::number(diff % 3600 / 60) + "m";
|
|
|
|
|
2018-01-05 00:58:25 +01:00
|
|
|
channel->setLive(true);
|
2017-11-04 14:57:29 +01:00
|
|
|
});
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
2017-11-04 14:57:29 +01:00
|
|
|
|
2017-12-28 00:03:52 +01:00
|
|
|
void TwitchChannel::fetchRecentMessages()
|
|
|
|
{
|
|
|
|
static QString genericURL =
|
|
|
|
"https://tmi.twitch.tv/api/rooms/%1/recent_messages?client_id=" + getDefaultClientID();
|
|
|
|
|
2018-01-05 00:58:25 +01:00
|
|
|
std::weak_ptr<Channel> weak = this->shared_from_this();
|
|
|
|
|
|
|
|
util::twitch::get(genericURL.arg(roomID), QThread::currentThread(), [weak](QJsonObject obj) {
|
2018-01-11 20:16:25 +01:00
|
|
|
SharedChannel shared = weak.lock();
|
2018-01-05 00:58:25 +01:00
|
|
|
|
|
|
|
if (!shared) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TwitchChannel *channel = dynamic_cast<TwitchChannel *>(shared.get());
|
|
|
|
static auto readConnection = singletons::IrcManager::getInstance().getReadConnection();
|
|
|
|
|
2017-12-28 00:03:52 +01:00
|
|
|
auto msgArray = obj.value("messages").toArray();
|
2018-01-01 22:29:21 +01:00
|
|
|
if (msgArray.size() > 0) {
|
2018-01-11 20:16:25 +01:00
|
|
|
std::vector<messages::MessagePtr> messages;
|
2018-01-01 22:29:21 +01:00
|
|
|
messages.resize(msgArray.size());
|
|
|
|
|
2017-12-28 00:03:52 +01:00
|
|
|
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);
|
2018-01-01 22:29:21 +01:00
|
|
|
|
|
|
|
messages::MessageParseArgs args;
|
2018-01-05 00:58:25 +01:00
|
|
|
twitch::TwitchMessageBuilder builder(channel, privMsg, args);
|
2018-01-01 22:29:21 +01:00
|
|
|
messages.at(i) = builder.parse();
|
2017-12-28 00:03:52 +01:00
|
|
|
}
|
2018-01-05 00:58:25 +01:00
|
|
|
channel->addMessagesAtStart(messages);
|
2018-01-01 22:29:21 +01:00
|
|
|
}
|
2017-12-28 00:03:52 +01:00
|
|
|
});
|
|
|
|
}
|
2017-11-04 14:57:29 +01:00
|
|
|
} // namespace twitch
|
|
|
|
} // namespace chatterino
|