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-07-07 13:08:57 +02:00
|
|
|
#include "controllers/accounts/AccountController.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "debug/Log.hpp"
|
|
|
|
#include "messages/Message.hpp"
|
2018-07-06 19:23:47 +02:00
|
|
|
#include "providers/twitch/PubsubClient.hpp"
|
2018-07-07 13:08:57 +02:00
|
|
|
#include "providers/twitch/TwitchCommon.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "providers/twitch/TwitchMessageBuilder.hpp"
|
2018-06-28 19:46:45 +02:00
|
|
|
#include "singletons/Emotes.hpp"
|
|
|
|
#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>
|
2018-07-08 11:42:48 +02:00
|
|
|
#include <QJsonArray>
|
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-07-06 19:23:47 +02:00
|
|
|
TwitchChannel::TwitchChannel(const QString &channelName, Communi::IrcConnection *readConnection)
|
2018-07-06 17:30:12 +02:00
|
|
|
: Channel(channelName, Channel::Type::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-07-06 19:23:47 +02:00
|
|
|
, mod_(false)
|
|
|
|
, 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-07-14 14:24:18 +02:00
|
|
|
this->refreshChannelEmotes();
|
|
|
|
this->refreshViewerList();
|
2018-06-11 11:51:46 +02:00
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
this->managedConnect(getApp()->accounts->twitch.currentUserChanged,
|
|
|
|
[=] { this->setMod(false); });
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
// pubsub
|
|
|
|
this->userStateChanged.connect([=] { this->refreshPubsub(); });
|
|
|
|
this->roomIDChanged.connect([=] { this->refreshPubsub(); });
|
|
|
|
this->managedConnect(getApp()->accounts->twitch.currentUserChanged,
|
|
|
|
[=] { this->refreshPubsub(); });
|
|
|
|
this->refreshPubsub();
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
// room id loaded -> refresh live status
|
|
|
|
this->roomIDChanged.connect([this]() { this->refreshLiveStatus(); });
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
// timers
|
|
|
|
QObject::connect(&this->chattersListTimer_, &QTimer::timeout,
|
|
|
|
[=] { this->refreshViewerList(); });
|
|
|
|
this->chattersListTimer_.start(5 * 60 * 1000);
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
QObject::connect(&this->liveStatusTimer_, &QTimer::timeout, [=] { this->refreshLiveStatus(); });
|
|
|
|
this->liveStatusTimer_.start(60 * 1000);
|
2018-01-17 18:36:12 +01:00
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
// --
|
2018-07-06 19:23:47 +02:00
|
|
|
this->messageSuffix_.append(' ');
|
|
|
|
this->messageSuffix_.append(QChar(0x206D));
|
2018-03-24 12:02:07 +01:00
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
// debugging
|
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
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
void TwitchChannel::refreshChannelEmotes()
|
2017-09-16 00:05:06 +02:00
|
|
|
{
|
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-28 19:54:40 +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-07-07 13:08:57 +02:00
|
|
|
parsedMessage = parsedMessage.trimmed();
|
2018-01-22 15:24:39 +01:00
|
|
|
|
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) {
|
2018-07-06 19:23:47 +02:00
|
|
|
if (parsedMessage == this->lastSentMessage_) {
|
|
|
|
parsedMessage.append(this->messageSuffix_);
|
2018-06-24 17:20:15 +02:00
|
|
|
}
|
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";
|
2018-07-06 19:23:47 +02:00
|
|
|
this->lastSentMessage_ = parsedMessage;
|
2018-06-06 18:57:22 +02:00
|
|
|
}
|
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
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
return this->mod_;
|
2018-01-17 17:17:26 +01:00
|
|
|
}
|
|
|
|
|
2018-01-17 18:36:12 +01:00
|
|
|
void TwitchChannel::setMod(bool value)
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
if (this->mod_ != value) {
|
|
|
|
this->mod_ = value;
|
2018-01-17 18:36:12 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
std::lock_guard<std::mutex> lock(this->recentChattersMutex_);
|
2018-03-30 12:16:12 +02:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
this->recentChatters_[message->loginName] = {message->displayName, message->localizedName};
|
2018-03-30 12:16:12 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
std::lock_guard<std::mutex> guard(this->joinedUserMutex_);
|
2018-05-26 18:06:55 +02:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
joinedUsers_ << user;
|
2018-05-26 18:06:55 +02:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
if (!this->joinedUsersMergeQueued_) {
|
|
|
|
this->joinedUsersMergeQueued_ = true;
|
2018-05-26 18:06:55 +02:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
QTimer::singleShot(500, &this->object_, [this] {
|
|
|
|
std::lock_guard<std::mutex> guard(this->joinedUserMutex_);
|
2018-05-26 18:06:55 +02:00
|
|
|
|
2018-06-28 19:54:40 +02:00
|
|
|
auto message =
|
2018-07-06 19:23:47 +02:00
|
|
|
Message::createSystemMessage("Users joined: " + 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-07-06 19:23:47 +02:00
|
|
|
this->joinedUsers_.clear();
|
|
|
|
this->joinedUsersMergeQueued_ = false;
|
2018-05-26 18:06:55 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchChannel::addPartedUser(const QString &user)
|
|
|
|
{
|
2018-07-14 14:24:18 +02:00
|
|
|
auto app = getApp();
|
2018-05-26 18:06:55 +02:00
|
|
|
|
2018-05-26 20:26:25 +02:00
|
|
|
if (user == app->accounts->twitch.getCurrent()->getUserName() ||
|
2018-07-14 14:24:18 +02:00
|
|
|
!getSettings()->showJoins.getValue()) {
|
2018-05-26 18:06:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
std::lock_guard<std::mutex> guard(this->partedUserMutex_);
|
2018-05-26 18:06:55 +02:00
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
this->partedUsers_ << user;
|
2018-05-26 18:06:55 +02:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
if (!this->partedUsersMergeQueued_) {
|
|
|
|
this->partedUsersMergeQueued_ = true;
|
2018-05-26 18:06:55 +02:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
QTimer::singleShot(500, &this->object_, [this] {
|
|
|
|
std::lock_guard<std::mutex> guard(this->partedUserMutex_);
|
2018-05-26 18:06:55 +02:00
|
|
|
|
2018-06-28 19:54:40 +02:00
|
|
|
auto message =
|
2018-07-06 19:23:47 +02:00
|
|
|
Message::createSystemMessage("Users parted: " + 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-07-06 19:23:47 +02:00
|
|
|
this->partedUsers_.clear();
|
2018-05-26 18:06:55 +02:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
this->partedUsersMergeQueued_ = false;
|
2018-05-26 18:06:55 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
QString TwitchChannel::getRoomID() const
|
|
|
|
{
|
|
|
|
return this->roomID_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchChannel::setRoomID(const QString &id)
|
|
|
|
{
|
|
|
|
this->roomID_.set(id);
|
|
|
|
this->roomIDChanged.invoke();
|
|
|
|
this->loadRecentMessages();
|
|
|
|
}
|
|
|
|
|
2018-05-24 08:58:34 +02:00
|
|
|
TwitchChannel::RoomModes TwitchChannel::getRoomModes()
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
std::lock_guard<std::mutex> lock(this->roomModeMutex_);
|
2018-05-24 08:58:34 +02:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
return this->roomModes_;
|
2018-05-24 08:58:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchChannel::setRoomModes(const RoomModes &_roomModes)
|
|
|
|
{
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
std::lock_guard<std::mutex> lock(this->roomModeMutex_);
|
|
|
|
this->roomModes_ = _roomModes;
|
2018-05-24 08:58:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this->roomModesChanged.invoke();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TwitchChannel::isLive() const
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
std::lock_guard<std::mutex> lock(this->streamStatusMutex_);
|
|
|
|
return this->streamStatus_.live;
|
2018-05-24 08:58:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TwitchChannel::StreamStatus TwitchChannel::getStreamStatus() const
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
std::lock_guard<std::mutex> lock(this->streamStatusMutex_);
|
|
|
|
return this->streamStatus_;
|
2018-05-24 08:58:34 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
std::lock_guard<std::mutex> lock(this->streamStatusMutex_);
|
|
|
|
if (this->streamStatus_.live != newLiveStatus) {
|
2018-04-14 18:27:13 +02:00
|
|
|
gotNewLiveStatus = true;
|
2018-07-06 19:23: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-07-14 14:24:18 +02:00
|
|
|
this->liveStatusChanged.invoke();
|
2018-04-08 14:45:47 +02:00
|
|
|
}
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
2017-11-04 14:57:29 +01:00
|
|
|
|
|
|
|
void TwitchChannel::refreshLiveStatus()
|
|
|
|
{
|
2018-07-14 14:24:18 +02:00
|
|
|
auto roomID = this->getRoomID();
|
|
|
|
|
|
|
|
if (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
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
QString url("https://api.twitch.tv/kraken/streams/" + roomID);
|
2017-11-04 14:57:29 +01:00
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
auto request = makeGetStreamRequest(roomID, QThread::currentThread());
|
2018-01-05 00:58:25 +01:00
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
request.onSuccess([weak = this->weak_from_this()](auto result) {
|
2018-07-07 13:08:57 +02:00
|
|
|
auto d = result.parseRapidJson();
|
2018-01-24 13:15:41 +01:00
|
|
|
ChannelPtr shared = weak.lock();
|
2018-01-05 00:58:25 +01:00
|
|
|
|
|
|
|
if (!shared) {
|
2018-07-07 13:08:57 +02:00
|
|
|
return false;
|
2018-01-05 00:58:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2018-07-07 13:08:57 +02:00
|
|
|
return false;
|
2017-12-28 17:47:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!d.HasMember("stream")) {
|
2018-06-26 17:06:17 +02:00
|
|
|
Log("[TwitchChannel:refreshLiveStatus] Missing stream in root");
|
2018-07-07 13:08:57 +02:00
|
|
|
return false;
|
2017-12-28 17:47:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2018-07-07 13:08:57 +02:00
|
|
|
return false;
|
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);
|
2018-07-07 13:08:57 +02:00
|
|
|
return false;
|
2017-12-28 17:47:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2018-07-07 13:08:57 +02:00
|
|
|
return false;
|
2017-12-28 17:47:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stream is live
|
2018-03-30 15:05:33 +02:00
|
|
|
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
std::lock_guard<std::mutex> lock(channel->streamStatusMutex_);
|
2018-07-14 14:24:18 +02:00
|
|
|
StreamStatus status;
|
2018-07-06 19:23:47 +02:00
|
|
|
channel->streamStatus_.live = true;
|
|
|
|
channel->streamStatus_.viewerCount = stream["viewers"].GetUint();
|
|
|
|
channel->streamStatus_.game = stream["game"].GetString();
|
|
|
|
channel->streamStatus_.title = streamChannel["status"].GetString();
|
2018-03-30 15:05:33 +02:00
|
|
|
QDateTime since = QDateTime::fromString(stream["created_at"].GetString(), Qt::ISODate);
|
|
|
|
auto diff = since.secsTo(QDateTime::currentDateTime());
|
2018-07-06 19:23:47 +02:00
|
|
|
channel->streamStatus_.uptime =
|
2018-03-30 15:05:33 +02:00
|
|
|
QString::number(diff / 3600) + "h " + QString::number(diff % 3600 / 60) + "m";
|
2018-04-08 15:14:14 +02:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
channel->streamStatus_.rerun = false;
|
2018-05-26 16:31:43 +02:00
|
|
|
if (stream.HasMember("stream_type")) {
|
2018-07-06 19:23:47 +02:00
|
|
|
channel->streamStatus_.streamType = stream["stream_type"].GetString();
|
2018-05-26 16:31:43 +02:00
|
|
|
} else {
|
2018-07-06 19:23:47 +02:00
|
|
|
channel->streamStatus_.streamType = QString();
|
2018-05-26 16:31:43 +02:00
|
|
|
}
|
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) {
|
2018-07-06 19:23:47 +02:00
|
|
|
channel->streamStatus_.rerun = true;
|
2018-04-08 15:14:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
2018-07-14 14:24:18 +02:00
|
|
|
channel->liveStatusChanged.invoke();
|
2018-07-07 13:08:57 +02:00
|
|
|
|
|
|
|
return true;
|
2018-06-11 11:51:46 +02:00
|
|
|
});
|
2018-07-07 13:08:57 +02:00
|
|
|
|
|
|
|
request.execute();
|
2018-06-11 11:51:46 +02:00
|
|
|
}
|
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
void TwitchChannel::initializeLiveStatusTimer(int intervalMS)
|
2018-06-11 11:51:46 +02:00
|
|
|
{
|
2017-09-16 00:05:06 +02:00
|
|
|
}
|
2017-11-04 14:57:29 +01:00
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
void TwitchChannel::loadRecentMessages()
|
2017-12-28 00:03:52 +01:00
|
|
|
{
|
|
|
|
static QString genericURL =
|
|
|
|
"https://tmi.twitch.tv/api/rooms/%1/recent_messages?client_id=" + getDefaultClientID();
|
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
NetworkRequest request(genericURL.arg(this->getRoomID()));
|
2018-07-07 13:08:57 +02:00
|
|
|
request.makeAuthorizedV5(getDefaultClientID());
|
|
|
|
request.setCaller(QThread::currentThread());
|
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
request.onSuccess([this, weak = this->weak_from_this()](auto result) {
|
|
|
|
// channel still exists?
|
2018-01-24 13:15:41 +01:00
|
|
|
ChannelPtr shared = weak.lock();
|
2018-07-14 14:24:18 +02:00
|
|
|
if (!shared) return false;
|
2018-01-05 00:58:25 +01:00
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
// parse json
|
|
|
|
return this->parseRecentMessages(result.parseJson());
|
|
|
|
});
|
2018-01-05 00:58:25 +01:00
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
request.execute();
|
|
|
|
}
|
2018-03-30 12:37:00 +02:00
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
bool TwitchChannel::parseRecentMessages(const QJsonObject &jsonRoot)
|
|
|
|
{
|
|
|
|
QJsonArray jsonMessages = jsonRoot.value("messages").toArray();
|
|
|
|
if (jsonMessages.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-05 00:58:25 +01:00
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
std::vector<MessagePtr> messages;
|
|
|
|
|
|
|
|
for (const auto jsonMessage : jsonMessages) {
|
|
|
|
auto content = jsonMessage.toString().toUtf8();
|
|
|
|
auto message = Communi::IrcMessage::fromData(content, this->readConnection_);
|
|
|
|
auto privMsg = dynamic_cast<Communi::IrcPrivateMessage *>(message);
|
|
|
|
assert(privMsg);
|
|
|
|
|
|
|
|
MessageParseArgs args;
|
|
|
|
TwitchMessageBuilder builder(this, privMsg, args);
|
|
|
|
if (!builder.isIgnored()) {
|
|
|
|
messages.push_back(builder.build());
|
2018-03-30 12:37:00 +02:00
|
|
|
}
|
2018-07-14 14:24:18 +02:00
|
|
|
}
|
2018-03-30 12:37:00 +02:00
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
this->addMessagesAtStart(messages);
|
2018-01-01 22:29:21 +01:00
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-01-01 22:29:21 +01:00
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
void TwitchChannel::refreshPubsub()
|
|
|
|
{
|
|
|
|
// listen to moderation actions
|
|
|
|
if (!this->hasModRights()) return;
|
|
|
|
auto roomId = this->getRoomID();
|
|
|
|
if (roomId.isEmpty()) return;
|
|
|
|
|
|
|
|
auto account = getApp()->accounts->twitch.getCurrent();
|
|
|
|
getApp()->twitch2->pubsub->listenToChannelModerationActions(roomId, account);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchChannel::refreshViewerList()
|
|
|
|
{
|
|
|
|
// setting?
|
|
|
|
const auto streamStatus = this->getStreamStatus();
|
|
|
|
|
|
|
|
if (getSettings()->onlyFetchChattersForSmallerStreamers) {
|
|
|
|
if (streamStatus.live && streamStatus.viewerCount > getSettings()->smallStreamerLimit) {
|
|
|
|
return;
|
2018-01-01 22:29:21 +01:00
|
|
|
}
|
2018-07-14 14:24:18 +02:00
|
|
|
}
|
2018-07-07 13:08:57 +02:00
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
// get viewer list
|
|
|
|
NetworkRequest request("https://tmi.twitch.tv/group/user/" + this->name + "/chatters");
|
2018-07-07 13:08:57 +02:00
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
request.setCaller(QThread::currentThread());
|
|
|
|
request.onSuccess([this, weak = this->weak_from_this()](auto result) {
|
|
|
|
// channel still exists?
|
|
|
|
auto shared = weak.lock();
|
|
|
|
if (!shared) return false;
|
|
|
|
|
|
|
|
return this->parseViewerList(result.parseJson());
|
2017-12-28 00:03:52 +01:00
|
|
|
});
|
2018-07-07 13:08:57 +02:00
|
|
|
|
|
|
|
request.execute();
|
2017-12-28 00:03:52 +01:00
|
|
|
}
|
2018-02-05 21:20:38 +01:00
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
bool TwitchChannel::parseViewerList(const QJsonObject &jsonRoot)
|
|
|
|
{
|
|
|
|
static QStringList categories = {"moderators", "staff", "admins", "global_mods", "viewers"};
|
|
|
|
|
|
|
|
// parse json
|
|
|
|
QJsonObject jsonCategories = jsonRoot.value("chatters").toObject();
|
|
|
|
|
|
|
|
for (const auto &category : categories) {
|
|
|
|
for (const auto jsonCategory : jsonCategories.value(category).toArray()) {
|
|
|
|
this->completionModel.addUser(jsonCategory.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
} // namespace chatterino
|