This repository has been archived by the owner on May 10, 2023. It is now read-only.
/
sentences-meta.js
557 lines (467 loc) · 17.1 KB
/
sentences-meta.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
import { getAllLanguages } from '../../languages';
import { BUCKET_NAME } from '../../db.js';
import { parseBatchResults } from '../api-result-parser';
import { authedCreateReadAndWrite, authedReadAndWrite } from '../permissions';
import hash from '../../hash';
const NAME = 'Sentences_Meta';
const PREFIX = NAME + '_';
const USER_PREFIX = PREFIX + 'UserVote_';
const USER_DATE_PREFIX = PREFIX + 'UserVoteDate_';
const VALID = true;
const INVALID = false;
// Two out of three votes need to be say the sentence is valid
// for it to be approved.
const APPROVAL_MIN_VALID_VOTES = 2;
const APPROVAL_MIN_TOTAL_VOTES = 3;
export default class SentencesMeta {
constructor(kintoServer, username) {
this.server = kintoServer;
this.username = username;
}
getCollectionName(code) {
return PREFIX + code;
}
getUserKey(username) {
return USER_PREFIX + username;
}
getUserDateKey(username) {
return USER_DATE_PREFIX + username;
}
getPreparedRecord({ sentence, reviewed, source }) {
const preparedRecord = {
id: hash(sentence),
sentence,
source,
valid: [],
invalid: [],
username: this.username,
createdAt: Date.now(),
};
if (reviewed) {
preparedRecord.valid = [this.username];
preparedRecord[`${USER_PREFIX}${this.username}`] = true;
}
return preparedRecord;
}
async deleteSentenceRecords(bucket) {
const languages = getAllLanguages();
for (const language of languages) {
const records = await this.getAllPaginated(language.code);
const collectionName = await this.getCollectionName(language.code);
console.log(`Found ${records.length} records to delete for ${language.code}`);
await bucket.batch(b => {
for (let i = 0; i < records.length; i++) {
b.collection(collectionName).deleteRecord(records[i].id);
}
});
}
}
async deleteSpecificSentenceRecords(bucket, locale, username) {
const collectionName = await this.getCollectionName(locale);
const records = await this.getAllUnapprovedByUsername(locale, username);
const minifiedRecords = records.map((record) => ({ id: record.id, sentence: record.sentence }));
console.log(`Found ${minifiedRecords.length} records to delete for ${locale} with username ${username}`);
await bucket.batch(b => {
for (let i = 0; i < minifiedRecords.length; i++) {
console.log('Deleting', minifiedRecords[i]);
b.collection(collectionName).deleteRecord(minifiedRecords[i].id);
}
});
}
async forceDeleteSentences(bucket, locale, sentences) {
const collectionName = await this.getCollectionName(locale);
const records = await this.getAllPaginated(locale);
const minifiedRecords = records.map((record) => ({ id: record.id, sentence: record.sentence }));
const { foundSentences, errorSentences } = sentences.reduce((acc, sentence) => {
const foundSentence = minifiedRecords.find((record) => record.sentence === sentence);
if (!foundSentence) {
acc.errorSentences.push(sentence);
return acc;
}
acc.foundSentences.push(foundSentence);
return acc;
}, { foundSentences: [], errorSentences: [] });
console.log(`Found ${foundSentences.length} records to delete for ${locale}`);
await bucket.batch(b => {
for (let i = 0; i < foundSentences.length; i++) {
console.log('Deleting', foundSentences[i]);
b.collection(collectionName).deleteRecord(foundSentences[i].id);
}
});
console.log('All deleted, except the following sentences which could not be found:');
errorSentences.forEach((sentence) => console.error(sentence));
}
async forceDeleteSpecificSentenceRecords(bucket, locale, username) {
const collectionName = await this.getCollectionName(locale);
const records = await this.getAllByUsername(locale, username);
const minifiedRecords = records.map((record) => ({ id: record.id, sentence: record.sentence }));
console.log(`Found ${minifiedRecords.length} records to delete for ${locale} with username ${username}`);
await bucket.batch(b => {
for (let i = 0; i < minifiedRecords.length; i++) {
console.log('Deleting', minifiedRecords[i]);
b.collection(collectionName).deleteRecord(minifiedRecords[i].id);
}
});
}
async correctApprovals(locale) {
const collection = await this.getCollection(locale);
const records = await this.getAllPaginated(locale);
console.log(`Found ${records.length} records to analyze for ${locale}`);
const adjustedSentences = records.map((record) => {
const isApproved = this.checkIfApproved(record);
if (typeof isApproved === 'undefined' || isApproved === record.approved) {
// nothing to do here
return;
}
record.approved = isApproved;
record.approvalDate = Date.now();
return record;
}).filter(Boolean);
console.log(`Found ${adjustedSentences.length} to adjust`);
const results = await collection.batch(c => {
adjustedSentences.forEach(record => {
c.updateRecord(record, {
safe: true,
last_modified: record.last_modified,
});
});
});
const adjustedResults = [];
const errors = [];
results.forEach(result => {
if (result.status === 200) {
adjustedResults.push(result.body.data);
} else {
console.error('Approval adjustment error', result.status, result.body);
errors.push(result);
}
});
if (adjustedResults.length > 0) {
console.log('All approvals adjusted:');
console.log(`Updated ${adjustedResults.length} records`);
}
if (errors.length > 0) {
console.log(`Errors updating ${errors.length} records!`);
}
}
async deleteVotes(locale, username, approvalOnly) {
const collection = await this.getCollection(locale);
const records = await this.getAllPaginated(locale);
const foundSentences = records.filter((record) => {
return record.valid.includes(username) || record.invalid.includes(username);
});
console.log(`Found ${foundSentences.length} to analyze`);
const adjustedSentences = foundSentences.map((record) => {
if (approvalOnly && !record.valid.includes(username)) {
// Nothing to do for this sentence as there is no approval
return;
}
if (record.valid.includes(username)) {
const index = record.valid.indexOf(username);
record.valid.splice(index, 1);
delete record[`Sentences_Meta_UserVote_${username}`];
delete record[`Sentences_Meta_UserVoteDate_${username}`];
}
if (!approvalOnly && record.invalid.includes(username)) {
const index = record.invalid.indexOf(username);
record.invalid.splice(index, 1);
delete record[`Sentences_Meta_UserVote_${username}`];
delete record[`Sentences_Meta_UserVoteDate_${username}`];
}
// Given that we manipulate votes, we need to re-write the
// approval.
const isApproved = this.checkIfApproved(record);
if (typeof isApproved === 'undefined') {
delete record.approved;
delete record.approvalDate;
} else {
record.approved = isApproved;
record.approvalDate = Date.now();
}
return record;
}).filter(Boolean);
console.log(`Found ${adjustedSentences.length} records to delete votes in ${locale} for ${username}`);
if (adjustedSentences.length === 0) {
return;
}
const results = await collection.batch(c => {
adjustedSentences.forEach(record => {
c.updateRecord(record, {
safe: true,
last_modified: record.last_modified,
});
});
});
const adjustedResults = [];
const errors = [];
results.forEach(result => {
if (result.status === 200 || result === 201) {
adjustedResults.push(result.body.data);
} else {
console.error('Voting deletion error', result.status, result.body);
errors.push(result);
}
});
console.log('All votes adjusted:');
console.log(`Updated ${adjustedResults.length} records`);
console.log(`Errors updating ${errors.length} records!`);
}
async createAllCollections(bucket) {
const languages = getAllLanguages();
const results = await bucket.batch(b => {
for (let i = 0; i < languages.length; i++) {
const code = languages[i].code;
const name = this.getCollectionName(code);
b.createCollection(name, authedCreateReadAndWrite());
}
});
const { successes, errors } = parseBatchResults(results);
if (errors.length > 0) {
throw new Error('Failed: create meta collections' + errors.join(','));
}
if (successes.length !== languages.length) {
throw new Error('Failed: missing meta collections');
}
}
async getLanguages(bucket) {
const result = await this.getLanguageTables(bucket);
return result.map(c => c.replace(PREFIX, ''));
}
async getLanguageTableNames(bucket) {
const result = await bucket.listCollections({
filters: {
like_id: PREFIX,
},
});
return result.data.map(c => c.id);
}
async getCollection(language) {
return this.server.bucket(BUCKET_NAME)
.collection(this.getCollectionName(language));
}
async getAll(language) {
const collection = await this.getCollection(language);
const result = await collection.listRecords();
return result.data;
}
async getAllPaginated(language, filters = {}) {
const collection = await this.getCollection(language);
let { data, hasNextPage, next } = await collection.listRecords(filters);
while (hasNextPage) {
const result = await next();
data = data.concat(result.data);
hasNextPage = result.hasNextPage;
next = result.next;
}
return data;
}
getAllRejectedByUsername(languages, username) {
const promises = languages.map((language) => this.getAllUnapprovedByUsername(language, username));
return Promise.all(promises)
.then((unapprovedPerLanguage) => {
return unapprovedPerLanguage.reduce((rejectedSentences, unapproved, index) => {
if (unapproved && unapproved.length > 0) {
// we can use the index here, as Promise.all guarantees that its
// resolved values will be in the same order
rejectedSentences[languages[index]] = unapproved;
}
return rejectedSentences;
}, {});
});
}
async getAllUnapprovedByUsername(language, username) {
const collection = await this.getCollection(language);
const filters = {
username,
approved: false,
};
const result = await collection.listRecords({ filters });
const unapprovedSentencesWithoutDetail = result.data.map((record) => record.sentence);
return unapprovedSentencesWithoutDetail;
}
async getAllByUsername(language, username) {
const filters = {
username,
};
return this.getAllPaginated(language, filters);
}
async getLanguageAndSentenceCounts(bucket) {
const languages = await this.getLanguageTableNames(bucket);
const responses = await this.server.batch((batch) => {
for (const cid of languages) {
batch.collection(cid).getTotalRecords();
}
}, { bucket: "App" });
return responses.reduce((info, response) => {
let numberOfSentences = 0;
try {
numberOfSentences = parseInt(response.headers['Total-Records'], 10);
} catch (err) { /* ignore */ }
info.sentences = info.sentences + numberOfSentences;
if (numberOfSentences > 0) {
info.languages = info.languages + 1;
}
return info;
}, { languages: 0, sentences: 0 });
}
async getNotVoted(language) {
const filters = {};
// filter current user from votes.
filters['has_' + this.getUserKey(this.username)] = false;
filters['has_approved'] = false;
const collection = await this.getCollection(language);
const result = await collection.listRecords({ filters, sort: 'createdAt' });
const sentences = result.data;
// This works as all modern browsers use a stable sorting algorithm
// if not, we're still fine..
// We always want to show sentences with more valid votes first
const additionallySortedByApprovalVotes = sentences.sort((a, b) => {
const aVoteLength = a.valid.length;
const bVoteLength = b.valid.length;
return bVoteLength - aVoteLength;
});
return additionallySortedByApprovalVotes;
}
async getValidatedSentences(language) {
const filters = {};
filters.approved = true;
const collection = await this.getCollection(language);
const result = await collection.listRecords({ filters });
return result.data;
}
async getAllValidatedSentences(language) {
const filters = {};
filters.approved = true;
return this.getAllPaginated(language, filters);
}
prepareForSubmission(sentences) {
const allUnreviewedSentences = sentences.unreviewed.map((sentence) => {
return { sentence, reviewed: false };
});
const allValidatedSentences = sentences.validated.map((sentence) => {
return { sentence, reviewed: true };
});
return allUnreviewedSentences.concat(allValidatedSentences);
}
async submitSentences(language, sentences, source = '') {
const allSentences = this.prepareForSubmission(sentences);
const collection = await this.getCollection(language);
const results = await collection.batch(batch => {
for (let i = 0; i < allSentences.length; i++) {
const record = this.getPreparedRecord({
sentence: allSentences[i].sentence,
reviewed: allSentences[i].reviewed,
source,
});
batch.createRecord(record, {
...authedReadAndWrite(),
safe: true,
});
}
});
const { successes, errors } = parseBatchResults(results);
return {
sentences: successes,
errors,
};
}
async getMyVotes(language) {
const collection = await this.getCollection(language);
const filters = {};
filters['has_' + this.getUserKey(this.username)] = true;
const result = await collection.listRecords({ filters });
return result.data;
}
getVoteRecords(isValid, sentences, existing) {
return sentences.map(sentence => {
let record = existing[sentence.id] ?
existing[sentence.id] : this.getPreparedRecord({
sentence: sentence.sentence,
reviewed: false,
});
if (isValid) {
record.valid.push(this.username);
record[this.getUserKey(this.username)] = VALID;
} else {
record.invalid.push(this.username);
record[this.getUserKey(this.username)] = INVALID;
}
record[this.getUserDateKey(this.username)] = Date.now();
const isApproved = this.checkIfApproved(record);
if (typeof isApproved === 'undefined') {
// We don't have any approval yet (not enough votes)
// therefore we leave the record as is
return record;
}
record.approved = isApproved;
record.approvalDate = Date.now();
return record;
});
}
checkIfApproved(record) {
const validVotes = record.valid.length;
const invalidVotes = record.invalid.length;
const totalVotes = validVotes + invalidVotes;
if(validVotes >= APPROVAL_MIN_VALID_VOTES) {
return true;
} else if(totalVotes >= APPROVAL_MIN_TOTAL_VOTES) {
return false;
}
return;
}
async vote(language, validated, invalidated) {
const collection = await this.getCollection(language);
const notVoted = await this.getNotVoted(language);
const existing = notVoted.reduce((accum, record) => {
accum[record.id] = record;
return accum;
}, {});
const results = await collection.batch(c => {
let updateRecords = [
...this.getVoteRecords(true, validated, existing),
...this.getVoteRecords(false, invalidated, existing),
];
updateRecords.forEach(record => {
c.updateRecord(record, {
safe: true,
last_modified: record.last_modified,
});
});
});
let votes = [];
let errors = [];
results.forEach(result => {
if (result.status === 200 || result === 201) {
votes.push(result.body.data);
} else {
console.error('vote error', result.status, result.body);
errors.push(result);
}
});
return {
votes,
errors,
};
}
async count(language) {
let collection = await this.getCollection(language);
return collection.getTotalRecords();
}
async getAlreadyExistingSubset(language, sentences) {
const idList = sentences.map(s => hash(s));
const result = await this.getAll(language);
const existingIDs = result.map((existingSentence) => existingSentence.id);
const existingSubmittedSentencesIDs = idList.filter((id) => existingIDs.includes(id));
return result.filter((existingSentence) => existingSubmittedSentencesIDs.includes(existingSentence.id));
}
async getMySentences(language) {
const collection = await this.getCollection(language);
const result = await collection.listRecords({
filters: {
username: this.username,
},
});
return result.data;
}
}
SentencesMeta.NAME = NAME;