Twitch SDK (Internal)
chatchannel.h
Go to the documentation of this file.
1 /********************************************************************************************
2 * Twitch Broadcasting SDK
3 *
4 * This software is supplied under the terms of a license agreement with Twitch Interactive, Inc. and
5 * may not be copied or used except in accordance with the terms of that agreement
6 * Copyright (c) 2012-2016 Twitch Interactive, Inc.
7 *********************************************************************************************/
8 
9 #pragma once
10 
16 
22 
23 
24 namespace ttv
25 {
26  class IThread;
27  class User;
28  class SettingRepository;
29 
30  namespace chat
31  {
32  class ChatAPI;
33  class ChatChannel;
34  class ChatSession;
35  class IChatTransport;
36  class ChatUserBlockList;
37  class BitsConfigRepository;
38  }
39 }
40 
41 
46 {
47 private:
48  struct State
49  {
50  enum Enum
51  {
57  };
58  };
59 
60 public:
61  using FetchUserListCallback = std::function<void(TTV_ErrorCode ec, UserList&& userList)>;
62  using SetBroadcasterLanguageChatCallback = std::function<void(TTV_ErrorCode ec)>;
63  using LookupId = uint64_t;
64 
65  ChatChannel(const std::shared_ptr<User>& user, ChannelId channelId, std::shared_ptr<IChatChannelListener> chatCallbacks, std::shared_ptr<TaskRunner> taskRunner);
66  virtual ~ChatChannel();
67 
69  void SetChannelRepository(const std::shared_ptr<ChannelRepository>& channelRepository) { mChannelRepository = channelRepository; }
70  void SetBitsConfigRepository(const std::shared_ptr<BitsConfigRepository>& repository) { mBitsConfigRepository = repository; }
74  void SetChatObjectFactory(std::shared_ptr<IChatObjectFactory> factory);
75  void SetSettingRepository(std::shared_ptr<SettingRepository> settings);
76 
92  bool IsShutdown() const { return mChatThread == nullptr; }
96  TTV_ErrorCode SendChatMessage(const std::string& message);
97 
100  TTV_ErrorCode OptInToBroadcasterLanguageChat(const std::string& language);
101  TTV_ErrorCode SetLocalLanguage(const std::string& language);
102 
106  inline UserId GetChannelId() const { return mChannelId; }
107 
108  void SetMessageFlushInterval(uint64_t milliseconds) { mUserMessageFlushInterval = milliseconds; }
110 
112 
113  std::shared_ptr<IChatChannelListener> GetChannelListener() { return mChatCallbacks; }
114  std::shared_ptr<User> GetUser() { return mUser.lock(); }
115 
116  // ChatConnection::Listener implementation
117  virtual void OnConnected(ChatConnection* source) override;
118  virtual void OnConnectionFailed(ChatConnection* source, TTV_ErrorCode ec) override;
119  virtual void OnConnectionLost(ChatConnection* source, TTV_ErrorCode ec) override;
120  virtual void OnCleared(ChatConnection* source, const std::string& username, const std::map<std::string, std::string>& messageTags) override;
121  virtual void OnPrivateMessageReceived(ChatConnection* source, const std::string& username, const std::string& message, const std::map<std::string, std::string>& messageTags, bool action) override;
122  virtual void OnUserNoticeReceived(ChatConnection* source, const std::string& message, const std::map<std::string, std::string>& messageTags) override;
123  virtual void OnUserStateChanged(ChatConnection *source, const std::map<std::string, std::string>& messageTags) override;
124  virtual void OnChatRestrictionsChanged(ChatConnection* source, const ChatChannelRestrictions& restrictions) override;
125  virtual void OnPermanentBanChanged(ChatConnection* source, bool banned) override;
126  virtual void OnTemporaryBanChanged(ChatConnection* source, bool temporarilyBanned, uint32_t timeout) override;
127  virtual void OnBadgesChanged(ChatConnection* source, const std::string& username, const std::string& badgesMessageTag) override;
128  virtual void OnHostTargetChanged(ChatConnection* source, const std::string& targetChannel, uint32_t numViewers) override;
129  virtual void OnNoticeReceived(ChatConnection* source, const std::string& id, const std::map<std::string, std::string>& params) override;
130  virtual void OnIgnoreChanged(ChatConnection* source, const std::string& blockUserName, bool ignore) override;
131 
132 private:
133  void FlushUserMessages(bool force);
134 
138  void ForceShutdown();
144  TTV_ErrorCode ProcessClientChatMessage(const std::string& msg);
149  TTV_ErrorCode ProcessOptInToBroadcasterLanguageChatRequest(const std::string& language);
150  TTV_ErrorCode ProcessSetLocalLanguageRequest(const std::string& language);
152  TTV_ErrorCode ProcessChatPropertyFetchResult(TTV_ErrorCode ec, std::shared_ptr<ChatPropertiesTask::Result> result);
153  TTV_ErrorCode ProcessBitsConfigFetchResult(TTV_ErrorCode ec, const std::shared_ptr<BitsConfiguration>& config);
154  void SetClientChannelInfo(const ChatChannelInfo& channelInfo);
155  void SetClientChatRestrictions(const ChatChannelRestrictions& restrictions);
156 
157  void HandleMessageReceived(const std::string& username, const std::string& message, const std::map<std::string, std::string>& messageTags, const MessageInfo::Flags& flags);
158  void HandleUserNotice(const std::string& message, const std::map<std::string, std::string>& messageTags);
159  void HandleSubscriptionNotice(SubscriptionNotice::Type type, const std::string& message, const std::map<std::string, std::string>& messageTags);
160  void HandleFirstTimeChatterNotice(const std::string& message, const std::map<std::string, std::string>& messageTags);
161  void HandleRaidNotice(const std::string& message, const std::map<std::string, std::string>& messageTags);
162  void HandleUnraidNotice(const std::string& message, const std::map<std::string, std::string>& messageTags);
163  void GenerateUserInfo(const std::string& username, const std::map<std::string, std::string>& messageTags, ChatUserInfo& userInfo);
164  void GenerateMessage(MessageInfo& msg, const TokenizationOptions& tokenizationOptions, const ChatUserInfo& userInfo, const std::string& message, const std::map<std::string, std::string>& messageTags, const MessageInfo::Flags& flags);
165  void GenerateLiveMessage(LiveChatMessage& msg, const TokenizationOptions& tokenizationOptions, const ChatUserInfo& userInfo, const std::string& message, const std::map<std::string, std::string>& messageTags, const MessageInfo::Flags& flags);
166 
170  void ThreadProc();
174  void CompleteShutdown();
175  bool UpdateRestriction();
178  void CloseConnection();
179 
180  void SetState(State::Enum state);
181  void HandleConnectionIssue(bool recoverableError);
182 
183  std::string mBroadcasterName;
184 
185  std::shared_ptr<IChatChannelListener> mChatCallbacks;
186  std::shared_ptr<ChatPropertiesTask::Result> mChatProperties;
187  std::vector<std::string> mHosts;
188 
189  std::shared_ptr<IChatObjectFactory> mChatObjectFactory;
190  std::shared_ptr<IThread> mChatThread;
191  std::shared_ptr<ChatConnection> mChatConnection;
192  std::shared_ptr<ChannelRepository> mChannelRepository;
193  std::shared_ptr<SettingRepository> mSettingRepository;
194  std::shared_ptr<BitsConfigRepository> mBitsConfigRepository;
195  std::shared_ptr<TaskRunner> mTaskRunner;
196  std::shared_ptr<TaskRunner> mBackgroundTaskRunner;
197 
202 
203  using ThreadEvent = std::function<void()>;
206 
207  std::vector<LiveChatMessage> mUserMessageBatch;
208 
214 
216 
219 
227 
228  std::weak_ptr<User> mUser;
229  std::vector< std::string > mQueuedChatMessages; // The list of messages that were sent from the client to the chat thread while a reconnect was pending.
230  std::shared_ptr<BitsConfiguration> mBitsConfiguration;
231 
232  std::string mLocalLanguage;
233 
237 
238  std::atomic<uint32_t> mNumOutstandingTasks;
239 
241  bool mAnonymous;
245 };
ConcurrentQueue< ThreadEvent > mToChatQ
The queue for messages from the main thread to chat thread.
Definition: chatchannel.h:205
Definition: timer.h:134
uint32_t UserId
Definition: coretypes.h:22
std::shared_ptr< User > GetUser()
Definition: chatchannel.h:114
uint64_t GetMessageFlushInterval() const
Definition: chatchannel.h:109
Definition: chattypes.h:329
ChatChannelInfo mServerChannelInfo
The current channel information on the chat thread.
Definition: chatchannel.h:224
ChannelId mChannelId
The ID of the channel.
Definition: chatchannel.h:236
Definition: chattypes.h:161
virtual void OnIgnoreChanged(ChatConnection *source, const std::string &blockUserName, bool ignore) override
void FlushUserMessages(bool force)
virtual void OnUserNoticeReceived(ChatConnection *source, const std::string &message, const std::map< std::string, std::string > &messageTags) override
void HandleFirstTimeChatterNotice(const std::string &message, const std::map< std::string, std::string > &messageTags)
WaitForExpiry mTimeoutTimer
When the user has been banned for a temporary amount of time this will mark the time when the next me...
Definition: chatchannel.h:213
TTV_ErrorCode SetLocalLanguage(const std::string &language)
std::shared_ptr< ChatPropertiesTask::Result > mChatProperties
The chat properties for the channel.
Definition: chatchannel.h:186
Definition: chatchannel.h:56
Definition: chatchannel.h:54
void SetChannelRepository(const std::shared_ptr< ChannelRepository > &channelRepository)
Definition: chatchannel.h:69
ChatChannelRestrictions mClientChatRestrictions
The chat restrictions that are active for the channel as the client sees it.
Definition: chatchannel.h:225
TTV_ErrorCode ProcessOptInToBroadcasterLanguageChatRequest(const std::string &language)
int mNextChatHostIndex
The next chat host to connect to.
Definition: chatchannel.h:201
ChatUserInfo mServerLocalUserInfo
The info for the local user on the chat thread.
Definition: chatchannel.h:222
Definition: chattypes.h:299
bool mDisconnectionRequested
Whether or not the request to disconnect has been satisfied (callback fired to client).
Definition: chatchannel.h:242
void SetSettingRepository(std::shared_ptr< SettingRepository > settings)
void HandleSubscriptionNotice(SubscriptionNotice::Type type, const std::string &message, const std::map< std::string, std::string > &messageTags)
TTV_ErrorCode ProcessBitsConfigFetchResult(TTV_ErrorCode ec, const std::shared_ptr< BitsConfiguration > &config)
Definition: chatconnection.h:40
Definition: chattypes.h:169
TTV_ErrorCode ProcessChannelInfoFetchResult(const ChannelInfo &channelInfo)
TTV_ErrorCode FetchChannelInfo()
State::Enum mChannelState
The current state, accessed only through the chat thread.
Definition: chatchannel.h:217
void HandleUserNotice(const std::string &message, const std::map< std::string, std::string > &messageTags)
Definition: chatconnection.h:43
std::string mBroadcasterName
Definition: chatchannel.h:183
void HandleUnraidNotice(const std::string &message, const std::map< std::string, std::string > &messageTags)
ChatUserInfo mClientLocalUserInfo
The info for the local user on the client thread.
Definition: chatchannel.h:221
WaitForExpiry mFetchBitsConfigTimer
The timer to use for fetching the channel&#39;s bit configuration.
Definition: chatchannel.h:199
TTV_ErrorCode SendChatMessage(const std::string &message)
ChatChannelState GetState() const
Definition: chatchannel.h:111
Definition: chattypes.h:293
bool mBroadcasterLanguageOptedIn
Whether or not the user opted in to native language chat.
Definition: chatchannel.h:244
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
virtual void OnPermanentBanChanged(ChatConnection *source, bool banned) override
WaitForExpiry mSlowModeTimer
When the channel is in slow mode this will mark the time when the next message is allowed...
Definition: chatchannel.h:212
RetryTimer mConnectionRetryTimer
The master timer to use for connection retries.
Definition: chatchannel.h:200
std::vector< std::string > mQueuedChatMessages
The local user connecting to the channel.
Definition: chatchannel.h:229
std::shared_ptr< IChatChannelListener > GetChannelListener()
Definition: chatchannel.h:113
virtual void OnConnectionFailed(ChatConnection *source, TTV_ErrorCode ec) override
Enum
Definition: chatchannel.h:50
bool mAnonymous
Whether or not connecting anonymously to the channel.
Definition: chatchannel.h:241
uint64_t LookupId
Definition: chatchannel.h:63
void SetClientChatRestrictions(const ChatChannelRestrictions &restrictions)
Definition: chathelpers.h:35
Definition: chatchannel.h:48
ChatChannelInfo mClientChannelInfo
The current channel information as the client sees it.
Definition: chatchannel.h:223
TTV_ErrorCode ProcessClientRequestQueue()
std::shared_ptr< SettingRepository > mSettingRepository
Definition: chatchannel.h:193
void UpdateBroadcasterLanguageChatState()
std::shared_ptr< IChatObjectFactory > mChatObjectFactory
The internal chat object factory.
Definition: chatchannel.h:189
bool IsShutdown() const
Definition: chatchannel.h:92
std::function< void()> ThreadEvent
Definition: chatchannel.h:203
Definition: timer.h:52
std::shared_ptr< IThread > mChatThread
The thread managing the socket.
Definition: chatchannel.h:190
std::vector< LiveChatMessage > mUserMessageBatch
A batch of messages to be sent together to the client instead of one at a time (for performance)...
Definition: chatchannel.h:207
uint64_t mUserMessageFlushInterval
The max amount of time between user message flushes to the main thread.
Definition: chatchannel.h:209
bool mBroadcasterLanguageMatch
Whether or not the local language matches the active native chat language.
Definition: chatchannel.h:243
TTV_ErrorCode SetBroadcasterLanguageChatEnabled(bool enabled, const SetBroadcasterLanguageChatCallback &callback)
LookupId mBitsConfigFetchToken
The cancellation token for fetching the BitsConfiguration.
Definition: chatchannel.h:235
std::shared_ptr< ChatConnection > mChatConnection
The active connection.
Definition: chatchannel.h:191
std::atomic< uint32_t > mNumOutstandingTasks
Definition: chatchannel.h:238
TTV_ErrorCode FetchChatProperties()
ChatUserInfo mSystemUserInfo
The display information for messages from JTV or TwitchNotify.
Definition: chatchannel.h:220
ChatChannel(const std::shared_ptr< User > &user, ChannelId channelId, std::shared_ptr< IChatChannelListener > chatCallbacks, std::shared_ptr< TaskRunner > taskRunner)
void SetClientChannelInfo(const ChatChannelInfo &channelInfo)
std::vector< std::string > mHosts
The hosts to use to connect to the channel.
Definition: chatchannel.h:187
std::shared_ptr< TaskRunner > mBackgroundTaskRunner
Definition: chatchannel.h:196
virtual void OnConnectionLost(ChatConnection *source, TTV_ErrorCode ec) override
Type
Definition: chattypes.h:375
uint32_t TTV_ErrorCode
Definition: errortypes.h:30
void GenerateUserInfo(const std::string &username, const std::map< std::string, std::string > &messageTags, ChatUserInfo &userInfo)
Definition: chatchannel.h:53
ChatChannelRestrictions mServerChatRestrictions
The chat restrictions that are active for the channel on the chat thread.
Definition: chatchannel.h:226
ChatChannelState
Definition: chattypes.h:60
UserId GetChannelId() const
Definition: chatchannel.h:106
virtual void OnBadgesChanged(ChatConnection *source, const std::string &username, const std::string &badgesMessageTag) override
ChatChannelState mReportedClientState
The last reported state.
Definition: chatchannel.h:218
virtual void OnPrivateMessageReceived(ChatConnection *source, const std::string &username, const std::string &message, const std::map< std::string, std::string > &messageTags, bool action) override
void HandleRaidNotice(const std::string &message, const std::map< std::string, std::string > &messageTags)
std::shared_ptr< IChatChannelListener > mChatCallbacks
Client-configured callbacks for chat notifications for this channel.
Definition: chatchannel.h:185
void HandleConnectionIssue(bool recoverableError)
TTV_ErrorCode ProcessSetLocalLanguageRequest(const std::string &language)
virtual void OnUserStateChanged(ChatConnection *source, const std::map< std::string, std::string > &messageTags) override
void HandleMessageReceived(const std::string &username, const std::string &message, const std::map< std::string, std::string > &messageTags, const MessageInfo::Flags &flags)
virtual void OnNoticeReceived(ChatConnection *source, const std::string &id, const std::map< std::string, std::string > &params) override
std::shared_ptr< BitsConfigRepository > mBitsConfigRepository
Definition: chatchannel.h:194
WaitForExpiry mFetchChatPropertiesTimer
The timer to use for fetching chat properties.
Definition: chatchannel.h:198
void GenerateLiveMessage(LiveChatMessage &msg, const TokenizationOptions &tokenizationOptions, const ChatUserInfo &userInfo, const std::string &message, const std::map< std::string, std::string > &messageTags, const MessageInfo::Flags &flags)
TTV_ErrorCode ProcessClientChatMessage(const std::string &msg)
TTV_ErrorCode mConnectionError
Definition: chatchannel.h:215
std::weak_ptr< User > mUser
Definition: chatchannel.h:228
ConcurrentQueue< ThreadEvent > mToMainQ
The queue for messages from the chat thread to main thread.
Definition: chatchannel.h:204
std::shared_ptr< BitsConfiguration > mBitsConfiguration
Definition: chatchannel.h:230
std::string mLocalLanguage
The language used by the local user.
Definition: chatchannel.h:232
TTV_ErrorCode AttemptConnection()
Definition: chatchannel.h:45
TTV_ErrorCode ProcessChatPropertyFetchResult(TTV_ErrorCode ec, std::shared_ptr< ChatPropertiesTask::Result > result)
ChatMessagePacer mMessagePacer
Prevents the local user from sending too many messages too close together in time and getting banned...
Definition: chatchannel.h:211
void SetBitsConfigRepository(const std::shared_ptr< BitsConfigRepository > &repository)
Definition: chatchannel.h:70
virtual void OnCleared(ChatConnection *source, const std::string &username, const std::map< std::string, std::string > &messageTags) override
TTV_ErrorCode FlushClientEvents()
TTV_ErrorCode Disconnect()
TTV_ErrorCode Connect()
void SetState(State::Enum state)
Definition: chatchannel.h:52
TTV_ErrorCode FetchBitsConfiguration()
virtual void OnHostTargetChanged(ChatConnection *source, const std::string &targetChannel, uint32_t numViewers) override
std::function< void(TTV_ErrorCode ec, UserList &&userList)> FetchUserListCallback
Definition: chatchannel.h:61
virtual void OnTemporaryBanChanged(ChatConnection *source, bool temporarilyBanned, uint32_t timeout) override
void SetMessageFlushInterval(uint64_t milliseconds)
Definition: chatchannel.h:108
uint32_t ChannelId
Definition: coretypes.h:23
TTV_ErrorCode OptInToBroadcasterLanguageChat(const std::string &language)
WaitForExpiry mNextUserMessageFlush
The next time in which to automatically flush all user messages to the main thread.
Definition: chatchannel.h:210
TokenizationOptions mTokenizationOptions
Marks what to tokenize in a message.
Definition: chatchannel.h:234
Definition: chattypes.h:126
Definition: coretypes.h:58
std::function< void(TTV_ErrorCode ec)> SetBroadcasterLanguageChatCallback
Definition: chatchannel.h:62
std::shared_ptr< ChannelRepository > mChannelRepository
Definition: chatchannel.h:192
Definition: chattypes.h:41
void SetTokenizationOptions(const TokenizationOptions &options)
Definition: chatchannel.h:68
bool mApplySlowMode
Definition: chatchannel.h:240
void GenerateMessage(MessageInfo &msg, const TokenizationOptions &tokenizationOptions, const ChatUserInfo &userInfo, const std::string &message, const std::map< std::string, std::string > &messageTags, const MessageInfo::Flags &flags)
virtual void OnConnected(ChatConnection *source) override
std::shared_ptr< TaskRunner > mTaskRunner
Definition: chatchannel.h:195
void SetChatObjectFactory(std::shared_ptr< IChatObjectFactory > factory)
virtual void OnChatRestrictionsChanged(ChatConnection *source, const ChatChannelRestrictions &restrictions) override
TTV_ErrorCode FetchUserList(const FetchUserListCallback &callback)
TTV_ErrorCode ProcessDisconnectRequest()