2018-06-26 14:09:39 +02:00
|
|
|
#include "providers/twitch/TwitchChannel.hpp"
|
2018-04-28 15:48:40 +02:00
|
|
|
|
2018-06-26 15:33:51 +02:00
|
|
|
#include "common/Common.hpp"
|
2018-06-26 17:20:03 +02:00
|
|
|
#include "common/UrlFetch.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "debug/Log.hpp"
|
|
|
|
#include "messages/Message.hpp"
|
|
|
|
#include "providers/twitch/Pubsub.hpp"
|
|
|
|
#include "providers/twitch/TwitchMessageBuilder.hpp"
|
2018-06-28 19:46:45 +02:00
|
|
|
#include "singletons/Emotes.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "singletons/IrcManager.hpp"
|
2018-06-28 19:46:45 +02:00
|
|
|
#include "singletons/Settings.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "util/PostToThread.hpp"
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
#include <IrcConnection>
|
2017-11-04 14:57:29 +01:00
|
|
|
#include <QThread>
|
|
|
|
#include <QTimer>
|
2017-09-16 00:05:06 +02:00
|
|
|
|
|
|
|
namespace chatterino {
|
2018-03-24 12:02:07 +01:00
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
TwitchChannel::TwitchChannel(const QString &channelName, Communi::IrcConnection *_readConnection)
|
2018-04-18 09:12:29 +02:00
|
|
|
: Channel(channelName, Channel::Twitch)
|
2018-06-26 17:06:17 +02:00
|
|
|
, bttvChannelEmotes(new EmoteMap)
|
|
|
|
, ffzChannelEmotes(new 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)
|
2018-01-17 18:36:12 +01:00
|
|
|
, mod(false)
|
2018-03-31 13:44:15 +02:00
|
|
|
, readConnection(_readConnection)
|
2017-09-16 00:05:06 +02:00
|
|
|
{
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("[TwitchChannel:{}] Opened", this->name);
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2018-06-11 11:51:46 +02:00
|
|
|
this->startRefreshLiveStatusTimer(60 * 1000);
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
auto app = getApp();
|
2017-12-31 00:50:07 +01:00
|
|
|
this->reloadChannelEmotes();
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2018-05-26 20:26:25 +02:00
|
|
|
this->managedConnect(app->accounts->twitch.currentUserChanged,
|
2018-05-17 12:16:13 +02:00
|
|
|
[this]() { this->setMod(false); });
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
auto refreshPubSubState = [=]() {
|
2018-04-15 15:09:31 +02:00
|
|
|
if (!this->hasModRights()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->roomID.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-26 20:26:25 +02:00
|
|
|
auto account = app->accounts->twitch.getCurrent();
|
2018-04-15 15:09:31 +02:00
|
|
|
if (account && !account->getUserId().isEmpty()) {
|
2018-04-28 16:07:18 +02:00
|
|
|
app->twitch.pubsub->listenToChannelModerationActions(this->roomID, account);
|
2018-04-15 15:09:31 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this->userStateChanged.connect(refreshPubSubState);
|
|
|
|
this->roomIDchanged.connect(refreshPubSubState);
|
2018-05-26 20:26:25 +02:00
|
|
|
this->managedConnect(app->accounts->twitch.currentUserChanged, refreshPubSubState);
|
2018-04-15 15:09:31 +02:00
|
|
|
refreshPubSubState();
|
|
|
|
|
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
|
|
|
|
2018-01-22 15:24:39 +01:00
|
|
|
this->messageSuffix.append(' ');
|
|
|
|
this->messageSuffix.append(QChar(0x206D));
|
2018-03-24 12:02:07 +01:00
|
|
|
|
|
|
|
static QStringList jsonLabels = {"moderators", "staff", "admins", "global_mods", "viewers"};
|
|
|
|
auto refreshChatters = [=](QJsonObject obj) {
|
|
|
|
QJsonObject chattersObj = obj.value("chatters").toObject();
|
|
|
|
for (int i = 0; i < jsonLabels.size(); i++) {
|
|
|
|
foreach (const QJsonValue &v, chattersObj.value(jsonLabels.at(i)).toArray()) {
|
|
|
|
this->completionModel.addUser(v.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
auto doRefreshChatters = [=]() {
|
2018-05-24 08:58:34 +02:00
|
|
|
const auto streamStatus = this->getStreamStatus();
|
2018-03-30 15:42:59 +02:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
if (app->settings->onlyFetchChattersForSmallerStreamers) {
|
|
|
|
if (streamStatus.live && streamStatus.viewerCount > app->settings->smallStreamerLimit) {
|
2018-03-30 15:42:59 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 17:12:50 +02:00
|
|
|
twitchApiGet("https://tmi.twitch.tv/group/user/" + this->name + "/chatters",
|
2018-06-26 17:20:03 +02:00
|
|
|
QThread::currentThread(), refreshChatters);
|
2018-03-24 12:02:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
doRefreshChatters();
|
|
|
|
|
|
|
|
this->chattersListTimer = new QTimer;
|
|
|
|
QObject::connect(this->chattersListTimer, &QTimer::timeout, doRefreshChatters);
|
|
|
|
this->chattersListTimer->start(5 * 60 * 1000);
|
2018-05-17 12:16:13 +02:00
|
|
|
|
2018-06-19 20:34:50 +02:00
|
|
|
#if 0
|
|
|
|
for (int i = 0; i < 1000; i++) {
|
2018-06-28 19:38:57 +02:00
|
|
|
this->addMessage(Message::createSystemMessage("asdf"));
|
2018-06-19 20:34:50 +02:00
|
|
|
}
|
|
|
|
#endif
|
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
|
|
|
{
|
2017-11-04 14:57:29 +01:00
|
|
|
this->liveStatusTimer->stop();
|
|
|
|
this->liveStatusTimer->deleteLater();
|
2018-03-24 12:02:07 +01:00
|
|
|
|
|
|
|
this->chattersListTimer->stop();
|
|
|
|
this->chattersListTimer->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;
|
2018-04-03 02:55:32 +02:00
|
|
|
this->roomIDchanged.invoke();
|
2017-12-28 00:03:52 +01:00
|
|
|
this->fetchMessages.invoke();
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchChannel::reloadChannelEmotes()
|
|
|
|
{
|
2018-04-27 22:11:19 +02:00
|
|
|
auto app = getApp();
|
2017-12-17 02:18:13 +01:00
|
|
|
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("[TwitchChannel:{}] Reloading channel emotes", this->name);
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2018-06-05 17:39:49 +02:00
|
|
|
app->emotes->bttv.loadChannelEmotes(this->name, this->bttvChannelEmotes);
|
2018-06-05 18:07:17 +02:00
|
|
|
app->emotes->ffz.loadChannelEmotes(this->name, this->ffzChannelEmotes);
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchChannel::sendMessage(const QString &message)
|
|
|
|
{
|
2018-04-27 22:11:19 +02:00
|
|
|
auto app = getApp();
|
2017-12-17 02:18:13 +01:00
|
|
|
|
2018-06-24 15:09:56 +02:00
|
|
|
if (!app->accounts->twitch.isLoggedIn()) {
|
|
|
|
// XXX: It would be nice if we could add a link here somehow that opened the "account
|
|
|
|
// manager" dialog
|
|
|
|
this->addMessage(
|
2018-06-28 19:38:57 +02:00
|
|
|
Message::createSystemMessage("You need to log in to send messages. You can "
|
2018-06-26 17:20:03 +02:00
|
|
|
"link your Twitch account in the settings."));
|
2018-06-24 15:09:56 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("[TwitchChannel:{}] Send message: {}", this->name, message);
|
2017-09-16 00:05:06 +02:00
|
|
|
|
|
|
|
// Do last message processing
|
2018-06-05 18:53:49 +02:00
|
|
|
QString parsedMessage = app->emotes->emojis.replaceShortCodes(message);
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2018-01-22 15:24:39 +01:00
|
|
|
parsedMessage = parsedMessage.trimmed();
|
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
if (parsedMessage.isEmpty()) {
|
2018-01-22 15:24:39 +01:00
|
|
|
return;
|
2018-02-05 15:11:50 +01:00
|
|
|
}
|
2018-01-22 15:24:39 +01:00
|
|
|
|
2018-06-24 17:20:15 +02:00
|
|
|
if (!this->hasModRights()) {
|
|
|
|
if (app->settings->allowDuplicateMessages) {
|
|
|
|
if (parsedMessage == this->lastSentMessage) {
|
|
|
|
parsedMessage.append(this->messageSuffix);
|
|
|
|
}
|
2018-03-24 12:02:07 +01:00
|
|
|
}
|
2018-01-22 15:24:39 +01:00
|
|
|
}
|
|
|
|
|
2018-06-06 18:57:22 +02:00
|
|
|
bool messageSent = false;
|
|
|
|
this->sendMessageSignal.invoke(this->name, parsedMessage, messageSent);
|
2018-02-11 21:13:23 +01:00
|
|
|
|
2018-06-06 18:57:22 +02:00
|
|
|
if (messageSent) {
|
|
|
|
qDebug() << "sent";
|
|
|
|
this->lastSentMessage = 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;
|
|
|
|
|
2018-04-03 02:55:32 +02:00
|
|
|
this->userStateChanged.invoke();
|
2018-01-17 18:36:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:19:52 +02:00
|
|
|
bool TwitchChannel::isBroadcaster() const
|
2018-01-17 17:17:26 +01:00
|
|
|
{
|
2018-04-27 22:11:19 +02:00
|
|
|
auto app = getApp();
|
|
|
|
|
2018-05-26 20:26:25 +02:00
|
|
|
return this->name == app->accounts->twitch.getCurrent()->getUserName();
|
2018-01-17 17:17:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TwitchChannel::hasModRights()
|
|
|
|
{
|
|
|
|
// fourtf: check if staff
|
|
|
|
return this->isMod() || this->isBroadcaster();
|
|
|
|
}
|
|
|
|
|
2018-06-28 19:38:57 +02:00
|
|
|
void TwitchChannel::addRecentChatter(const std::shared_ptr<Message> &message)
|
2018-03-30 12:16:12 +02:00
|
|
|
{
|
|
|
|
assert(!message->loginName.isEmpty());
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(this->recentChattersMutex);
|
|
|
|
|
|
|
|
this->recentChatters[message->loginName] = {message->displayName, message->localizedName};
|
|
|
|
|
|
|
|
this->completionModel.addUser(message->displayName);
|
|
|
|
}
|
|
|
|
|
2018-05-26 18:06:55 +02:00
|
|
|
void TwitchChannel::addJoinedUser(const QString &user)
|
|
|
|
{
|
|
|
|
auto *app = getApp();
|
2018-05-26 20:26:25 +02:00
|
|
|
if (user == app->accounts->twitch.getCurrent()->getUserName() ||
|
2018-05-26 18:06:55 +02:00
|
|
|
!app->settings->showJoins.getValue()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> guard(this->joinedUserMutex);
|
|
|
|
|
|
|
|
joinedUsers << user;
|
|
|
|
|
|
|
|
if (!this->joinedUsersMergeQueued) {
|
|
|
|
this->joinedUsersMergeQueued = true;
|
|
|
|
|
|
|
|
QTimer::singleShot(500, &this->object, [this] {
|
|
|
|
std::lock_guard<std::mutex> guard(this->joinedUserMutex);
|
|
|
|
|
2018-06-28 19:38:57 +02:00
|
|
|
auto message = Message::createSystemMessage("Users joined: " +
|
2018-06-26 17:20:03 +02:00
|
|
|
this->joinedUsers.join(", "));
|
2018-06-28 19:38:57 +02:00
|
|
|
message->flags |= Message::Collapsed;
|
2018-05-31 16:02:20 +02:00
|
|
|
this->addMessage(message);
|
2018-05-27 13:37:49 +02:00
|
|
|
this->joinedUsers.clear();
|
2018-05-26 18:06:55 +02:00
|
|
|
this->joinedUsersMergeQueued = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchChannel::addPartedUser(const QString &user)
|
|
|
|
{
|
|
|
|
auto *app = getApp();
|
|
|
|
|
2018-05-26 20:26:25 +02:00
|
|
|
if (user == app->accounts->twitch.getCurrent()->getUserName() ||
|
2018-05-26 18:06:55 +02:00
|
|
|
!app->settings->showJoins.getValue()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> guard(this->partedUserMutex);
|
|
|
|
|
|
|
|
partedUsers << user;
|
|
|
|
|
|
|
|
if (!this->partedUsersMergeQueued) {
|
|
|
|
this->partedUsersMergeQueued = true;
|
|
|
|
|
|
|
|
QTimer::singleShot(500, &this->object, [this] {
|
|
|
|
std::lock_guard<std::mutex> guard(this->partedUserMutex);
|
|
|
|
|
2018-06-28 19:38:57 +02:00
|
|
|
auto message = Message::createSystemMessage("Users parted: " +
|
2018-06-26 17:20:03 +02:00
|
|
|
this->partedUsers.join(", "));
|
2018-06-28 19:38:57 +02:00
|
|
|
message->flags |= Message::Collapsed;
|
2018-05-31 16:02:20 +02:00
|
|
|
this->addMessage(message);
|
2018-05-27 13:37:49 +02:00
|
|
|
this->partedUsers.clear();
|
2018-05-26 18:06:55 +02:00
|
|
|
|
|
|
|
this->partedUsersMergeQueued = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 08:58:34 +02:00
|
|
|
TwitchChannel::RoomModes TwitchChannel::getRoomModes()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(this->roomModeMutex);
|
|
|
|
|
|
|
|
return this->roomModes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchChannel::setRoomModes(const RoomModes &_roomModes)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(this->roomModeMutex);
|
|
|
|
this->roomModes = _roomModes;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->roomModesChanged.invoke();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TwitchChannel::isLive() const
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(this->streamStatusMutex);
|
|
|
|
return this->streamStatus.live;
|
|
|
|
}
|
|
|
|
|
|
|
|
TwitchChannel::StreamStatus TwitchChannel::getStreamStatus() const
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(this->streamStatusMutex);
|
|
|
|
return this->streamStatus;
|
|
|
|
}
|
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
void TwitchChannel::setLive(bool newLiveStatus)
|
|
|
|
{
|
2018-04-14 18:27:13 +02:00
|
|
|
bool gotNewLiveStatus = false;
|
2018-03-30 15:05:33 +02:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(this->streamStatusMutex);
|
2018-04-08 14:45:47 +02:00
|
|
|
if (this->streamStatus.live != newLiveStatus) {
|
2018-04-14 18:27:13 +02:00
|
|
|
gotNewLiveStatus = true;
|
2018-04-08 14:45:47 +02:00
|
|
|
this->streamStatus.live = newLiveStatus;
|
2018-03-30 15:05:33 +02:00
|
|
|
}
|
2017-11-04 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2018-04-14 18:27:13 +02:00
|
|
|
if (gotNewLiveStatus) {
|
2018-04-08 14:45:47 +02:00
|
|
|
this->updateLiveInfo.invoke();
|
|
|
|
}
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
2017-11-04 14:57:29 +01:00
|
|
|
|
|
|
|
void TwitchChannel::refreshLiveStatus()
|
|
|
|
{
|
|
|
|
if (this->roomID.isEmpty()) {
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("[TwitchChannel:{}] Refreshing live status (Missing ID)", this->name);
|
2017-11-04 14:57:29 +01:00
|
|
|
this->setLive(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("[TwitchChannel:{}] Refreshing live status", this->name);
|
2017-11-04 14:57:29 +01:00
|
|
|
|
|
|
|
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-06-26 17:12:50 +02:00
|
|
|
twitchApiGet2(url, QThread::currentThread(), false, [weak](const rapidjson::Document &d) {
|
2018-01-24 13:15:41 +01:00
|
|
|
ChannelPtr 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()) {
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("[TwitchChannel:refreshLiveStatus] root is not an object");
|
2017-12-28 17:47:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!d.HasMember("stream")) {
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("[TwitchChannel:refreshLiveStatus] Missing stream in root");
|
2017-12-28 17:47:00 +01:00
|
|
|
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")) {
|
2018-06-26 17:06:17 +02:00
|
|
|
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")) {
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("[TwitchChannel:refreshLiveStatus] Missing member \"status\" in channel");
|
2017-12-28 17:47:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stream is live
|
2018-03-30 15:05:33 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(channel->streamStatusMutex);
|
2018-06-11 11:51:46 +02:00
|
|
|
channel->streamStatus.live = true;
|
2018-03-30 15:05:33 +02:00
|
|
|
channel->streamStatus.viewerCount = stream["viewers"].GetUint();
|
|
|
|
channel->streamStatus.game = stream["game"].GetString();
|
|
|
|
channel->streamStatus.title = streamChannel["status"].GetString();
|
|
|
|
QDateTime since = QDateTime::fromString(stream["created_at"].GetString(), Qt::ISODate);
|
|
|
|
auto diff = since.secsTo(QDateTime::currentDateTime());
|
|
|
|
channel->streamStatus.uptime =
|
|
|
|
QString::number(diff / 3600) + "h " + QString::number(diff % 3600 / 60) + "m";
|
2018-04-08 15:14:14 +02:00
|
|
|
|
|
|
|
channel->streamStatus.rerun = false;
|
2018-05-26 16:31:43 +02:00
|
|
|
if (stream.HasMember("stream_type")) {
|
|
|
|
channel->streamStatus.streamType = stream["stream_type"].GetString();
|
|
|
|
} else {
|
|
|
|
channel->streamStatus.streamType = QString();
|
|
|
|
}
|
2018-04-08 15:14:14 +02:00
|
|
|
|
|
|
|
if (stream.HasMember("broadcast_platform")) {
|
|
|
|
const auto &broadcastPlatformValue = stream["broadcast_platform"];
|
|
|
|
|
|
|
|
if (broadcastPlatformValue.IsString()) {
|
|
|
|
const char *broadcastPlatform = stream["broadcast_platform"].GetString();
|
|
|
|
if (strcmp(broadcastPlatform, "rerun") == 0) {
|
|
|
|
channel->streamStatus.rerun = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-30 15:05:33 +02:00
|
|
|
}
|
2017-12-28 17:47:00 +01:00
|
|
|
|
2018-06-11 11:51:46 +02:00
|
|
|
// Signal all listeners that the stream status has been updated
|
|
|
|
channel->updateLiveInfo.invoke();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchChannel::startRefreshLiveStatusTimer(int intervalMS)
|
|
|
|
{
|
|
|
|
this->liveStatusTimer = new QTimer;
|
|
|
|
QObject::connect(this->liveStatusTimer, &QTimer::timeout, [this]() {
|
|
|
|
this->refreshLiveStatus(); //
|
2017-11-04 14:57:29 +01:00
|
|
|
});
|
2018-06-11 11:51:46 +02:00
|
|
|
|
|
|
|
// When the Room ID of a twitch channel has been set, refresh the live status an extra time
|
|
|
|
this->roomIDchanged.connect([this]() {
|
|
|
|
this->refreshLiveStatus(); //
|
|
|
|
});
|
|
|
|
|
|
|
|
this->liveStatusTimer->start(intervalMS);
|
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();
|
|
|
|
|
2018-06-26 17:12:50 +02:00
|
|
|
twitchApiGet(genericURL.arg(roomID), QThread::currentThread(), [weak](QJsonObject obj) {
|
2018-01-24 13:15:41 +01:00
|
|
|
ChannelPtr shared = weak.lock();
|
2018-01-05 00:58:25 +01:00
|
|
|
|
|
|
|
if (!shared) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-30 12:37:00 +02:00
|
|
|
auto channel = dynamic_cast<TwitchChannel *>(shared.get());
|
|
|
|
assert(channel != nullptr);
|
|
|
|
|
2018-03-31 13:44:15 +02:00
|
|
|
static auto readConnection = channel->readConnection;
|
2018-01-05 00:58:25 +01:00
|
|
|
|
2018-03-30 12:37:00 +02:00
|
|
|
QJsonArray msgArray = obj.value("messages").toArray();
|
|
|
|
if (msgArray.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-28 19:38:57 +02:00
|
|
|
std::vector<MessagePtr> messages;
|
2018-01-01 22:29:21 +01:00
|
|
|
|
2018-03-30 12:37:00 +02:00
|
|
|
for (const QJsonValueRef _msg : msgArray) {
|
|
|
|
QByteArray content = _msg.toString().toUtf8();
|
|
|
|
auto msg = Communi::IrcMessage::fromData(content, readConnection);
|
|
|
|
auto privMsg = static_cast<Communi::IrcPrivateMessage *>(msg);
|
2018-01-01 22:29:21 +01:00
|
|
|
|
2018-06-28 19:38:57 +02:00
|
|
|
MessageParseArgs args;
|
2018-06-26 17:06:17 +02:00
|
|
|
TwitchMessageBuilder builder(channel, privMsg, args);
|
2018-03-30 12:37:00 +02:00
|
|
|
if (!builder.isIgnored()) {
|
|
|
|
messages.push_back(builder.build());
|
2017-12-28 00:03:52 +01:00
|
|
|
}
|
2018-01-01 22:29:21 +01:00
|
|
|
}
|
2018-03-30 12:37:00 +02:00
|
|
|
channel->addMessagesAtStart(messages);
|
2017-12-28 00:03:52 +01:00
|
|
|
});
|
|
|
|
}
|
2018-02-05 21:20:38 +01:00
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
} // namespace chatterino
|