Twitch SDK (Internal)
chatuserthread.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 #include "twitchsdk/core/timer.h"
15 #include <string>
16 #include <vector>
17 #include <unordered_set>
18 #include <map>
19 #include <memory>
20 
21 namespace ttv
22 {
23  class TaskRunner;
24  class Task;
25  class User;
26 
27  namespace chat
28  {
29  class ChatUserThreads;
30  class ChatUserThread;
31  class ChatGetUserThreadsTask;
32  }
33 }
34 
35 
40 {
41 public:
42  class Listener
43  {
44  public:
48  virtual void OnMetadataFetchComplete(ChatUserThread* source, TTV_ErrorCode ec) = 0;
49 
54  virtual void OnRealtimeMessageReceived(ChatUserThread* source, const WhisperMessage& messageList) = 0;
55 
59  virtual void OnParticipantsUpdated(ChatUserThread* source, const std::vector<ChatUserInfo>& participants) = 0;
60 
64  virtual void OnMuteStatusChanged(ChatUserThread *source, bool muted) = 0;
65 
69  virtual void OnUnreadMessageWindowChanged(ChatUserThread* source, MessageId lastMessageId, MessageId lastReadMessageId) = 0;
70  };
71 
72 public:
73  using FetchMessagesCallback = std::function<void(TTV_ErrorCode ec, const std::vector<WhisperMessage>& messages)>;
74  using SetMutedCallback = std::function<void(TTV_ErrorCode ec)>;
75  using SetArchivedCallback = std::function<void(TTV_ErrorCode ec)>;
76  using SetLastMessageReadIdCallback = std::function<void(TTV_ErrorCode ec)>;
77 
78  ChatUserThread(const std::string& threadId, const std::shared_ptr<User>& user, const ThreadData& data);
79  ChatUserThread(const std::string& threadId, const std::shared_ptr<User>& user);
80  virtual ~ChatUserThread();
81 
82  std::string GetId() const { return mThreadId; }
83 
84  void SetTokenizationOptions(const TokenizationOptions& tokenizationOptions) { mTokenizationOptions = tokenizationOptions; }
85  void SetBitsConfiguration(std::shared_ptr<BitsConfiguration> bitsConfiguration) { mBitsConfiguration = bitsConfiguration; }
86 
87  bool GetPlaceholder() const { return mPlaceholder; }
88  bool GetFetchFailed() const { return mFetchMetadataFailed; }
89  void SetFailedSend(bool failed) { mFailedSend = failed; }
90  bool GetFailedSend() const { return mFailedSend; }
91 
92  void AddListener(std::shared_ptr<Listener> listener);
93  void RemoveListener(std::shared_ptr<Listener> listener);
94 
95  // IComponent implementation
96  virtual void Update() override;
97  virtual std::string GetLoggerName() const override;
98 
102  void UpdateMetadata(const ThreadData& thread);
103 
104  TTV_ErrorCode FetchMessages(MessageId start, uint32_t beforeCount, const FetchMessagesCallback& callback);
105 
109  void AddRealtimeMessage(const WhisperMessage& message);
110 
111  void GetMetadata(ThreadData& thread) const;
112 
116  bool GetLocalParticipant(ChatUserInfo& userInfo) const;
117 
118  const std::vector<ChatUserInfo>& GetParticipants() { return mParticipants; }
122 
123  TTV_ErrorCode SetMuted(bool muted, const SetMutedCallback& callback);
124  TTV_ErrorCode SetArchived(bool archived, const SetMutedCallback& callback);
126 
130  void SetUnreadMessageWindow(MessageId lastMessageId, MessageId lastReadMessageId);
131 
132 private:
133  ChatUserThread(const std::string& threadId, const std::shared_ptr<User>& user, bool placeholder);
134 
135  void BumpLastMessage(const WhisperMessage& message);
136  void UpdateParticipants(const WhisperMessage& message);
137  void ScheduleFetch();
139 
140  // Component overrides
141  virtual void CompleteShutdown() override;
142 
144  std::shared_ptr<BitsConfiguration> mBitsConfiguration;
145 
146  std::shared_ptr<WhisperMessage> mLastMessage;
147  std::vector<ChatUserInfo> mParticipants;
148 
149  std::string mThreadId;
151 
156 
158 
159  bool mMuted;
160  bool mArchived;
167 };
Definition: chattypes.h:528
TTV_ErrorCode FetchMetadata()
void GetMetadata(ThreadData &thread) const
virtual void OnMetadataFetchComplete(ChatUserThread *source, TTV_ErrorCode ec)=0
ChatUserThread(const std::string &threadId, const std::shared_ptr< User > &user, const ThreadData &data)
bool mParticipantsUpdated
Definition: chatuserthread.h:161
virtual void Update() override
virtual void OnMuteStatusChanged(ChatUserThread *source, bool muted)=0
Definition: chatuserthread.h:42
void BumpLastMessage(const WhisperMessage &message)
std::string GetId() const
Definition: chatuserthread.h:82
void AddListener(std::shared_ptr< Listener > listener)
uint32_t MessageId
Definition: chattypes.h:26
std::string mThreadId
Definition: chatuserthread.h:149
std::function< void(TTV_ErrorCode ec)> SetMutedCallback
Definition: chatuserthread.h:74
virtual void CompleteShutdown() override
uint32_t Timestamp
Definition: coretypes.h:27
MessageId GetLastReadMessageId() const
Definition: chatuserthread.h:121
MessageId GetLastMessageId() const
Definition: chatuserthread.h:120
std::function< void(TTV_ErrorCode ec, const std::vector< WhisperMessage > &messages)> FetchMessagesCallback
Definition: chatuserthread.h:73
bool mMuted
Definition: chatuserthread.h:159
MessageId mLastMessageId
Definition: chatuserthread.h:152
void RemoveListener(std::shared_ptr< Listener > listener)
void SetFailedSend(bool failed)
Definition: chatuserthread.h:89
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
Definition: timer.h:52
Definition: eventsource.h:18
std::shared_ptr< BitsConfiguration > mBitsConfiguration
Definition: chatuserthread.h:144
std::vector< ChatUserInfo > mParticipants
Definition: chatuserthread.h:147
bool mArchived
Definition: chatuserthread.h:160
bool GetFetchFailed() const
Definition: chatuserthread.h:88
TTV_ErrorCode SetArchived(bool archived, const SetMutedCallback &callback)
bool GetPlaceholder() const
Definition: chatuserthread.h:87
const std::vector< ChatUserInfo > & GetParticipants()
Definition: chatuserthread.h:118
uint32_t TTV_ErrorCode
Definition: errortypes.h:30
Definition: component.h:87
void UpdateMetadata(const ThreadData &thread)
std::function< void(TTV_ErrorCode ec)> SetLastMessageReadIdCallback
Definition: chatuserthread.h:76
Definition: chatuserthread.h:39
EventSource< Listener > mListeners
Definition: chatuserthread.h:143
WaitForExpiry mMetadataFetchTimer
Definition: chatuserthread.h:150
Timestamp mLastUpdateTimestamp
Definition: chatuserthread.h:154
void AddRealtimeMessage(const WhisperMessage &message)
void SetUnreadMessageWindow(MessageId lastMessageId, MessageId lastReadMessageId)
bool mUnreadMessageWindowUpdate
Definition: chatuserthread.h:163
Timestamp GetLastUpdateTimestamp() const
Definition: chatuserthread.h:119
virtual void OnParticipantsUpdated(ChatUserThread *source, const std::vector< ChatUserInfo > &participants)=0
Timestamp mLastFetchedTimestamp
Definition: chatuserthread.h:155
std::function< void(TTV_ErrorCode ec)> SetArchivedCallback
Definition: chatuserthread.h:75
bool mPlaceholder
Definition: chatuserthread.h:164
TTV_ErrorCode SetLastMessageReadId(MessageId id, const SetLastMessageReadIdCallback &callback)
bool mFetchMetadataFailed
Definition: chatuserthread.h:165
void SetBitsConfiguration(std::shared_ptr< BitsConfiguration > bitsConfiguration)
Definition: chatuserthread.h:85
void UpdateParticipants(const WhisperMessage &message)
TokenizationOptions mTokenizationOptions
Definition: chatuserthread.h:157
bool GetFailedSend() const
Definition: chatuserthread.h:90
void SetTokenizationOptions(const TokenizationOptions &tokenizationOptions)
Definition: chatuserthread.h:84
Definition: chattypes.h:126
Definition: chattypes.h:343
Definition: chattypes.h:41
virtual std::string GetLoggerName() const override
TTV_ErrorCode FetchMessages(MessageId start, uint32_t beforeCount, const FetchMessagesCallback &callback)
bool mLastMessageUpdated
Definition: chatuserthread.h:162
TTV_ErrorCode SetMuted(bool muted, const SetMutedCallback &callback)
bool mFailedSend
Definition: chatuserthread.h:166
virtual void OnUnreadMessageWindowChanged(ChatUserThread *source, MessageId lastMessageId, MessageId lastReadMessageId)=0
bool GetLocalParticipant(ChatUserInfo &userInfo) const
virtual void OnRealtimeMessageReceived(ChatUserThread *source, const WhisperMessage &messageList)=0
MessageId mLastReadMessageId
Definition: chatuserthread.h:153
std::shared_ptr< WhisperMessage > mLastMessage
Definition: chatuserthread.h:146