-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusers.ts
More file actions
executable file
·146 lines (123 loc) · 4.73 KB
/
users.ts
File metadata and controls
executable file
·146 lines (123 loc) · 4.73 KB
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
import * as functions from "firebase-functions/v1";
import { onDocumentDeleted } from "firebase-functions/v2/firestore";
import { HttpsError, onCall } from "firebase-functions/v2/https";
import * as admin from "firebase-admin";
import { Active, Const, RoomUser, User, constToJSON } from "./Gen/data";
import { generateRandomAnimal, generateRandomString } from "./name_gen";
import { isAlphanumeric } from "./Helpers/regex";
import { deleteFCMToken } from "./messaging";
export const onUserCreate = functions
.region('asia-south1')
.auth.user().onCreate((user) => {
const { uid, email, displayName, phoneNumber, photoURL } = user;
admin.firestore().collection(constToJSON(Const.users)).doc(uid)
.set(
User.toJSON(User.create({
email: email,
nick: generateRandomString(8),
displayName: displayName ?? generateRandomAnimal(),
phoneNumber: phoneNumber,
photoURL: photoURL,
created: new Date(),
updated: new Date(),
status: Active.ONLINE,
admin: false,
})) as Map<string, any>
, { merge: true });
return 'Created';
});
export const callUserUpdate = onCall({
enforceAppCheck: true,
region: 'asia-south1',
}, async (request): Promise<void> => {
let uid = request.auth?.uid;
const { displayName, nick, photoURL } = request.data;
const user = User.create({
displayName: displayName,
nick: nick,
photoURL: photoURL,
});
if (user.nick != null && user.nick != '') {
if (!isAlphanumeric(user.nick) || user.nick.length < 7) {
throw new HttpsError('aborted', 'Nick name error');
}
let nickCount = await admin.firestore().collection(constToJSON(Const.users))
.where(constToJSON(Const.nick), '==', user.nick)
.count().get().then((v) => v.data().count);
if (nickCount != 0) {
throw new HttpsError('aborted', 'Nick name already present');
}
}
if (uid != null) {
admin.auth().updateUser(uid, User.toJSON(user)!)
admin.firestore().collection(constToJSON(Const.users)).doc(uid)
.update(User.toJSON(user)!).catch((error) => {
console.error('Error callUserUpdate'/*, error*/);
});
}
});
export const reportUser = onCall({
enforceAppCheck: true,
region: 'asia-south1',
}, async (request) => {
let userId = request.auth!.uid;
let user = User.fromJSON(request.data)
console.log(request.data.reason)
// if (user.uid) {
// await admin.firestore().collection(constToJSON(Const.users))
// .doc(user.uid).delete()
// .catch((error) => {
// console.error('Error deleteAuthUser'/*, error*/);
// });
// }
});
export const deleteAuthUser = functions
.region('asia-south1')
.auth.user().onDelete(async (user) => {
const roomUserQuery = await admin.firestore().collection(constToJSON(Const.roomusers))
.where('user', '==', user.uid).get()
const db = admin.firestore();
const batch = db.batch();
roomUserQuery.forEach((doc) => {
let ru = RoomUser.fromJSON(doc.data());
//
admin.storage().bucket().deleteFiles({
prefix: `tweet/${ru.room}/${ru.user}`
});
//
batch.delete(doc.ref);
});
await batch.commit();
//
admin.storage().bucket().deleteFiles({
prefix: `profile/users/${user.uid}`
});
//
admin.firestore().collection(constToJSON(Const.users))
.doc(user.uid).delete()
.catch((error) => {
console.error('Error deleteAuthUser'/*, error*/);
});
//
deleteFCMToken(user.uid);
return { message: `Deleted all ${user.uid} documents.` };
});
export const deleteUser = onDocumentDeleted({
document: "users/{userId}",
region: 'asia-south1',
}, async (event) => {
admin.auth().deleteUser(event.params.userId)
.catch((error) => {
console.error('Error deleteUser'/*, error*/);
});
});
export const checkUserExists = async (userId: string) => {
return (await admin.firestore().collection(constToJSON(Const.users)).doc(userId).get()).exists;
}
export const getUserById = async (userId: string) => {
let user = (await admin.firestore().collection(constToJSON(Const.users)).doc(userId).get().catch((e) => {
console.error('No User');
throw new HttpsError('aborted', 'No user found');
})).data();
return user != null ? User.fromJSON(user) : null;
}