This repository has been archived by the owner on Feb 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server-side-functions.js
559 lines (461 loc) · 17.5 KB
/
server-side-functions.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
558
559
const functions = require('firebase-functions');
const firebase_tools = require('firebase-tools');
const admin = require('firebase-admin');
admin.initializeApp();
exports.changeUsername = functions
.https.onCall(async(data, context) => {
const previousUsername = data.previousUsername
const newUsername = data.newUsername
const following = Number(data.following)
const follower = Number(data.follower)
const biography = data.biography
const location = data.location
const profileImageUrl = data.profileImageUrl
const userId = data.userId
const backgroundImageUrl = data.backgroundImageUrl
const lastDateUsernameChange = Number(data.lastDateUsernameChange)
//USERNAME
const usernameSnapshotQuery = await admin.firestore().collection('usernames').where('username', '==', `${previousUsername}`).get();
if (usernameSnapshotQuery.isEmpty) {
return;
}
usernameSnapshotQuery.forEach(doc => {
doc.ref.update({
username: `${newUsername}`
});
});
//USER
const oldUserDoc = await admin.firestore().collection('users').doc(`${previousUsername}`).get();
const newUserData = {
username: `${newUsername}`,
location: `${location}`,
following: following,
follower: follower,
biography: `${biography}`,
profileImageUrl: `${profileImageUrl}`,
backgroundImageUrl: `${backgroundImageUrl}`,
lastDateUsernameChange: lastDateUsernameChange,
userId: `${userId}`
}
const newUserRef = admin.firestore().collection('users').doc(`${newUsername}`);
newUserRef.update(newUserData, { merge: true });
oldUserDoc.ref.delete()
//POSTS
/* const userPostsSnapshotQuery = await admin.firestore().collection('posts').where('username', '==', `${previousUsername}`).get(); */
const userPostsDoc = await admin.firestore().collection('posts').doc(userId).get();
if (!userPostsDoc.exists) {
return;
}
userPostsDoc.ref.update({ username: `${newUsername}` });
/* userPostsSnapshotQuery.forEach(doc => {
doc.ref.update();
}); */
//FOLLOWING
//Change username document of user following collection
const userFollowingDoc = await admin.firestore().collection('following').doc(userId).get();
if (!userFollowingDoc.exists) {
return;
}
userFollowingDoc.ref.update({
username: `${newUsername}`
});
//FOLLOWER
//Change username document of user follower collection
const userFollowerDoc = await admin.firestore().collection('follower').doc(userId).get();
if (!userFollowerDoc.exists) {
return;
}
userFollowerDoc.ref.update({
username: `${newUsername}`
});
/* eslint-enable no-await-in-loop */
});
/* exports.signUpWith = functions
.https.onCall(async(data, context) => {
const email = data.email;
const password = data.password;
});
*/
exports.changeUserDetails = functions
.https.onCall(async(data, context) => {
const username = data.username
const newBiography = data.biography
const newLocation = data.location
const profileImageUrl = data.profileImageUrl
const backgroundImageUrl = data.backgroundImageUrl
const userCollectionRef = admin.firestore().collection('users')
const userDocRef = userCollectionRef.doc(`${username}`)
return await userDocRef.update({
location: `${newLocation}`,
biography: `${newBiography}`,
profileImageUrl: `${profileImageUrl}`,
backgroundImageUrl: `${backgroundImageUrl}`
});
});
//Disable in Auth and set disabled time in Firestore
exports.disableAccount = functions
.https.onCall(async(data, context) => {
const uid = context.auth.uid;
const disabled = await admin.auth().updateUser(uid, {
disabled: true
}).then(function(userRecord) {
if (userRecord.disabled) {
return true;
} else {
return false;
}
}).catch(error => {
functions.logger.log("Error!", error);
throw new error
});
if (!disabled) {
return false
}
const dateDisabledMilliseconds = new Date().getTime();
const disabledObject = {
dateDisabledMilliseconds: dateDisabledMilliseconds,
disabled: true
}
await admin.firestore().collection('disabled').doc(uid).set(disabledObject);
return true;
});
exports.enableAccount = functions
.https.onCall(async(data, context) => {
const uid = data.uid;
const disabled = await admin.auth().updateUser(uid, {
disabled: false
}).then(function(userRecord) {
if (!userRecord.disabled) {
return true;
} else {
return false;
}
}).catch(error => {
functions.logger.log("Error!", error);
throw new error
});
if (!disabled) {
return false
}
const disabledObject = {
dateDisabledMilliseconds: 0,
disabled: false
}
await admin.firestore().collection('disabled').doc(uid).update(disabledObject);
return true;
});
exports.addNewPost = functions
.https.onCall(async(data, context) => {
const postTitle = data.postTitle;
const postText = data.postText;
const postLikes = Number(data.postLikes);
const postCreatedAt = Number(data.postCreated_at);
const postUserCreatorId = data.postUserCreatorId
const postDocRef = admin.firestore().collection('posts').doc(postUserCreatorId).collection('user_posts').doc();
await postDocRef
.set({
title: `${postTitle}`,
text: `${postText}`,
likes: postLikes,
createdAt: postCreatedAt,
postId: `${postDocRef.id}`,
userCreatorId: `${postUserCreatorId}`
});
const countPostsDocRef = admin.firestore().collection('posts').doc(userPostsDocUid).collection('user_count_posts').doc('countPosts');
return await countPostsDocRef.set({ countPosts: admin.firestore.FieldValue.increment(1) }, { merge: true })
});
exports.updatePost = functions
.https.onCall(async(data, context) => {
const postID = data.postID;
const title = data.title;
const text = data.text;
const userId = data.userId;
return admin.firestore()
.collection("posts").doc(userId)
.collection("user_posts").doc(postID)
.update({
title: title,
text: text
}).then(() => {
return true;
}).catch(error => {
return error;
});
});
exports.signUpWithEmail = functions
.https.onCall(async(data, context) => {
const username = data.username;
const email = data.email;
const password = data.password;
functions.logger.log("Username", username);
const isUsernameAvailableResult = await isUsernameAvailable(username);
if (!isUsernameAvailableResult) {
functions.logger.log("error", "Username isn't available");
throw new functions.https.HttpsError('already-exists', 'Username is already picked up.');
}
const createUserAuthenticationResult = await createUserAuthentication(email, password);
const status = createUserAuthenticationResult.status
const uid = createUserAuthenticationResult.uid
functions.logger.log("Status", status);
functions.logger.log("uid", uid);
if (!status) {
functions.logger.log("error", "There was an error when creating the user");
throw new functions.https.HttpsError('already-exists', 'Email is already picked up.');
}
if (uid === '' || uid === '0') {
functions.logger.log("Error", "UID is not available");
throw new functions.https.HttpsError('failed-precondition', 'UID is not available.');
}
const saveUsernameDocumentResult = await saveInUsernameCollection(uid, username, email)
if (!saveUsernameDocumentResult) {
functions.logger.log("Error", "Couldn't save in Username collection");
throw new functions.https.HttpsError('failed-precondition', "Couldn't save in Username collection");
}
const saveUserDocumentResult = await saveInUserCollection(uid, username);
if (!saveUserDocumentResult) {
functions.logger.log("Error", "Couldn't save in Username collection");
throw new functions.https.HttpsError('failed-precondition', "Couldn't save in Username collection");
}
await createDocumentsInCollection(uid, username, 'following');
await createDocumentsInCollection(uid, username, 'follower');
await createDocumentsInCollection(uid, username, 'posts');
return true;
});
async function isUsernameAvailable(username) {
const usersCollection = admin.firestore().collection('users');
const usersSnapshot = await usersCollection.where('username', '==', username).get();
if (usersSnapshot.size > 0) {
return false;
} else {
return true;
}
}
async function createUserAuthentication(emailUser, passwordUser) {
return admin.auth().createUser({
email: emailUser,
password: passwordUser,
emailVerified: false,
disabled: false
}).then(userRecord => {
if (userRecord.email !== emailUser) {
return {
"status": false,
"uid": "0"
}
}
return {
"status": true,
"uid": userRecord.uid
}
}).catch(error => {
return error;
});
}
async function saveInUsernameCollection(uid, username, email) {
const usernameObject = {
uid: uid,
username: username,
email: email
};
const usernameCollection = admin.firestore().collection("usernames").doc(uid);
return await usernameCollection.set(usernameObject)
.then(() => {
return true;
}).catch(error => {
return false;
});
}
async function saveInUserCollection(uid, username) {
const userObject = {
userId: uid,
username: username,
biography: "",
location: "",
following: 0,
follower: 0,
backgroundImageUrl: "",
profileImageUrl: "",
lastDateUsernameChange: 0
};
const userCollection = admin.firestore().collection("users").doc(username);
return await userCollection.set(userObject)
.then(() => {
return true;
}).catch(error => {
return false;
});
}
async function createDocumentsInCollection(uid, username, nameCollection) {
return await admin.firestore()
.collection(nameCollection)
.doc(uid)
.set({
username: username
});
}
exports.checkIfAccountIsDisabled = functions
.https.onCall(async(data, context) => {
const uid = data.uid;
/* admin.auth().getUser({
}) */
const docDisabled = await admin.firestore().collection('disabled').doc(uid).get();
if (!docDisabled.exists) {
return {
disabled: false
};
}
const dateNow = new Date().getTime();
const dateDisabled = docDisabled.data().dateDisabledMilliseconds;
if (dateDisabled === '' || dateDisabled === null) {
const VALUE_NOT_FOUND = 101
return {
disabled: true,
errorCode: VALUE_NOT_FOUND
};
/* throw new functions.https.HttpsError('notFound', "There's not such value in our database"); */
}
const dateSecondsDifference = (dateNow - dateDisabled) / 1000;
const dayDifference = (dateSecondsDifference / 86400);
//ERRORS
const ACCOUNT_DISABLED_LESS_30_DAYS = 3
const ACCOUNT_DISABLED_MORE_30_DAYS = 4
if (dayDifference <= 30) {
return {
disabled: true,
errorCode: ACCOUNT_DISABLED_LESS_30_DAYS
};
} else {
return {
disabled: true,
errorCode: ACCOUNT_DISABLED_MORE_30_DAYS
};
}
});
exports.getUidByEmail = functions
.https.onCall(async(data, context) => {
const email = data.email;
const usernameSnapshotQuery = await admin.firestore().collection("usernames").where('email', '==', email).get();
if (usernameSnapshotQuery.empty)
throw functions.https.HttpsError('value-not-found', "We couldn't find an user with that email");
var uid = ''
usernameSnapshotQuery.forEach(doc => {
uid = doc.data().uid
});
if (uid === '' || uid === null) {
functions.logger.log('Throw')
throw functions.https.HttpsError('value-not-found', "We couldn't find an user with that email");
}
return {
status: true,
uid: uid
}
/*
return await admin.auth()
.getUser(uid)
.then(userRecord => {
return {
status: true,
email: userRecord.email
}
}).catch(error => {
return {
status: false,
error: error
}
}); */
});
exports.deleteUser = functions
.https.onCall(async(data, context) => {
const uid = data.uid
return await admin.auth().deleteUser(uid)
.then(() => {
return true;
})
.catch(error => {
return error;
});
});
exports.onDeleteUserTrigger = functions
.auth.user().onDelete((user) => {
const uid = user.uid;
return onDeleteUser(uid);
});
async function onDeleteUser(uid) {
//User
const usersSnapshotQuery = await admin.firestore().collection('users').where('userId', '==', uid).get();
usersSnapshotQuery.forEach(doc => {
doc.ref.delete();
functions.logger.log('Deleted User');
});
//Username
await admin.firestore().collection('usernames').doc(uid).delete();
//Reports
await admin.firestore().collection('reports').doc(uid).delete();
functions.logger.log('Delete followings');
//Following
//Get all followingID to delete the current uid in Follower of other users
const followingSnapshotQuery = await admin.firestore().collection('following').doc(uid).collection('user_following').get();
const listFollowingID = [];
if (!followingSnapshotQuery.empty) {
followingSnapshotQuery.forEach(doc => {
listFollowingID.push(doc.data().followingId);
});
}
const followingPromises = [];
for (const followingID of listFollowingID) {
followingPromises.push(deleteIdInFollowerCollection(followingID, uid));
}
functions.logger.log('FollowingPromises LENGTH:', followingPromises.length);
await Promise.all(followingPromises);
//Delete followingID
deleteNestedDocument(`following/${uid}`)
await admin.firestore().collection('following').doc(uid).delete();
//Follower
//Get all followerID to delete currentID in Following of other users
const followerSnapshotQuery = await admin.firestore().collection('follower').doc(uid).collection('user_followers').get();
const listFollowerID = [];
if (!followerSnapshotQuery.empty) {
followerSnapshotQuery.forEach(doc => {
listFollowerID.push(doc.data().followerId);
});
}
const followerPromises = [];
for (const followerID of listFollowerID) {
followerPromises.push(deleteIdInFollowingCollection(followerID, uid));
}
await Promise.all(followerPromises)
//Delete Nested FollowerID
deleteNestedDocument(`follower/${uid}`)
await admin.firestore().collection('follower').doc(uid).delete();
//Disabled
await admin.firestore().collection('disabled').doc(uid).delete();
return true;
}
async function deleteIdInFollowerCollection(followingID, uid) {
const docSnapshotFollower = await admin.firestore().collection('follower').doc(followingID).collection('user_followers').where('followerId', '==', uid).get();
if (docSnapshotFollower.empty) {
return;
}
docSnapshotFollower.forEach(doc => {
doc.ref.delete();
});
}
async function deleteIdInFollowingCollection(followerID, uid) {
const docSnapshotFollowing = await admin.firestore().collection('following').doc(followerID).collection('user_following').where('followingId', '==', uid).get();
if (docSnapshotFollowing.empty) {
return;
}
docSnapshotFollowing.forEach(doc => {
doc.ref.delete();
});
}
async function deleteNestedDocument(path) {
await firebase_tools.firestore
.delete(path, {
project: process.env.GCLOUD_PROJECT,
recursive: true,
yes: true,
token: functions.config().fb.token
});
return true;
}