-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
330 lines (276 loc) · 7.67 KB
/
index.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
const PouchDB = orDefault(require("pouchdb"));
PouchDB.plugin(orDefault(require("pouchdb-find")));
const HyperbeePlugin = orDefault(require("pouchdb-adapter-hyperbee"));
const AUTHOR_KEY = "author";
const REGEX_NON_WORDS = /\W+/;
const DEFAULT_SORT = [{ type: "desc" }, { createdAt: "desc" }];
// Based on USHIN data model
// https://github.com/USHIN-Inc/ushin-ui-components/blob/master/src/dataModels/dataModels.ts
let initialized = null;
class USHINBase {
static init(opts) {
if (initialized) return initialized;
initialized = HyperbeePlugin(opts);
PouchDB.plugin(initialized);
return initialized;
}
static async close() {
if (initialized) initialized.close();
}
constructor({ url, ...opts }) {
USHINBase.init(opts);
this.db = new PouchDB(url, {
adapter: "hyperbee",
});
}
get writable() {
return this.db.bee.feed.writable;
}
get peers() {
return this.db.bee.feed.peers || [];
}
async init() {
// Wait for the DB to load
await new Promise((resolve, reject) => {
this.db.once("open", resolve);
this.db.once("error", reject);
});
this.authorURL = await this.db.getURL();
// TODO create indexes here based on the sorts of queries we want
await this.createIndex("type");
await this.createIndex("type", "createdAt");
await this.createIndex("type", "createdAt", "textSearch");
await this.createIndex("type", "createdAt", "allPoints");
}
async createIndex(...fields) {
return this.db.createIndex({
index: { fields },
});
}
async setAuthorInfo(info = {}) {
const { _rev, _id, ...data } = await this.getAuthorInfo();
await this.db.put({
...data,
...info,
_id: AUTHOR_KEY,
_rev,
});
}
async getAuthorInfo() {
try {
const info = await this.db.get(AUTHOR_KEY);
return info;
} catch (e) {
if (e.name === "not_found") {
await this.db.put({ _id: AUTHOR_KEY });
// No need to await when returning from async fn
return this.db.get(AUTHOR_KEY);
} else {
throw e;
}
}
}
async addMessage(
{
_id,
_rev,
revisionOf,
main,
responseHistory,
createdAt = new Date(),
shapes = {},
},
pointStore = {}
) {
if (!main) throw new Error("Message lacks main point");
const { authorURL } = this;
let createdAtTime;
if (typeof createdAt === "string") {
createdAtTime = new Date(createdAt).getTime();
} else if (typeof createdAt === "object") {
createdAtTime = createdAt.getTime();
} else {
throw new Error(
"message's createdAt attribute is neither of type string nor object"
);
}
const allPoints = new Set([main, ...Object.values(shapes).flat()]);
// Convert allPoints set to array to avoid iterating over pointIds which are
// added from referenceHistory
for (const pointId of [...allPoints]) {
const point = pointStore[pointId];
if (!point) {
const error = new Error("Point ID not found in store");
error.pointId = pointId;
throw error;
}
if (!point._id) throw new Error("Must specify point ID");
if (!point._rev) {
await this.addPoint({ createdAt: createdAtTime, ...point });
}
if (point.referenceHistory) {
for (const log of point.referenceHistory) allPoints.add(log.pointId);
}
}
const toSave = {
_id,
type: "message",
revisionOf,
main,
responseHistory,
createdAt: createdAtTime,
author: authorURL,
shapes,
allPoints: [...allPoints],
};
if (_id && _rev) {
await this.db.put({ ...toSave, _rev });
return _id;
} else {
const { id } = await this.db.post(toSave);
return id;
}
}
async getMessage(id) {
const rawMessage = await this.db.get(id);
const { createdAt } = rawMessage;
const createdAtDate = new Date(createdAt);
return { ...rawMessage, createdAt: createdAtDate };
}
async searchMessages(
selector = {},
{ limit = 32, skip, sort = DEFAULT_SORT } = {}
) {
const finalSelector = {
createdAt: { $exists: true },
...selector,
type: "message",
};
const result = await this.db.find({
selector: finalSelector,
sort,
limit,
skip,
});
const { docs } = result;
return docs.map((rawMessage) => {
const createdAtDate = new Date(rawMessage.createdAt);
return { ...rawMessage, createdAt: createdAtDate };
});
}
async searchMessagesForPoints(points, ...args) {
const allPoints = points.map(({ _id }) => _id);
return this.searchMessages(
{
allPoints: {
$elemMatch: {
$in: allPoints,
},
},
},
...args
);
}
async getPointsForMessage(
{ main, shapes, responseHistory = [] },
existingPoints = {}
) {
let pointIds = [main, ...Object.values(shapes).flat()];
const allResponsePointIds = new Set();
for (const response of responseHistory) {
allResponsePointIds.add(response.mainPointId);
if (response.secondaryPointId !== undefined) {
allResponsePointIds.add(response.secondaryPointId);
}
}
pointIds = pointIds.concat(Array.from(allResponsePointIds));
const dedupedPointIds = pointIds.filter((id) => !existingPoints[id]);
const points = await Promise.all(
dedupedPointIds.map((id) => this.getPoint(id))
);
const referencePointIds = new Set();
for (const point of points) {
if (point.referenceHistory) {
for (const log of point.referenceHistory) {
referencePointIds.add(log.pointId);
}
}
}
const dedupedReferencePointIds = [...referencePointIds].filter(
(id) => !existingPoints[id]
);
const referencePoints = await Promise.all(
dedupedReferencePointIds.map((id) => this.getPoint(id))
);
points.push(...referencePoints);
return points.reduce((result, point) => {
result[point._id] = point;
return result;
}, {});
}
async searchPointsByContent(
query,
{ limit = 32, skip, sort = DEFAULT_SORT } = {}
) {
const tokens = stringToTokens(query);
const { docs } = await this.db.find({
selector: {
type: "point",
textSearch: { $all: tokens },
createdAt: { $exists: true },
},
sort,
limit,
skip,
});
return docs;
}
async addPoint(point) {
let textSearch;
const { _id, content, createdAt } = point;
// Only set the textSearch property if there's content for this point
if (content) {
const tokens = stringToTokens(content);
if (tokens.length) textSearch = tokens;
}
const doc = {
...point,
_id,
type: "point",
content,
createdAt: createdAt || Date.now(),
textSearch,
};
if (!_id) {
const { id } = await this.db.post(doc);
return id;
} else {
await this.db.put(doc);
return _id;
}
}
// TODO: Throw error if document isn't a point?
async getPoint(id) {
return this.db.get(id);
}
async close() {
return this.db.close();
}
}
module.exports = {
USHINBase,
};
// This is necessary to account for Webpack environments
// Pouch exports ESM when possible, and Webpack doesn't normalize it back
function orDefault(module) {
if (module.default) return module.default;
return module;
}
// Convert some text to tokens which can be used for searching
function stringToTokens(content) {
const lowered = content.toLowerCase();
const rawTokens = lowered.split(REGEX_NON_WORDS);
const nonEmpty = rawTokens.filter((item) => !!item);
const deduped = new Set(nonEmpty);
return [...deduped];
}