Twitch SDK (Internal)
chatuserthreads.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-2017 Twitch Interactive, Inc.
7 *********************************************************************************************/
8 
9 #pragma once
10 
20 #include <string>
21 #include <vector>
22 #include <unordered_set>
23 #include <map>
24 #include <memory>
25 #include <atomic>
26 
27 namespace ttv
28 {
29  class TaskRunner;
30  class Task;
31  class User;
32  class UserRepository;
33  class SettingRepository;
34  class PagedRequestFetcher;
35 
36  namespace chat
37  {
38  class ChatUserThreads;
39  class ChatGetDisplayInfoTask;
40  class ChatGetUserThreadsTask;
41  class ChatGetUnreadMessageCountTask;
42  class BitsConfigRepository;
43  }
44 }
45 
46 
51 {
52 public:
53  using SendMessageCallback = std::function<void(TTV_ErrorCode ec, MessageId messageId)>;
54  using FetchThreadDataPageCallback = std::function<void(TTV_ErrorCode ec, const std::vector<ThreadData>& page, uint32_t total)>;
55  using FetchThreadDataCallback = std::function<void(TTV_ErrorCode ec, const ThreadData& data)>;
56  using FetchUnreadCountsCallback = std::function<void(TTV_ErrorCode ec, const UnreadThreadCounts& counts)>;
57 
58  ChatUserThreads(std::shared_ptr<User> user);
59  virtual ~ChatUserThreads();
60 
62  void SetUserRepository(std::shared_ptr<UserRepository> repository) { mUserRepository = repository; }
63  void SetSettingRepository(std::shared_ptr<SettingRepository> settings);
64  void SetBitsConfigRepository(std::shared_ptr<BitsConfigRepository> repository) { mBitsConfigRepository = repository; }
65  void SetListener(std::shared_ptr<IChatUserThreadsListener> listener);
66 
67  // Component overrides
68  virtual TTV_ErrorCode Initialize() override;
69  virtual void Update() override;
70  virtual TTV_ErrorCode Shutdown() override;
71  virtual std::string GetLoggerName() const override;
72  virtual void OnUserInfoFetchComplete(TTV_ErrorCode ec) override;
73 
78  TTV_ErrorCode SendMessageToUser(UserId userId, const std::string& message, WhisperMessage& placeholderMessage, const SendMessageCallback& callback);
79 
83  TTV_ErrorCode FetchThreadDataPage(uint32_t offset, uint32_t pageSize, const FetchThreadDataPageCallback& callback);
87  TTV_ErrorCode FetchThreadData(const std::string& threadId, const FetchThreadDataCallback& callback);
88 
92  TTV_ErrorCode RemoveThread(const std::string& threadId);
93 
98 
103 
108 
113 
114  std::shared_ptr<ChatUserThread> GetThread(const std::string& threadId);
115 
116  static std::string GenerateThreadId(UserId a, UserId b);
117 
118  static std::string GetComponentName() { return "ttv::chat::ChatUserThreads"; }
119 
120 protected:
121  // Component overrides
122  virtual bool CheckShutdown() override;
123  virtual void CompleteShutdown() override;
124 
125 private:
127  {
128  public:
130 
131  // PubSubClient::ITopicListener implementation
132  virtual void OnTopicSubscribeStateChanged(PubSubClient* source, const std::string& topic, PubSubClient::SubscribeState::Enum state, TTV_ErrorCode ec) override;
133  virtual void OnTopicMessageReceived(PubSubClient* source, const std::string& topic, const json::Value& msg) override;
134  virtual void OnTopicListenerRemoved(PubSubClient* source, const std::string& topic, TTV_ErrorCode ec) override;
135 
136  private:
138  };
139 
141  {
142  public:
144 
145  // ChatThread::Listener implementation
146  virtual void OnMetadataFetchComplete(ChatUserThread* source, TTV_ErrorCode ec) override;
147  virtual void OnRealtimeMessageReceived(ChatUserThread* source, const WhisperMessage& message) override;
148  virtual void OnParticipantsUpdated(ChatUserThread* source, const std::vector<ChatUserInfo>& participants) override;
149  virtual void OnMuteStatusChanged(ChatUserThread* source, bool muted) override;
150  virtual void OnUnreadMessageWindowChanged(ChatUserThread* source, MessageId lastMessageId, MessageId lastReadMessageId) override;
151 
152  private:
154  };
155 
156  // PubSubClient::ITopicListener implementation
157  void OnTopicSubscribeStateChanged(const std::string& topic, PubSubClient::SubscribeState::Enum state, TTV_ErrorCode ec);
158  void OnTopicMessageReceived(const std::string& topic, const json::Value& msg);
159 
160  // Private ChatThread::Listener implementation
162  void OnRealtimeMessageReceived(ChatUserThread* source, const WhisperMessage& message);
163  void OnParticipantsUpdated(ChatUserThread* source, const std::vector<ChatUserInfo>& participants);
164  void OnMuteStatusChanged(ChatUserThread* source, bool muted);
165  void OnUnreadMessageWindowChanged(ChatUserThread* source, MessageId lastMessageId, MessageId lastReadMessageId);
166 
168  TTV_ErrorCode SendMessageWithThread(const std::shared_ptr<ChatUserThread>& thread, UserId otherUserId, const std::string& message, const SendMessageCallback& callback);
169  void ProcessSentNonce(const std::string& nonce, MessageId messageId, TTV_ErrorCode ec);
171  void MergeThreads(const std::vector<ThreadData>& threadList);
172  std::shared_ptr<ChatUserThread> CreateThread(const std::string& threadId, const ThreadData* data);
173  void BumpThread(const std::string& threadId);
175  TTV_ErrorCode TokenizeLocalMessage(const std::shared_ptr<User>& user, const std::string& message, const std::string& threadId, WhisperMessage& chatMessage);
177  void RequestUnreadCounts();
178 
179  std::weak_ptr<UserRepository> mUserRepository;
180  std::shared_ptr<SettingRepository> mSettingRepository;
181  std::shared_ptr<BitsConfigRepository> mBitsConfigRepository;
182  std::shared_ptr<BitsConfiguration> mBitsConfiguration;
183  std::map<std::string, std::shared_ptr<ChatUserThread>> mThreadMap;
184  std::vector<std::shared_ptr<ChatUserThread>> mThreadList;
185  std::map<std::string, SendMessageCallback> mPendingSentCallbacksByNonce;
186  std::shared_ptr<ChatGetUnreadMessageCountTask> mChatGetUnreadMessageCountTask;
187  std::shared_ptr<ChatGetDisplayInfoTask> mChatGetDisplayInfoTask;
188  std::shared_ptr<ChatUserThread::Listener> mInternalThreadListener;
190 
191  std::shared_ptr<PagedRequestFetcher> mThreadPageFetcher;
192 
193  std::shared_ptr<PubSubClient> mPubSub;
194  std::shared_ptr<PubSubTopicListener> mPubSubTopicListener;
195  std::shared_ptr<PubSubTopicListenerHelper> mPubSubTopicListenerHelper;
196  std::string mWhisperPubSubTopic;
197 
199 
204  std::unique_ptr<UnreadThreadCache> mUnreadThreadCache;
210 };
Definition: timer.h:134
uint32_t UserId
Definition: coretypes.h:22
Definition: chattypes.h:528
EventSource< IChatUserThreadsListener > mListeners
Definition: chatuserthreads.h:189
std::shared_ptr< PagedRequestFetcher > mThreadPageFetcher
Definition: chatuserthreads.h:191
std::function< void(TTV_ErrorCode ec, MessageId messageId)> SendMessageCallback
Definition: chatuserthreads.h:53
void OnMetadataFetchComplete(ChatUserThread *source, TTV_ErrorCode ec)
TTV_ErrorCode FetchBitsConfig()
void SetBitsConfigRepository(std::shared_ptr< BitsConfigRepository > repository)
Definition: chatuserthreads.h:64
void OnUnreadMessageWindowChanged(ChatUserThread *source, MessageId lastMessageId, MessageId lastReadMessageId)
TTV_ErrorCode FetchThreadData(const std::string &threadId, const FetchThreadDataCallback &callback)
Definition: chatuserthread.h:42
virtual TTV_ErrorCode Initialize() override
std::shared_ptr< PubSubClient > mPubSub
Definition: chatuserthreads.h:193
uint32_t MessageId
Definition: chattypes.h:26
virtual std::string GetLoggerName() const override
std::shared_ptr< PubSubTopicListener > mPubSubTopicListener
Definition: chatuserthreads.h:194
void SetSettingRepository(std::shared_ptr< SettingRepository > settings)
std::shared_ptr< BitsConfiguration > mBitsConfiguration
Definition: chatuserthreads.h:182
Enum
Definition: pubsubclient.h:91
std::function< void(TTV_ErrorCode ec, const ThreadData &data)> FetchThreadDataCallback
Definition: chatuserthreads.h:55
WaitForExpiry mUnreadSyncTimer
When to update the unread count information from the backend.
Definition: chatuserthreads.h:200
bool mBitsConfigFetchInFlight
Definition: chatuserthreads.h:208
virtual void Update() override
uint32_t mMaxFetchedThreadOffset
The maximum offset for loaded thread pages.
Definition: chatuserthreads.h:206
static std::string GenerateThreadId(UserId a, UserId b)
void OnParticipantsUpdated(ChatUserThread *source, const std::vector< ChatUserInfo > &participants)
TTV_ErrorCode RemoveThread(const std::string &threadId)
CallbackQueue< FetchUnreadCountsCallback > mUnreadCountCallbacks
Definition: chatuserthreads.h:205
void UpdateCachedThreadWithLiveMessage(const WhisperMessage &message)
std::shared_ptr< BitsConfigRepository > mBitsConfigRepository
Definition: chatuserthreads.h:181
void SetTokenizationOptions(const TokenizationOptions &options)
Definition: chatuserthreads.h:61
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
virtual void OnUserInfoFetchComplete(TTV_ErrorCode ec) override
virtual void OnTopicSubscribeStateChanged(PubSubClient *source, const std::string &topic, PubSubClient::SubscribeState::Enum state, TTV_ErrorCode ec) override
std::function< void(TTV_ErrorCode ec, const std::vector< ThreadData > &page, uint32_t total)> FetchThreadDataPageCallback
Definition: chatuserthreads.h:54
std::map< std::string, std::shared_ptr< ChatUserThread > > mThreadMap
The mapping of thread id to thread.
Definition: chatuserthreads.h:183
std::shared_ptr< ChatGetUnreadMessageCountTask > mChatGetUnreadMessageCountTask
Definition: chatuserthreads.h:186
Represents a JSON value.
Definition: value.h:114
Definition: timer.h:52
std::string mWhisperPubSubTopic
The topic used to subscribe for realtime whisper events.
Definition: chatuserthreads.h:196
void OnRealtimeMessageReceived(ChatUserThread *source, const WhisperMessage &message)
std::function< void(TTV_ErrorCode ec, const UnreadThreadCounts &counts)> FetchUnreadCountsCallback
Definition: chatuserthreads.h:56
Definition: pubsubclient.h:86
std::map< std::string, SendMessageCallback > mPendingSentCallbacksByNonce
Sent message nonces on which we are waiting for an acknowledgement from the server.
Definition: chatuserthreads.h:185
Definition: eventsource.h:18
TTV_ErrorCode SendMessageToUser(UserId userId, const std::string &message, WhisperMessage &placeholderMessage, const SendMessageCallback &callback)
void ProcessSentNonce(const std::string &nonce, MessageId messageId, TTV_ErrorCode ec)
void SetListener(std::shared_ptr< IChatUserThreadsListener > listener)
RetryTimer mCachedThreadSyncTimer
When to refresh the content of cached threads.
Definition: chatuserthreads.h:202
TTV_ErrorCode FetchUnreadCounts(const FetchUnreadCountsCallback &callback)
TokenizationOptions mTokenizationOptions
Definition: chatuserthreads.h:207
uint32_t TTV_ErrorCode
Definition: errortypes.h:30
TTV_ErrorCode SubscribeTopics()
Definition: component.h:87
void BumpThread(const std::string &threadId)
Definition: chatuserthread.h:39
TTV_ErrorCode RemoveThreadWithUser(UserId userId)
Definition: pubsubclient.h:117
Definition: chatuserthreads.h:126
TTV_ErrorCode FetchChatDisplayInfo()
void SetUserRepository(std::shared_ptr< UserRepository > repository)
Definition: chatuserthreads.h:62
virtual bool CheckShutdown() override
std::shared_ptr< ChatUserThread > GetThread(const std::string &threadId)
virtual void OnTopicMessageReceived(PubSubClient *source, const std::string &topic, const json::Value &msg) override
bool mHasFetchedBitsConfig
Definition: chatuserthreads.h:209
std::vector< std::shared_ptr< ChatUserThread > > mThreadList
The ordered list of cached threads, sorted by decreasing timestamp.
Definition: chatuserthreads.h:184
virtual TTV_ErrorCode Shutdown() override
ChatUserInfo mChatUserInfo
The cached user info for the local user.
Definition: chatuserthreads.h:198
TTV_ErrorCode FetchThreadDataPage(uint32_t offset, uint32_t pageSize, const FetchThreadDataPageCallback &callback)
TTV_ErrorCode TokenizeLocalMessage(const std::shared_ptr< User > &user, const std::string &message, const std::string &threadId, WhisperMessage &chatMessage)
WaitForExpiry mUserChatDisplayFetchTimer
When to fetch the enabled badges and user color info from the backend.
Definition: chatuserthreads.h:201
ChatUserThreads * mOwner
Definition: chatuserthreads.h:153
std::unique_ptr< UnreadThreadCache > mUnreadThreadCache
Definition: chatuserthreads.h:204
std::shared_ptr< ChatUserThread::Listener > mInternalThreadListener
Definition: chatuserthreads.h:188
ChatUserThreads * mOwner
Definition: chatuserthreads.h:137
Definition: chattypes.h:126
Definition: chattypes.h:343
virtual void CompleteShutdown() override
std::shared_ptr< ChatGetDisplayInfoTask > mChatGetDisplayInfoTask
Definition: chatuserthreads.h:187
TTV_ErrorCode SyncCachedThreads()
RetryTimer mFetchBitsConfigRetryTimer
Definition: chatuserthreads.h:203
Definition: chattypes.h:41
void OnMuteStatusChanged(ChatUserThread *source, bool muted)
TTV_ErrorCode FetchEmoteSets()
virtual void OnTopicListenerRemoved(PubSubClient *source, const std::string &topic, TTV_ErrorCode ec) override
std::weak_ptr< UserRepository > mUserRepository
Definition: chatuserthreads.h:179
std::shared_ptr< SettingRepository > mSettingRepository
Definition: chatuserthreads.h:180
ChatUserThreads(std::shared_ptr< User > user)
std::shared_ptr< PubSubTopicListenerHelper > mPubSubTopicListenerHelper
Definition: chatuserthreads.h:195
std::shared_ptr< ChatUserThread > CreateThread(const std::string &threadId, const ThreadData *data)
static std::string GetComponentName()
Definition: chatuserthreads.h:118
Definition: chatuserthreads.h:50
TTV_ErrorCode SendMessageWithThread(const std::shared_ptr< ChatUserThread > &thread, UserId otherUserId, const std::string &message, const SendMessageCallback &callback)
void MergeThreads(const std::vector< ThreadData > &threadList)