@@ -2,6 +2,7 @@ const { MongoClient } = require('mongodb');
2
2
const _ = require ( 'lodash' ) ;
3
3
const { ErrorForClient } = require ( './types' ) ;
4
4
const { parseQuery } = require ( './TextQueryParser' ) ;
5
+ const models = require ( './dbModels' ) ;
5
6
6
7
7
8
let dbClient ;
@@ -72,26 +73,26 @@ const sourceTypes = {
72
73
authPass : { type : 'string' , name : 'auth_pass' } ,
73
74
} ,
74
75
} ;
75
- function parseClientSourceValues ( values , fieldValues ) {
76
+ function parseClientSourceValuesToDbValues ( values , fieldValues ) {
76
77
// TODO: Move this validation to a validation lib
77
78
let toUpdate = { } ;
78
- for ( let prop in values ) {
79
+ for ( let prop in fieldValues ) {
79
80
let field = fieldValues [ prop ] ;
80
- if ( ! field ) {
81
- continue ;
82
- }
81
+ let userVal = values [ prop ] ;
83
82
84
- let val = values [ prop ] ;
83
+ if ( userVal === undefined && field . required ) {
84
+ throw new ErrorForClient ( `Missing required property '${ prop } '` , 'missing_property' ) ;
85
+ }
85
86
86
87
if (
87
- field . type === 'string' && typeof val !== 'string' ||
88
- field . type === 'number' && typeof val !== 'number' ||
89
- field . type === 'boolean' && typeof val !== 'boolean'
88
+ field . type === 'string' && typeof userVal !== 'string' ||
89
+ field . type === 'number' && typeof userVal !== 'number' ||
90
+ field . type === 'boolean' && typeof userVal !== 'boolean'
90
91
) {
91
92
throw new ErrorForClient ( `Invalid type for property '${ prop } '` , 'invalid_type' ) ;
92
93
}
93
94
94
- toUpdate [ field . name ] = val ;
95
+ toUpdate [ field . name ] = userVal ;
95
96
}
96
97
97
98
return toUpdate ;
@@ -161,6 +162,28 @@ const apiv1 = {
161
162
throw new ErrorForClient ( 'Invalid login' , 'bad_auth' ) ;
162
163
}
163
164
} ,
165
+ async register ( apiCtx , userInfo ) {
166
+ let existingUser = await dbUsersCol . findOne ( { name : userInfo . name } ) ;
167
+ if ( existingUser ) {
168
+ throw new ErrorForClient ( 'Username is unavailable' , 'username_unavailable' ) ;
169
+ }
170
+
171
+ let newVals = parseClientSourceValuesToDbValues ( userInfo , {
172
+ name : { type : 'string' , name : 'name' , required : true } ,
173
+ password : { type : 'string' , name : 'password' , required : true } ,
174
+ } ) ;
175
+
176
+ let newUser = models . User ( {
177
+ _id : generateId ( ) ,
178
+ ...newVals ,
179
+ } ) ;
180
+ await dbUsersCol . insertOne ( newUser ) ;
181
+
182
+ return {
183
+ id : newUser . _id ,
184
+ name : newUser . name ,
185
+ } ;
186
+ } ,
164
187
} ,
165
188
account : {
166
189
_meta : {
@@ -206,7 +229,7 @@ const apiv1 = {
206
229
throw new ErrorForClient ( 'Unknown type of message source' , 'unknown_source_type' ) ;
207
230
}
208
231
209
- let toUpdate = parseClientSourceValues ( values , fieldValues ) ;
232
+ let toUpdate = parseClientSourceValuesToDbValues ( values , fieldValues ) ;
210
233
// Prepend the db doc field to each property
211
234
for ( let prop in toUpdate ) {
212
235
toUpdate [ 'sources.$.' + prop ] = toUpdate [ prop ] ;
@@ -234,15 +257,16 @@ const apiv1 = {
234
257
throw new ErrorForClient ( 'Unknown type of message source' , 'unknown_source_type' ) ;
235
258
}
236
259
237
- let newSource = parseClientSourceValues ( values , fieldValues ) ;
238
- if ( Object . keys ( newSource ) . length === 0 ) {
239
- throw new ErrorForClient ( 'Missing name for the new source' , 'missing_params' ) ;
260
+ let newVals = parseClientSourceValuesToDbValues ( values , fieldValues ) ;
261
+ if ( Object . keys ( newVals ) . length === 0 ) {
262
+ throw new ErrorForClient ( 'Missing parameters for the new source' , 'missing_params' ) ;
240
263
}
241
264
242
- let newId = generateId ( ) ;
243
- newSource . _id = newId ;
244
- newSource . name = name . trim ( ) ;
245
- newSource . imapUid = '' ;
265
+ let newSource = models . UserSource ( {
266
+ _id : generateId ( ) ,
267
+ name : name . trim ( ) ,
268
+ ...newVals ,
269
+ } ) ;
246
270
247
271
await dbUsersCol . updateOne (
248
272
{
@@ -251,7 +275,7 @@ const apiv1 = {
251
275
{ $push : { sources : newSource } }
252
276
) ;
253
277
254
- return { id : newId } ;
278
+ return { id : newSource . _id } ;
255
279
} ,
256
280
257
281
async delete ( apiCtx , sourceId ) {
@@ -313,22 +337,22 @@ const apiv1 = {
313
337
let query = values . filter || '' ;
314
338
let parsedQuery = parseQuery ( query ) ;
315
339
316
- let newId = generateId ( ) ;
340
+ let newLabel = models . UserLabel ( {
341
+ _id : generateId ( ) ,
342
+ name : labelName ,
343
+ unread : 0 ,
344
+ filter : { raw : query , parsed : parsedQuery }
345
+ } ) ;
317
346
318
347
await dbUsersCol . updateOne (
319
348
{ _id : apiCtx . session . uid } ,
320
349
{
321
350
$addToSet : {
322
- labels : {
323
- _id : newId ,
324
- name : labelName ,
325
- unread : 0 ,
326
- filter : { raw : query , parsed : parsedQuery }
327
- } ,
351
+ labels : newLabel ,
328
352
} ,
329
353
}
330
354
) ;
331
- return { id : newId , name : labelName } ;
355
+ return { id : newLabel . _id , name : labelName } ;
332
356
} ,
333
357
delete : async ( apiCtx , labelId ) => {
334
358
await dbUsersCol . updateOne (
@@ -344,30 +368,6 @@ const apiv1 = {
344
368
_meta : {
345
369
runBefore : [ requireAuth ] ,
346
370
} ,
347
- // TODO: should be "system" user only
348
- ingest : async ( apiCtx , incoming ) => {
349
- // TODO: validate the incoming message before inserting
350
- let message = {
351
- id : incoming . id ,
352
- threadId : incoming . threadId || '' ,
353
- from :
incoming . from , // 'some name <[email protected] >'
354
- to :
incoming . to , //['[email protected] '],
355
- cc :
incoming . cc , //['[email protected] '],
356
- bcc :
incoming . bcc , //['[email protected] '],
357
- subject : incoming . subject , //'RE: RE: FW: help pls',
358
- bodyText : incoming . bodyText , //'wooo my body ' + i,
359
- bodyHtml : incoming . bodyHtml , //'wooo my <b>body</b> ' + i,
360
- labels : [
361
- 1 ,
362
- ] ,
363
- recieved : incoming . recieved ? incoming . recieved : Date . now ( ) ,
364
- read : incoming . read ? incoming . read : 0 ,
365
- inReplyTo : incoming . inReplyTo || '' ,
366
- raw : incoming . raw || '' ,
367
- } ;
368
- insertMessage ( message ) ;
369
-
370
- } ,
371
371
// Get the latest threads, and all messages within each thread. size=the number of threads
372
372
latest : async ( apiCtx , labelIds , size = 100 ) => {
373
373
let filter = { } ;
0 commit comments