summaryrefslogtreecommitdiff
path: root/src/seventv.cpp
blob: 54e259fe6097f8456eac346c2b85a6aeaee9047e (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "emotespp/seventv.hpp"

#include <algorithm>
#include <nlohmann/json.hpp>
#include <optional>
#include <string>

namespace emotespp {
  SevenTVWebsocketClient::SevenTVWebsocketClient() {
    this->websocket.setUrl("wss://events.7tv.io/v3");
    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);

              int opcode = j["op"].get<int>();

              // hello opcode
              if (opcode == 1) {
                this->is_connected = true;
                std::for_each(this->ids.begin(), this->ids.end(),
                              [this](const std::string &id) {
                                this->subscribe_emote_set(id);
                              });
                return;
              } else if (opcode != 0) {
                return;
              }

              nlohmann::json data = j["d"];

              std::string type = data["type"];
              if (type != "emote_set.update") {
                return;
              }

              data = data["body"];

              std::string channel_id = data["id"];

              nlohmann::json actor = data["actor"];
              std::string actor_id = actor["id"];

              if (data.contains("pushed") && this->emote_create.has_value()) {
                for (const nlohmann::json &d : data["pushed"]) {
                  nlohmann::json emote = d["value"];
                  this->emote_create.value()(channel_id, actor_id,
                                             this->create_emote(emote));
                }
              }

              if (data.contains("pulled") && this->emote_delete.has_value()) {
                for (const nlohmann::json &d : data["pulled"]) {
                  nlohmann::json emote = d["old_value"];
                  this->emote_delete.value()(channel_id, actor_id,
                                             this->create_emote(emote));
                }
              }

              if (data.contains("updated") && this->emote_update.has_value()) {
                for (const nlohmann::json &d : data["updated"]) {
                  nlohmann::json old_emote = d["old_value"];
                  nlohmann::json emote = d["value"];

                  std::string id = old_emote["id"];
                  std::string code = emote["name"];

                  std::optional<std::string> original_code = old_emote["name"];
                  if (code == original_code) {
                    original_code = std::nullopt;
                  }

                  Emote emote_data{id, code, original_code};

                  this->emote_update.value()(channel_id, actor_id, emote_data);
                }
              }

              break;
            }
            case ix::WebSocketMessageType::Close:
            case ix::WebSocketMessageType::Error: {
              this->is_connected = false;
              break;
            }
            case ix::WebSocketMessageType::Open:
            case ix::WebSocketMessageType::Ping:
            case ix::WebSocketMessageType::Pong:
            case ix::WebSocketMessageType::Fragment:
              break;
          }
        });
  }

  void SevenTVWebsocketClient::subscribe_emote_set(
      const std::string &emote_set_id) {
    if (!std::any_of(this->ids.begin(), this->ids.end(),
                     [&emote_set_id](const std::string &id) {
                       return id == emote_set_id;
                     })) {
      this->ids.push_back(emote_set_id);
    }

    if (this->is_connected) {
      nlohmann::json j;
      j["op"] = 35;

      nlohmann::json d;

      nlohmann::json c;
      c["object_id"] = emote_set_id;

      d["type"] = "emote_set.update";
      d["condition"] = c;

      j["d"] = d;

      this->websocket.sendUtf8Text(j.dump());
    }
  }

  void SevenTVWebsocketClient::unsubscribe_emote_set(
      const std::string &emote_set_id) {
    if (this->is_connected) {
      nlohmann::json j;
      j["op"] = 36;

      nlohmann::json d;

      nlohmann::json c;
      c["object_id"] = emote_set_id;

      d["type"] = "emote_set.update";
      d["condition"] = c;

      j["d"] = d;

      this->websocket.sendUtf8Text(j.dump());
    }

    auto it = std::find_if(
        this->ids.begin(), this->ids.end(),
        [&emote_set_id](const std::string &x) { return x == emote_set_id; });

    if (it != this->ids.end()) {
      this->ids.erase(it);
    }
  }

  void SevenTVWebsocketClient::start() { this->websocket.start(); }

  const std::vector<std::string> &SevenTVWebsocketClient::get_ids() const {
    return this->ids;
  }

  Emote SevenTVWebsocketClient::create_emote(const nlohmann::json &data) {
    std::string id = data["id"];
    std::string code = data["name"];
    std::optional<std::string> original_code = data["data"]["name"];

    if (code == original_code) {
      original_code = std::nullopt;
    }

    return {id, code, original_code};
  }

  std::vector<Emote> SevenTVAPIClient::parse_emoteset(
      const nlohmann::json &value) const {
    std::vector<Emote> emotes;

    nlohmann::json e = value["emotes"];

    for (const nlohmann::json v : e) {
      std::string id = v["id"];
      std::string code = v["name"];
      std::optional<std::string> original_code = v["data"]["name"];

      if (code == original_code) {
        original_code = std::nullopt;
      }

      emotes.push_back({id, code, original_code});
    }

    return emotes;
  }

  std::optional<User> SevenTVAPIClient::get_user_by_twitch_id(
      const unsigned int &twitch_id) const {
    cpr::Response r = cpr::Get(cpr::Url{this->base_url + "/users/twitch/" +
                                        std::to_string(twitch_id)});

    if (r.status_code != 200) {
      return std::nullopt;
    }

    nlohmann::json j = nlohmann::json::parse(r.text);

    std::string alias_id = j["id"];
    std::string username = j["username"];
    std::string emote_set_id = j["emote_set_id"];
    std::string id = j["user"]["id"];

    return (User){alias_id, id, username, emote_set_id};
  }

  std::optional<User> SevenTVAPIClient::get_user(const std::string &id) const {
    cpr::Response r = cpr::Get(cpr::Url{this->base_url + "/users/" + id});

    if (r.status_code != 200) {
      return std::nullopt;
    }

    nlohmann::json j = nlohmann::json::parse(r.text);

    return this->parse_user(j);
  }

  std::optional<EmoteSet> SevenTVAPIClient::get_emote_set(
      const std::string &emote_set_id) const {
    cpr::Response r =
        cpr::Get(cpr::Url{this->base_url + "/emote-sets/" + emote_set_id});

    if (r.status_code != 200) {
      return std::nullopt;
    }

    nlohmann::json j = nlohmann::json::parse(r.text);

    std::string id = j["id"];
    std::string name = j["name"];
    std::optional<User> owner = this->parse_user(j["owner"]);

    if (!owner.has_value()) {
      return std::nullopt;
    }

    return (EmoteSet){id, name, owner.value(), this->parse_emoteset(j)};
  }

  std::optional<User> SevenTVAPIClient::parse_user(
      const nlohmann::json &value) const {
    std::string id = value["id"];

    nlohmann::json conns = value["connections"];
    auto twitchconn = std::find_if(
        conns.begin(), conns.end(),
        [](const nlohmann::json &x) { return x["platform"] == "TWITCH"; });

    if (twitchconn == conns.end()) {
      return std::nullopt;
    }

    nlohmann::json c = *twitchconn;

    return (User){c["id"], id, c["username"], c["emote_set_id"]};
  }
}