2021-12-15 00:45:13 +00:00
|
|
|
import {
|
|
|
|
getFirestore,
|
|
|
|
doc,
|
|
|
|
setDoc,
|
|
|
|
getDoc,
|
|
|
|
onSnapshot,
|
2021-12-16 01:34:36 +00:00
|
|
|
collection,
|
|
|
|
query,
|
|
|
|
where,
|
|
|
|
limit,
|
|
|
|
getDocs,
|
2022-01-18 03:20:25 +00:00
|
|
|
orderBy,
|
2021-12-15 00:45:13 +00:00
|
|
|
} from 'firebase/firestore'
|
2021-12-08 16:42:50 +00:00
|
|
|
import { getAuth } from 'firebase/auth'
|
|
|
|
import { ref, getStorage, uploadBytes, getDownloadURL } from 'firebase/storage'
|
|
|
|
import {
|
|
|
|
onAuthStateChanged,
|
|
|
|
GoogleAuthProvider,
|
|
|
|
signInWithPopup,
|
|
|
|
} from 'firebase/auth'
|
|
|
|
|
2022-01-18 07:18:38 +00:00
|
|
|
import { app } from './init'
|
2022-01-24 06:48:06 +00:00
|
|
|
import { PrivateUser, User } from '../../../common/user'
|
2022-01-18 07:18:38 +00:00
|
|
|
import { createUser } from './api-call'
|
2022-01-18 03:20:25 +00:00
|
|
|
import { getValues, listenForValues } from './utils'
|
2022-01-10 21:07:57 +00:00
|
|
|
export type { User }
|
2021-12-14 17:36:23 +00:00
|
|
|
|
2021-12-08 16:42:50 +00:00
|
|
|
const db = getFirestore(app)
|
|
|
|
export const auth = getAuth(app)
|
|
|
|
|
|
|
|
export async function getUser(userId: string) {
|
|
|
|
const docSnap = await getDoc(doc(db, 'users', userId))
|
|
|
|
return docSnap.data() as User
|
|
|
|
}
|
|
|
|
|
2021-12-16 01:34:36 +00:00
|
|
|
export async function getUserByUsername(username: string) {
|
|
|
|
// Find a user whose username matches the given username, or null if no such user exists.
|
|
|
|
const userCollection = collection(db, 'users')
|
|
|
|
const q = query(userCollection, where('username', '==', username), limit(1))
|
|
|
|
const docs = await getDocs(q)
|
|
|
|
const users = docs.docs.map((doc) => doc.data() as User)
|
|
|
|
return users[0] || null
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:42:50 +00:00
|
|
|
export async function setUser(userId: string, user: User) {
|
|
|
|
await setDoc(doc(db, 'users', userId), user)
|
|
|
|
}
|
|
|
|
|
2021-12-15 00:45:13 +00:00
|
|
|
export function listenForUser(userId: string, setUser: (user: User) => void) {
|
|
|
|
const userRef = doc(db, 'users', userId)
|
|
|
|
return onSnapshot(userRef, (userSnap) => {
|
|
|
|
setUser(userSnap.data() as User)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:42:50 +00:00
|
|
|
const CACHED_USER_KEY = 'CACHED_USER_KEY'
|
2022-01-18 07:18:38 +00:00
|
|
|
|
|
|
|
// used to avoid weird race condition
|
|
|
|
let createUserPromise: Promise<User | null> | undefined = undefined
|
|
|
|
|
2021-12-17 04:09:38 +00:00
|
|
|
export function listenForLogin(onUser: (user: User | null) => void) {
|
|
|
|
const cachedUser = localStorage.getItem(CACHED_USER_KEY)
|
|
|
|
onUser(cachedUser ? JSON.parse(cachedUser) : null)
|
|
|
|
|
2022-01-18 07:18:38 +00:00
|
|
|
if (!cachedUser) createUser() // warm up cloud function
|
|
|
|
|
2021-12-17 04:09:38 +00:00
|
|
|
return onAuthStateChanged(auth, async (fbUser) => {
|
|
|
|
if (fbUser) {
|
2022-01-18 07:18:38 +00:00
|
|
|
let user: User | null = await getUser(fbUser.uid)
|
|
|
|
|
2021-12-17 04:09:38 +00:00
|
|
|
if (!user) {
|
2022-01-18 07:18:38 +00:00
|
|
|
if (!createUserPromise) {
|
|
|
|
createUserPromise = createUser()
|
2021-12-08 16:42:50 +00:00
|
|
|
}
|
2022-01-18 07:18:38 +00:00
|
|
|
user = (await createUserPromise) || null
|
2021-12-08 16:42:50 +00:00
|
|
|
}
|
2022-01-18 07:18:38 +00:00
|
|
|
|
2021-12-17 04:09:38 +00:00
|
|
|
onUser(user)
|
2021-12-08 16:42:50 +00:00
|
|
|
|
|
|
|
// Persist to local storage, to reduce login blink next time.
|
|
|
|
// Note: Cap on localStorage size is ~5mb
|
2021-12-17 04:09:38 +00:00
|
|
|
localStorage.setItem(CACHED_USER_KEY, JSON.stringify(user))
|
2021-12-08 16:42:50 +00:00
|
|
|
} else {
|
2021-12-09 23:37:26 +00:00
|
|
|
// User logged out; reset to null
|
|
|
|
onUser(null)
|
2021-12-13 18:20:31 +00:00
|
|
|
localStorage.removeItem(CACHED_USER_KEY)
|
2022-01-18 07:18:38 +00:00
|
|
|
createUserPromise = undefined
|
2021-12-08 16:42:50 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function firebaseLogin() {
|
|
|
|
const provider = new GoogleAuthProvider()
|
|
|
|
signInWithPopup(auth, provider)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function firebaseLogout() {
|
|
|
|
auth.signOut()
|
|
|
|
}
|
|
|
|
|
|
|
|
const storage = getStorage(app)
|
|
|
|
// Example: uploadData('avatars/ajfi8iejsf.png', data)
|
|
|
|
export async function uploadData(
|
|
|
|
path: string,
|
|
|
|
data: ArrayBuffer | Blob | Uint8Array
|
|
|
|
) {
|
|
|
|
const uploadRef = ref(storage, path)
|
|
|
|
// Uploaded files should be cached for 1 day, then revalidated
|
|
|
|
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
|
|
|
|
const metadata = { cacheControl: 'public, max-age=86400, must-revalidate' }
|
|
|
|
await uploadBytes(uploadRef, data, metadata)
|
|
|
|
return await getDownloadURL(uploadRef)
|
|
|
|
}
|
2022-01-16 03:09:15 +00:00
|
|
|
|
|
|
|
export async function listAllUsers() {
|
|
|
|
const userCollection = collection(db, 'users')
|
|
|
|
const q = query(userCollection)
|
|
|
|
const docs = await getDocs(q)
|
|
|
|
return docs.docs.map((doc) => doc.data() as User)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function listenForAllUsers(setUsers: (users: User[]) => void) {
|
|
|
|
const userCollection = collection(db, 'users')
|
|
|
|
const q = query(userCollection)
|
|
|
|
listenForValues(q, setUsers)
|
|
|
|
}
|
2022-01-18 03:20:25 +00:00
|
|
|
|
2022-01-24 06:48:06 +00:00
|
|
|
export function listenForPrivateUsers(
|
|
|
|
setUsers: (users: PrivateUser[]) => void
|
|
|
|
) {
|
|
|
|
const userCollection = collection(db, 'private-users')
|
|
|
|
const q = query(userCollection)
|
|
|
|
listenForValues(q, setUsers)
|
|
|
|
}
|
|
|
|
|
2022-01-18 03:20:25 +00:00
|
|
|
const topTradersQuery = query(
|
|
|
|
collection(db, 'users'),
|
|
|
|
orderBy('totalPnLCached', 'desc'),
|
|
|
|
limit(21)
|
|
|
|
)
|
|
|
|
|
|
|
|
export async function getTopTraders() {
|
|
|
|
const users = await getValues<User>(topTradersQuery)
|
|
|
|
return users.filter((user) => user.username !== 'SG').slice(0, 20)
|
|
|
|
}
|
|
|
|
|
|
|
|
const topCreatorsQuery = query(
|
|
|
|
collection(db, 'users'),
|
|
|
|
orderBy('creatorVolumeCached', 'desc'),
|
|
|
|
limit(20)
|
|
|
|
)
|
|
|
|
|
|
|
|
export function getTopCreators() {
|
|
|
|
return getValues<User>(topCreatorsQuery)
|
|
|
|
}
|