improving TwitchChannel
This commit is contained in:
@@ -31,78 +31,35 @@ TwitchChannel::TwitchChannel(const QString &channelName, Communi::IrcConnection
|
||||
{
|
||||
Log("[TwitchChannel:{}] Opened", this->name);
|
||||
|
||||
this->startRefreshLiveStatusTimer(60 * 1000);
|
||||
this->refreshChannelEmotes();
|
||||
this->refreshViewerList();
|
||||
|
||||
auto app = getApp();
|
||||
this->reloadChannelEmotes();
|
||||
this->managedConnect(getApp()->accounts->twitch.currentUserChanged,
|
||||
[=] { this->setMod(false); });
|
||||
|
||||
this->managedConnect(app->accounts->twitch.currentUserChanged,
|
||||
[this]() { this->setMod(false); });
|
||||
// pubsub
|
||||
this->userStateChanged.connect([=] { this->refreshPubsub(); });
|
||||
this->roomIDChanged.connect([=] { this->refreshPubsub(); });
|
||||
this->managedConnect(getApp()->accounts->twitch.currentUserChanged,
|
||||
[=] { this->refreshPubsub(); });
|
||||
this->refreshPubsub();
|
||||
|
||||
auto refreshPubSubState = [=]() {
|
||||
if (!this->hasModRights()) {
|
||||
return;
|
||||
}
|
||||
// room id loaded -> refresh live status
|
||||
this->roomIDChanged.connect([this]() { this->refreshLiveStatus(); });
|
||||
|
||||
if (this->roomID.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
// timers
|
||||
QObject::connect(&this->chattersListTimer_, &QTimer::timeout,
|
||||
[=] { this->refreshViewerList(); });
|
||||
this->chattersListTimer_.start(5 * 60 * 1000);
|
||||
|
||||
auto account = app->accounts->twitch.getCurrent();
|
||||
if (account && !account->getUserId().isEmpty()) {
|
||||
app->twitch.pubsub->listenToChannelModerationActions(this->roomID, account);
|
||||
}
|
||||
};
|
||||
|
||||
this->userStateChanged.connect(refreshPubSubState);
|
||||
this->roomIDchanged.connect(refreshPubSubState);
|
||||
this->managedConnect(app->accounts->twitch.currentUserChanged, refreshPubSubState);
|
||||
refreshPubSubState();
|
||||
|
||||
this->fetchMessages.connect([this] {
|
||||
this->fetchRecentMessages(); //
|
||||
});
|
||||
QObject::connect(&this->liveStatusTimer_, &QTimer::timeout, [=] { this->refreshLiveStatus(); });
|
||||
this->liveStatusTimer_.start(60 * 1000);
|
||||
|
||||
// --
|
||||
this->messageSuffix_.append(' ');
|
||||
this->messageSuffix_.append(QChar(0x206D));
|
||||
|
||||
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 = [=]() {
|
||||
const auto streamStatus = this->getStreamStatus();
|
||||
|
||||
if (app->settings->onlyFetchChattersForSmallerStreamers) {
|
||||
if (streamStatus.live && streamStatus.viewerCount > app->settings->smallStreamerLimit) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
NetworkRequest request("https://tmi.twitch.tv/group/user/" + this->name + "/chatters");
|
||||
|
||||
request.setCaller(QThread::currentThread());
|
||||
request.onSuccess([refreshChatters](auto result) {
|
||||
refreshChatters(result.parseJson()); //
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
request.execute();
|
||||
};
|
||||
|
||||
doRefreshChatters();
|
||||
|
||||
this->chattersListTimer = new QTimer;
|
||||
QObject::connect(this->chattersListTimer, &QTimer::timeout, doRefreshChatters);
|
||||
this->chattersListTimer->start(5 * 60 * 1000);
|
||||
|
||||
// debugging
|
||||
#if 0
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
this->addMessage(Message::createSystemMessage("asdf"));
|
||||
@@ -110,15 +67,6 @@ TwitchChannel::TwitchChannel(const QString &channelName, Communi::IrcConnection
|
||||
#endif
|
||||
}
|
||||
|
||||
TwitchChannel::~TwitchChannel()
|
||||
{
|
||||
this->liveStatusTimer->stop();
|
||||
this->liveStatusTimer->deleteLater();
|
||||
|
||||
this->chattersListTimer->stop();
|
||||
this->chattersListTimer->deleteLater();
|
||||
}
|
||||
|
||||
bool TwitchChannel::isEmpty() const
|
||||
{
|
||||
return this->name.isEmpty();
|
||||
@@ -129,14 +77,7 @@ bool TwitchChannel::canSendMessage() const
|
||||
return !this->isEmpty();
|
||||
}
|
||||
|
||||
void TwitchChannel::setRoomID(const QString &_roomID)
|
||||
{
|
||||
this->roomID = _roomID;
|
||||
this->roomIDchanged.invoke();
|
||||
this->fetchMessages.invoke();
|
||||
}
|
||||
|
||||
void TwitchChannel::reloadChannelEmotes()
|
||||
void TwitchChannel::refreshChannelEmotes()
|
||||
{
|
||||
auto app = getApp();
|
||||
|
||||
@@ -249,16 +190,16 @@ void TwitchChannel::addJoinedUser(const QString &user)
|
||||
|
||||
void TwitchChannel::addPartedUser(const QString &user)
|
||||
{
|
||||
auto *app = getApp();
|
||||
auto app = getApp();
|
||||
|
||||
if (user == app->accounts->twitch.getCurrent()->getUserName() ||
|
||||
!app->settings->showJoins.getValue()) {
|
||||
!getSettings()->showJoins.getValue()) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> guard(this->partedUserMutex_);
|
||||
|
||||
partedUsers_ << user;
|
||||
this->partedUsers_ << user;
|
||||
|
||||
if (!this->partedUsersMergeQueued_) {
|
||||
this->partedUsersMergeQueued_ = true;
|
||||
@@ -277,6 +218,18 @@ void TwitchChannel::addPartedUser(const QString &user)
|
||||
}
|
||||
}
|
||||
|
||||
QString TwitchChannel::getRoomID() const
|
||||
{
|
||||
return this->roomID_.get();
|
||||
}
|
||||
|
||||
void TwitchChannel::setRoomID(const QString &id)
|
||||
{
|
||||
this->roomID_.set(id);
|
||||
this->roomIDChanged.invoke();
|
||||
this->loadRecentMessages();
|
||||
}
|
||||
|
||||
TwitchChannel::RoomModes TwitchChannel::getRoomModes()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(this->roomModeMutex_);
|
||||
@@ -318,13 +271,15 @@ void TwitchChannel::setLive(bool newLiveStatus)
|
||||
}
|
||||
|
||||
if (gotNewLiveStatus) {
|
||||
this->updateLiveInfo.invoke();
|
||||
this->liveStatusChanged.invoke();
|
||||
}
|
||||
}
|
||||
|
||||
void TwitchChannel::refreshLiveStatus()
|
||||
{
|
||||
if (this->roomID.isEmpty()) {
|
||||
auto roomID = this->getRoomID();
|
||||
|
||||
if (roomID.isEmpty()) {
|
||||
Log("[TwitchChannel:{}] Refreshing live status (Missing ID)", this->name);
|
||||
this->setLive(false);
|
||||
return;
|
||||
@@ -332,13 +287,11 @@ void TwitchChannel::refreshLiveStatus()
|
||||
|
||||
Log("[TwitchChannel:{}] Refreshing live status", this->name);
|
||||
|
||||
QString url("https://api.twitch.tv/kraken/streams/" + this->roomID);
|
||||
QString url("https://api.twitch.tv/kraken/streams/" + roomID);
|
||||
|
||||
std::weak_ptr<Channel> weak = this->shared_from_this();
|
||||
auto request = makeGetStreamRequest(roomID, QThread::currentThread());
|
||||
|
||||
auto request = makeGetStreamRequest(this->roomID, QThread::currentThread());
|
||||
|
||||
request.onSuccess([weak](auto result) {
|
||||
request.onSuccess([weak = this->weak_from_this()](auto result) {
|
||||
auto d = result.parseRapidJson();
|
||||
ChannelPtr shared = weak.lock();
|
||||
|
||||
@@ -384,6 +337,7 @@ void TwitchChannel::refreshLiveStatus()
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(channel->streamStatusMutex_);
|
||||
StreamStatus status;
|
||||
channel->streamStatus_.live = true;
|
||||
channel->streamStatus_.viewerCount = stream["viewers"].GetUint();
|
||||
channel->streamStatus_.game = stream["game"].GetString();
|
||||
@@ -413,7 +367,7 @@ void TwitchChannel::refreshLiveStatus()
|
||||
}
|
||||
|
||||
// Signal all listeners that the stream status has been updated
|
||||
channel->updateLiveInfo.invoke();
|
||||
channel->liveStatusChanged.invoke();
|
||||
|
||||
return true;
|
||||
});
|
||||
@@ -421,70 +375,109 @@ void TwitchChannel::refreshLiveStatus()
|
||||
request.execute();
|
||||
}
|
||||
|
||||
void TwitchChannel::startRefreshLiveStatusTimer(int intervalMS)
|
||||
void TwitchChannel::initializeLiveStatusTimer(int intervalMS)
|
||||
{
|
||||
this->liveStatusTimer = new QTimer;
|
||||
QObject::connect(this->liveStatusTimer, &QTimer::timeout, [this]() {
|
||||
this->refreshLiveStatus(); //
|
||||
});
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
void TwitchChannel::fetchRecentMessages()
|
||||
void TwitchChannel::loadRecentMessages()
|
||||
{
|
||||
static QString genericURL =
|
||||
"https://tmi.twitch.tv/api/rooms/%1/recent_messages?client_id=" + getDefaultClientID();
|
||||
|
||||
NetworkRequest request(genericURL.arg(this->roomID));
|
||||
NetworkRequest request(genericURL.arg(this->getRoomID()));
|
||||
request.makeAuthorizedV5(getDefaultClientID());
|
||||
request.setCaller(QThread::currentThread());
|
||||
|
||||
std::weak_ptr<Channel> weak = this->shared_from_this();
|
||||
|
||||
request.onSuccess([weak](auto result) {
|
||||
auto obj = result.parseJson();
|
||||
request.onSuccess([this, weak = this->weak_from_this()](auto result) {
|
||||
// channel still exists?
|
||||
ChannelPtr shared = weak.lock();
|
||||
if (!shared) return false;
|
||||
|
||||
if (!shared) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto channel = dynamic_cast<TwitchChannel *>(shared.get());
|
||||
assert(channel != nullptr);
|
||||
|
||||
static auto readConnection = channel->readConnection_;
|
||||
|
||||
QJsonArray msgArray = obj.value("messages").toArray();
|
||||
if (msgArray.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<MessagePtr> messages;
|
||||
|
||||
for (const QJsonValueRef _msg : msgArray) {
|
||||
QByteArray content = _msg.toString().toUtf8();
|
||||
auto msg = Communi::IrcMessage::fromData(content, readConnection);
|
||||
auto privMsg = static_cast<Communi::IrcPrivateMessage *>(msg);
|
||||
|
||||
MessageParseArgs args;
|
||||
TwitchMessageBuilder builder(channel, privMsg, args);
|
||||
if (!builder.isIgnored()) {
|
||||
messages.push_back(builder.build());
|
||||
}
|
||||
}
|
||||
|
||||
channel->addMessagesAtStart(messages);
|
||||
|
||||
return true;
|
||||
// parse json
|
||||
return this->parseRecentMessages(result.parseJson());
|
||||
});
|
||||
|
||||
request.execute();
|
||||
}
|
||||
|
||||
bool TwitchChannel::parseRecentMessages(const QJsonObject &jsonRoot)
|
||||
{
|
||||
QJsonArray jsonMessages = jsonRoot.value("messages").toArray();
|
||||
if (jsonMessages.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
this->addMessagesAtStart(messages);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// get viewer list
|
||||
NetworkRequest request("https://tmi.twitch.tv/group/user/" + this->name + "/chatters");
|
||||
|
||||
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());
|
||||
});
|
||||
|
||||
request.execute();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
Reference in New Issue
Block a user