Merge branch 'main' into feat/search

This commit is contained in:
liyasthomas
2021-08-29 20:59:47 +05:30
12 changed files with 3348 additions and 2300 deletions

View File

@@ -1,5 +1,11 @@
import firebase from "firebase/app"
import "firebase/analytics"
import {
Analytics,
getAnalytics,
logEvent,
setAnalyticsCollectionEnabled,
setUserId,
setUserProperties,
} from "firebase/analytics"
import { authEvents$ } from "./auth"
import {
HoppAccentColor,
@@ -8,7 +14,7 @@ import {
settingsStore,
} from "~/newstore/settings"
let analytics: firebase.analytics.Analytics | null
let analytics: Analytics | null = null
type SettingsCustomDimensions = {
usesProxy: boolean
@@ -29,7 +35,7 @@ type HoppRequestEvent =
| { platform: "wss" | "sse" | "socketio" | "mqtt" }
export function initAnalytics() {
analytics = firebase.app().analytics()
analytics = getAnalytics()
initLoginListeners()
initSettingsListeners()
@@ -38,16 +44,16 @@ export function initAnalytics() {
function initLoginListeners() {
authEvents$.subscribe((ev) => {
if (ev.event === "login") {
if (settingsStore.value.TELEMETRY_ENABLED) {
analytics?.setUserId(ev.user.uid)
if (settingsStore.value.TELEMETRY_ENABLED && analytics) {
setUserId(analytics, ev.user.uid)
analytics?.logEvent("login", {
logEvent(analytics, "login", {
method: ev.user.providerData[0]?.providerId, // Assume the first provider is the login provider
})
}
} else if (ev.event === "logout") {
if (settingsStore.value.TELEMETRY_ENABLED) {
analytics?.logEvent("logout")
if (settingsStore.value.TELEMETRY_ENABLED && analytics) {
logEvent(analytics, "logout")
}
}
})
@@ -71,29 +77,30 @@ function initSettingsListeners() {
// User toggled telemetry mode to off or to on
if (
(telemetryStatus && !settings.TELEMETRY_ENABLED) ||
settings.TELEMETRY_ENABLED
((telemetryStatus && !settings.TELEMETRY_ENABLED) ||
settings.TELEMETRY_ENABLED) &&
analytics
) {
analytics?.setUserProperties(conf)
setUserProperties(analytics, conf)
}
telemetryStatus = settings.TELEMETRY_ENABLED
analytics?.setAnalyticsCollectionEnabled(telemetryStatus)
if (analytics) setAnalyticsCollectionEnabled(analytics, telemetryStatus)
})
analytics?.setAnalyticsCollectionEnabled(telemetryStatus)
if (analytics) setAnalyticsCollectionEnabled(analytics, telemetryStatus)
}
export function logHoppRequestRunToAnalytics(ev: HoppRequestEvent) {
if (settingsStore.value.TELEMETRY_ENABLED) {
analytics?.logEvent("hopp-request", ev)
if (settingsStore.value.TELEMETRY_ENABLED && analytics) {
logEvent(analytics, "hopp-request", ev)
}
}
export function logPageView(pagePath: string) {
if (settingsStore.value.TELEMETRY_ENABLED) {
analytics?.logEvent("page_view", {
if (settingsStore.value.TELEMETRY_ENABLED && analytics) {
logEvent(analytics, "page_view", {
page_path: pagePath,
})
}

View File

@@ -1,6 +1,29 @@
import firebase from "firebase/app"
import "firebase/firestore"
import "firebase/auth"
import {
User,
getAuth,
onAuthStateChanged,
onIdTokenChanged,
signInWithPopup,
GoogleAuthProvider,
GithubAuthProvider,
signInWithEmailAndPassword as signInWithEmailAndPass,
isSignInWithEmailLink as isSignInWithEmailLinkFB,
fetchSignInMethodsForEmail,
sendSignInLinkToEmail,
signInWithEmailLink as signInWithEmailLinkFB,
ActionCodeSettings,
signOut,
linkWithCredential,
AuthCredential,
UserCredential,
} from "firebase/auth"
import {
onSnapshot,
getFirestore,
setDoc,
doc,
updateDoc,
} from "firebase/firestore"
import {
BehaviorSubject,
distinctUntilChanged,
@@ -11,7 +34,7 @@ import {
} from "rxjs"
import { onBeforeUnmount, onMounted } from "@nuxtjs/composition-api"
export type HoppUser = firebase.User & {
export type HoppUser = User & {
provider?: string
accessToken?: string
}
@@ -39,9 +62,12 @@ export const authEvents$ = new Subject<AuthEvents>()
* Initializes the firebase authentication related subjects
*/
export function initAuth() {
const auth = getAuth()
const firestore = getFirestore()
let extraSnapshotStop: (() => void) | null = null
firebase.auth().onAuthStateChanged((user) => {
onAuthStateChanged(auth, (user) => {
/** Whether the user was logged in before */
const wasLoggedIn = currentUser$.value !== null
@@ -62,19 +88,14 @@ export function initAuth() {
uid: profile.uid,
}
firebase
.firestore()
.collection("users")
.doc(user.uid)
.set(us, { merge: true })
.catch((e) => console.error("error updating", us, e))
setDoc(doc(firestore, "users", user.uid), us, { merge: true }).catch(
(e) => console.error("error updating", us, e)
)
})
extraSnapshotStop = firebase
.firestore()
.collection("users")
.doc(user.uid)
.onSnapshot((doc) => {
extraSnapshotStop = onSnapshot(
doc(firestore, "users", user.uid),
(doc) => {
const data = doc.data()
const userUpdate: HoppUser = user
@@ -86,7 +107,8 @@ export function initAuth() {
}
currentUser$.next(userUpdate)
})
}
)
}
currentUser$.next(user)
@@ -104,7 +126,7 @@ export function initAuth() {
}
})
firebase.auth().onIdTokenChanged(async (user) => {
onIdTokenChanged(auth, async (user) => {
if (user) {
authIdToken$.next(await user.getIdToken())
@@ -123,18 +145,17 @@ export function initAuth() {
* Sign user in with a popup using Google
*/
export async function signInUserWithGoogle() {
return await firebase
.auth()
.signInWithPopup(new firebase.auth.GoogleAuthProvider())
return await signInWithPopup(getAuth(), new GoogleAuthProvider())
}
/**
* Sign user in with a popup using Github
*/
export async function signInUserWithGithub() {
return await firebase
.auth()
.signInWithPopup(new firebase.auth.GithubAuthProvider().addScope("gist"))
return await signInWithPopup(
getAuth(),
new GithubAuthProvider().addScope("gist")
)
}
/**
@@ -144,7 +165,7 @@ export async function signInWithEmailAndPassword(
email: string,
password: string
) {
return await firebase.auth().signInWithEmailAndPassword(email, password)
return await signInWithEmailAndPass(getAuth(), email, password)
}
/**
@@ -155,7 +176,14 @@ export async function signInWithEmailAndPassword(
* @returns Promise for string array of the auth provider methods accessible
*/
export async function getSignInMethodsForEmail(email: string) {
return await firebase.auth().fetchSignInMethodsForEmail(email)
return await fetchSignInMethodsForEmail(getAuth(), email)
}
export async function linkWithFBCredential(
user: User,
credential: AuthCredential
) {
return await linkWithCredential(user, credential)
}
/**
@@ -166,9 +194,9 @@ export async function getSignInMethodsForEmail(email: string) {
*/
export async function signInWithEmail(
email: string,
actionCodeSettings: firebase.auth.ActionCodeSettings
actionCodeSettings: ActionCodeSettings
) {
return await firebase.auth().sendSignInLinkToEmail(email, actionCodeSettings)
return await sendSignInLinkToEmail(getAuth(), email, actionCodeSettings)
}
/**
@@ -177,7 +205,7 @@ export async function signInWithEmail(
* @param url - The URL to look in
*/
export function isSignInWithEmailLink(url: string) {
return firebase.auth().isSignInWithEmailLink(url)
return isSignInWithEmailLinkFB(getAuth(), url)
}
/**
@@ -187,7 +215,7 @@ export function isSignInWithEmailLink(url: string) {
* @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)
return await signInWithEmailLinkFB(getAuth(), email, url)
}
/**
@@ -196,7 +224,7 @@ export async function signInWithEmailLink(email: string, url: string) {
export async function signOutUser() {
if (!currentUser$.value) throw new Error("No user has logged in")
await firebase.auth().signOut()
await signOut(getAuth())
}
/**
@@ -216,18 +244,20 @@ export async function setProviderInfo(id: string, token: string) {
}
try {
await firebase
.firestore()
.collection("users")
.doc(currentUser$.value.uid)
.update(us)
.catch((e) => console.error("error updating", us, e))
await updateDoc(
doc(getFirestore(), "users", currentUser$.value.uid),
us
).catch((e) => console.error("error updating", us, e))
} catch (e) {
console.error("error updating", e)
throw e
}
}
export function getGithubCredentialFromResult(result: UserCredential) {
return GithubAuthProvider.credentialFromResult(result)
}
/**
* A Vue composable function that is called when the auth status
* is being updated to being logged in (fired multiple times),

View File

@@ -1,5 +1,10 @@
import firebase from "firebase/app"
import "firebase/firestore"
import {
collection,
doc,
getFirestore,
onSnapshot,
setDoc,
} from "firebase/firestore"
import { currentUser$ } from "./auth"
import {
restCollections$,
@@ -49,13 +54,10 @@ export async function writeCollections(
}
try {
await firebase
.firestore()
.collection("users")
.doc(currentUser$.value.uid)
.collection(flag)
.doc("sync")
.set(cl)
await setDoc(
doc(getFirestore(), "users", currentUser$.value.uid, flag, "sync"),
cl
)
} catch (e) {
console.error("error updating", cl, e)
throw e
@@ -98,12 +100,9 @@ export function initCollections() {
graphqlSnapshotStop = null
}
} else {
restSnapshotStop = firebase
.firestore()
.collection("users")
.doc(user.uid)
.collection("collections")
.onSnapshot((collectionsRef) => {
restSnapshotStop = onSnapshot(
collection(getFirestore(), "users", user.uid, "collections"),
(collectionsRef) => {
const collections: any[] = []
collectionsRef.forEach((doc) => {
const collection = doc.data()
@@ -124,14 +123,12 @@ export function initCollections() {
}
loadedRESTCollections = true
})
}
)
graphqlSnapshotStop = firebase
.firestore()
.collection("users")
.doc(user.uid)
.collection("collectionsGraphql")
.onSnapshot((collectionsRef) => {
graphqlSnapshotStop = onSnapshot(
collection(getFirestore(), "users", user.uid, "collectionsGraphql"),
(collectionsRef) => {
const collections: any[] = []
collectionsRef.forEach((doc) => {
const collection = doc.data()
@@ -150,7 +147,8 @@ export function initCollections() {
}
loadedGraphqlCollections = true
})
}
)
}
})
}

View File

@@ -1,5 +1,10 @@
import firebase from "firebase/app"
import "firebase/firestore"
import {
collection,
doc,
getFirestore,
onSnapshot,
setDoc,
} from "firebase/firestore"
import { currentUser$ } from "./auth"
import {
Environment,
@@ -39,13 +44,16 @@ async function writeEnvironments(environment: Environment[]) {
}
try {
await firebase
.firestore()
.collection("users")
.doc(currentUser$.value.uid)
.collection("environments")
.doc("sync")
.set(ev)
await setDoc(
doc(
getFirestore(),
"users",
currentUser$.value.uid,
"envrionments",
"sync"
),
ev
)
} catch (e) {
console.error("error updating", ev, e)
throw e
@@ -65,13 +73,10 @@ async function writeGlobalEnvironment(variables: Environment["variables"]) {
}
try {
await firebase
.firestore()
.collection("users")
.doc(currentUser$.value.uid)
.collection("globalEnv")
.doc("sync")
.set(ev)
await setDoc(
doc(getFirestore(), "users", currentUser$.value.uid, "globalEnv", "sync"),
ev
)
} catch (e) {
console.error("error updating", ev, e)
throw e
@@ -115,12 +120,9 @@ export function initEnvironments() {
globalsSnapshotStop = null
}
} else if (user) {
envSnapshotStop = firebase
.firestore()
.collection("users")
.doc(user.uid)
.collection("environments")
.onSnapshot((environmentsRef) => {
envSnapshotStop = onSnapshot(
collection(getFirestore(), "users", user.uid, "environments"),
(environmentsRef) => {
const environments: any[] = []
environmentsRef.forEach((doc) => {
@@ -134,13 +136,11 @@ export function initEnvironments() {
replaceEnvironments(environments[0].environment)
}
loadedEnvironments = true
})
globalsSnapshotStop = firebase
.firestore()
.collection("users")
.doc(user.uid)
.collection("globalEnv")
.onSnapshot((globalsRef) => {
}
)
globalsSnapshotStop = onSnapshot(
collection(getFirestore(), "users", user.uid, "globalEnv"),
(globalsRef) => {
if (globalsRef.docs.length === 0) {
loadedGlobals = true
return
@@ -150,7 +150,8 @@ export function initEnvironments() {
loadedGlobals = false
setGlobalEnvVariables(doc.variables)
loadedGlobals = true
})
}
)
}
})
}

View File

@@ -1,5 +1,16 @@
import firebase from "firebase/app"
import "firebase/firestore"
import {
addDoc,
collection,
deleteDoc,
doc,
getDocs,
getFirestore,
limit,
onSnapshot,
orderBy,
query,
updateDoc,
} from "firebase/firestore"
import { currentUser$ } from "./auth"
import { settingsStore } from "~/newstore/settings"
import {
@@ -46,12 +57,10 @@ async function writeHistory(entry: any, col: HistoryFBCollections) {
}
try {
await firebase
.firestore()
.collection("users")
.doc(currentUser$.value.uid)
.collection(col)
.add(hs)
await addDoc(
collection(getFirestore(), "users", currentUser$.value.uid, col),
hs
)
} catch (e) {
console.error("error writing to history", hs, e)
throw e
@@ -63,13 +72,9 @@ async function deleteHistory(entry: any, col: HistoryFBCollections) {
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()
await deleteDoc(
doc(getFirestore(), "users", currentUser$.value.uid, col, entry.id)
)
} catch (e) {
console.error("error deleting history", entry, e)
throw e
@@ -80,12 +85,9 @@ 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()
const { docs } = await getDocs(
collection(getFirestore(), "users", currentUser$.value.uid, col)
)
await Promise.all(docs.map((e) => deleteHistory(e, col)))
}
@@ -95,13 +97,10 @@ async function toggleStar(entry: any, col: HistoryFBCollections) {
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({ star: !entry.star })
await updateDoc(
doc(getFirestore(), "users", currentUser$.value.uid, col, entry.id),
{ star: !entry.star }
)
} catch (e) {
console.error("error toggling star", entry, e)
throw e
@@ -161,14 +160,13 @@ export function initHistory() {
graphqlSnapshotStop = null
}
} else {
restSnapshotStop = firebase
.firestore()
.collection("users")
.doc(user.uid)
.collection("history")
.orderBy("updatedOn", "desc")
.limit(HISTORY_LIMIT)
.onSnapshot((historyRef) => {
restSnapshotStop = onSnapshot(
query(
collection(getFirestore(), "users", user.uid, "history"),
orderBy("updatedOn", "desc"),
limit(HISTORY_LIMIT)
),
(historyRef) => {
const history: RESTHistoryEntry[] = []
historyRef.forEach((doc) => {
@@ -180,16 +178,16 @@ export function initHistory() {
loadedRESTHistory = false
setRESTHistoryEntries(history)
loadedRESTHistory = true
})
}
)
graphqlSnapshotStop = firebase
.firestore()
.collection("users")
.doc(user.uid)
.collection("graphqlHistory")
.orderBy("updatedOn", "desc")
.limit(HISTORY_LIMIT)
.onSnapshot((historyRef) => {
graphqlSnapshotStop = onSnapshot(
query(
collection(getFirestore(), "users", user.uid, "graphqlHistory"),
orderBy("updatedOn", "desc"),
limit(HISTORY_LIMIT)
),
(historyRef) => {
const history: GQLHistoryEntry[] = []
historyRef.forEach((doc) => {
@@ -201,7 +199,8 @@ export function initHistory() {
loadedGraphqlHistory = false
setGraphqlHistoryEntries(history)
loadedGraphqlHistory = true
})
}
)
}
})
}

View File

@@ -1,4 +1,4 @@
import firebase from "firebase/app"
import { initializeApp } from "firebase/app"
import { initAnalytics } from "./analytics"
import { initAuth } from "./auth"
import { initCollections } from "./collections"
@@ -22,7 +22,7 @@ let initialized = false
export function initializeFirebase() {
if (!initialized) {
try {
firebase.initializeApp(firebaseConfig)
initializeApp(firebaseConfig)
initAuth()
initSettings()

View File

@@ -1,5 +1,3 @@
import firebase from "firebase/app"
import "firebase/firestore"
import {
audit,
combineLatest,
@@ -9,6 +7,7 @@ import {
map,
Subscription,
} from "rxjs"
import { doc, getDoc, getFirestore, setDoc } from "firebase/firestore"
import {
HoppRESTRequest,
translateToNewRequest,
@@ -23,13 +22,10 @@ import { restRequest$ } from "~/newstore/RESTSession"
* @param request The request to write to the request sync
*/
function writeCurrentRequest(user: HoppUser, request: HoppRESTRequest) {
return firebase
.firestore()
.collection("users")
.doc(user.uid)
.collection("requests")
.doc("rest")
.set(request)
return setDoc(
doc(getFirestore(), "users", user.uid, "requests", "rest"),
request
)
}
/**
@@ -43,15 +39,11 @@ export async function loadRequestFromSync(): Promise<HoppRESTRequest | null> {
if (!currentUser)
throw new Error("Cannot load request from sync without login")
const doc = await firebase
.firestore()
.collection("users")
.doc(currentUser.uid)
.collection("requests")
.doc("rest")
.get()
const fbDoc = await getDoc(
doc(getFirestore(), "users", currentUser.uid, "requests", "rest")
)
const data = doc.data()
const data = fbDoc.data()
if (!data) return null
else return translateToNewRequest(data)

View File

@@ -1,5 +1,10 @@
import firebase from "firebase/app"
import "firebase/firestore"
import {
collection,
doc,
getFirestore,
onSnapshot,
setDoc,
} from "@firebase/firestore"
import { currentUser$ } from "./auth"
import { applySetting, settingsStore, SettingsType } from "~/newstore/settings"
@@ -28,13 +33,10 @@ async function writeSettings(setting: string, value: any) {
}
try {
await firebase
.firestore()
.collection("users")
.doc(currentUser$.value.uid)
.collection("settings")
.doc(setting)
.set(st)
await setDoc(
doc(getFirestore(), "users", currentUser$.value.uid, "settings", setting),
st
)
} catch (e) {
console.error("error updating", st, e)
throw e
@@ -66,12 +68,9 @@ export function initSettings() {
snapshotStop()
snapshotStop = null
} else if (user) {
snapshotStop = firebase
.firestore()
.collection("users")
.doc(user.uid)
.collection("settings")
.onSnapshot((settingsRef) => {
snapshotStop = onSnapshot(
collection(getFirestore(), "users", user.uid, "settings"),
(settingsRef) => {
const settings: any[] = []
settingsRef.forEach((doc) => {
@@ -87,7 +86,8 @@ export function initSettings() {
}
})
loadedSettings = true
})
}
)
}
})
}