-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.ts
257 lines (222 loc) · 7.62 KB
/
db.ts
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
/* eslint-disable @typescript-eslint/camelcase */
// TODO get types
// eslint-disable-next-line @typescript-eslint/no-var-requires
const Firestore = require("@google-cloud/firestore");
import Robot from "./robot";
import User from "./user";
const GLOBAL_KEY = "GLOBAL";
export default class DB {
robot: Robot;
db: any;
userTokenMap: {[token: string]: string} = {};
constructor(robot: Robot) {
this.robot = robot;
if (
!process.env.VAL_FIRESTORE_CLIENT_EMAIL ||
process.env.VAL_FIRESTORE_CLIENT_EMAIL.trim() === ""
) {
throw new Error("You must set the VAL_FIRESTORE_CLIENT_EMAIL environment variable");
}
if (
!process.env.VAL_FIRESTORE_PRIVATE_KEY ||
process.env.VAL_FIRESTORE_PRIVATE_KEY.trim() === ""
) {
throw new Error("You must set the VAL_FIRESTORE_PRIVATE_KEY environment variable");
}
if (
!process.env.VAL_FIRESTORE_PROJECT_ID ||
process.env.VAL_FIRESTORE_PROJECT_ID.trim() === ""
) {
throw new Error("You must set the VAL_FIRESTORE_PROJECT_ID environment variable");
}
robot.logger.debug(
`[db] connecting to firestore project ${process.env.VAL_FIRESTORE_PROJECT_ID}`
);
this.db = new Firestore({
projectId: process.env.VAL_FIRESTORE_PROJECT_ID,
credentials: {
client_email: process.env.VAL_FIRESTORE_CLIENT_EMAIL,
private_key: process.env.VAL_FIRESTORE_PRIVATE_KEY.replace(/\\n/g, "\n"),
},
});
this.initUserTokenMap();
}
initUserTokenMap = async (users?: any): Promise<void> => {
if (!users) {
users = await this.getUsers();
}
for (let user of Object.values(users)) {
if (user.authToken) {
this.userTokenMap[user.authToken] = user.id;
}
}
};
// By default, everything is stored per user.
private getKey(userId, key): string {
if (userId === GLOBAL_KEY || userId === null) {
return `GLOBAL/${key}`;
} else {
return `${userId}/${key}`;
}
}
set(userId, key, value): Promise<void> {
return this.db.doc(this.getKey(userId, key)).set(value);
}
async get(userId: string, key: string, defaultReturn?: any): Promise<any[] | any> {
let docs = [];
let all = await this.db.doc(this.getKey(userId, key)).get();
if (all.forEach) {
all.forEach((doc) => docs.push(doc.data()));
} else {
docs = all.data();
}
return docs !== undefined ? docs : defaultReturn;
}
public async getConfig(): Promise<{[key: string]: any}> {
return this.get(GLOBAL_KEY, "config");
}
public async setConfig(config: any): Promise<void> {
return this.set(GLOBAL_KEY, "config", config);
}
// Update or create new user
public async updateUser(user: User): Promise<void> {
if (!user || !user.id) {
this.robot.logger.warn(`[db] Cannot update undefined user: ${user}`);
return;
}
let users = await this.get(GLOBAL_KEY, "users");
if (!users) {
users = {};
}
const updatedUser = user.serialize ? user.serialize() : user;
users[user.id] = Object.assign({}, user, updatedUser);
await this.set(GLOBAL_KEY, "users", users);
await this.initUserTokenMap(users);
}
public async getUser(userId: string): Promise<User> {
let users = (await this.get(GLOBAL_KEY, "users")) || {};
return new User(users[userId]);
}
public async getUsers(teamId?: string): Promise<{[id: string]: User}> {
let rawUsers = (await this.get(GLOBAL_KEY, "users")) || {};
let users = {};
for (let id of Object.keys(rawUsers)) {
let user = new User(rawUsers[id]);
if (!teamId || user.onTeam(teamId)) {
users[id] = user;
}
}
return users;
}
public async userForId(id: string): Promise<User> {
if (!id) {
this.robot.logger.warn("[db] userForId cannot search for undefined id");
return undefined;
}
// Ask each user object if the id is contained in thir user object
let user: User;
let users = await this.getUsers();
for (let u of Object.values(users)) {
if (u.containsId(id)) {
user = u;
break;
}
}
if (!user) {
return undefined;
} else {
return user;
}
}
// Keeps a cached mapping of user token to user id so we can easily check if a token belongs
// or not
public getUserFromAuthToken(token: string): string {
return this.userTokenMap[token];
}
// Categories
//
// This is a simple way to manage a series of grouped items for the bot, dynamically.
// A good example is a bot that replies to a certain phrase with a random gif. If the list of gifs
// is hard coded in the plugin, it will get boring eventually. This lets the plugin register a
// list of default gifs, then a user can add more gifs to the category, and when a phrase triggers
// the plugin, it fetchs a random item. The items are stored as strings, but could be response
// phrases, image links, or serialized objects.
CATEGORY_KEY = "categories";
public async listCategories(): Promise<string[]> {
let allItems = (await this.get(GLOBAL_KEY, this.CATEGORY_KEY)) || {};
return Object.keys(allItems);
}
public async addItemToCategory(category: string, item: string): Promise<void> {
let allItems = (await this.get(GLOBAL_KEY, this.CATEGORY_KEY)) || {};
if (!allItems[category]) {
allItems[category] = [];
}
allItems[category].push(item);
await this.set(GLOBAL_KEY, this.CATEGORY_KEY, allItems);
}
public async removeItemAtIndexInCategory(category: string, index: number): Promise<void> {
let allItems = (await this.get(GLOBAL_KEY, this.CATEGORY_KEY)) || {};
let items = allItems[category] || [];
items.splice(index, 1);
allItems[category] = items;
await this.set(GLOBAL_KEY, this.CATEGORY_KEY, allItems);
}
public async getRandomItemFromCategory(category: string): Promise<string> {
let combined = await this.listItemsInCategory(category);
return combined[Math.floor(Math.random() * combined.length)];
}
public async listItemsInCategory(category: string): Promise<string[]> {
let allItems = (await this.get(GLOBAL_KEY, this.CATEGORY_KEY)) || {};
let items = allItems[category] || [];
return items;
}
public async registerDefaultsForCateogry(category: string, items: string[]): Promise<void> {
let allItems = (await this.get(GLOBAL_KEY, this.CATEGORY_KEY)) || {};
let existingItems = allItems[category] || [];
if (existingItems.length > 0) {
return;
}
this.robot.logger.debug(`[db] Loading defaults for category ${category}`);
allItems[category] = items;
this.set(GLOBAL_KEY, this.CATEGORY_KEY, allItems);
}
}
// // Provide a non-async, backwards compat brain that wraps DB.
// export class FirebaseBrain extends EventEmitter {
// robot: Robot;
// data: any;
// constructor(robot) {
// super();
// this.robot = robot;
// this.data = {
// users: {}, // id: User
// _private: {},
// };
// }
// public set(key, value) {
// let pair: any;
// if (key === Object(key)) {
// pair = key;
// } else {
// pair = {};
// pair[key] = value;
// }
// _.extend(this.data._private, pair);
// this.emit("loaded", this.data);
// return this;
// }
// public get(key) {
// return this.data._private[key] != null ? this.data._private[key] : null;
// }
// public mergeData(data) {
// if (data) {
// _.extend(this.data, data);
// }
// this.robot.logger.info(`[db] Merged data, current keys: ${Object.keys(this.data._private)}`);
// this.emit("loaded", this.data);
// }
// public save() {}
// public close() {}
// public setAutoSave(enabled) {}
// public resetSaveInterval(seconds) {}
// }