-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfirebase.js
More file actions
235 lines (207 loc) · 6.7 KB
/
firebase.js
File metadata and controls
235 lines (207 loc) · 6.7 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
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
var firebaseConfig = {
// Firebase config goes here
};
const app = firebase.initializeApp(firebaseConfig);
const appDb = app.firestore();
var myUserName = null;
var myCrush = [];
var doesMyCrushLikesMe = false;
var doILikeThisUsername = false;
var crushCt = 0;
var credential;
chrome.identity.getAuthToken({ 'interactive': true }, function(token) {
// Use the token.
credential = firebase.auth.GoogleAuthProvider.credential(null, token);
firebase.auth().signInWithCredential(credential);
});
chrome.runtime.onConnect.addListener(function(port){
console.assert(port.name == "knockknock");
port.onMessage.addListener(async(request) => {
if (request.type == "statCrush"){
myUserName = request.username;
///////////////////////
myCrush = [];
crushCt = 0;
await appDb.collection(myUserName)
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
myCrush = [...myCrush, {crushname: doc.data().crushname, hasCrush: doc.data().hasCrush, crushFullName: doc.data().crushFullName}];
crushCt++;
});
})
.catch((error) => {
console.log("Error getting documents: ", error);
});
///////////////////////////
console.log('checkCrush');
var isCrush_ = false;
await appDb.collection(request.crushname).where("crushname", "==", request.username)
.get()
.then((querySnapshot) => {
if (querySnapshot.docs.length > 0){
isCrush_ = true;
}
})
.catch((error) => {
console.log("Error getting documents: ", error);
});
doesMyCrushLikesMe = isCrush_;
/////////////////////////
isCrush_ = false;
await appDb.collection(request.username).where("crushname", "==", request.crushname)
.get()
.then((querySnapshot) => {
if (querySnapshot.docs.length > 0){
if(!doesMyCrushLikesMe) doesMyCrushLikesMe = querySnapshot.docs[0].data().hasCrush;
isCrush_ = true;
}
})
.catch((error) => {
console.log("Error getting documents: ", error);
});
doILikeThisUsername = isCrush_;
//////////////////////////////
if(doesMyCrushLikesMe && doILikeThisUsername)
port.postMessage({
crushCt: crushCt,
msg:'statCrush Query recieved',
statCrush: 2
});
else if(doILikeThisUsername)
port.postMessage({
crushCt: crushCt,
msg:'statCrush Query recieved',
statCrush: 1
});
else
port.postMessage({
crushCt: crushCt,
msg:'statCrush Query recieved',
statCrush: 0
});
}
});
});
chrome.runtime.onMessage.addListener(
async (request, sender, sendResponse) => {
if (request.type == 'addDb'){
const myCrushNames = myCrush.filter((item)=> item.crushname != '').map((item)=>item.crushname);
if (request.crushname == '' || myCrushNames.includes(request.crushname)){
const errorMsg = 'error: crushname is blank';
}
else{
appDb.collection(request.username).add({
crushname: request.crushname,
hasCrush: doesMyCrushLikesMe,
crushFullName: request.fullname
})
.then((docRef) => {
console.log("Document written with ID: ", docRef.id);
})
.catch((error) => {
console.error("Error adding document: ", error);
});
if(doesMyCrushLikesMe){
await appDb.collection(request.crushname).where("crushname", "==", request.username)
.get()
.then((querySnapshot) => {
querySnapshot.forEach(function(doc) {
var upDocRef = doc.ref;
return appDb.runTransaction((transaction) => {
return transaction.get(upDocRef).then((upDoc) => {
if (!upDoc.exists) {
throw "Document doesn't exists";
}
transaction.update(upDocRef, {hasCrush:true});
});
});
});
})
.catch((error) => {
console.log("Error getting documents: ", error);
});
}
}
}
else if (request.type == 'queryDb'){
myCrush = [];
crushCt = 0;
await appDb.collection(myUserName)
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
myCrush = [...myCrush, {crushname: doc.data().crushname, hasCrush: doc.data().hasCrush, crushFullName: doc.data().crushFullName}];
crushCt++;
});
})
.catch((error) => {
console.log("Error getting documents: ", error);
});
}
else if (request.type == 'crushCt'){
sendResponse({
msg:'crushCt Query recieved',
crushCt: crushCt
});
}
else if (request.type == 'setUserName'){
myUserName = request.username;
sendResponse({
msg:'setUserName Query recieved'
});
}
else if (request.type == 'popupClick'){
sendResponse({msg:'query recieved',
username: myUserName,
crush: myCrush
});
}
else if (request.type == 'checkCrushDb'){
var isCrush_ = false;
await appDb.collection(request.crushname).where("crushname", "==", request.username)
.get()
.then((querySnapshot) => {
if (querySnapshot.docs.length > 0){
isCrush_ = true;
}
})
.catch((error) => {
console.log("Error getting documents: ", error);
});
doesMyCrushLikesMe = isCrush_;
}
else if (request.type == 'checkUserDb'){
var isCrush_ = false;
await appDb.collection(request.username).where("crushname", "==", request.crushname)
.get()
.then((querySnapshot) => {
if (querySnapshot.docs.length > 0){
if(!doesMyCrushLikesMe) doesMyCrushLikesMe = querySnapshot.docs[0].data().hasCrush;
isCrush_ = true;
}
})
.catch((error) => {
console.log("Error getting documents: ", error);
});
console.log("isCrush_ ", isCrush_);
doILikeThisUsername = isCrush_;
}
else if( request.type == 'deleteDB' ){
await appDb.collection(request.username).where("crushname", "==", request.crushname)
.get()
.then((querySnapshot) => {
querySnapshot.forEach(function(doc) {
doc.ref.delete();
});
})
.catch((error) => {
console.log("Error getting documents: ", error);
});
}
setTimeout(function() {
sendResponse({status: true});
}, 100);
return true;
}
);