mirror-chatterino2/src/providers/twitch/TwitchChannel.hpp

472 lines
16 KiB
C++
Raw Normal View History

#pragma once
#include "common/Aliases.hpp"
2018-08-11 17:15:17 +02:00
#include "common/Atomic.hpp"
2018-06-26 15:33:51 +02:00
#include "common/Channel.hpp"
2019-09-17 12:10:38 +02:00
#include "common/ChannelChatters.hpp"
#include "common/Common.hpp"
2018-07-15 20:28:54 +02:00
#include "common/UniqueAccess.hpp"
#include "providers/ffz/FfzBadges.hpp"
#include "providers/ffz/FfzEmotes.hpp"
2018-08-13 13:54:39 +02:00
#include "providers/twitch/TwitchEmotes.hpp"
#include "util/QStringHash.hpp"
#include "util/ThreadGuard.hpp"
#include <boost/circular_buffer/space_optimized.hpp>
#include <boost/signals2.hpp>
#include <IrcMessage>
#include <pajlada/signals/signalholder.hpp>
#include <QColor>
#include <QElapsedTimer>
#include <QRegularExpression>
#include <atomic>
#include <mutex>
#include <optional>
2018-08-02 14:23:27 +02:00
#include <unordered_map>
namespace chatterino {
// This is for compatibility with older Chatterino versions. Twitch didn't use
// to allow ZERO WIDTH JOINER unicode character, so Chatterino used ESCAPE_TAG
// instead.
// See https://github.com/Chatterino/chatterino2/issues/3384 and
// https://mm2pl.github.io/emoji_rfc.pdf for more details
const QString ZERO_WIDTH_JOINER = QString(QChar(0x200D));
// Here be MSVC: Do NOT replace with "\U" literal, it will fail silently.
namespace {
const QChar ESCAPE_TAG_CHARS[2] = {QChar::highSurrogate(0xE0002),
QChar::lowSurrogate(0xE0002)};
}
const QString ESCAPE_TAG = QString(ESCAPE_TAG_CHARS, 2);
const static QRegularExpression COMBINED_FIXER(
QString("(?<!%1)%1").arg(ESCAPE_TAG),
QRegularExpression::UseUnicodePropertiesOption);
2018-08-29 19:25:37 +02:00
enum class HighlightState;
struct Emote;
using EmotePtr = std::shared_ptr<const Emote>;
class EmoteMap;
2018-08-14 17:45:17 +02:00
class TwitchBadges;
class FfzEmotes;
class BttvEmotes;
struct BttvLiveUpdateEmoteUpdateAddMessage;
struct BttvLiveUpdateEmoteRemoveMessage;
class SeventvEmotes;
namespace seventv::eventapi {
struct EmoteAddDispatch;
struct EmoteUpdateDispatch;
struct EmoteRemoveDispatch;
struct UserConnectionUpdateDispatch;
} // namespace seventv::eventapi
struct ChannelPointReward;
class MessageThread;
struct CheerEmoteSet;
struct HelixStream;
class TwitchIrcServer;
const int MAX_QUEUED_REDEMPTIONS = 16;
class TwitchChannel final : public Channel, public ChannelChatters
{
public:
struct StreamStatus {
bool live = false;
bool rerun = false;
unsigned viewerCount = 0;
QString title;
QString game;
QString gameId;
QString uptime;
int uptimeSeconds = 0;
QString streamType;
QString streamId;
};
2018-05-24 08:58:34 +02:00
struct RoomModes {
bool submode = false;
bool r9k = false;
bool emoteOnly = false;
/**
* @brief Number of minutes required for users to be followed before typing in chat
*
* Special cases:
* -1 = follower mode off
* 0 = follower mode on, no time requirement
**/
int followerOnly = -1;
/**
* @brief Number of seconds required to wait before typing emotes
*
* 0 = slow mode off
**/
2018-05-24 08:58:34 +02:00
int slowMode = 0;
};
explicit TwitchChannel(const QString &channelName);
~TwitchChannel() override;
TwitchChannel(const TwitchChannel &) = delete;
TwitchChannel(TwitchChannel &&) = delete;
TwitchChannel &operator=(const TwitchChannel &) = delete;
TwitchChannel &operator=(TwitchChannel &&) = delete;
2018-08-13 13:54:39 +02:00
void initialize();
2018-01-17 17:17:26 +01:00
2018-07-15 20:28:54 +02:00
// Channel methods
bool isEmpty() const override;
bool canSendMessage() const override;
void sendMessage(const QString &message) override;
void sendReply(const QString &message, const QString &replyId);
bool isMod() const override;
2019-10-07 20:31:34 +02:00
bool isVip() const;
bool isStaff() const;
bool isBroadcaster() const override;
bool hasHighRateLimit() const override;
bool canReconnect() const override;
void reconnect() override;
QString getCurrentStreamID() const override;
void createClip();
2018-07-14 14:24:18 +02:00
// Data
const QString &subscriptionUrl();
const QString &channelUrl();
const QString &popoutPlayerUrl();
int chatterCount() const;
bool isLive() const override;
bool isRerun() const override;
2018-08-11 17:15:17 +02:00
QString roomId() const;
SharedAccessGuard<const RoomModes> accessRoomModes() const;
SharedAccessGuard<const StreamStatus> accessStreamStatus() const;
/**
* Records that the channel is no longer joined.
*/
void markDisconnected();
/**
* Records that the channel's read connection is healthy.
*/
void markConnected();
// Emotes
std::optional<EmotePtr> bttvEmote(const EmoteName &name) const;
std::optional<EmotePtr> ffzEmote(const EmoteName &name) const;
std::optional<EmotePtr> seventvEmote(const EmoteName &name) const;
2018-08-11 17:15:17 +02:00
std::shared_ptr<const EmoteMap> bttvEmotes() const;
std::shared_ptr<const EmoteMap> ffzEmotes() const;
std::shared_ptr<const EmoteMap> seventvEmotes() const;
void refreshBTTVChannelEmotes(bool manualRefresh);
void refreshFFZChannelEmotes(bool manualRefresh);
void refreshSevenTVChannelEmotes(bool manualRefresh);
void setBttvEmotes(std::shared_ptr<const EmoteMap> &&map);
void setFfzEmotes(std::shared_ptr<const EmoteMap> &&map);
void setSeventvEmotes(std::shared_ptr<const EmoteMap> &&map);
const QString &seventvUserID() const;
const QString &seventvEmoteSetID() const;
/** Adds a BTTV channel emote to this channel. */
void addBttvEmote(const BttvLiveUpdateEmoteUpdateAddMessage &message);
/** Updates a BTTV channel emote in this channel. */
void updateBttvEmote(const BttvLiveUpdateEmoteUpdateAddMessage &message);
/** Removes a BTTV channel emote from this channel. */
void removeBttvEmote(const BttvLiveUpdateEmoteRemoveMessage &message);
/** Adds a 7TV channel emote to this channel. */
void addSeventvEmote(const seventv::eventapi::EmoteAddDispatch &dispatch);
/** Updates a 7TV channel emote's name in this channel */
void updateSeventvEmote(
const seventv::eventapi::EmoteUpdateDispatch &dispatch);
/** Removes a 7TV channel emote from this channel */
void removeSeventvEmote(
const seventv::eventapi::EmoteRemoveDispatch &dispatch);
/** Updates the current 7TV user. Currently, only the emote-set is updated. */
void updateSeventvUser(
const seventv::eventapi::UserConnectionUpdateDispatch &dispatch);
// Update the channel's 7TV information (the channel's 7TV user ID and emote set ID)
void updateSeventvData(const QString &newUserID,
const QString &newEmoteSetID);
2018-08-13 13:54:39 +02:00
// Badges
std::optional<EmotePtr> ffzCustomModBadge() const;
std::optional<EmotePtr> ffzCustomVipBadge() const;
std::optional<EmotePtr> twitchBadge(const QString &set,
const QString &version) const;
/**
* Returns a list of channel-specific FrankerFaceZ badges for the given user
*/
std::vector<FfzBadges::Badge> ffzChannelBadges(const QString &userID) const;
2018-08-02 14:23:27 +02:00
// Cheers
std::optional<CheerEmote> cheerEmote(const QString &string);
// Replies
/**
* Stores the given thread in this channel.
*
* Note: This method not take ownership of the MessageThread; this
* TwitchChannel instance will store a weak_ptr to the thread.
*/
void addReplyThread(const std::shared_ptr<MessageThread> &thread);
const std::unordered_map<QString, std::weak_ptr<MessageThread>> &threads()
const;
/**
* Get the thread for the given message
* If no thread can be found for the message, create one
*/
std::shared_ptr<MessageThread> getOrCreateThread(const MessagePtr &message);
/**
* This signal fires when the local user has joined the channel
**/
pajlada::Signals::NoArgSignal joined;
// Only TwitchChannel may invoke this signal
pajlada::Signals::NoArgSignal userStateChanged;
/**
* This signals fires whenever the live status is changed
*
* Streams are counted as offline by default, so if a stream does not go online
* this signal will never fire
**/
pajlada::Signals::Signal<bool> liveStatusChanged;
/**
* This signal fires whenever the stream status is changed
*
* This includes when the stream goes from offline to online,
* or the viewer count changes, or the title has been updated
**/
pajlada::Signals::NoArgSignal streamStatusChanged;
2018-05-24 08:58:34 +02:00
pajlada::Signals::NoArgSignal roomModesChanged;
// Channel point rewards
void addQueuedRedemption(const QString &rewardId,
const QString &originalContent,
Communi::IrcMessage *message);
/**
* A rich & hydrated redemption from PubSub has arrived, add it to the channel.
* This will look at queued up partial messages, and if one is found it will add the queued up partial messages fully hydrated.
**/
void addChannelPointReward(const ChannelPointReward &reward);
bool isChannelPointRewardKnown(const QString &rewardId);
std::optional<ChannelPointReward> channelPointReward(
const QString &rewardId) const;
// Live status
void updateStreamStatus(const std::optional<HelixStream> &helixStream);
void updateStreamTitle(const QString &title);
/**
* Returns the display name of the user
*
* If the display name contained chinese, japenese, or korean characters, the user's login name is returned instead
**/
const QString &getDisplayName() const override;
void updateDisplayName(const QString &displayName);
private:
2018-07-15 20:28:54 +02:00
struct NameOptions {
// displayName is the non-CJK-display name for this user
// This will always be the same as their `name_`, but potentially with different casing
2018-07-15 20:28:54 +02:00
QString displayName;
// localizedName is their display name that *may* contain CJK characters
// If the display name does not contain any CJK characters, this will be
// the same as `displayName`
2018-07-15 20:28:54 +02:00
QString localizedName;
// actualDisplayName is the raw display name string received from Twitch
QString actualDisplayName;
} nameOptions;
2018-07-15 20:28:54 +02:00
struct QueuedRedemption {
QString rewardID;
QString originalContent;
QObjectPtr<Communi::IrcMessage> message;
};
void refreshPubSub();
2018-08-13 13:54:39 +02:00
void refreshChatters();
void refreshBadges();
void refreshCheerEmotes();
2018-07-14 14:24:18 +02:00
void loadRecentMessages();
void loadRecentMessagesReconnect();
void cleanUpReplyThreads();
void showLoginMessage();
/// roomIdChanged is called whenever this channel's ID has been changed
/// This should only happen once per channel, whenever the ID goes from unset to set
void roomIdChanged();
/** Joins (subscribes to) a Twitch channel for updates on BTTV. */
void joinBttvChannel() const;
/**
* Indicates an activity to 7TV in this channel for this user.
* This is done at most once every 60s.
*/
void updateSevenTVActivity();
void listenSevenTVCosmetics() const;
2018-07-14 14:24:18 +02:00
/**
* @brief Sets the live status of this Twitch channel
*
* Returns true if the live status changed with this call
**/
bool setLive(bool newLiveStatus);
2018-08-13 13:54:39 +02:00
void setMod(bool value);
void setVIP(bool value);
void setStaff(bool value);
2018-08-13 13:54:39 +02:00
void setRoomId(const QString &id);
void setRoomModes(const RoomModes &newRoomModes);
void setDisplayName(const QString &name);
void setLocalizedName(const QString &name);
/**
* Returns the localized name of the user
**/
const QString &getLocalizedName() const override;
QString prepareMessage(const QString &message) const;
/**
* Either adds a message mentioning the updated emotes
* or replaces an existing message. For criteria on existing messages,
* see `tryReplaceLastLiveUpdateAddOrRemove`.
*
* @param isEmoteAdd true if the emote was added, false if it was removed.
* @param platform The platform the emote was updated on ("7TV", "BTTV", "FFZ")
* @param actor The actor performing the update (possibly empty)
* @param emoteName The emote's name
*/
void addOrReplaceLiveUpdatesAddRemove(bool isEmoteAdd,
const QString &platform,
const QString &actor,
const QString &emoteName);
/**
* Tries to replace the last emote update message.
*
* A last message is valid if:
* * The actors match
* * The operations match
* * The platform matches
* * The last message isn't older than 5s
*
* @param op The emote operation (LiveUpdatesAdd or LiveUpdatesRemove)
* @param platform The emote platform ("7TV", "BTTV", "FFZ")
* @param actor The actor performing the action (possibly empty)
* @param emoteName The updated emote's name
* @return true, if the last message was replaced
*/
bool tryReplaceLastLiveUpdateAddOrRemove(MessageFlag op,
const QString &platform,
const QString &actor,
const QString &emoteName);
// Data
2018-07-15 20:28:54 +02:00
const QString subscriptionUrl_;
const QString channelUrl_;
const QString popoutPlayerUrl_;
int chatterCount_{};
2018-07-15 20:28:54 +02:00
UniqueAccess<StreamStatus> streamStatus_;
UniqueAccess<RoomModes> roomModes;
bool disconnected_{};
std::optional<std::chrono::time_point<std::chrono::system_clock>>
lastConnectedAt_{};
std::atomic_flag loadingRecentMessages_ = ATOMIC_FLAG_INIT;
std::unordered_map<QString, std::weak_ptr<MessageThread>> threads_;
2019-02-26 21:00:57 +01:00
protected:
void messageRemovedFromStart(const MessagePtr &msg) override;
2018-08-11 17:15:17 +02:00
Atomic<std::shared_ptr<const EmoteMap>> bttvEmotes_;
Atomic<std::shared_ptr<const EmoteMap>> ffzEmotes_;
Atomic<std::shared_ptr<const EmoteMap>> seventvEmotes_;
Atomic<std::optional<EmotePtr>> ffzCustomModBadge_;
Atomic<std::optional<EmotePtr>> ffzCustomVipBadge_;
FfzChannelBadgeMap ffzChannelBadges_;
ThreadGuard tgFfzChannelBadges_;
2019-02-26 21:00:57 +01:00
private:
2018-08-13 13:54:39 +02:00
// Badges
UniqueAccess<std::map<QString, std::map<QString, EmotePtr>>>
badgeSets_; // "subscribers": { "0": ... "3": ... "6": ...
UniqueAccess<std::vector<CheerEmoteSet>> cheerEmoteSets_;
UniqueAccess<std::map<QString, ChannelPointReward>> channelPointRewards_;
boost::circular_buffer_space_optimized<QueuedRedemption>
waitingRedemptions_{MAX_QUEUED_REDEMPTIONS};
2018-07-06 19:23:47 +02:00
bool mod_ = false;
bool vip_ = false;
bool staff_ = false;
2018-08-10 19:00:14 +02:00
UniqueAccess<QString> roomID_;
2018-01-17 17:17:26 +01:00
2018-07-15 20:28:54 +02:00
// --
QString lastSentMessage_;
QObject lifetimeGuard_;
2018-07-14 14:24:18 +02:00
QTimer chattersListTimer_;
QTimer threadClearTimer_;
QElapsedTimer titleRefreshedTimer_;
QElapsedTimer clipCreationTimer_;
bool isClipCreationInProgress{false};
/**
* This channels 7TV user-id,
* empty if this channel isn't connected with 7TV.
*/
QString seventvUserID_;
/**
* This channels current 7TV emote-set-id,
* empty if this channel isn't connected with 7TV
*/
QString seventvEmoteSetID_;
/**
* The index of the twitch connection in
* 7TV's user representation.
*/
size_t seventvUserTwitchConnectionIndex_{};
/**
* The next moment in time to signal activity in this channel to 7TV.
* Or: Up until this moment we don't need to send activity.
*/
QDateTime nextSeventvActivity_;
/** The platform of the last live emote update ("7TV", "BTTV", "FFZ"). */
QString lastLiveUpdateEmotePlatform_;
/** The actor name of the last live emote update. */
QString lastLiveUpdateEmoteActor_;
/** A weak reference to the last live emote update message. */
std::weak_ptr<const Message> lastLiveUpdateMessage_;
/** A list of the emotes listed in the lat live emote update message. */
std::vector<QString> lastLiveUpdateEmoteNames_;
pajlada::Signals::SignalHolder signalHolder_;
std::vector<boost::signals2::scoped_connection> bSignals_;
friend class TwitchIrcServer;
2018-08-13 13:54:39 +02:00
friend class TwitchMessageBuilder;
friend class IrcMessageHandler;
2024-06-16 12:22:51 +02:00
friend class Commands_E2E_Test;
2018-02-05 15:11:50 +01:00
};
} // namespace chatterino