This repository has been archived by the owner on Aug 17, 2020. It is now read-only.
/
api.js
421 lines (299 loc) · 13.3 KB
/
api.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
// ROUTE: --> /api
const router = require( 'express' ).Router(),
db = require( '../modules/dbQuery' ),
fs = require( 'fs' ),
path = require( 'path' ),
multer = require( 'multer' ),
uidSafe = require( 'uid-safe' ),
knox = require( 'knox' );
//_ MUTER & UIDSAFE_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
const diskStorage = multer.diskStorage( {
// path.resolve;
destination: ( req, file, callback ) => {
callback( null, `${ path.dirname( __dirname ) }/uploads` );
},
filename: ( req, file, callback ) => {
uidSafe( 24 )
.then( ( uid ) => {
callback( null, uid + path.extname( file.originalname ) );
} );
}
} );
const uploader = multer( {
storage: diskStorage,
limits: {
filesize: 2097152
}
} );
//_ KNOX_ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
let secrets;
if ( process.env.NODE_ENV == 'production' ) {
secrets = process.env;
// in prod the secrets are environment variables
} else {
secrets = require( '../config/secrets.json' );
// secrets.json is in .gitignore
}
const client = knox.createClient( {
key: secrets.AWS_KEY,
secret: secrets.AWS_SECRET,
bucket: secrets.AWS_BUCKET
} );
// _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
// _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
// _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
// ROOT OF THE API _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
router.get( '/', ( req, res ) => {
console.log( 'API: ', 'method: GET ', '/api/' );
res.json( { message: 'api route working fine' } );
} );
// _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
// REGISTER USER _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
router.post( '/register', ( req, res ) => {
console.log( 'API: ', 'method: POST ', '/api/register' );
const firstName = req.body.firstName.toLowerCase();
const lastName = req.body.lastName.toLowerCase();
const email = req.body.email.toLowerCase();
const password = req.body.password.toLowerCase();
if ( firstName && lastName && email && password ) {
return db.createUser( firstName, lastName, email, password )
.then( result => {
if ( result ) {
// set the session to be true
req.session.user = {
uid: result.uid,
firstName: result.firstName,
lastName: result.lastName,
email: result.email
};
// log user session
console.log( 'successfully set the session', req.session.user );
res.json( { success: true } );
} else {
res.json( { error: true } );
}
} )
.catch( ( err ) => {
console.log( err.stack );
} );
} else {
res.json( { error: true } );
}
} );
// _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
// LOGIN USER_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
router.post( '/login', ( req, res ) => {
console.log( 'API: method: POST /api/login' );
const email = req.body.email.toLowerCase();
const password = req.body.password.toLowerCase();
if ( email && password ) {
return db.checkUser( email, password )
.then( ( resp ) => {
if ( resp ) {
// set the session to be true
req.session.user = {
uid: resp.uid,
// firstName: resp.firstName,
// lastName: resp.lastName,
// email: resp.email
};
res.json( { success: true } );
} else {
res.json( { error: true } );
}
} )
.catch( ( err ) => {
console.log( err.stack );
} );
} else {
res.json( { error: true } );
}
} );
// _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
// LOGOUT USER _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
router.get( '/logout', ( req, res ) => {
console.log( 'API: method: POST /api/logout' );
req.session = null;
// res.redirect('/welcome');
return res.json( { success: true } );
} );
// _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
// GET USER DATA _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
router.get( '/user', ( req, res ) => {
console.log( `API: method: GET /api/user - uid = ${req.session.user.uid}` );
if ( req.session.user ) {
return db.readUser( req.session.user.uid )
.then( userData => res.json( { userData } ) )
.catch( err => console.error( err.stack ) );
}
} );
// _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
// SET USER PROFILE PICTURE PROFILE_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
router.put( '/user/profile_pic', uploader.single( 'file' ), ( req, res ) => {
console.log( 'API: method: PUT /api/user/profile_pic' );
console.log( req.file );
if ( req.file ) {
/*
You can call the put method of the client you've created to create a request
object using the data in the file object that multer added to the req.
* The first argument to put is the name you want the file to have in
the bucket
* The second argument is an object with additional parameters.
The Content-Type and Content-Length parameters are the headers you
want S3 to use when it serves the file. The x-amz-acl parameter
tells S3 to serve the file to anybody who requests it
(the default is for files to be private)
*/
const s3Request = client.put( req.file.filename, {
'Content-Type': req.file.size,
'Content-Length': req.file.size,
'x-amz-acl': 'public-read'
} );
/* You can now create a read stream out of the file and pipe it to the
request you've created.
*/
const readStream = fs.createReadStream( req.file.path );
readStream.pipe( s3Request );
s3Request.on( 'response', ( s3Response ) => {
const wasSuccessful = s3Response.statusCode == 200;
if ( wasSuccessful ) {
const profilePic = req.file.filename;
const uid = req.session.user.uid;
return db.saveUserProfilePic( uid, profilePic )
.then( userData => {
res.json( { success: wasSuccessful, userData } );
// remove image from server/uploads
fs.unlink( req.file.path, () => {} );
} );
}
} );
} else {
return res.json( { success: false } );
}
} );
// _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
// SET USER BIO_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
router.put( '/user/:uid/bio', ( req, res ) => {
console.log( 'API: ', 'method: PUT ', `/api/user/${req.params.uid}/bio \n` );
const bio = req.body.bio.toLowerCase();
return db.saveUserBio( req.params.uid, bio )
.then( ( userData ) => {
res.json( {
success: true,
userData: userData
} );
} )
.catch( ( err ) => {
console.error( err.stack );
} );
} );
// _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
// GET OTHER USER'S DATA _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
router.get( '/user/:uid', ( req, res ) => {
console.log( `API: method: GET /api/user/${req.params.uid} \n` );
return db.getOtherUserInfo( req.params.uid )
.then( otherUserData => res.json( { success: true, otherUserData } ) )
.catch( err => console.error( err.stack ) );
} );
// _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
// GET USERS DATA FROM LATEST 20 TO REGISTER:
router.get( '/users', ( req, res ) => {
console.log( 'API: method: GET /api/users \n' );
return db.readLatestUsers()
.then( latestUsers => {
// console.log( 'latestUsers: ', latestUsers );
res.json( { users: latestUsers } );
} )
.catch( err => console.error( err.stack ) );
} );
// GET USERS DATA FOR SERCHED USERS:
router.post( '/users/search', ( req, res ) => {
const search = req.body.search.toLowerCase();
console.log( 'API: method: GET /api/users/search - search: ', search );
return db.readSearchedUsers( search )
.then( searchedUsers => res.json( { users: searchedUsers } ) )
.catch( err => console.error( err.stack ) );
} );
// ____________________________________________________________________________
// FRIENDSHIP ROUTES:
// C: CREATE - POST - /api/friends/:fromUserId/:toUserId BECOME FRIEND
// R: READ - GET - /api/friends/:fromUserId/ SEE ALL USERS'S FRIEND
// U: UPDATE - PUT - /api/friends/:fromUserId/:toUserId UPDATE FRIENDSHIP
// D: DELETE - DELETE - /api/friends/:fromUserId/:toUserId TERMINATE FREINDSHIP
// R: READ - GET - /api/friends/:fromUserId/ SEE ALL USERS'S FRIEND
router.get( '/friends', ( req, res ) => {
const fromUserId = req.session.user.uid;
console.log( `API: method: GET /api/friends/${fromUserId}` );
return db.readAllFriends( fromUserId )
.then( friends => res.json( { friends } ) )
.catch( err => console.error( err.stack ) );
} );
//_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
// R: READ - GET - /api/friends/:fromUserId/:toUserI SEE FRIENDSHIP STATUS OF TWO USERS
router.get( '/friends/:fromUserId/:toUserId', ( req, res ) => {
const fromUserId = req.params.fromUserId;
const toUserId = req.params.toUserId;
console.log( `API: method: GET /api/friends/${fromUserId}/${toUserId}` );
return db.readFriendshipStatus( fromUserId, toUserId )
.then( resp => {
if ( !resp ) {
res.json( {
success: true,
btnAction: 'MAKE FRIEND REQ'
} );
} else {
return res.json( Object.assign( resp, { success: true } ) );
}
} )
.catch( err => console.error( err.stack ) );
} );
//_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
// C: CREATE - POST - /api/friends/:fromUserId/:toUserId && newStatus === 'PENDING'
router.post( '/friends/:fromUserId/:toUserId/new', ( req, res ) => {
const fromUserId = req.params.fromUserId;
const toUserId = req.params.toUserId;
const status = req.body.status;
console.log( `API: method: POST /api/friends/${fromUserId}/${toUserId}` );
if ( status === 'PENDING' ) {
return db.createFriendshipReq( fromUserId, toUserId, status )
.then( resp => res.json( resp ) )
.catch( err => console.error( err.stack ) );
} else {
res.json( { success: false } );
throw 'ERROR: WRONG VERB. ONLY PENDING';
}
} );
//_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
// U: UPDATE - PUT - /api/friends/:fromUserId/:toUserId
// UPDATE FRIENDSHIP { PENDING, ACCEPTED, CANCELED, TERMINATED }
router.put( '/friends/:fromUserId/:toUserId', ( req, res ) => {
const fromUserId = req.params.fromUserId;
const toUserId = req.params.toUserId;
const status = req.body.status;
console.log( `API: method: PUT /api/friends/${fromUserId}/${toUserId} - status: ${status}` );
if ( status === 'PENDING' ||
status === 'ACCEPTED' ||
status === 'CANCELED' ||
status === 'TERMINATED' ) {
console.log('WE ARE IN THIS B!', status, fromUserId, toUserId );
return db.updateFriendshipStatus( fromUserId, toUserId, status )
.then( newFriendshipStatus => res.json( { newFriendshipStatus } ) )
.catch( err => console.error( err.stack ) );
} else {
res.json( { success: false } );
throw 'ERROR: WRONG VERB. ONLY PENDING || ACCEPTED || CANCELED || TERMINATED';
}
} );
//_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
// D: DELETE - DELETE - /api/friends/:fromUserId/:toUserId TERMINATE FREINDSHIP
router.delete( '/friends/:fromUserId/:toUserId/delete', ( req, res ) => {
const fromUserId = req.params.fromUserId;
const toUserId = req.params.toUserId;
console.log( `API: method: DELETE /api/friends/${fromUserId}/${toUserId}/delete - status: ${status}` );
return db.deleteFriendship( fromUserId, toUserId )
.then( resp => res.json( resp ) )
.catch( err => console.error( err.stack ) );
} );
//_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/* MODULE EXPORTS */
module.exports = router;