This repository has been archived by the owner on May 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
db.js
171 lines (136 loc) · 4.37 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
import btoa from 'btoa';
import KintoClient from 'kinto-http';
import {
lockDown,
authedCreateNoRead
} from './db/permissions';
import User from './db/collections/user';
import Sentences from './db/collections/sentences-meta';
import CVSentences from './db/collections/cv-sentences';
export const BUCKET_NAME = 'App';
export default class DB {
constructor(remote, username, password) {
this.username = username;
this.password = password;
const defaultOptions = {
remote,
};
if (username && password) {
defaultOptions['headers'] = {
Authorization: "Basic " + btoa(`${username}:${password}`),
};
}
this.server = new KintoClient(remote, defaultOptions);
this.user = new User(this.server, username);
this.sentences = new Sentences(this.server, username);
this.cvSentences = new CVSentences(this.server, username);
}
async getBucket() {
return this.server.bucket(BUCKET_NAME);
}
async auth() {
return this.user.tryAuth();
}
async initDB() {
await this.server.createBucket(BUCKET_NAME, lockDown());
const bucket = await this.getBucket();
// Create collections.
await bucket.createCollection(User.NAME, authedCreateNoRead());
await this.sentences.createAllCollections(bucket);
}
async initCV(metadata) {
const bucket = await this.getBucket();
return this.cvSentences.createFromMeta(bucket, metadata);
}
async deleteSentenceRecords() {
const bucket = await this.getBucket();
return this.sentences.deleteSentenceRecords(bucket);
}
async deleteSpecificSentenceRecords(locale, username) {
const bucket = await this.getBucket();
return this.sentences.deleteSpecificSentenceRecords(bucket, locale, username);
}
async forceDeleteSpecificSentenceRecords(locale, username) {
const bucket = await this.getBucket();
return this.sentences.forceDeleteSpecificSentenceRecords(bucket, locale, username);
}
async forceDeleteSentences(locale, sentences) {
const bucket = await this.getBucket();
return this.sentences.forceDeleteSentences(bucket, locale, sentences);
}
async deleteVotes(locale, username, approvalOnly) {
return this.sentences.deleteVotes(locale, username, approvalOnly);
}
async getCVMetadata() {
const bucket = await this.getBucket();
return this.cvSentences.getLanguageAndSentenceCounts(bucket);
}
async getSiteMetadata() {
const bucket = await this.getBucket();
return this.sentences.getLanguageAndSentenceCounts(bucket);
}
async getUsers() {
return this.user.getAllUsers();
}
async addLanguage(language) {
return this.user.addLanguage(language);
}
async removeLanguage(language) {
return this.user.removeLanguage(language);
}
async getSentences(language) {
return this.sentences.getAll(language);
}
async getSentencesNotVoted(language) {
return this.sentences.getNotVoted(language);
}
async getValidatedSentences(language) {
return this.sentences.getValidatedSentences(language);
}
async getAllValidatedSentences(language) {
return this.sentences.getAllValidatedSentences(language);
}
async correctApprovals(language) {
return this.sentences.correctApprovals(language);
}
async submitSentences(language, sentences, source) {
return this.sentences.submitSentences(language, sentences, source);
}
async getSentenceCount(language) {
return this.sentences.count(language);
}
async getAlreadyExistingSubset(language, sentences) {
return this.sentences.getAlreadyExistingSubset(language, sentences);
}
async vote(language, validated, invalidated) {
return this.sentences.vote(language, validated, invalidated);
}
async getAllRejectedByUsername(languages, username) {
return this.sentences.getAllRejectedByUsername(languages, username);
}
async getLanguageInfoForMe(language) {
try {
const [ submitted, validated ] = await Promise.all([
this.sentences.getMySentences(language),
this.sentences.getMyVotes(language),
]);
return {
language,
submitted,
validated,
};
} catch(err) {
return {
language,
submitted: [],
validated: [],
};
}
}
async getLanguagesMetaForMe(languages) {
return Promise.all(languages.map(language => {
return this.getLanguageInfoForMe(language);
}));
}
}
DB.BUCKET_NAME = BUCKET_NAME;