This repository was archived by the owner on Sep 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcloud_function.js
More file actions
101 lines (94 loc) · 3.09 KB
/
Copy pathcloud_function.js
File metadata and controls
101 lines (94 loc) · 3.09 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
/** các cloud function này được triển khai tại một thư mục độc lập dưới dạng 1 Firebase Project
File này mang tính biểu diễn các cài đặt logic và KHÔNG THỂ TRIỂN KHAI TRỰC TIẾP TỪ ĐÂY.**/
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const serviceAccount = require("./mobile-foodnet-firebase-adminsdk-86jzq-71d45cbb23.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
const firestore = admin.firestore();
exports.reactionListener = functions
.region("asia-east1")
.firestore
.document('flatten-reactions/{reactionId}')
.onWrite((change, context) => {
// onCreate
if (!change.before.exists && change.after.exists) {
const postId = change.after.data().postId;
firestore
.collection("posts")
.doc(postId)
.update({react: FieldValue.increment(1)});
}
// onDelete
else if (change.before.exists && !change.after.exists) {
const postId = change.before.data().postId;
firestore
.collection("posts")
.doc(postId)
.update({react: FieldValue.increment(-1)});
};
return;
})
exports.calcMutualism = functions
.region("asia-east1")
.https
.onCall((data, context) => {
const senderId = context.auth.uid;
const needCalcIdList = data.idList;
if (needCalcIdList.length == 0) return {data: null};
const result = {}
return firestore.collection("friends").doc(senderId).collection("profiles").get()
.then(async (snapshot) => {
let senderFriendIdList = snapshot.docs.map(item => item.id);
for (let id of needCalcIdList) {
result[id] = 0;
const needCalcFriendDoc = await firestore.collection("friends").doc(id).collection("profiles").get();
for (let doc of needCalcFriendDoc.docs) {
if (senderFriendIdList.includes(doc.id)) {
result[id]++;
}
}
}
return {data: result};
})
});
exports.numCite = functions
.region("asia-east1")
.https
.onCall((data, context) => {
const title = data.title;
return firestore.collection("posts").where("cateList", "array-contains", title)
.get()
.then((snapshot) => {
return {total: snapshot.size};
})
});
exports.getUpvote = functions
.region("asia-east1")
.https
.onCall((data, context) => {
const postId = data.postId;
return firestore
.collection("flatten-reactions")
.where("postId", "==", postId)
.where("type", "==", 1)
.get()
.then((snapshot) => {
return {total: snapshot.size}
})
});
exports.getDownvote = functions
.region("asia-east1")
.https
.onCall((data, context) => {
const postId = data.postId;
return firestore
.collection("flatten-reactions")
.where("postId", "==", postId)
.where("type", "==", -1)
.get()
.then((snapshot) => {
return {total: snapshot.size}
})
});