Twitch SDK (Internal)
chatchannelset.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 
14 
15 #include <string>
16 #include <vector>
17 #include <unordered_set>
18 #include <map>
19 #include <memory>
20 #include <atomic>
21 
22 namespace ttv
23 {
24  class TaskRunner;
25  class User;
26  class SettingRepository;
27  class ChannelRepository;
28 
29  namespace chat
30  {
31  class ChatChannel;
32  class IChatObjectFactory;
33  class ChatChannelSet;
34  class ChatChannelWrapper;
35  class BitsConfigRepository;
36  }
37 }
38 
39 
41 {
42 public:
43  using FetchUserListCallback = std::function<void(TTV_ErrorCode ec, UserList&& userList)>;
44  using SetBroadcasterLanguageChatCallback = std::function<void(TTV_ErrorCode ec)>;
45 
46  ChatChannelSet(const std::shared_ptr<User>& user);
47  virtual ~ChatChannelSet() override;
48 
49  void SetTokenizationOptions(const TokenizationOptions& tokenizationOptions) { mTokenizationOptions = tokenizationOptions; }
50  void SetChatObjectFactory(std::shared_ptr<IChatObjectFactory> factory) { mChatObjectFactory = factory; }
51  void SetSettingRepository(std::shared_ptr<SettingRepository> settings) { mSettingRepository = settings; }
52  void SetChannelRepository(const std::shared_ptr<ChannelRepository>& channelRepository) { mChannelRepository = channelRepository; }
53  void SetBitsConfigRepository(std::shared_ptr<BitsConfigRepository> repository) { mBitsConfigRepository = repository; }
54 
55  virtual void Update() override;
56  virtual TTV_ErrorCode Shutdown() override;
57 
58  TTV_ErrorCode Connect(ChannelId channelId, const std::shared_ptr<IChatChannelListener>& listener);
60  TTV_ErrorCode SendChatMessage(ChannelId channelId, const std::string& message);
61  TTV_ErrorCode FetchUserList(ChannelId channelId, const FetchUserListCallback& callback);
62 
64  TTV_ErrorCode OptInToBroadcasterLanguageChat(ChannelId channelId, const std::string& language);
65  TTV_ErrorCode SetLocalLanguage(const std::string& language);
66 
67  void SetMessageFlushInterval(uint64_t milliseconds);
69 
70  virtual std::string GetLoggerName() const override;
71  static std::string GetComponentName() { return "ttv::chat::ChatChannelSet"; }
72 
73 protected:
74  virtual bool CheckShutdown() override;
75  virtual void CompleteShutdown() override;
76 
77 private:
78  struct ChannelEntry
79  {
80  std::shared_ptr<ChatChannel> channel;
81  std::shared_ptr<IChatChannelListener> channelListener;
82  };
83 
85  {
86  public:
88 
89  // IChatChannelListener implementation
90  virtual void ChatChannelStateChanged(UserId userId, ChannelId channelId, ChatChannelState state, TTV_ErrorCode ec) override;
91  virtual void ChatChannelInfoChanged(UserId userId, ChannelId channelId, const ChatChannelInfo& channelInfo) override;
92  virtual void ChatChannelRestrictionsChanged(UserId userId, ChannelId channelId, const ChatChannelRestrictions& restrictions) override;
93  virtual void ChatChannelLocalUserChanged(UserId userId, ChannelId channelId, const ChatUserInfo& userInfo) override;
94  virtual void ChatChannelMessagesReceived(UserId userId, ChannelId channelId, const std::vector<LiveChatMessage>& messageList) override;
95  virtual void ChatChannelSubscriptionNoticeReceived(UserId userId, ChannelId channelId, const SubscriptionNotice& notice) override;
96  virtual void ChatChannelFirstTimeChatterNoticeReceived(UserId userId, ChannelId channelId, const FirstTimeChatterNotice& notice) override;
97  virtual void ChatChannelRaidNoticeReceived(UserId userId, ChannelId channelId, const RaidNotice& notice) override;
98  virtual void ChatChannelUnraidNoticeReceived(UserId userId, ChannelId channelId, const UnraidNotice& notice) override;
99  virtual void ChatChannelMessagesCleared(UserId userId, ChannelId channelId) override;
100  virtual void ChatChannelUserMessagesCleared(UserId userId, ChannelId channelId, UserId clearUserId) override;
101  virtual void ChatChannelHostTargetChanged(UserId userId, ChannelId channelId, const std::string& targetChannelName, uint32_t numViewers) override;
102  virtual void ChatChannelNoticeReceived(UserId userId, ChannelId channelId, const std::string& id, const std::map<std::string, std::string>& params) override;
103 
104  private:
106  };
107 
108  // Private IChatChannelListener implementation
109  void ChatChannelStateChanged(UserId userId, ChannelId channelId, ChatChannelState state, TTV_ErrorCode ec);
110  void ChatChannelInfoChanged(UserId userId, ChannelId channelId, const ChatChannelInfo& channelInfo);
111  void ChatChannelRestrictionsChanged(UserId userId, ChannelId channelId, const ChatChannelRestrictions& restrictions);
112  void ChatChannelLocalUserChanged(UserId userId, ChannelId channelId, const ChatUserInfo& userInfo);
113  void ChatChannelMessagesReceived(UserId userId, ChannelId channelId, const std::vector<LiveChatMessage>& messageList);
114  void ChatChannelSubscriptionNoticeReceived(UserId userId, ChannelId channelId, const SubscriptionNotice& notice);
116  void ChatChannelRaidNoticeReceived(UserId userId, ChannelId channelId, const RaidNotice& notice);
117  void ChatChannelUnraidNoticeReceived(UserId userId, ChannelId channelId, const UnraidNotice& notice);
118  void ChatChannelMessagesCleared(UserId userId, ChannelId channelId);
119  void ChatChannelUserMessagesCleared(UserId userId, ChannelId channelId, UserId clearUserId);
120  void ChatChannelHostTargetChanged(UserId userId, ChannelId channelId, const std::string& targetChannelName, uint32_t numViewers);
121  void ChatChannelNoticeReceived(UserId userId, ChannelId channelId, const std::string& id, const std::map<std::string, std::string>& params);
122 
123  TTV_ErrorCode LookupChannel(ChannelId channelId, std::shared_ptr<ChatChannel>& channel);
125  std::shared_ptr<ChannelEntry> CreateChannel(ChannelId channelId);
126 
127  std::shared_ptr<IChatObjectFactory> mChatObjectFactory;
128  std::shared_ptr<IChatChannelListener> mInternalChannelListener;
129  std::shared_ptr<SettingRepository> mSettingRepository;
130  std::shared_ptr<ChannelRepository> mChannelRepository;
131  std::shared_ptr<BitsConfigRepository> mBitsConfigRepository;
132  std::map<ChannelId, std::shared_ptr<ChannelEntry>> mChannels;
133  std::vector<std::shared_ptr<ChannelEntry>> mCleanupChannels;
134 
135  std::string mLocalLanguage;
136 
138 
140 };
141 
142 
144 {
145 public:
146  using DisposerFunc = std::function<void()>;
147 
148 public:
149  ChatChannelWrapper(const std::shared_ptr<User>& user, ChannelId channelId, const std::shared_ptr<IChatChannelListener>& listener);
150  virtual ~ChatChannelWrapper();
151 
152  void SetDisposer(DisposerFunc&& func) { mDisposerFunc = func; }
153  std::shared_ptr<ChatChannelSet> GetChatChannelSet() const { return mChatChannelSet; }
154 
155  // IChatChannel implementation
156  virtual TTV_ErrorCode Dispose() override;
157  virtual TTV_ErrorCode Connect() override;
158  virtual TTV_ErrorCode Disconnect() override;
159  virtual TTV_ErrorCode SendMessage(const std::string& message) override;
160  virtual TTV_ErrorCode SetBroadcasterLanguageChatEnabled(bool enabled, const SetBroadcasterLanguageChatEnabledCallback& callback) override;
161  virtual TTV_ErrorCode OptInToBroadcasterLanguageChat(const std::string& language) override;
162 
163 private:
165  std::shared_ptr<ChatChannelSet> mChatChannelSet;
166  std::shared_ptr<IChatChannelListener> mChatChannelListener;
168 };
uint32_t UserId
Definition: coretypes.h:22
std::shared_ptr< IChatObjectFactory > mChatObjectFactory
Definition: chatchannelset.h:127
std::shared_ptr< IChatChannelListener > mInternalChannelListener
The internal chat object factory.
Definition: chatchannelset.h:128
ChatChannelSet(const std::shared_ptr< User > &user)
void ChatChannelUnraidNoticeReceived(UserId userId, ChannelId channelId, const UnraidNotice &notice)
void SetChatObjectFactory(std::shared_ptr< IChatObjectFactory > factory)
Definition: chatchannelset.h:50
Definition: ichatchannel.h:27
Definition: chattypes.h:161
DisposerFunc mDisposerFunc
Definition: chatchannelset.h:164
Definition: chattypes.h:369
Definition: chatchannelset.h:78
void SetTokenizationOptions(const TokenizationOptions &tokenizationOptions)
Definition: chatchannelset.h:49
void ChatChannelUserMessagesCleared(UserId userId, ChannelId channelId, UserId clearUserId)
uint64_t GetMessageFlushInterval() const
Definition: chatchannelset.h:68
TTV_ErrorCode Disconnect(ChannelId channelId)
static std::string GetComponentName()
Definition: chatchannelset.h:71
virtual void CompleteShutdown() override
std::vector< std::shared_ptr< ChannelEntry > > mCleanupChannels
The channels that should be released after all events have been flushed.
Definition: chatchannelset.h:133
void SetChannelRepository(const std::shared_ptr< ChannelRepository > &channelRepository)
Definition: chatchannelset.h:52
Definition: chattypes.h:169
std::shared_ptr< IChatChannelListener > mChatChannelListener
Definition: chatchannelset.h:166
ChannelId mChannelId
Definition: chatchannelset.h:167
TTV_ErrorCode SendChatMessage(ChannelId channelId, const std::string &message)
void ChatChannelMessagesReceived(UserId userId, ChannelId channelId, const std::vector< LiveChatMessage > &messageList)
virtual bool CheckShutdown() override
std::shared_ptr< ChannelEntry > CreateChannel(ChannelId channelId)
TTV_ErrorCode SetLocalLanguage(const std::string &language)
std::function< void()> DisposerFunc
Definition: chatchannelset.h:146
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
Definition: chatlistener.h:43
TokenizationOptions mTokenizationOptions
Definition: chatchannelset.h:137
std::function< void(TTV_ErrorCode ec)> SetBroadcasterLanguageChatCallback
Definition: chatchannelset.h:44
std::function< void(TTV_ErrorCode ec, UserList &&userList)> FetchUserListCallback
Definition: chatchannelset.h:43
std::function< void(TTV_ErrorCode ec)> SetBroadcasterLanguageChatEnabledCallback
Definition: ichatchannel.h:30
virtual std::string GetLoggerName() const override
TTV_ErrorCode SetBroadcasterLanguageChatEnabled(ChannelId channelId, bool enabled, const SetBroadcasterLanguageChatCallback &callback)
void ChatChannelNoticeReceived(UserId userId, ChannelId channelId, const std::string &id, const std::map< std::string, std::string > &params)
std::shared_ptr< ChatChannel > channel
Definition: chatchannelset.h:80
void SetDisposer(DisposerFunc &&func)
Definition: chatchannelset.h:152
void ChatChannelMessagesCleared(UserId userId, ChannelId channelId)
ChatChannelSet * mOwner
Definition: chatchannelset.h:105
void ChatChannelHostTargetChanged(UserId userId, ChannelId channelId, const std::string &targetChannelName, uint32_t numViewers)
TTV_ErrorCode FlushChannelEvents()
void SetMessageFlushInterval(uint64_t milliseconds)
uint32_t TTV_ErrorCode
Definition: errortypes.h:30
void ChatChannelFirstTimeChatterNoticeReceived(UserId userId, ChannelId channelId, const FirstTimeChatterNotice &notice)
Definition: component.h:87
ChatChannelState
Definition: chattypes.h:60
TTV_ErrorCode LookupChannel(ChannelId channelId, std::shared_ptr< ChatChannel > &channel)
std::shared_ptr< IChatChannelListener > channelListener
Definition: chatchannelset.h:81
void ChatChannelLocalUserChanged(UserId userId, ChannelId channelId, const ChatUserInfo &userInfo)
uint64_t mUserMessageFlushInterval
The max amount of time between user message flushes to the main thread.
Definition: chatchannelset.h:139
virtual ~ChatChannelSet() override
void ChatChannelStateChanged(UserId userId, ChannelId channelId, ChatChannelState state, TTV_ErrorCode ec)
std::shared_ptr< SettingRepository > mSettingRepository
The listener for channel events.
Definition: chatchannelset.h:129
void ChatChannelRaidNoticeReceived(UserId userId, ChannelId channelId, const RaidNotice &notice)
std::shared_ptr< ChatChannelSet > GetChatChannelSet() const
Definition: chatchannelset.h:153
virtual TTV_ErrorCode Shutdown() override
TTV_ErrorCode FetchUserList(ChannelId channelId, const FetchUserListCallback &callback)
void SetSettingRepository(std::shared_ptr< SettingRepository > settings)
Definition: chatchannelset.h:51
std::shared_ptr< ChatChannelSet > mChatChannelSet
Definition: chatchannelset.h:165
TTV_ErrorCode OptInToBroadcasterLanguageChat(ChannelId channelId, const std::string &language)
virtual void Update() override
Definition: chatchannelset.h:40
TTV_ErrorCode Connect(ChannelId channelId, const std::shared_ptr< IChatChannelListener > &listener)
uint32_t ChannelId
Definition: coretypes.h:23
std::shared_ptr< BitsConfigRepository > mBitsConfigRepository
Definition: chatchannelset.h:131
Definition: chattypes.h:447
Definition: chattypes.h:126
Definition: chattypes.h:41
std::string mLocalLanguage
The local user&#39;s language.
Definition: chatchannelset.h:135
std::map< ChannelId, std::shared_ptr< ChannelEntry > > mChannels
The mapping of lower case channel name to channel.
Definition: chatchannelset.h:132
Definition: chattypes.h:434
void ChatChannelInfoChanged(UserId userId, ChannelId channelId, const ChatChannelInfo &channelInfo)
Definition: chattypes.h:423
std::shared_ptr< ChannelRepository > mChannelRepository
Definition: chatchannelset.h:130
void ChatChannelRestrictionsChanged(UserId userId, ChannelId channelId, const ChatChannelRestrictions &restrictions)
void SetBitsConfigRepository(std::shared_ptr< BitsConfigRepository > repository)
Definition: chatchannelset.h:53
void ChatChannelSubscriptionNoticeReceived(UserId userId, ChannelId channelId, const SubscriptionNotice &notice)
Definition: chatchannelset.h:143