Initial Firebase refactor pass

This commit is contained in:
Andrew Bastin
2021-06-14 00:07:30 -04:00
parent ced2f1b911
commit 85c6932f8f
30 changed files with 979 additions and 1029 deletions

View File

@@ -7,12 +7,12 @@ import {
import { WebSocketLink } from "@apollo/client/link/ws"
import { setContext } from "@apollo/client/link/context"
import { getMainDefinition } from "@apollo/client/utilities"
import { fb } from "./fb"
import { authIdToken$ } from "./fb/auth"
let authToken: String | null = null
export function registerApolloAuthUpdate() {
fb.idToken$.subscribe((token: String | null) => {
authIdToken$.subscribe((token) => {
authToken = token
})
}

View File

@@ -1,572 +0,0 @@
import firebase from "firebase/app"
import "firebase/firestore"
import "firebase/auth"
import { ReplaySubject } from "rxjs"
import { applySettingFB, settingsStore } from "~/newstore/settings"
import {
restHistoryStore,
setRESTHistoryEntries,
graphqlHistoryStore,
setGraphqlHistoryEntries,
HISTORY_LIMIT,
} from "~/newstore/history"
import {
restCollectionStore,
setRESTCollections,
graphqlCollectionStore,
setGraphqlCollections,
} from "~/newstore/collections"
import { environments$, replaceEnvironments } from "~/newstore/environments"
// Initialize Firebase, copied from cloud console
const firebaseConfig = {
apiKey: process.env.API_KEY,
authDomain: process.env.AUTH_DOMAIN,
databaseURL: process.env.DATABASE_URL,
projectId: process.env.PROJECT_ID,
storageBucket: process.env.STORAGE_BUCKET,
messagingSenderId: process.env.MESSAGING_SENDER_ID,
appId: process.env.APP_ID,
measurementId: process.env.MEASUREMENT_ID,
}
export const authProviders = {
google: () => new firebase.auth.GoogleAuthProvider(),
github: () => new firebase.auth.GithubAuthProvider(),
}
export class FirebaseInstance {
constructor(fbapp, authProviders) {
this.app = fbapp
this.authProviders = authProviders
this.usersCollection = this.app.firestore().collection("users")
this.currentUser = null
this.idToken = null
this.currentFeeds = []
this.currentSettings = []
this.currentUser$ = new ReplaySubject(1)
this.idToken$ = new ReplaySubject(1)
let loadedSettings = false
let loadedRESTHistory = false
let loadedGraphqlHistory = false
let loadedRESTCollections = false
let loadedGraphqlCollections = false
let loadedEnvironments = false
graphqlCollectionStore.subject$.subscribe(({ state }) => {
if (
loadedGraphqlCollections &&
this.currentUser &&
settingsStore.value.syncCollections
) {
this.writeCollections(state, "collectionsGraphql")
}
})
restCollectionStore.subject$.subscribe(({ state }) => {
if (
loadedRESTCollections &&
this.currentUser &&
settingsStore.value.syncCollections
) {
this.writeCollections(state, "collections")
}
})
restHistoryStore.dispatches$.subscribe((dispatch) => {
if (
loadedRESTHistory &&
this.currentUser &&
settingsStore.value.syncHistory
) {
if (dispatch.dispatcher === "addEntry") {
this.writeHistory(dispatch.payload.entry)
} else if (dispatch.dispatcher === "deleteEntry") {
this.deleteHistory(dispatch.payload.entry)
} else if (dispatch.dispatcher === "clearHistory") {
this.clearHistory()
} else if (dispatch.dispatcher === "toggleStar") {
this.toggleStar(dispatch.payload.entry)
}
}
})
graphqlHistoryStore.dispatches$.subscribe((dispatch) => {
if (
loadedGraphqlHistory &&
this.currentUser &&
settingsStore.value.syncHistory
) {
if (dispatch.dispatcher === "addEntry") {
this.writeGraphqlHistory(dispatch.payload.entry)
} else if (dispatch.dispatcher === "deleteEntry") {
this.deleteGraphqlHistory(dispatch.payload.entry)
} else if (dispatch.dispatcher === "clearHistory") {
this.clearGraphqlHistory()
} else if (dispatch.dispatcher === "toggleStar") {
this.toggleGraphqlHistoryStar(dispatch.payload.entry)
}
}
})
settingsStore.dispatches$.subscribe((dispatch) => {
if (this.currentUser && loadedSettings) {
if (dispatch.dispatcher === "bulkApplySettings") {
Object.keys(dispatch.payload).forEach((key) => {
this.writeSettings(key, dispatch.payload[key])
})
} else if (dispatch.dispatcher !== "applySettingFB") {
this.writeSettings(
dispatch.payload.settingKey,
settingsStore.value[dispatch.payload.settingKey]
)
}
}
})
environments$.subscribe((envs) => {
if (this.currentUser && loadedEnvironments) {
this.writeEnvironments(envs)
}
})
this.app.auth().onIdTokenChanged((user) => {
if (user) {
user.getIdToken().then((token) => {
this.idToken = token
this.idToken$.next(token)
})
} else {
this.idToken = null
this.idToken$.next(null)
}
})
this.app.auth().onAuthStateChanged((user) => {
this.currentUser$.next(user)
if (user) {
this.currentUser = user
this.currentUser.providerData.forEach((profile) => {
const us = {
updatedOn: new Date(),
provider: profile.providerId,
name: profile.displayName,
email: profile.email,
photoUrl: profile.photoURL,
uid: profile.uid,
}
this.usersCollection
.doc(this.currentUser.uid)
.set(us, { merge: true })
.catch((e) => console.error("error updating", us, e))
})
this.usersCollection.doc(this.currentUser.uid).onSnapshot((doc) => {
this.currentUser.provider = doc.data().provider
this.currentUser.accessToken = doc.data().accessToken
})
this.usersCollection
.doc(this.currentUser.uid)
.collection("feeds")
.orderBy("createdOn", "desc")
.onSnapshot((feedsRef) => {
const feeds = []
feedsRef.forEach((doc) => {
const feed = doc.data()
feed.id = doc.id
feeds.push(feed)
})
this.currentFeeds = feeds
})
this.usersCollection
.doc(this.currentUser.uid)
.collection("settings")
.onSnapshot((settingsRef) => {
const settings = []
settingsRef.forEach((doc) => {
const setting = doc.data()
setting.id = doc.id
settings.push(setting)
})
this.currentSettings = settings
settings.forEach((e) => {
if (e && e.name && e.value != null) {
applySettingFB(e.name, e.value)
}
})
loadedSettings = true
})
this.usersCollection
.doc(this.currentUser.uid)
.collection("history")
.orderBy("updatedOn", "desc")
.limit(HISTORY_LIMIT)
.onSnapshot((historyRef) => {
const history = []
historyRef.forEach((doc) => {
const entry = doc.data()
entry.id = doc.id
history.push(entry)
})
setRESTHistoryEntries(history)
loadedRESTHistory = true
})
this.usersCollection
.doc(this.currentUser.uid)
.collection("graphqlHistory")
.orderBy("updatedOn", "desc")
.limit(HISTORY_LIMIT)
.onSnapshot((historyRef) => {
const history = []
historyRef.forEach((doc) => {
const entry = doc.data()
entry.id = doc.id
history.push(entry)
})
setGraphqlHistoryEntries(history)
loadedGraphqlHistory = true
})
this.usersCollection
.doc(this.currentUser.uid)
.collection("collections")
.onSnapshot((collectionsRef) => {
const collections = []
collectionsRef.forEach((doc) => {
const collection = doc.data()
collection.id = doc.id
collections.push(collection)
})
// Prevent infinite ping-pong of updates
loadedRESTCollections = false
// TODO: Wth is with collections[0]
if (collections.length > 0) {
setRESTCollections(collections[0].collection)
}
loadedRESTCollections = true
})
this.usersCollection
.doc(this.currentUser.uid)
.collection("collectionsGraphql")
.onSnapshot((collectionsRef) => {
const collections = []
collectionsRef.forEach((doc) => {
const collection = doc.data()
collection.id = doc.id
collections.push(collection)
})
// Prevent infinite ping-pong of updates
loadedGraphqlCollections = false
// TODO: Wth is with collections[0]
if (collections.length > 0) {
setGraphqlCollections(collections[0].collection)
}
loadedGraphqlCollections = true
})
this.usersCollection
.doc(this.currentUser.uid)
.collection("environments")
.onSnapshot((environmentsRef) => {
const environments = []
environmentsRef.forEach((doc) => {
const environment = doc.data()
environment.id = doc.id
environments.push(environment)
})
loadedEnvironments = false
replaceEnvironments(environments[0].environment)
loadedEnvironments = true
})
} else {
this.currentUser = null
}
})
}
async signInUserWithGoogle() {
return await this.app.auth().signInWithPopup(this.authProviders.google())
}
async signInUserWithGithub() {
return await this.app
.auth()
.signInWithPopup(this.authProviders.github().addScope("gist"))
}
async signInWithEmailAndPassword(email, password) {
return await this.app.auth().signInWithEmailAndPassword(email, password)
}
async getSignInMethodsForEmail(email) {
return await this.app.auth().fetchSignInMethodsForEmail(email)
}
async signInWithEmail(email, actionCodeSettings) {
return await this.app
.auth()
.sendSignInLinkToEmail(email, actionCodeSettings)
}
async isSignInWithEmailLink(url) {
return await this.app.auth().isSignInWithEmailLink(url)
}
async signInWithEmailLink(email, url) {
return await this.app.auth().signInWithEmailLink(email, url)
}
async signOutUser() {
if (!this.currentUser) throw new Error("No user has logged in")
await this.app.auth().signOut()
this.currentUser = null
}
async writeFeeds(message, label) {
const dt = {
createdOn: new Date(),
author: this.currentUser.uid,
author_name: this.currentUser.displayName,
author_image: this.currentUser.photoURL,
message,
label,
}
try {
await this.usersCollection
.doc(this.currentUser.uid)
.collection("feeds")
.add(dt)
} catch (e) {
console.error("error inserting", dt, e)
throw e
}
}
async deleteFeed(id) {
try {
await this.usersCollection
.doc(this.currentUser.uid)
.collection("feeds")
.doc(id)
.delete()
} catch (e) {
console.error("error deleting", id, e)
throw e
}
}
async writeSettings(setting, value) {
const st = {
updatedOn: new Date(),
author: this.currentUser.uid,
author_name: this.currentUser.displayName,
author_image: this.currentUser.photoURL,
name: setting,
value,
}
try {
await this.usersCollection
.doc(this.currentUser.uid)
.collection("settings")
.doc(setting)
.set(st)
} catch (e) {
console.error("error updating", st, e)
throw e
}
}
async writeHistory(entry) {
const hs = entry
try {
await this.usersCollection
.doc(this.currentUser.uid)
.collection("history")
.add(hs)
} catch (e) {
console.error("error inserting", hs, e)
throw e
}
}
async writeGraphqlHistory(entry) {
const hs = entry
try {
await this.usersCollection
.doc(this.currentUser.uid)
.collection("graphqlHistory")
.add(hs)
} catch (e) {
console.error("error inserting", hs, e)
throw e
}
}
async deleteHistory(entry) {
try {
await this.usersCollection
.doc(this.currentUser.uid)
.collection("history")
.doc(entry.id)
.delete()
} catch (e) {
console.error("error deleting", entry, e)
throw e
}
}
async deleteGraphqlHistory(entry) {
try {
await this.usersCollection
.doc(this.currentUser.uid)
.collection("graphqlHistory")
.doc(entry.id)
.delete()
} catch (e) {
console.error("error deleting", entry, e)
throw e
}
}
async clearHistory() {
const { docs } = await this.usersCollection
.doc(this.currentUser.uid)
.collection("history")
.get()
await Promise.all(docs.map((e) => this.deleteHistory(e)))
}
async clearGraphqlHistory() {
const { docs } = await this.usersCollection
.doc(this.currentUser.uid)
.collection("graphqlHistory")
.get()
await Promise.all(docs.map((e) => this.deleteGraphqlHistory(e)))
}
async toggleStar(entry) {
try {
await this.usersCollection
.doc(this.currentUser.uid)
.collection("history")
.doc(entry.id)
.update({ star: !entry.star })
} catch (e) {
console.error("error deleting", entry, e)
throw e
}
}
async toggleGraphqlHistoryStar(entry) {
try {
await this.usersCollection
.doc(this.currentUser.uid)
.collection("graphqlHistory")
.doc(entry.id)
.update({ star: !entry.star })
} catch (e) {
console.error("error deleting", entry, e)
throw e
}
}
async writeCollections(collection, flag) {
const cl = {
updatedOn: new Date(),
author: this.currentUser.uid,
author_name: this.currentUser.displayName,
author_image: this.currentUser.photoURL,
collection,
}
try {
await this.usersCollection
.doc(this.currentUser.uid)
.collection(flag)
.doc("sync")
.set(cl)
} catch (e) {
console.error("error updating", cl, e)
throw e
}
}
async writeEnvironments(environment) {
const ev = {
updatedOn: new Date(),
author: this.currentUser.uid,
author_name: this.currentUser.displayName,
author_image: this.currentUser.photoURL,
environment,
}
try {
await this.usersCollection
.doc(this.currentUser.uid)
.collection("environments")
.doc("sync")
.set(ev)
} catch (e) {
console.error("error updating", ev, e)
throw e
}
}
async setProviderInfo(id, token) {
const us = {
updatedOn: new Date(),
provider: id,
accessToken: token,
}
try {
await this.usersCollection
.doc(this.currentUser.uid)
.update(us)
.catch((e) => console.error("error updating", us, e))
} catch (e) {
console.error("error updating", e)
throw e
}
}
}
export const fb = new FirebaseInstance(
firebase.initializeApp(firebaseConfig),
authProviders
)

186
helpers/fb/auth.ts Normal file
View File

@@ -0,0 +1,186 @@
import firebase from "firebase"
import { BehaviorSubject } from "rxjs"
export type HoppUser = firebase.User & {
provider?: string
accessToken?: string
}
/**
* A BehaviorSubject emitting the currently logged in user (or null if not logged in)
*/
export const currentUser$ = new BehaviorSubject<HoppUser | null>(null)
/**
* A BehaviorSubject emitting the current idToken
*/
export const authIdToken$ = new BehaviorSubject<string | null>(null)
/**
* Initializes the firebase authentication related subjects
*/
export function initAuth() {
let extraSnapshotStop: (() => void) | null = null
firebase.auth().onAuthStateChanged((user) => {
if (!user && extraSnapshotStop) {
extraSnapshotStop()
extraSnapshotStop = null
} else if (user) {
// Merge all the user info from all the authenticated providers
user.providerData.forEach((profile) => {
if (!profile) return
const us = {
updatedOn: new Date(),
provider: profile.providerId,
name: profile.displayName,
email: profile.email,
photoUrl: profile.photoURL,
uid: profile.uid,
}
firebase
.firestore()
.collection("users")
.doc(user.uid)
.set(us, { merge: true })
.catch((e) => console.error("error updating", us, e))
})
extraSnapshotStop = firebase
.firestore()
.collection("users")
.doc(user.uid)
.onSnapshot((doc) => {
const data = doc.data()
const userUpdate: HoppUser = user
if (data) {
// Write extra provider data
userUpdate.provider = data.provider
userUpdate.accessToken = data.accessToken
}
})
}
currentUser$.next(user)
})
firebase.auth().onIdTokenChanged(async (user) => {
if (user) {
authIdToken$.next(await user.getIdToken())
} else {
authIdToken$.next(null)
}
})
}
/**
* Sign user in with a popup using Google
*/
export async function signInUserWithGoogle() {
return await firebase
.auth()
.signInWithPopup(new firebase.auth.GoogleAuthProvider())
}
/**
* Sign user in with a popup using Github
*/
export async function signInUserWithGithub() {
return await firebase
.auth()
.signInWithPopup(new firebase.auth.GithubAuthProvider().addScope("gist"))
}
/**
* Sign user in with email and password
*/
export async function signInWithEmailAndPassword(
email: string,
password: string
) {
return await firebase.auth().signInWithEmailAndPassword(email, password)
}
/**
* Gets the sign in methods for a given email address
*
* @param email - Email to get the methods of
*
* @returns Promise for string array of the auth provider methods accessible
*/
export async function getSignInMethodsForEmail(email: string) {
return await firebase.auth().fetchSignInMethodsForEmail(email)
}
/**
* Sends an email with the signin link to the user
*
* @param email - Email to send the email to
* @param actionCodeSettings - The settings to apply to the link
*/
export async function signInWithEmail(
email: string,
actionCodeSettings: firebase.auth.ActionCodeSettings
) {
return await firebase.auth().sendSignInLinkToEmail(email, actionCodeSettings)
}
/**
* Checks and returns whether the sign in link is an email link
*
* @param url - The URL to look in
*/
export function isSignInWithEmailLink(url: string) {
return firebase.auth().isSignInWithEmailLink(url)
}
/**
* Sends an email with sign in with email link
*
* @param email - Email to log in to
* @param url - The action URL which is used to validate login
*/
export async function signInWithEmailLink(email: string, url: string) {
return await firebase.auth().signInWithEmailLink(email, url)
}
/**
* Signs out the user
*/
export async function signOutUser() {
if (!currentUser$.value) throw new Error("No user has logged in")
await firebase.auth().signOut()
}
/**
* Sets the provider id and relevant provider auth token
* as user metadata
*
* @param id - The provider ID
* @param token - The relevant auth token for the given provider
*/
export async function setProviderInfo(id: string, token: string) {
if (!currentUser$.value) throw new Error("No user has logged in")
const us = {
updatedOn: new Date(),
provider: id,
accessToken: token,
}
try {
await firebase
.firestore()
.collection("users")
.doc(currentUser$.value.uid)
.update(us)
.catch((e) => console.error("error updating", us, e))
} catch (e) {
console.error("error updating", e)
throw e
}
}

147
helpers/fb/collections.ts Normal file
View File

@@ -0,0 +1,147 @@
import firebase from "firebase"
import { currentUser$ } from "./auth"
import {
restCollections$,
graphqlCollections$,
setRESTCollections,
setGraphqlCollections,
} from "~/newstore/collections"
import { settingsStore } from "~/newstore/settings"
type CollectionFlags = "collectionsGraphql" | "collections"
/**
* Whether the collections are loaded. If this is set to true
* Updates to the collections store are written into firebase.
*
* If you have want to update the store and not fire the store update
* subscription, set this variable to false, do the update and then
* set it to true
*/
let loadedRESTCollections = false
/**
* Whether the collections are loaded. If this is set to true
* Updates to the collections store are written into firebase.
*
* If you have want to update the store and not fire the store update
* subscription, set this variable to false, do the update and then
* set it to true
*/
let loadedGraphqlCollections = false
export async function writeCollections(
collection: any[],
flag: CollectionFlags
) {
if (currentUser$.value === null)
throw new Error("User not logged in to write collections")
const cl = {
updatedOn: new Date(),
author: currentUser$.value.uid,
author_name: currentUser$.value.displayName,
author_image: currentUser$.value.photoURL,
collection,
}
try {
await firebase
.firestore()
.collection("users")
.doc(currentUser$.value.uid)
.collection(flag)
.doc("sync")
.set(cl)
} catch (e) {
console.error("error updating", cl, e)
throw e
}
}
export function initCollections() {
restCollections$.subscribe((collections) => {
if (
loadedRESTCollections &&
currentUser$.value &&
settingsStore.value.syncCollections
) {
writeCollections(collections, "collections")
}
})
graphqlCollections$.subscribe((collections) => {
if (
loadedGraphqlCollections &&
currentUser$.value &&
settingsStore.value.syncCollections
) {
writeCollections(collections, "collectionsGraphql")
}
})
let restSnapshotStop: (() => void) | null = null
let graphqlSnapshotStop: (() => void) | null = null
currentUser$.subscribe((user) => {
if (!user) {
if (restSnapshotStop) {
restSnapshotStop()
restSnapshotStop = null
}
if (graphqlSnapshotStop) {
graphqlSnapshotStop()
graphqlSnapshotStop = null
}
} else {
restSnapshotStop = firebase
.firestore()
.collection("users")
.doc(user.uid)
.collection("collections")
.onSnapshot((collectionsRef) => {
const collections: any[] = []
collectionsRef.forEach((doc) => {
const collection = doc.data()
collection.id = doc.id
collections.push(collection)
})
// Prevent infinite ping-pong of updates
loadedRESTCollections = false
// TODO: Wth is with collections[0]
if (collections.length > 0) {
setRESTCollections(collections[0].collection)
}
loadedRESTCollections = true
})
graphqlSnapshotStop = firebase
.firestore()
.collection("users")
.doc(user.uid)
.collection("collectionsGraphql")
.onSnapshot((collectionsRef) => {
const collections: any[] = []
collectionsRef.forEach((doc) => {
const collection = doc.data()
collection.id = doc.id
collections.push(collection)
})
// Prevent infinite ping-pong of updates
loadedGraphqlCollections = false
// TODO: Wth is with collections[0]
if (collections.length > 0) {
setGraphqlCollections(collections[0].collection)
}
loadedGraphqlCollections = true
})
}
})
}

View File

@@ -0,0 +1,84 @@
import firebase from "firebase"
import { currentUser$ } from "./auth"
import {
Environment,
environments$,
replaceEnvironments,
} from "~/newstore/environments"
import { settingsStore } from "~/newstore/settings"
/**
* Used locally to prevent infinite loop when environment sync update
* is applied to the store which then fires the store sync listener.
* When you want to update environments and not want to fire the update listener,
* set this to true and then set it back to false once it is done
*/
let loadedEnvironments = false
async function writeEnvironments(environment: Environment[]) {
if (currentUser$.value == null)
throw new Error("Cannot write environments when signed out")
const ev = {
updatedOn: new Date(),
author: currentUser$.value.uid,
author_name: currentUser$.value.displayName,
author_image: currentUser$.value.photoURL,
environment,
}
try {
await firebase
.firestore()
.collection("users")
.doc(currentUser$.value.uid)
.collection("environments")
.doc("sync")
.set(ev)
} catch (e) {
console.error("error updating", ev, e)
throw e
}
}
export function initEnvironments() {
environments$.subscribe((envs) => {
if (
currentUser$.value &&
settingsStore.value.syncEnvironments &&
loadedEnvironments
) {
writeEnvironments(envs)
}
})
let snapshotStop: (() => void) | null = null
currentUser$.subscribe((user) => {
if (!user && snapshotStop) {
// User logged out, clean up snapshot listener
snapshotStop()
snapshotStop = null
} else if (user) {
snapshotStop = firebase
.firestore()
.collection("users")
.doc(user.uid)
.collection("environments")
.onSnapshot((environmentsRef) => {
const environments: any[] = []
environmentsRef.forEach((doc) => {
const environment = doc.data()
environment.id = doc.id
environments.push(environment)
})
loadedEnvironments = false
replaceEnvironments(environments[0].environment)
loadedEnvironments = true
})
}
})
}

91
helpers/fb/feeds.ts Normal file
View File

@@ -0,0 +1,91 @@
import firebase from "firebase"
import { BehaviorSubject } from "rxjs"
import { currentUser$ } from "./auth"
type HoppFeed = firebase.firestore.DocumentData & {
id: string
label: string
message: string
}
/**
* An observable subject which is defined as an array of feeds
* the current user has.
*
* Note: If this is null, then it means the user is not signed in
*/
export const currentFeeds$ = new BehaviorSubject<HoppFeed[] | null>(null)
export function initFeeds() {
let snapshotStop: (() => void) | null = null
currentUser$.subscribe((user) => {
if (!user && snapshotStop) {
// User has logged out, clean up snapshot listeners
snapshotStop()
snapshotStop = null
} else if (user) {
snapshotStop = firebase
.firestore()
.collection("users")
.doc(user.uid)
.collection("feeds")
.orderBy("createdOn", "desc")
.onSnapshot((feedsRef) => {
const feeds: HoppFeed[] = []
feedsRef.forEach((doc) => {
const feed = doc.data()
feed.id = doc.id
feeds.push(feed as HoppFeed)
})
currentFeeds$.next(feeds)
})
}
})
}
export async function writeFeed(label: string, message: string) {
if (currentUser$.value == null)
throw new Error("Logged out user cannot write to feeds")
const dt = {
createdOn: new Date(),
author: currentUser$.value.uid,
author_name: currentUser$.value.displayName,
author_image: currentUser$.value.photoURL,
message,
label,
}
try {
await firebase
.firestore()
.collection("users")
.doc(currentUser$.value.uid)
.collection("feeds")
.add(dt)
} catch (e) {
console.error("error inserting", dt, e)
throw e
}
}
export async function deleteFeed(id: string) {
if (currentUser$.value == null)
throw new Error("Logged out user cannot delete feed")
try {
await firebase
.firestore()
.collection("users")
.doc(currentUser$.value.uid)
.collection("feeds")
.doc(id)
.delete()
} catch (e) {
console.error("error deleting", id, e)
throw e
}
}

199
helpers/fb/history.ts Normal file
View File

@@ -0,0 +1,199 @@
import firebase from "firebase"
import { currentUser$ } from "./auth"
import { settingsStore } from "~/newstore/settings"
import {
graphqlHistoryStore,
HISTORY_LIMIT,
restHistoryStore,
setGraphqlHistoryEntries,
setRESTHistoryEntries,
} from "~/newstore/history"
type HistoryFBCollections = "history" | "graphqlHistory"
/**
* Whether the history are loaded. If this is set to true
* Updates to the history store are written into firebase.
*
* If you have want to update the store and not fire the store update
* subscription, set this variable to false, do the update and then
* set it to true
*/
let loadedRESTHistory = false
/**
* Whether the history are loaded. If this is set to true
* Updates to the history store are written into firebase.
*
* If you have want to update the store and not fire the store update
* subscription, set this variable to false, do the update and then
* set it to true
*/
let loadedGraphqlHistory = false
async function writeHistory(entry: any, col: HistoryFBCollections) {
if (currentUser$.value == null)
throw new Error("User not logged in to sync history")
const hs = entry
try {
await firebase
.firestore()
.collection("users")
.doc(currentUser$.value.uid)
.collection(col)
.add(hs)
} catch (e) {
console.error("error writing to history", hs, e)
throw e
}
}
async function deleteHistory(entry: any, col: HistoryFBCollections) {
if (currentUser$.value == null)
throw new Error("User not logged in to delete history")
try {
await firebase
.firestore()
.collection("users")
.doc(currentUser$.value.uid)
.collection(col)
.doc(entry.id)
.delete()
} catch (e) {
console.error("error deleting history", entry, e)
throw e
}
}
async function clearHistory(col: HistoryFBCollections) {
if (currentUser$.value == null)
throw new Error("User not logged in to clear history")
const { docs } = await firebase
.firestore()
.collection("users")
.doc(currentUser$.value.uid)
.collection(col)
.get()
await Promise.all(docs.map((e) => deleteHistory(e, col)))
}
async function toggleStar(entry: any, col: HistoryFBCollections) {
if (currentUser$.value == null)
throw new Error("User not logged in to toggle star")
try {
await firebase
.firestore()
.collection("users")
.doc(currentUser$.value.uid)
.collection(col)
.doc(entry.id)
.update({ state: !entry.star })
} catch (e) {
console.error("error toggling star", entry, e)
throw e
}
}
export function initHistory() {
restHistoryStore.dispatches$.subscribe((dispatch) => {
if (
loadedRESTHistory &&
currentUser$.value &&
settingsStore.value.syncHistory
) {
if (dispatch.dispatcher === "addEntry") {
writeHistory(dispatch.payload.entry, "history")
} else if (dispatch.dispatcher === "deleteEntry") {
deleteHistory(dispatch.payload.entry, "history")
} else if (dispatch.dispatcher === "clearHistory") {
clearHistory("history")
} else if (dispatch.dispatcher === "toggleStar") {
toggleStar(dispatch.payload.entry, "history")
}
}
})
graphqlHistoryStore.dispatches$.subscribe((dispatch) => {
if (
loadedGraphqlHistory &&
currentUser$.value &&
settingsStore.value.syncHistory
) {
if (dispatch.dispatcher === "addEntry") {
writeHistory(dispatch.payload.entry, "graphqlHistory")
} else if (dispatch.dispatcher === "deleteEntry") {
deleteHistory(dispatch.payload.entry, "graphqlHistory")
} else if (dispatch.dispatcher === "clearHistory") {
clearHistory("graphqlHistory")
} else if (dispatch.dispatcher === "toggleStar") {
toggleStar(dispatch.payload.entry, "graphqlHistory")
}
}
})
let restSnapshotStop: (() => void) | null = null
let graphqlSnapshotStop: (() => void) | null = null
currentUser$.subscribe((user) => {
if (!user) {
// Clear the snapshot listeners when the user logs out
if (restSnapshotStop) {
restSnapshotStop()
restSnapshotStop = null
}
if (graphqlSnapshotStop) {
graphqlSnapshotStop()
graphqlSnapshotStop = null
}
} else {
restSnapshotStop = firebase
.firestore()
.collection("users")
.doc(user.uid)
.collection("history")
.orderBy("updatedOn", "desc")
.limit(HISTORY_LIMIT)
.onSnapshot((historyRef) => {
const history: any[] = []
historyRef.forEach((doc) => {
const entry = doc.data()
entry.id = doc.id
history.push(entry)
})
loadedRESTHistory = false
setRESTHistoryEntries(history)
loadedRESTHistory = true
})
graphqlSnapshotStop = firebase
.firestore()
.collection("users")
.doc(user.uid)
.collection("graphqlHistory")
.orderBy("updatedOn", "desc")
.limit(HISTORY_LIMIT)
.onSnapshot((historyRef) => {
const history: any[] = []
historyRef.forEach((doc) => {
const entry = doc.data()
entry.id = doc.id
history.push(entry)
})
loadedGraphqlHistory = false
setGraphqlHistoryEntries(history)
loadedGraphqlHistory = true
})
}
})
}

29
helpers/fb/index.ts Normal file
View File

@@ -0,0 +1,29 @@
import firebase from "firebase"
import { initAuth } from "./auth"
import { initCollections } from "./collections"
import { initEnvironments } from "./environments"
import { initFeeds } from "./feeds"
import { initHistory } from "./history"
import { initSettings } from "./settings"
const firebaseConfig = {
apiKey: process.env.API_KEY,
authDomain: process.env.AUTH_DOMAIN,
databaseURL: process.env.DATABASE_URL,
projectId: process.env.PROJECT_ID,
storageBucket: process.env.STORAGE_BUCKET,
messagingSenderId: process.env.MESSAGING_SENDER_ID,
appId: process.env.APP_ID,
measurementId: process.env.MEASUREMENT_ID,
}
export function initializeFirebase() {
firebase.initializeApp(firebaseConfig)
initAuth()
initSettings()
initCollections()
initHistory()
initEnvironments()
initFeeds()
}

96
helpers/fb/settings.ts Normal file
View File

@@ -0,0 +1,96 @@
import firebase from "firebase"
import { currentUser$ } from "./auth"
import {
applySettingFB,
settingsStore,
SettingsType,
} from "~/newstore/settings"
/**
* Used locally to prevent infinite loop when settings sync update
* is applied to the store which then fires the store sync listener.
* When you want to update settings and not want to fire the update listener,
* set this to true and then set it back to false once it is done
*/
let loadedSettings = false
/**
* Write Transform
*/
async function writeSettings(setting: string, value: any) {
if (currentUser$.value === null)
throw new Error("Cannot write setting, user not signed in")
const st = {
updatedOn: new Date(),
author: currentUser$.value.uid,
author_name: currentUser$.value.displayName,
author_image: currentUser$.value.photoURL,
name: setting,
value,
}
try {
await firebase
.firestore()
.collection("users")
.doc(currentUser$.value.uid)
.collection("settings")
.doc(setting)
.set(st)
} catch (e) {
console.error("error updating", st, e)
throw e
}
}
export function initSettings() {
settingsStore.dispatches$.subscribe((dispatch) => {
if (currentUser$.value && loadedSettings) {
if (dispatch.dispatcher === "bulkApplySettings") {
Object.keys(dispatch.payload).forEach((key) => {
writeSettings(key, dispatch.payload[key])
})
} else if (dispatch.dispatcher !== "applySettingFB") {
writeSettings(
dispatch.payload.settingKey,
settingsStore.value[dispatch.payload.settingKey as keyof SettingsType]
)
}
}
})
let snapshotStop: (() => void) | null = null
// Subscribe and unsubscribe event listeners
currentUser$.subscribe((user) => {
if (!user && snapshotStop) {
// User logged out
snapshotStop()
snapshotStop = null
} else if (user) {
snapshotStop = firebase
.firestore()
.collection("users")
.doc(user.uid)
.collection("settings")
.onSnapshot((settingsRef) => {
const settings: any[] = []
settingsRef.forEach((doc) => {
const setting = doc.data()
setting.id = doc.id
settings.push(setting)
})
loadedSettings = false
settings.forEach((e) => {
if (e && e.name && e.value != null) {
applySettingFB(e.name, e.value)
}
})
loadedSettings = true
})
}
})
}

View File

@@ -1,6 +1,6 @@
import { BehaviorSubject } from "rxjs"
import gql from "graphql-tag"
import { fb } from "../fb"
import { authIdToken$ } from "../fb/auth"
import { apolloClient } from "../apollo"
/*
@@ -11,7 +11,7 @@ import { apolloClient } from "../apollo"
/**
* Defines the information provided about a user
*/
interface UserInfo {
export interface UserInfo {
/**
* UID of the user
*/
@@ -42,10 +42,8 @@ export const currentUserInfo$ = new BehaviorSubject<UserInfo | null>(null)
/**
* Initializes the currenUserInfo$ view and sets up its update mechanism
*/
export async function initUserInfo() {
if (fb.idToken) await updateUserInfo()
fb.idToken$.subscribe((token) => {
export function initUserInfo() {
authIdToken$.subscribe((token) => {
if (token) {
updateUserInfo()
} else {