2023-07-02 15:52:15 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "util/QStringHash.hpp"
|
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
#include <optional>
|
|
|
|
#include <shared_mutex>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
class TwitchChannel;
|
|
|
|
|
|
|
|
class ITwitchLiveController
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~ITwitchLiveController() = default;
|
|
|
|
|
|
|
|
virtual void add(const std::shared_ptr<TwitchChannel> &newChannel) = 0;
|
|
|
|
};
|
|
|
|
|
2024-07-21 15:09:59 +02:00
|
|
|
class TwitchLiveController : public ITwitchLiveController
|
2023-07-02 15:52:15 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Controls how often all channels have their stream status refreshed
|
|
|
|
static constexpr std::chrono::seconds REFRESH_INTERVAL{30};
|
|
|
|
|
|
|
|
// Controls how quickly new channels have their stream status loaded
|
|
|
|
static constexpr std::chrono::seconds IMMEDIATE_REQUEST_INTERVAL{1};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* How many channels to include in a single request
|
|
|
|
*
|
|
|
|
* Should not be more than 100
|
|
|
|
**/
|
|
|
|
static constexpr int BATCH_SIZE{100};
|
|
|
|
|
|
|
|
TwitchLiveController();
|
|
|
|
|
|
|
|
// Add a Twitch channel to be queried for live status
|
|
|
|
// A request is made within a few seconds if this is the first time this channel is added
|
|
|
|
void add(const std::shared_ptr<TwitchChannel> &newChannel) override;
|
|
|
|
|
|
|
|
private:
|
2024-07-20 14:19:27 +02:00
|
|
|
struct ChannelEntry {
|
|
|
|
std::weak_ptr<TwitchChannel> ptr;
|
|
|
|
bool wasChecked = false;
|
|
|
|
};
|
|
|
|
|
2023-07-02 15:52:15 +02:00
|
|
|
/**
|
|
|
|
* Run batched Helix Channels & Stream requests for channels
|
|
|
|
*
|
|
|
|
* If a list of channel IDs is passed to request, we only make a request for those channels
|
|
|
|
*
|
|
|
|
* If no list of channels is passed to request (the default behaviour), we make requests for all channels
|
|
|
|
* in the `channels` map.
|
|
|
|
**/
|
|
|
|
void request(std::optional<QStringList> optChannelIDs = std::nullopt);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of channel IDs pointing to their Twitch Channel
|
|
|
|
*
|
|
|
|
* These channels will have their stream status updated every REFRESH_INTERVAL seconds
|
|
|
|
**/
|
2024-07-20 14:19:27 +02:00
|
|
|
std::unordered_map<QString, ChannelEntry> channels;
|
2023-07-02 15:52:15 +02:00
|
|
|
std::shared_mutex channelsMutex;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of channels that need an immediate live status update
|
|
|
|
*
|
|
|
|
* These channels will have their stream status updated after at most IMMEDIATE_REQUEST_INTERVAL seconds
|
|
|
|
**/
|
|
|
|
std::unordered_set<QString> immediateRequests;
|
|
|
|
std::mutex immediateRequestsMutex;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Timer responsible for refreshing `channels`
|
|
|
|
**/
|
|
|
|
QTimer refreshTimer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Timer responsible for refreshing `immediateRequests`
|
|
|
|
**/
|
|
|
|
QTimer immediateRequestTimer;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace chatterino
|