-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebaseConfig.js
393 lines (347 loc) · 11.4 KB
/
firebaseConfig.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
import { initializeApp } from 'firebase/app'
import { getFirestore, doc, collectionGroup, getDoc, getDocs, setDoc, updateDoc, arrayUnion, arrayRemove, deleteDoc, increment, DocumentSnapshot, query, collection, where } from 'firebase/firestore/lite'
import { Post } from './Post'
import { User } from './User'
import { sha224 } from 'js-sha256'
// import { setScreen, setUser, PAGES } from './Utility'
const firebaseConfig = { // SUPER INSECURE, EXPOSED API KEYS FOR NON-DEV USE IS REALLY BAD
apiKey: 'AIzaSyBVaQdvRQcffg60M_zZS9zuLBTgFbCFGWo',
authDomain: 'apex-mesosphere.firebaseapp.com',
projectId: 'apex-mesosphere',
storageBucket: 'apex-mesosphere.appspot.com',
messagingSenderId: '585675242764',
appId: '1:585675242764:web:5593184374b2c2c192dd29',
measurementId: 'G-XTQEV03BPS'
}
const app = initializeApp(firebaseConfig)
const database = getFirestore(app)
const IDSRef = doc(database, 'main', 'IDS')
const usernameRef = doc(database, 'main', 'Usernames')
export async function returnMIDSDatabaseLength () {
const IDSSnap = await getDoc(IDSRef)
if (IDSSnap.exists()) {
// console.log(IDSSnap.data().MesosphereIDs)
}
return IDSSnap.data().MesosphereIDs.length
}
export async function returnMIDSDatabaseArray () {
const userRef = collectionGroup(database, 'accounts')
const docSnap = await getDocs(userRef)
const activeIDs = []
if (docSnap.size != 0) {
for (const document of docSnap.docs) {
activeIDs.push(document.data().MID)
}
} else {
console.log('No active IDs!')
}
return activeIDs
}
export async function returnPostIDDatabaseLength () {
const IDSSnap = await getDoc(IDSRef)
if (IDSSnap.exists()) {
// console.log(IDSSnap.data().postIDs)
return IDSSnap.data().postIDs.length
}
}
export async function pushMIDToDatabase (MID) {
await updateDoc(IDSRef, {
MesosphereIDs: arrayUnion(MID)
})
}
export async function pushPostIDToDatabase (postID) {
await updateDoc(IDSRef, {
postIDs: arrayUnion(postID)
})
}
export async function pushUsernameToDatabase (username) {
await updateDoc(usernameRef, {
usernames: arrayUnion(username)
})
}
export async function removeUsernameFromDatabase (username) {
await updateDoc(usernameRef, {
usernames: arrayRemove(username)
})
}
export async function pushAccountToDatabase (u) {
await setDoc(doc(database, 'accounts', u.MiD), {
MID: u.MiD,
biography: u.biography,
username: u.username,
displayname: u.realName,
friends: u.myPeers,
posts: u.myPosts,
password: u.password,
notifications: u.notifications
})
}
export async function pullAccountFromDatabase (mesosphereID) {
const userRef = doc(database, 'accounts', mesosphereID)
const docSnap = await getDoc(userRef)
if (docSnap.exists()) {
const data = docSnap.data()
console.log('Document data:', docSnap.data())
return new User(data.username, data.password, data.displayname, data.biography, data.MID, data.posts, data.friends)
} else {
console.log('Error: Requested acc does not exist.')
return null
}
}
export async function removeAccountFromDatabase (u) {
await deleteDoc(doc(database, 'accounts', u.MiD))
await updateDoc(usernameRef, {
usernames: arrayRemove(u.username)
})
for (let i = 0; i < u.myPosts.length; i++) {
await deleteDoc(doc(database, 'posts', u.myPosts[i].postID))
}
}
export async function returnDisplayNameFromMID (MID) {
const userRef = doc(database, 'accounts', mesosphereID)
const docSnap = await getDoc(userRef)
if (docSnap.exists()) {
return docSnap.data().displayname
}
}
export async function addPeerToDatabase (u, peerID) {
await updateDoc(doc(database, 'accounts', u.MiD), {
friends: arrayUnion(peerID)
})
}
export async function removePeerFromDatabase (u, peerID) {
await updateDoc(doc(database, 'accounts', u.MiD), {
friends: arrayRemove(peerID)
})
}
export async function pushPostToDatabase (p) {
await setDoc(doc(database, 'posts', p.postID), {
MID: p.attachedMiD,
postID: p.postID,
score: p.starting_score,
text: p.textContent,
interactedUsers: p.interactedUsers,
timestamp: p.timestamp
})
await updateDoc(doc(database, 'accounts', p.attachedMiD), {
posts: arrayUnion(p.postID)
})
}
export async function alterPostScore (u, postID, change) { // increment/decrement in units of 0.5; upvote: change = 0.5, downvote: change = -0.5
const postRef = doc(database, 'posts', postID)
await updateDoc(doc(database, 'posts', postID), {
score: increment(change)
})
// Post component handles interaction
}
export async function alterPostScoreClean (postID, change) {
const postRef = doc(database, 'posts', postID)
await updateDoc(doc(database, 'posts', postID), {
score: increment(change)
})
// Post component handles interaction
}
export async function addUserInteraction (u, postID, change) {
let actionTaken = ''
if (change > 0) {
actionTaken = 'like'
} else {
actionTaken = 'dislike'
}
await updateDoc(doc(database, 'posts', postID), { // updates interactedUsers in the post with the MID and action taken
interactedUsers: arrayUnion(
{
user: u.MiD,
action: actionTaken
}
)
})
} // NOTE: arrayUnion ensures that an interaction either like/dislike can only appear once each time per user, however, the score can keep increasing if validation isn't ensured
export async function removeInteractions (u, postID) {
while (await hasInteractedWith(u, postID)) {
const actionTaken = await returnInteractionFromDatabase(u, postID)
await updateDoc(doc(database, 'posts', postID), {
interactedUsers: arrayRemove(
{
user: u.MiD,
action: actionTaken
}
)
})
}
}
export async function returnInteractionFromDatabase (u, postID) {
const postRef = doc(database, 'posts', postID)
const postSnap = await getDoc(postRef)
if (!postSnap.exists()) {
console.log('Error: Requested post does not exist.')
} else if (hasInteractedWith(u, postID) === false) {
return null
} else {
try {
return postSnap.data().interactedUsers.find(item => item.user === u.MiD).action
} catch (error) {
console.error(error)
}
}
}
export async function removePostFromDatabase (p) {
await updateDoc(doc(database, 'accounts', p.attachedMiD), {
posts: arrayRemove(p.postID)
})
await deleteDoc(doc(database, 'posts', p.postID))
}
export async function pullPostFromDatabase (postID) {
console.log('i am a post puller for post ' + postID)
const postRef = doc(database, 'posts', postID)
console.log('and i will not elp you')
const docSnap = await getDoc(postRef)
console.log(docSnap)
if (docSnap.exists()) {
console.log('Doc Snap exists!')
const data = docSnap.data()
console.log('Document data:', docSnap.data())
console.log(data.timestamp)
return new Post(data.MID, data.postID, null, data.text, data.score, data.interactedUsers, data.timestamp)
} else {
console.log('Error: Requested post does not exist.')
return null
}
}
export async function getScoreFromPostInDatabase (postID) {
const postRef = doc(database, 'posts', postID)
const docSnap = await getDoc(postRef)
if (docSnap.exists()) {
const data = docSnap.data()
try {
return data.score
} catch (error) {
console.error(error)
}
} else {
console.log('Error: Requested post does not exist.')
}
}
// user manipulation from local changes
export async function changeUserBiographyInDatabase (mesosphereID, newBio) {
await updateDoc(doc(database, 'accounts', mesosphereID), {
biography: newBio
})
}
export async function changeUserDisplayNameInDatabase (mesosphereID, newDisplayName) {
await updateDoc(doc(database, 'accounts', mesosphereID), {
displayname: newDisplayName
})
}
export async function changeUserPasswordInDatabase (mesosphereID, newPassword) {
await updateDoc(doc(database, 'accounts', mesosphereID), {
password: newPassword
})
}
// testing and validation methods
export async function doesAccountExist (mesosphereID) {
const accountRef = doc(database, 'accounts', mesosphereID)
const accountSnap = await getDoc(accountRef)
return accountSnap.exists()
}
export async function doesPostExist (postID) {
const postRef = doc(database, 'posts', postID)
const postSnap = await getDoc(postRef)
return postSnap.exists()
}
export async function hasInteractedWith (u, postID) {
const postRef = doc(database, 'posts', postID)
const postSnap = await getDoc(postRef)
if (!postSnap.exists()) {
console.log('Error: Requested post does not exist.')
}
return postSnap.data().interactedUsers.some(search => search.user === u.MiD)
}
export async function doesUsernameExist (username) {
const uSnap = await getDoc(usernameRef)
// console.log(uSnap.data().usernames.includes(username))
if (uSnap.data().usernames.includes(username)) {
return true
} else {
return false
}
}
// remote login
export async function parseRemoteLogin (username, password) {
let dataString = ''
const q = query(collection(database, 'accounts'), where('username', '==', username), where('password', '==', String(sha224(String(password)))))
const querySnapshot = await getDocs(q)
querySnapshot.forEach((doc) => {
// console.log(doc.data())
// setUser(pullAccountFromDatabase(doc.data.MID))
// setScreen(PAGES.ACCOUNTPAGE)
// console.log(doc.data().MID)
dataString += doc.data().MID
// return doc.data().MID
})
return dataString
}
export async function parseRemoteLoginEncrypted (username, password) {
let dataString = ''
const q = query(collection(database, 'accounts'), where('username', '==', username), where('password', '==', password))
const querySnapshot = await getDocs(q)
querySnapshot.forEach((doc) => {
// console.log(doc.data())
// setUser(pullAccountFromDatabase(doc.data.MID))
// setScreen(PAGES.ACCOUNTPAGE)
// console.log(doc.data().MID)
dataString += doc.data().MID
// return doc.data().MID
})
return dataString
}
// notifications
export async function sendNotification (u, recipientID) {
// If this user doesn't have me as a friend yet
const peer = await pullAccountFromDatabase(recipientID)
if (!(peer.getAllPeers().includes(u.MiD))) {
await updateDoc(doc(database, 'accounts', recipientID), {
notifications: arrayUnion(u.MiD)
})
}
}
export async function removeNotification (u, notificationMID) {
await updateDoc(doc(database, 'accounts', u.MiD), {
notifications: arrayRemove(notificationMID)
})
}
/**
* User removes a notification on their account.
* This should happen upon accepting the friend request, or manual deletion of the notification with no action taken.
*/
export async function pullNotifications (u) {
const accountRef = doc(database, 'accounts', u.MiD)
const accountSnap = await getDoc(accountRef)
const notis = []
const data = accountSnap.data()
// console.log(data.notifications)
try {
if(data.notifications && data.notifications.size != 0) {
console.log(data.notifications)
return data.notifications
} else {
console.log('No notifications for this user.')
return null
}
} catch (e) {
console.log("Try/Catch triggered! Error:")
console.log(e)
return null
}
}
export async function getDisplayNameFromMID (MID) {
const accountRef = doc(database, 'accounts', MID)
const accountSnap = await getDoc(accountRef)
const data = accountSnap.data()
if (data) {
return data.displayname
} else {
console.log('Error!')
}
}
export { database }