Skip to content

Commit

Permalink
Batch checking live status for all channels after startup. (Chatterin…
Browse files Browse the repository at this point in the history
  • Loading branch information
xel86 authored May 22, 2022
1 parent 4239666 commit dc34c16
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- Bugfix: Fixed viewer list not closing after pressing escape key. (#3734)
- Bugfix: Fixed links with no thumbnail having previous link's thumbnail. (#3720)
- Dev: Use Game Name returned by Get Streams instead of querying it from the Get Games API. (#3662)
- Dev: Batch checking live status for all channels after startup. (#3757)

## 2.3.5

Expand Down
5 changes: 0 additions & 5 deletions src/providers/twitch/TwitchChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,6 @@ TwitchChannel::TwitchChannel(const QString &name)
});
this->chattersListTimer_.start(5 * 60 * 1000);

QObject::connect(&this->liveStatusTimer_, &QTimer::timeout, [=] {
this->refreshLiveStatus();
});
this->liveStatusTimer_.start(60 * 1000);

// debugging
#if 0
for (int i = 0; i < 1000; i++) {
Expand Down
1 change: 0 additions & 1 deletion src/providers/twitch/TwitchChannel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ class TwitchChannel : public Channel, public ChannelChatters
// --
QString lastSentMessage_;
QObject lifetimeGuard_;
QTimer liveStatusTimer_;
QTimer chattersListTimer_;
QElapsedTimer titleRefreshedTimer_;
QElapsedTimer clipCreationTimer_;
Expand Down
64 changes: 64 additions & 0 deletions src/providers/twitch/TwitchIrcServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ void TwitchIrcServer::initialize(Settings &settings, Paths &paths)

this->bttv.loadEmotes();
this->ffz.loadEmotes();

/* Refresh all twitch channel's live status in bulk every 30 seconds after starting chatterino */
QObject::connect(&this->bulkLiveStatusTimer_, &QTimer::timeout, [=] {
this->bulkRefreshLiveStatus();
});
this->bulkLiveStatusTimer_.start(30 * 1000);
}

void TwitchIrcServer::initializeConnection(IrcConnection *connection,
Expand Down Expand Up @@ -296,6 +302,64 @@ std::shared_ptr<Channel> TwitchIrcServer::getChannelOrEmptyByID(
return Channel::getEmpty();
}

namespace {
// TODO: combine this with getEmoteSetBatches in TwitchAccount.cpp, maybe some templated thing
std::vector<QStringList> getChannelsInBatches(QStringList channels)
{
constexpr int batchSize = 100;

int batchCount = (channels.size() / batchSize) + 1;

std::vector<QStringList> batches;
batches.reserve(batchCount);

for (int i = 0; i < batchCount; i++)
{
QStringList batch;

// I hate you, msvc
int last = (std::min)(batchSize, channels.size() - batchSize * i);
for (int j = 0; j < last; j++)
{
batch.push_back(channels.at(j + (batchSize * i)));
}
batches.emplace_back(batch);
}

return batches;
}
} // namespace

void TwitchIrcServer::bulkRefreshLiveStatus()
{
QStringList userIDs;
this->forEachChannel([&userIDs](ChannelPtr chan) {
auto twitchChan = dynamic_cast<TwitchChannel *>(chan.get());
if (!twitchChan->roomId().isEmpty())
userIDs.push_back(twitchChan->roomId());
});

for (const auto &batch : getChannelsInBatches(userIDs))
{
getHelix()->fetchStreams(
batch, QStringList(),
[this](std::vector<HelixStream> streams) {
for (const auto &stream : streams)
{
auto chan = this->getChannelOrEmptyByID(stream.userId);
if (chan->getType() != Channel::Type::Twitch)
continue;

auto twitchChan = dynamic_cast<TwitchChannel *>(chan.get());
twitchChan->parseLiveStatus(true, stream);
}
},
[]() {
// failure
});
}
}

QString TwitchIrcServer::cleanChannelName(const QString &dirtyChannelName)
{
if (dirtyChannelName.startsWith('#'))
Expand Down
3 changes: 3 additions & 0 deletions src/providers/twitch/TwitchIrcServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class TwitchIrcServer final : public AbstractIrcServer, public Singleton

std::shared_ptr<Channel> getChannelOrEmptyByID(const QString &channelID);

void bulkRefreshLiveStatus();

Atomic<QString> lastUserThatWhisperedMe;

const ChannelPtr whispersChannel;
Expand Down Expand Up @@ -74,6 +76,7 @@ class TwitchIrcServer final : public AbstractIrcServer, public Singleton

BttvEmotes bttv;
FfzEmotes ffz;
QTimer bulkLiveStatusTimer_;

pajlada::Signals::SignalHolder signalHolder_;
};
Expand Down

0 comments on commit dc34c16

Please sign in to comment.