-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.js
214 lines (178 loc) · 5.9 KB
/
db.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
const config = require('./config.js');
const SQL = require('sql-template-strings');
const {promisify} = require('util');
const fn = promisify(config.db.query).bind(config.db);
// Define a custom Error class for database errors. The constructor
// implementation allows more specific errors to be derived as
// one-liners.
class DatabaseError extends Error {
constructor(message) {
super(message);
this.name = this.constructor.name
}
}
class NotFoundError extends DatabaseError{}
class AlreadyPresentError extends DatabaseError{}
class InvalidRecordError extends DatabaseError{}
// Get the ID of the group with the provided name.
async function gid(groupname) {
var results = await fn(SQL`
SELECT id FROM _group
WHERE name = ${groupname}`);
if(results.length < 1) {
throw new NotFoundError(`group "${groupname}" not found`);
}
var id = results[0].id;
return id;
}
// Return a list of all groups in the database by name.
async function list_groups() {
var results = await fn(SQL`
SELECT name FROM _group`);
var groups = [];
for (let result of results) {
groups.push(result.name);
}
return groups;
}
// Add a group with the provided name and description.
async function add_group(groupname, description) {
try {
var group_id = await gid(groupname);
throw new AlreadyPresentError(`group "${groupname}" already exists`);
} catch (err) {
if (!(err instanceof NotFoundError))
throw err;
}
await fn(SQL`
INSERT INTO _group(name, description)
VALUES(${groupname}, ${description})`)
}
// Delete the group with the provided name.
async function delete_group(groupname) {
var group_id = await gid(groupname);
await fn(SQL`
DELETE FROM _group
WHERE id = ${group_id}`);
}
// List the members in the group with the provided name.
async function group_list_members(groupname) {
var group_id = await gid(groupname);
var results = await fn(SQL`
SELECT DISTINCT name
FROM (SELECT * FROM blacklist
WHERE _group = ${group_id}
UNION
SELECT * FROM low_quality
WHERE _group = ${group_id}) AS combined`);
var users = []
for(let result of results) {
users.push(result.name);
}
return users;
}
// Get the description of the group with the provided name.
async function group_get_description(groupname) {
var group_id = await gid(groupname);
var results = await fn(SQL`
SELECT description FROM _group
WHERE id = ${group_id}`);
return results[0].description;
}
// Get the list of lists by name. This one is "cheating" in a way,
// because the internal database representation knows these as
// separate tables. Unless we want to change the database schema,
// the set of lists will remain hard-coded and immutable.
async function list_lists() {
return ["blacklist", "low_quality"];
}
// Validate the given list name. Used to sanitise input. Together
// with sql-template-strings, this prevents SQL injection attacks.
async function validate_listname(listname) {
lists = await list_lists();
if(!(lists.includes(listname)))
{
throw new NotFoundError(`unsupported list "${listname}"`);
}
}
// List all of the members of the provided list by name.
async function list_list_members(listname) {
await validate_listname(listname);
var results = await fn(SQL`
SELECT name FROM `.append(listname));
var members = [];
for (let result of results) {
members.push(result.name);
}
return members;
}
// Get the database record for a list member. Includes
// group name, category, and who added the user.
async function list_get_member(listname, membername) {
await validate_listname(listname);
var record = await fn(SQL`
SELECT _group, category, added_by FROM `.append(listname).append(SQL`
WHERE name = ${membername}`));
if (record[0] === undefined)
throw new NotFoundError(`member "${membername}" not found in list "${listname}"`);
var groupname = await fn(SQL`
SELECT name FROM _group
WHERE id = ${record[0]._group}`);
return {group: groupname[0].name,
category: record[0].category,
added_by: record[0].added_by}
}
// Add a user to the provided list with the specified information.
// record should contain the same fields as list_get_member returns.
async function list_add_member(listname, membername, record) {
await validate_listname(listname);
var check = await fn(SQL`
SELECT 1 FROM `.append(listname).append(SQL`
WHERE name = ${membername}`));
if(check.length > 0)
throw new AlreadyPresentError(`member "${membername}" is already in list "${listname}"`);
if(record.group === undefined)
throw new InvalidRecordError("must provide a group");
if(record.category === undefined)
throw new InvalidRecordError("must provide a category");
if(record.added_by === undefined)
throw new InvalidRecordError("must provide 'added_by' username");
try {
var group_id = await gid(record.group)
await fn(SQL`
INSERT INTO `.append(listname).append(SQL`(name, _group, category, added_by)
VALUES(${membername}, ${group_id}, ${record.category}, ${record.added_by})`));
} catch (err) {
if (err instanceof NotFoundError) {
throw new InvalidRecordError(`specified group "${record.group}" does not exist`);
} else {
throw err;
}
}
}
// Delete the specified member from a list.
async function list_delete_member(listname, membername) {
await validate_listname(listname);
var check = await fn(SQL`
SELECT 1 FROM `.append(listname).append(SQL`
WHERE name = ${membername}`));
if(check.length < 1)
throw new NotFoundError(`member "${membername}" not found in list "${listname}"`);
await fn(SQL`
DELETE FROM `.append(listname).append(SQL`
WHERE name = ${membername}`));
}
module.exports = {DatabaseError,
NotFoundError,
AlreadyPresentError,
InvalidRecordError,
list_groups,
delete_group,
add_group,
group_list_members,
group_get_description,
list_lists,
list_list_members,
list_get_member,
list_add_member,
list_delete_member}