1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
#ifdef BUILD_BETTERTTV
#include "emotespp/betterttv.hpp"
#include <algorithm>
#include <map>
#include <nlohmann/json.hpp>
#include <optional>
#include <string>
#include <vector>
#include "emotespp/emotes.hpp"
namespace emotespp {
BetterTTVWebsocketClient::BetterTTVWebsocketClient() {
this->websocket.setUrl("wss://sockets.betterttv.net/ws");
this->websocket.enableAutomaticReconnection();
this->websocket.setOnMessageCallback(
[this](const ix::WebSocketMessagePtr &msg) {
switch (msg->type) {
case ix::WebSocketMessageType::Message: {
nlohmann::json j = nlohmann::json::parse(msg->str);
std::string name = j["name"];
nlohmann::json d = j["data"];
std::string channel = d["channel"];
std::vector<Emote> &cache = this->ids.at(channel);
if (name == "emote_create") {
nlohmann::json e = d["emote"];
Emote emote{e["id"], e["code"], std::nullopt};
cache.push_back(emote);
if (this->emote_create.has_value()) {
this->emote_create.value()(channel, std::nullopt, emote);
}
} else if (name == "emote_update") {
nlohmann::json e = d["emote"];
Emote emote{e["id"], e["code"], std::nullopt};
auto cache_e = std::find_if(
cache.begin(), cache.end(),
[&emote](const Emote &e) { return emote.id == e.id; });
if (cache_e != cache.end()) {
emote.original_code = cache_e->code;
cache_e->code = emote.code;
} else {
cache.push_back(emote);
}
if (this->emote_update.has_value()) {
this->emote_update.value()(channel, std::nullopt, emote);
}
} else if (name == "emote_delete") {
std::string emote_id = d["emoteId"];
Emote emote{emote_id, "-", std::nullopt};
auto cache_e = std::find_if(
cache.begin(), cache.end(),
[&emote](const Emote &e) { return emote.id == e.id; });
if (cache_e != cache.end()) {
emote.code = cache_e->code;
emote.original_code = cache_e->original_code;
cache.erase(cache_e);
}
if (this->emote_delete.has_value()) {
this->emote_delete.value()(channel, std::nullopt, emote);
}
}
break;
}
case ix::WebSocketMessageType::Error:
case ix::WebSocketMessageType::Close: {
this->is_connected = false;
break;
}
case ix::WebSocketMessageType::Open: {
this->is_connected = true;
std::for_each(this->ids.begin(), this->ids.end(),
[this](const auto &pair) {
this->subscribe_emote_set(pair.first);
});
break;
}
case ix::WebSocketMessageType::Ping:
case ix::WebSocketMessageType::Pong:
case ix::WebSocketMessageType::Fragment:
break;
}
});
}
void BetterTTVWebsocketClient::subscribe_emote_set(
const std::string &emote_set_id) {
if (!std::any_of(this->ids.begin(), this->ids.end(),
[&emote_set_id](const auto &pair) {
return pair.first == emote_set_id;
})) {
this->ids.insert({emote_set_id, {}});
}
if (this->is_connected) {
nlohmann::json j;
j["name"] = "join_channel";
nlohmann::json d;
d["name"] = emote_set_id;
j["data"] = d;
this->websocket.sendUtf8Text(j.dump());
}
}
void BetterTTVWebsocketClient::unsubscribe_emote_set(
const std::string &emote_set_id) {
if (this->is_connected) {
nlohmann::json j;
j["name"] = "part_channel";
nlohmann::json d;
d["name"] = emote_set_id;
j["data"] = d;
this->websocket.sendUtf8Text(j.dump());
}
auto it = std::find_if(this->ids.begin(), this->ids.end(),
[&emote_set_id](const auto &pair) {
return pair.first == emote_set_id;
});
if (it != this->ids.end()) {
this->ids.erase(it);
}
}
void BetterTTVWebsocketClient::start() { this->websocket.start(); }
const std::map<std::string, std::vector<Emote>> &
BetterTTVWebsocketClient::get_ids() const {
return this->ids;
}
Emote BetterTTVAPIClient::parse_emote(const nlohmann::json &value) const {
return {value["id"], value["code"], std::nullopt};
}
}
#endif
|