Twitch SDK (Internal)
chathelpers.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 
12 #include "twitchsdk/core/mutex.h"
13 #include "twitchsdk/core/timer.h"
15 #include <queue>
16 #include <array>
17 
18 namespace ttv
19 {
20  namespace chat
21  {
22  class ChatMessagePacer;
23  class UnreadThreadCache;
24 
25  Color GetRandomUserColor(const std::string& username);
26  SubscriptionStatus ParseSubscriptionStatus(const std::string& str);
27  UserMode ParseUserType(const std::string& str);
28 
29  // Helper function to unescape emoticon tokens received from back-end. Required since the server is escaping the JSON data as HTML.
30  void UnescapeEmoticonToken(std::string& token);
31  }
32 }
33 
34 
36 {
37 public:
39  bool TrackMessage();
40  void Clear();
41 
42 private:
43  // Twitch is currently set to blacklist if the client sends more than 20 messages in 30 seconds but we'll be conservative here.
44  static const uint kMessagePacingWindowSize = 18;
46 
47  std::array<uint64_t, kMessagePacingWindowSize> mMessageSubmissionTimes;
49 };
50 
51 
53 {
54 public:
55  struct ThreadData
56  {
57  std::string threadId;
60 
61  int32_t GetUnreadCount() const;
62  };
63 
65 
66  uint32_t GetNumUnreadThreads() const { return static_cast<uint32_t>(mNumUnreadThreads); }
67  uint32_t GetNumUnreadMessages() const { return static_cast<uint32_t>(mNumUnreadMessages); }
68 
69  UnreadThreadCounts GetUnreadThreadCounts() const;
70 
71  void UpdateThread(const std::string& threadId, MessageId lastMessageId, MessageId lastReadMessageId);
72  void SetExhaustive(bool exhaustive) { mExhaustive = exhaustive; }
73  bool GetExhaustive() const { return mExhaustive; }
74  void Clear();
75  bool ContainsThread(const std::string& threadId) const;
76 
77  bool GetDirty() { return mDirty; }
78  void MarkClean() { mDirty = false; }
79 
80  void RealtimeMessageReceived(const std::string& threadId, MessageId id);
81  void RealtimeMessageSent(const std::string& threadId, MessageId id);
82  void MarkThreadRead(const std::string& threadId, MessageId lastReadId);
83  void MarkThreadRead(const std::string& threadId);
84 
85 private:
86  std::map<std::string, ThreadData> mUnreadThreads;
89  bool mExhaustive;
90 
91  bool mDirty;
92 };
std::string threadId
Definition: chathelpers.h:57
Definition: chathelpers.h:55
UserMode ParseUserType(const std::string &str)
static const uint kMessagePacingWindowSize
Definition: chathelpers.h:44
uint32_t MessageId
Definition: chattypes.h:26
Definition: chathelpers.h:52
int32_t mNumUnreadThreads
Definition: chathelpers.h:87
int32_t mNumUnreadMessages
Definition: chathelpers.h:88
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
Color GetRandomUserColor(const std::string &username)
Definition: chathelpers.h:35
bool GetExhaustive() const
Definition: chathelpers.h:73
Definition: chattypes.h:745
bool mDirty
Whether the cache has changed since the last time the unread counts callbacks notified the user...
Definition: chathelpers.h:91
void MarkClean()
Definition: chathelpers.h:78
void SetExhaustive(bool exhaustive)
Definition: chathelpers.h:72
unsigned int uint
Definition: coretypes.h:18
bool GetDirty()
Definition: chathelpers.h:77
void UnescapeEmoticonToken(std::string &token)
uint32_t GetNumUnreadThreads() const
Definition: chathelpers.h:66
MessageId lastReadMessageId
Definition: chathelpers.h:58
std::map< std::string, ThreadData > mUnreadThreads
Definition: chathelpers.h:86
MessageId lastMessageId
Definition: chathelpers.h:59
SubscriptionStatus ParseSubscriptionStatus(const std::string &str)
std::array< uint64_t, kMessagePacingWindowSize > mMessageSubmissionTimes
The ring buffer of timestamps for the last n messages submitted. This is used for preventing the clie...
Definition: chathelpers.h:47
uint32_t Color
Definition: coretypes.h:28
static const uint kMessagePacingTimeSpanSeconds
Definition: chathelpers.h:45
uint mNextMessageSubmissionIndex
The index of the next entry to write in mMessageSubmissionTimes.
Definition: chathelpers.h:48
uint32_t GetNumUnreadMessages() const
Definition: chathelpers.h:67
bool mExhaustive
exhaustive = false means we need to poll the endpoint after each action for more accurate counts ...
Definition: chathelpers.h:89