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
|
#include "stream.hpp"
#include <algorithm>
#include <chrono>
#include <pqxx/pqxx>
#include <set>
#include <string>
#include <thread>
#include <utility>
#include <vector>
#include "api/twitch/schemas/stream.hpp"
#include "config.hpp"
#include "logger.hpp"
#include "schemas/stream.hpp"
#include "utils/string.hpp"
namespace bot::stream {
void StreamListenerClient::listen_channel(const int &id) {
this->ids.push_back(id);
}
void StreamListenerClient::unlisten_channel(const int &id) {
auto x = std::find_if(this->ids.begin(), this->ids.end(),
[&](const auto &x) { return x == id; });
if (x != this->ids.end()) {
this->ids.erase(x);
}
auto y = std::find_if(this->online_ids.begin(), this->online_ids.end(),
[&](const auto &x) { return x == id; });
if (y != this->online_ids.end()) {
this->online_ids.erase(y);
}
}
void StreamListenerClient::run() {
while (true) {
this->update_channel_ids();
this->check();
std::this_thread::sleep_for(std::chrono::seconds(5));
}
}
void StreamListenerClient::check() {
auto streams = this->helix_client.get_streams(this->ids);
auto now = std::chrono::system_clock::now();
auto now_time_it = std::chrono::system_clock::to_time_t(now);
auto now_tm = std::gmtime(&now_time_it);
now = std::chrono::system_clock::from_time_t(std::mktime(now_tm));
// adding new ids
for (const auto &stream : streams) {
bool is_already_live =
std::any_of(this->online_ids.begin(), this->online_ids.end(),
[&](const auto &x) { return x == stream.get_user_id(); });
if (!is_already_live) {
this->online_ids.insert(stream.get_user_id());
auto difference = now - stream.get_started_at();
auto difference_min =
std::chrono::duration_cast<std::chrono::minutes>(difference);
if (difference_min.count() < 1) {
this->handler(schemas::EventType::LIVE, stream);
}
}
}
// removing old ids
for (auto i = this->online_ids.begin(); i != this->online_ids.end();) {
auto stream =
std::find_if(streams.begin(), streams.end(),
[&](const auto &x) { return x.get_user_id() == *i; });
if (stream == streams.end()) {
this->handler(schemas::EventType::OFFLINE,
api::twitch::schemas::Stream{*i});
i = this->online_ids.erase(i);
} else {
++i;
}
}
}
void StreamListenerClient::handler(
const schemas::EventType &type,
const api::twitch::schemas::Stream &stream) {
pqxx::connection conn(GET_DATABASE_CONNECTION_URL(this->configuration));
pqxx::work work(conn);
pqxx::result events = work.exec(
"SELECT id, channel_id, message, flags FROM events WHERE event_type "
"= " +
std::to_string(type) +
" AND target_alias_id = " + std::to_string(stream.get_user_id()));
for (const auto &event : events) {
pqxx::row channel = work.exec1(
"SELECT alias_id, alias_name, opted_out_at FROM channels WHERE id "
"= " +
std::to_string(event[1].as<int>()));
if (!channel[2].is_null()) {
continue;
}
pqxx::result subs = work.exec(
"SELECT user_id FROM event_subscriptions WHERE event_id = " +
std::to_string(event[0].as<int>()));
std::set<std::string> user_ids;
if (!subs.empty()) {
for (const auto &sub : subs) {
user_ids.insert(std::to_string(sub[0].as<int>()));
}
pqxx::result users = work.exec(
"SELECT alias_name FROM users WHERE id IN (" +
utils::string::str(user_ids.begin(), user_ids.end(), ',') + ")");
user_ids.clear();
for (const auto &user : users) {
user_ids.insert(user[0].as<std::string>());
}
}
auto flags = event[3].as_array();
std::pair<pqxx::array_parser::juncture, std::string> elem;
do {
elem = flags.get_next();
if (elem.first == pqxx::array_parser::juncture::string_value) {
if (std::stoi(elem.second) == schemas::EventFlag::MASSPING) {
auto chatters = this->helix_client.get_chatters(
channel[0].as<int>(), this->irc_client.get_bot_id());
for (const auto &chatter : chatters) {
user_ids.insert(chatter.login);
}
}
}
} while (elem.first != pqxx::array_parser::juncture::done);
std::string base = "⚡ " + event[2].as<std::string>();
std::vector<std::string> msgs = {""};
int index = 0;
if (!user_ids.empty()) {
base.append(" · ");
}
for (const auto &user_id : user_ids) {
const std::string ¤t_msg = msgs.at(index);
std::string x = "@" + user_id;
if (base.length() + current_msg.length() + 1 + x.length() >= 500) {
index += 1;
}
if (index > msgs.size() - 1) {
msgs.push_back(x);
} else {
msgs[index] = current_msg + " " + x;
}
}
for (const auto &msg : msgs) {
this->irc_client.say(channel[1].as<std::string>(), base + msg);
}
}
work.commit();
conn.close();
}
void StreamListenerClient::update_channel_ids() {
pqxx::connection conn(GET_DATABASE_CONNECTION_URL(this->configuration));
pqxx::work work(conn);
pqxx::result ids =
work.exec("SELECT target_alias_id FROM events WHERE event_type < 10");
for (const auto &row : ids) {
int id = row[0].as<int>();
if (std::any_of(this->ids.begin(), this->ids.end(),
[&](const auto &x) { return x == id; })) {
continue;
}
log::info("TwitchStreamListener",
"Listening stream events for ID " + std::to_string(id));
this->ids.push_back(id);
}
work.commit();
conn.close();
}
}
|