-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbservice.js
214 lines (185 loc) · 7.48 KB
/
dbservice.js
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
// Copyright (c) 2020 Mitchell Adair
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
const mysql = require("mysql2");
const defaultEvents = require("./defaultEvents.json");
const { DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_PORT, DB_NAME, CLIENT_SECRET } = process.env;
class DBService {
constructor() {
if (!DBService._instance) {
DBService._instance = this;
}
this.db = mysql
.createPool({
host: DB_HOSTNAME,
port: DB_PORT,
user: DB_USERNAME,
password: DB_PASSWORD,
database: DB_NAME,
ssl: { rejectUnauthorized: true },
})
.promise();
return DBService._instance;
}
async getUserCount() {
const [results] = await this.db.execute("SELECT COUNT(*) AS users FROM channels WHERE enabled=1");
return results[0].users;
}
async getEnabledChannels() {
const [results] = await this.db.execute("SELECT id FROM channels WHERE enabled=1");
return results.map(({ id }) => id);
}
async initChannel(id, name, authToken, refreshToken) {
const entries = Object.entries(defaultEvents);
const placeholders = new Array(entries.length).fill("(?, ?, ?, ?)").join(",");
const values = entries.map(([event, { message, enabled }]) => [id, event, message, enabled]).flat();
await this.db.execute(
"INSERT INTO channels (id, name, token, refresh_token, enabled) VALUES (?, ?, AES_ENCRYPT(?, ?), AES_ENCRYPT(?, ?), false)",
[id, name, authToken, CLIENT_SECRET, refreshToken, CLIENT_SECRET]
);
await this.db.execute(`INSERT INTO events (channel_id, name, message, enabled) VALUES ${placeholders}`, values);
}
async updateTokensForChannel(id, accessToken, refreshToken) {
await this.db.execute(
"UPDATE channels SET token=AES_ENCRYPT(?, ?), refresh_token=AES_ENCRYPT(?, ?) WHERE id=?",
[accessToken, CLIENT_SECRET, refreshToken, CLIENT_SECRET, id]
);
}
async getTokensForChannel(id) {
const [results] = await this.db.execute(
"SELECT AES_DECRYPT(token, ?) as access_token, AES_DECRYPT(refresh_token, ?) as refresh_token FROM channels WHERE id=?",
[CLIENT_SECRET, CLIENT_SECRET, id]
);
return results[0];
}
async getChannel(id) {
const [results] = await this.db.execute("SELECT * FROM channels WHERE id=?", [id]);
if (results.length) {
return results[0];
}
}
async updateNameForChannel(name, id) {
await this.db.execute("UPDATE channels SET name=? WHERE id=?", [name, id]);
}
async enableChannel(id) {
await this.db.execute("UPDATE channels SET enabled=true WHERE id=?", [id]);
}
async disableChannel(id) {
await this.db.execute("UPDATE channels SET enabled=false WHERE id=?", [id]);
}
async getAllCommandsForChannel(id) {
const [results] = await this.db.execute(
"SELECT alias,message,cooldown,user_level FROM commands WHERE channel_id=?",
[id]
);
return results;
}
async getCommandForChannel(alias, id) {
const [results] = await this.db.execute(
"SELECT alias,message,cooldown,user_level FROM commands WHERE channel_id=? and alias=?",
[id, alias]
);
if (results.length) {
return results[0];
}
}
async addCommandForChannel(data, id) {
const existing = await this.getCommandForChannel(data.alias, id);
if (!existing) {
await this.db.execute(
"INSERT INTO commands (channel_id, alias, message, cooldown, user_level) VALUES (?, ?, ?, ?, ?)",
[id, data.alias, data.message, data.cooldown, data.user_level]
);
return data;
}
}
async updateCommandForChannel(alias, data, id) {
const [result] = await this.db.execute(
"UPDATE commands SET alias=?, message=?, cooldown=?, user_level=? where channel_id=? and alias=?",
[data.alias, data.message, data.cooldown, data.user_level, id, alias]
);
if (result.affectedRows > 0) {
return data;
}
}
async deleteCommandForChannel(alias, id) {
const [result] = await this.db.execute("DELETE FROM commands where channel_id=? and alias=?", [id, alias]);
if (result.affectedRows > 0) {
return true;
}
}
async getAllEventsForChannel(id) {
const [results] = await this.db.execute("SELECT name,message,enabled FROM events WHERE channel_id=?", [id]);
return results.map((event) => {
return { ...event, enabled: Boolean(event.enabled) };
});
}
async getEventForChannel(name, id) {
const [results] = await this.db.execute(
"SELECT name,message,enabled FROM events WHERE channel_id=? and name=?",
[id, name]
);
if (results.length) {
const { name, message, enabled } = results[0];
return { name, message, enabled: Boolean(enabled) };
}
}
async updateEventForChannel(name, data, id) {
const [result] = await this.db.execute(
"UPDATE events SET name=?, message=?, enabled=? where channel_id=? and name=?",
[name, data.message, data.enabled, id, name]
);
if (result.affectedRows > 0) {
return { name, ...data };
}
}
async getAllTimersForChannel(id) {
const [results] = await this.db.execute(
"SELECT name,message,enabled,`interval`,message_threshold FROM timers WHERE channel_id=?",
[id]
);
return results.map((timer) => {
return { ...timer, enabled: Boolean(timer.enabled) };
});
}
async getTimerForChannel(name, id) {
const [results] = await this.db.execute(
"SELECT name,message,enabled,`interval`,message_threshold FROM timers WHERE channel_id=? and name=?",
[id, name]
);
if (results.length) {
const timer = results[0];
return { ...timer, enabled: Boolean(timer.enabled) };
}
}
async addTimerForChannel(data, id) {
const { name, enabled, message, interval, message_threshold } = data;
const existing = await this.getTimerForChannel(name, id);
if (!existing) {
await this.db.execute(
"INSERT INTO timers (channel_id, name, enabled, message, `interval`, message_threshold) VALUES (?, ?, ?, ?, ?, ?)",
[id, name, enabled, message, interval, message_threshold]
);
return data;
}
}
async updateTimerForChannel(name, data, id) {
const { name: newName, enabled, message, interval, message_threshold } = data;
const [result] = await this.db.execute(
"UPDATE timers SET name=?, enabled=?, message=?, `interval`=?, message_threshold=? where channel_id=? and name=?",
[newName, enabled, message, interval, message_threshold, id, name]
);
if (result.affectedRows > 0) {
return data;
}
}
async deleteTimerForChannel(name, id) {
const [result] = await this.db.execute("DELETE FROM timers where channel_id=? and name=?", [id, name]);
if (result.affectedRows > 0) {
return true;
}
}
}
const instance = new DBService();
module.exports = instance;