api-client/functions/fb.js

222 lines
5.9 KiB
JavaScript
Raw Normal View History

2020-02-24 18:44:50 +00:00
import firebase from 'firebase/app'
import 'firebase/firestore'
import 'firebase/auth'
2020-01-20 16:55:48 +00:00
// Initialize Firebase, copied from cloud console
2020-01-20 17:31:31 +00:00
const firebaseConfig = {
2020-02-24 18:44:50 +00:00
apiKey: 'AIzaSyCMsFreESs58-hRxTtiqQrIcimh4i1wbsM',
authDomain: 'postwoman-api.firebaseapp.com',
databaseURL: 'https://postwoman-api.firebaseio.com',
projectId: 'postwoman-api',
storageBucket: 'postwoman-api.appspot.com',
messagingSenderId: '421993993223',
appId: '1:421993993223:web:ec0baa8ee8c02ffa1fc6a2',
measurementId: 'G-ERJ6025CEB',
}
firebase.initializeApp(firebaseConfig)
2020-01-20 16:55:48 +00:00
2020-01-23 02:21:23 +00:00
// a reference to the users collection
2020-02-24 18:44:50 +00:00
const usersCollection = firebase.firestore().collection('users')
2020-01-20 16:55:48 +00:00
// the shared state object that any vue component
// can get access to
2020-01-21 12:25:35 +00:00
export const fb = {
2020-01-21 03:02:43 +00:00
currentUser: {},
2020-01-23 13:37:36 +00:00
currentFeeds: [],
2020-01-21 12:25:35 +00:00
currentSettings: [],
2020-01-23 13:37:36 +00:00
currentHistory: [],
2020-01-25 06:51:47 +00:00
currentCollections: [],
2020-02-23 19:00:22 +00:00
currentEnvironments: [],
2020-01-23 13:37:36 +00:00
writeFeeds: async (message, label) => {
2020-01-20 16:55:48 +00:00
const dt = {
createdOn: new Date(),
2020-01-21 12:25:35 +00:00
author: fb.currentUser.uid,
author_name: fb.currentUser.displayName,
author_image: fb.currentUser.photoURL,
2020-01-22 01:57:38 +00:00
message,
2020-02-24 18:44:50 +00:00
label,
}
2020-01-25 06:51:47 +00:00
usersCollection
.doc(fb.currentUser.uid)
2020-02-24 18:44:50 +00:00
.collection('feeds')
2020-01-25 06:51:47 +00:00
.add(dt)
2020-02-24 18:44:50 +00:00
.catch(e => console.error('error inserting', dt, e))
2020-01-21 03:02:43 +00:00
},
2020-01-22 07:17:09 +00:00
deleteFeed: id => {
2020-01-23 02:21:23 +00:00
usersCollection
.doc(fb.currentUser.uid)
2020-02-24 18:44:50 +00:00
.collection('feeds')
2020-01-21 03:02:43 +00:00
.doc(id)
.delete()
2020-02-24 18:44:50 +00:00
.catch(e => console.error('error deleting', id, e))
2020-01-22 07:17:09 +00:00
},
2020-01-21 16:57:52 +00:00
writeSettings: async (setting, value) => {
2020-01-21 12:25:35 +00:00
const st = {
updatedOn: new Date(),
author: fb.currentUser.uid,
author_name: fb.currentUser.displayName,
author_image: fb.currentUser.photoURL,
2020-01-21 16:57:52 +00:00
name: setting,
2020-02-24 18:44:50 +00:00
value,
}
2020-01-25 06:51:47 +00:00
usersCollection
.doc(fb.currentUser.uid)
2020-02-24 18:44:50 +00:00
.collection('settings')
2020-01-25 06:51:47 +00:00
.doc(setting)
.set(st)
2020-02-24 18:44:50 +00:00
.catch(e => console.error('error updating', st, e))
2020-01-23 13:37:36 +00:00
},
writeHistory: async entry => {
2020-02-24 18:44:50 +00:00
const hs = entry
2020-01-25 06:51:47 +00:00
usersCollection
.doc(fb.currentUser.uid)
2020-02-24 18:44:50 +00:00
.collection('history')
2020-01-25 06:51:47 +00:00
.add(hs)
2020-02-24 18:44:50 +00:00
.catch(e => console.error('error inserting', hs, e))
2020-01-23 13:37:36 +00:00
},
deleteHistory: entry => {
usersCollection
.doc(fb.currentUser.uid)
2020-02-24 18:44:50 +00:00
.collection('history')
2020-01-23 13:37:36 +00:00
.doc(entry.id)
.delete()
2020-02-24 18:44:50 +00:00
.catch(e => console.error('error deleting', entry, e))
2020-01-23 13:37:36 +00:00
},
clearHistory: () => {
usersCollection
.doc(fb.currentUser.uid)
2020-02-24 18:44:50 +00:00
.collection('history')
2020-01-23 13:37:36 +00:00
.get()
.then(({ docs }) => {
2020-02-24 18:44:50 +00:00
docs.forEach(e => fb.deleteHistory(e))
})
},
toggleStar: (entry, value) => {
usersCollection
.doc(fb.currentUser.uid)
2020-02-24 18:44:50 +00:00
.collection('history')
.doc(entry.id)
.update({ star: value })
2020-02-24 18:44:50 +00:00
.catch(e => console.error('error deleting', entry, e))
2020-01-25 06:51:47 +00:00
},
writeCollections: async collection => {
const cl = {
updatedOn: new Date(),
author: fb.currentUser.uid,
author_name: fb.currentUser.displayName,
author_image: fb.currentUser.photoURL,
2020-02-24 18:44:50 +00:00
collection: collection,
}
2020-01-25 06:51:47 +00:00
usersCollection
.doc(fb.currentUser.uid)
2020-02-24 18:44:50 +00:00
.collection('collections')
.doc('sync')
2020-01-25 06:51:47 +00:00
.set(cl)
2020-02-24 18:44:50 +00:00
.catch(e => console.error('error updating', cl, e))
2020-02-23 19:00:22 +00:00
},
writeEnvironments: async environment => {
const ev = {
updatedOn: new Date(),
author: fb.currentUser.uid,
author_name: fb.currentUser.displayName,
author_image: fb.currentUser.photoURL,
2020-02-24 18:44:50 +00:00
environment: environment,
}
2020-02-23 19:00:22 +00:00
usersCollection
.doc(fb.currentUser.uid)
2020-02-24 18:44:50 +00:00
.collection('environments')
.doc('sync')
2020-02-23 19:00:22 +00:00
.set(ev)
2020-02-24 18:44:50 +00:00
.catch(e => console.error('error updating', ev, e))
},
}
2020-01-20 16:55:48 +00:00
2020-01-23 02:21:23 +00:00
// When a user logs in or out, save that in the store
firebase.auth().onAuthStateChanged(user => {
if (user) {
2020-02-24 18:44:50 +00:00
fb.currentUser = user
2020-01-23 02:21:23 +00:00
fb.currentUser.providerData.forEach(profile => {
let us = {
updatedOn: new Date(),
provider: profile.providerId,
name: profile.displayName,
email: profile.email,
photoUrl: profile.photoURL,
2020-02-24 18:44:50 +00:00
uid: profile.uid,
}
2020-01-25 06:51:47 +00:00
usersCollection
.doc(fb.currentUser.uid)
.set(us)
2020-02-24 18:44:50 +00:00
.catch(e => console.error('error updating', us, e))
})
2020-01-21 12:25:35 +00:00
2020-01-23 02:21:23 +00:00
usersCollection
.doc(fb.currentUser.uid)
2020-02-24 18:44:50 +00:00
.collection('feeds')
.orderBy('createdOn', 'desc')
2020-01-23 02:21:23 +00:00
.onSnapshot(feedsRef => {
2020-02-24 18:44:50 +00:00
const feeds = []
2020-01-23 02:21:23 +00:00
feedsRef.forEach(doc => {
2020-02-24 18:44:50 +00:00
const feed = doc.data()
feed.id = doc.id
feeds.push(feed)
})
fb.currentFeeds = feeds
})
2020-01-20 16:55:48 +00:00
2020-01-23 13:37:36 +00:00
usersCollection
.doc(fb.currentUser.uid)
2020-02-24 18:44:50 +00:00
.collection('settings')
2020-01-23 13:37:36 +00:00
.onSnapshot(settingsRef => {
2020-02-24 18:44:50 +00:00
const settings = []
2020-01-23 13:37:36 +00:00
settingsRef.forEach(doc => {
2020-02-24 18:44:50 +00:00
const setting = doc.data()
setting.id = doc.id
settings.push(setting)
})
fb.currentSettings = settings
})
2020-01-23 13:37:36 +00:00
usersCollection
.doc(fb.currentUser.uid)
2020-02-24 18:44:50 +00:00
.collection('history')
2020-01-23 13:37:36 +00:00
.onSnapshot(historyRef => {
2020-02-24 18:44:50 +00:00
const history = []
2020-01-23 13:37:36 +00:00
historyRef.forEach(doc => {
2020-02-24 18:44:50 +00:00
const entry = doc.data()
entry.id = doc.id
history.push(entry)
})
fb.currentHistory = history
})
2020-01-25 06:51:47 +00:00
usersCollection
.doc(fb.currentUser.uid)
2020-02-24 18:44:50 +00:00
.collection('collections')
2020-01-25 06:51:47 +00:00
.onSnapshot(collectionsRef => {
2020-02-24 18:44:50 +00:00
const collections = []
2020-01-25 06:51:47 +00:00
collectionsRef.forEach(doc => {
2020-02-24 18:44:50 +00:00
const collection = doc.data()
collection.id = doc.id
collections.push(collection)
})
fb.currentCollections = collections[0].collection
})
2020-02-23 19:00:22 +00:00
usersCollection
.doc(fb.currentUser.uid)
2020-02-24 18:44:50 +00:00
.collection('environments')
2020-02-23 19:00:22 +00:00
.onSnapshot(environmentsRef => {
2020-02-24 18:44:50 +00:00
const environments = []
2020-02-23 19:00:22 +00:00
environmentsRef.forEach(doc => {
2020-02-24 18:44:50 +00:00
const environment = doc.data()
environment.id = doc.id
environments.push(environment)
})
fb.currentEnvironments = environments[0].environment
})
2020-01-23 02:21:23 +00:00
} else {
2020-02-24 18:44:50 +00:00
fb.currentUser = null
2020-01-23 02:21:23 +00:00
}
2020-02-24 18:44:50 +00:00
})