-
Notifications
You must be signed in to change notification settings - Fork 2
/
database.js
290 lines (254 loc) · 7.44 KB
/
database.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* eslint-disable new-cap */
const sqlite = require("better-sqlite3");
const db = new sqlite("./dbfile");
const metadata = require("./metadata.js");
function getOrSetEncryptor(bufferIv) {
// Can only have one encryptor value. Get an existing, or set and return passed
let stmt = db.prepare("SELECT ivValue FROM encryptor");
const row = stmt.get();
if (row) {
return Buffer.from(row.ivValue, "hex");
}
stmt = db.prepare("INSERT INTO encryptor VALUES (?)");
stmt.run(bufferIv.toString("hex"));
return bufferIv;
}
function setChannelDestinations(colName, channelId) {
const stmt = db.prepare(
"UPDATE channelDestinations SET " + colName + " = " + channelId
);
stmt.run();
}
function getChannelDestination(colName) {
const stmt = db.prepare(
"SELECT " + colName + " AS channelID FROM channelDestinations"
);
return stmt.get().channelID;
}
function setConfigurationTimer(colName, seconds) {
const stmt = db.prepare(
"UPDATE configuration SET " + colName + " = " + seconds
);
stmt.run();
}
function getConfigurationTimer(colName) {
const stmt = db.prepare(
"SELECT " + colName + " AS config FROM configuration"
);
return stmt.get().config;
}
function getBanList() {
const statement = db.prepare(
"SELECT * FROM messageBlocker WHERE reason != ?"
);
return statement.all(metadata.blockReason.SLOWMODE);
}
function setMessageBlocker(encryptedUser, reason, explanation, dateOfUnban) {
// Update if exists, else create
let stmt = db.prepare(
"SELECT reason, explanation, date FROM messageBlocker WHERE encryptedUserId = ?"
);
if (stmt.get(encryptedUser)) {
stmt = db.prepare(
"UPDATE messageBlocker SET reason = ?, explanation = ?, date = ? WHERE encryptedUserId = ?"
);
stmt.run(reason, explanation, dateOfUnban, encryptedUser);
return;
}
stmt = db.prepare("INSERT INTO messageBlocker VALUES (?, ?, ?, ?)");
stmt.run(encryptedUser, reason, explanation, dateOfUnban);
}
function getMessageBlocker(encryptedUser) {
const stmt = db.prepare(
"SELECT reason, explanation, date FROM messageBlocker WHERE encryptedUserId = ?"
);
return stmt.get(encryptedUser);
}
function deleteMessageBlocker(encryptedUser) {
const stmt = db.prepare(
"DELETE FROM messageBlocker WHERE encryptedUserId = ?"
);
stmt.run(encryptedUser);
}
function deleteAllSlowdowns() {
const stmt = db.prepare("DELETE FROM messageBlocker WHERE reason = ?");
stmt.run(metadata.blockReason.SLOWMODE);
}
// The getAndIncrementMessageCounter function was deleted, and was removed from module.exports
function addMessageAndGetNumber(msg) {
let stmt = db.prepare(
"SELECT * FROM messages WHERE number=(SELECT MAX(number) FROM messages)"
);
let result = stmt.get();
let messageNumber;
if (!result) {
stmt = db.prepare(
"SELECT name FROM sqlite_master WHERE type='table' AND name='messageCounter'"
);
result = stmt.get();
if (typeof result === "undefined") {
messageNumber = 0;
} else {
stmt = db.prepare("SELECT count FROM messageCounter");
result = stmt.get();
if (!result) {
messageNumber = 0;
} else {
messageNumber = result.count;
}
}
} else {
messageNumber = result.number + 1;
}
stmt = db.prepare("INSERT INTO messages VALUES (?, ?, ?)");
stmt.run(messageNumber, msg, "");
return messageNumber;
}
function updateMessageWithUrl(number, url) {
const stmt = db.prepare(
"UPDATE messages SET message_url = ? WHERE number = ?"
);
stmt.run(url, number);
}
function getMessageByNumber(num) {
if (!messageNumberIsRepliable(num)) {
return "";
}
const stmt = db.prepare(
"SELECT message_content FROM messages WHERE number = " + num.toString()
);
const result = stmt.get();
return result.message_content;
}
function getMessageUrlByNumber(num) {
if (!messageNumberIsRepliable(num)) {
return "";
}
const stmt = db.prepare(
"SELECT message_url FROM messages WHERE number = " + num.toString()
);
const result = stmt.get();
return result.message_url;
}
function messageNumberIsRepliable(num) {
let stmt = db.prepare(
"SELECT * FROM messages WHERE number=(SELECT MIN(number) from messages)"
);
let result = stmt.get();
if (!result) {
return "";
}
const firstRepliableMessageNumber = result.number;
stmt = db.prepare(
"SELECT * FROM messages WHERE number=(SELECT MAX(number) from messages)"
);
result = stmt.get();
const lastRepliableMessageNumber = result.number;
return (
num >= firstRepliableMessageNumber && num <= lastRepliableMessageNumber
);
}
function getAnonIdFromMsgId(msgId) {
const stmt = db.prepare("SELECT anon_id FROM msgMap WHERE msg_id = ?");
const result = stmt.get(msgId);
if (!result) {
return;
}
return result.anon_id;
}
function insertMsgMap(anonId, msgId) {
// Update if exists, else create
let stmt = db.prepare("SELECT anon_id FROM msgMap WHERE msg_id = ?");
if (stmt.get(msgId)) {
stmt = db.prepare(
"UPDATE msgMap SET anon_id = ? WHERE encryptedUserId = ?"
);
stmt.run(anonId, msgId);
return;
}
stmt = db.prepare("INSERT INTO msgMap VALUES (?, ?)");
stmt.run(msgId, anonId);
}
function getSlurs() {
const stmt = db.prepare("SELECT * FROM slurs");
const results = stmt.all();
if (!results) {
return;
}
return results;
}
function insertSlur(word) {
// Create if it does not exist
let stmt = db.prepare("SELECT word FROM slurs WHERE word = ?");
if (stmt.get(word)) {
console.log(stmt.get(word));
return;
}
stmt = db.prepare("INSERT INTO slurs VALUES (?)");
stmt.run(word);
}
module.exports = {
getOrSetEncryptor,
setChannelDestinations,
getChannelDestination,
setConfigurationTimer,
getConfigurationTimer,
setMessageBlocker,
getMessageBlocker,
getBanList,
deleteMessageBlocker,
deleteAllSlowdowns,
addMessageAndGetNumber,
getMessageByNumber,
getMessageUrlByNumber,
updateMessageWithUrl,
getAnonIdFromMsgId,
insertMsgMap,
getSlurs,
insertSlur,
};
// Initial setup
function initializeTables() {
// Encryption storage for persistency of IDs
let stmt = db.prepare("CREATE TABLE IF NOT EXISTS encryptor (ivValue TEXT)");
stmt.run();
// anon_id to msg_id table
stmt = db.prepare(
"CREATE TABLE IF NOT EXISTS msgMap ( msg_id INTEGER PRIMARY KEY, anon_id TEXT NOT NULL )"
);
stmt.run();
// Channel storage
stmt = db.prepare(
"CREATE TABLE IF NOT EXISTS channelDestinations (" +
Object.values(metadata.channels).join(" TEXT, ") +
" TEXT)"
);
stmt.run();
stmt = db.prepare("INSERT INTO channelDestinations VALUES ('', '', '')");
stmt.run();
// Configuration settings
stmt = db.prepare(
"CREATE TABLE IF NOT EXISTS configuration (" +
Object.values(metadata.configuration).join(" INTEGER, ") +
" TEXT)"
);
stmt.run();
stmt = db.prepare("INSERT INTO configuration VALUES (0, 0)");
stmt.run();
// Message blockers
stmt = db.prepare(
"CREATE TABLE IF NOT EXISTS messageBlocker (encryptedUserId TEXT, reason TEXT, explanation TEXT, date TEXT)"
);
stmt.run();
// The code that created the messageCounter table if it didn't already exist was deleted
// Message number, content, and url
stmt = db.prepare(
"CREATE TABLE IF NOT EXISTS messages (number INTEGER PRIMARY KEY, " +
"message_content TEXT NOT NULL, message_url TEXT)"
);
stmt.run();
// Table for hate speech filter
stmt = db.prepare("CREATE TABLE IF NOT EXISTS slurs (word TEXT NOT NULL)");
stmt.run();
}
initializeTables();