2022-01-26 20:03:32 +00:00
|
|
|
import {
|
|
|
|
collection,
|
2022-02-04 00:13:04 +00:00
|
|
|
collectionGroup,
|
2022-01-26 20:03:32 +00:00
|
|
|
deleteDoc,
|
|
|
|
doc,
|
2022-02-04 00:13:04 +00:00
|
|
|
getDocs,
|
2022-02-21 05:12:35 +00:00
|
|
|
onSnapshot,
|
2022-01-26 20:03:32 +00:00
|
|
|
query,
|
|
|
|
setDoc,
|
|
|
|
updateDoc,
|
|
|
|
where,
|
|
|
|
} from 'firebase/firestore'
|
2022-02-01 18:47:31 +00:00
|
|
|
import _ from 'lodash'
|
2022-01-21 23:21:46 +00:00
|
|
|
import { Fold } from '../../../common/fold'
|
|
|
|
import { Contract, contractCollection } from './contracts'
|
|
|
|
import { db } from './init'
|
2022-01-26 20:03:32 +00:00
|
|
|
import { User } from './users'
|
2022-02-04 00:13:04 +00:00
|
|
|
import { getValue, getValues, listenForValue, listenForValues } from './utils'
|
2022-01-21 23:21:46 +00:00
|
|
|
|
|
|
|
const foldCollection = collection(db, 'folds')
|
|
|
|
|
2022-01-22 17:39:31 +00:00
|
|
|
export function foldPath(
|
|
|
|
fold: Fold,
|
|
|
|
subpath?: 'edit' | 'markets' | 'leaderboards'
|
|
|
|
) {
|
2022-01-21 23:21:46 +00:00
|
|
|
return `/fold/${fold.slug}${subpath ? `/${subpath}` : ''}`
|
|
|
|
}
|
|
|
|
|
|
|
|
export function updateFold(fold: Fold, updates: Partial<Fold>) {
|
|
|
|
return updateDoc(doc(foldCollection, fold.id), updates)
|
|
|
|
}
|
|
|
|
|
2022-01-26 20:56:15 +00:00
|
|
|
export function deleteFold(fold: Fold) {
|
|
|
|
return deleteDoc(doc(foldCollection, fold.id))
|
|
|
|
}
|
|
|
|
|
2022-01-21 23:21:46 +00:00
|
|
|
export async function listAllFolds() {
|
|
|
|
return getValues<Fold>(foldCollection)
|
|
|
|
}
|
|
|
|
|
2022-01-24 05:47:20 +00:00
|
|
|
export function listenForFolds(setFolds: (folds: Fold[]) => void) {
|
|
|
|
return listenForValues(foldCollection, setFolds)
|
|
|
|
}
|
|
|
|
|
2022-02-04 00:13:04 +00:00
|
|
|
export function getFold(foldId: string) {
|
|
|
|
return getValue<Fold>(doc(foldCollection, foldId))
|
|
|
|
}
|
|
|
|
|
2022-01-21 23:21:46 +00:00
|
|
|
export async function getFoldBySlug(slug: string) {
|
|
|
|
const q = query(foldCollection, where('slug', '==', slug))
|
|
|
|
const folds = await getValues<Fold>(q)
|
|
|
|
|
|
|
|
return folds.length === 0 ? null : folds[0]
|
|
|
|
}
|
|
|
|
|
2022-01-31 04:12:12 +00:00
|
|
|
function contractsByTagsQuery(tags: string[]) {
|
2022-02-23 04:21:00 +00:00
|
|
|
// TODO: if tags.length > 10, execute multiple parallel queries
|
|
|
|
const lowercaseTags = tags.map((tag) => tag.toLowerCase()).slice(0, 10)
|
|
|
|
|
2022-01-31 04:12:12 +00:00
|
|
|
return query(
|
|
|
|
contractCollection,
|
2022-02-23 04:21:00 +00:00
|
|
|
where('lowercaseTags', 'array-contains-any', lowercaseTags)
|
2022-01-31 04:12:12 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-21 23:21:46 +00:00
|
|
|
export async function getFoldContracts(fold: Fold) {
|
|
|
|
const {
|
|
|
|
tags,
|
|
|
|
contractIds,
|
|
|
|
excludedContractIds,
|
|
|
|
creatorIds,
|
|
|
|
excludedCreatorIds,
|
|
|
|
} = fold
|
|
|
|
|
|
|
|
const [tagsContracts, includedContracts] = await Promise.all([
|
2022-01-31 04:12:12 +00:00
|
|
|
tags.length > 0 ? getValues<Contract>(contractsByTagsQuery(tags)) : [],
|
2022-01-21 23:21:46 +00:00
|
|
|
|
|
|
|
// TODO: if contractIds.length > 10, execute multiple parallel queries
|
|
|
|
contractIds.length > 0
|
|
|
|
? getValues<Contract>(
|
|
|
|
query(contractCollection, where('id', 'in', contractIds))
|
|
|
|
)
|
|
|
|
: [],
|
|
|
|
])
|
|
|
|
|
|
|
|
const excludedContractsSet = new Set(excludedContractIds)
|
|
|
|
|
|
|
|
const creatorSet = creatorIds ? new Set(creatorIds) : undefined
|
|
|
|
const excludedCreatorSet = excludedCreatorIds
|
|
|
|
? new Set(excludedCreatorIds)
|
|
|
|
: undefined
|
|
|
|
|
|
|
|
const approvedContracts = tagsContracts.filter((contract) => {
|
|
|
|
const { id, creatorId } = contract
|
|
|
|
|
|
|
|
if (excludedContractsSet.has(id)) return false
|
|
|
|
if (creatorSet && !creatorSet.has(creatorId)) return false
|
|
|
|
if (excludedCreatorSet && excludedCreatorSet.has(creatorId)) return false
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return [...approvedContracts, ...includedContracts]
|
|
|
|
}
|
2022-01-23 06:51:19 +00:00
|
|
|
|
2022-01-31 04:12:12 +00:00
|
|
|
export function listenForTaggedContracts(
|
|
|
|
tags: string[],
|
|
|
|
setContracts: (contracts: Contract[]) => void
|
|
|
|
) {
|
|
|
|
return listenForValues<Contract>(contractsByTagsQuery(tags), setContracts)
|
|
|
|
}
|
|
|
|
|
2022-01-23 06:51:19 +00:00
|
|
|
export function listenForFold(
|
|
|
|
foldId: string,
|
|
|
|
setFold: (fold: Fold | null) => void
|
|
|
|
) {
|
|
|
|
return listenForValue(doc(foldCollection, foldId), setFold)
|
|
|
|
}
|
2022-01-26 20:03:32 +00:00
|
|
|
|
2022-02-19 23:17:36 +00:00
|
|
|
export function followFold(foldId: string, userId: string) {
|
|
|
|
const followDoc = doc(foldCollection, foldId, 'followers', userId)
|
|
|
|
return setDoc(followDoc, { userId })
|
2022-01-26 20:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function unfollowFold(fold: Fold, user: User) {
|
|
|
|
const followDoc = doc(foldCollection, fold.id, 'followers', user.id)
|
|
|
|
return deleteDoc(followDoc)
|
|
|
|
}
|
|
|
|
|
2022-02-19 23:17:36 +00:00
|
|
|
export async function followFoldFromSlug(slug: string, userId: string) {
|
|
|
|
const snap = await getDocs(query(foldCollection, where('slug', '==', slug)))
|
|
|
|
if (snap.empty) return undefined
|
|
|
|
|
|
|
|
const foldDoc = snap.docs[0]
|
|
|
|
const followDoc = doc(foldDoc.ref, 'followers', userId)
|
|
|
|
|
|
|
|
return setDoc(followDoc, { userId })
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function unfollowFoldFromSlug(slug: string, userId: string) {
|
|
|
|
const snap = await getDocs(query(foldCollection, where('slug', '==', slug)))
|
|
|
|
if (snap.empty) return undefined
|
|
|
|
|
|
|
|
const foldDoc = snap.docs[0]
|
|
|
|
const followDoc = doc(foldDoc.ref, 'followers', userId)
|
|
|
|
|
|
|
|
return deleteDoc(followDoc)
|
|
|
|
}
|
|
|
|
|
2022-01-26 20:03:32 +00:00
|
|
|
export function listenForFollow(
|
|
|
|
fold: Fold,
|
|
|
|
user: User,
|
|
|
|
setFollow: (following: boolean) => void
|
|
|
|
) {
|
|
|
|
const followDoc = doc(foldCollection, fold.id, 'followers', user.id)
|
|
|
|
return listenForValue(followDoc, (value) => {
|
|
|
|
setFollow(!!value)
|
|
|
|
})
|
|
|
|
}
|
2022-02-01 18:06:42 +00:00
|
|
|
|
2022-02-01 18:47:31 +00:00
|
|
|
export async function getFoldsByTags(tags: string[]) {
|
2022-02-01 18:06:42 +00:00
|
|
|
if (tags.length === 0) return []
|
|
|
|
|
2022-02-23 04:21:00 +00:00
|
|
|
// TODO: split into multiple queries if tags.length > 10.
|
|
|
|
const lowercaseTags = tags.map((tag) => tag.toLowerCase()).slice(0, 10)
|
|
|
|
|
2022-02-01 18:47:31 +00:00
|
|
|
const folds = await getValues<Fold>(
|
2022-02-01 18:29:14 +00:00
|
|
|
query(
|
|
|
|
foldCollection,
|
|
|
|
where('lowercaseTags', 'array-contains-any', lowercaseTags)
|
|
|
|
)
|
2022-02-01 18:06:42 +00:00
|
|
|
)
|
2022-02-01 18:47:31 +00:00
|
|
|
|
|
|
|
return _.sortBy(folds, (fold) => -1 * fold.followCount)
|
2022-02-01 18:06:42 +00:00
|
|
|
}
|
2022-02-04 00:13:04 +00:00
|
|
|
|
2022-02-06 22:55:14 +00:00
|
|
|
export function listenForFoldsWithTags(
|
|
|
|
tags: string[],
|
|
|
|
setFolds: (folds: Fold[]) => void
|
|
|
|
) {
|
2022-02-23 04:21:00 +00:00
|
|
|
// TODO: split into multiple queries if tags.length > 10.
|
|
|
|
const lowercaseTags = tags.map((tag) => tag.toLowerCase()).slice(0, 10)
|
|
|
|
|
|
|
|
const q = query(
|
|
|
|
foldCollection,
|
|
|
|
where('lowercaseTags', 'array-contains-any', lowercaseTags)
|
|
|
|
)
|
2022-02-06 22:55:14 +00:00
|
|
|
|
|
|
|
return listenForValues<Fold>(q, (folds) => {
|
|
|
|
const sorted = _.sortBy(folds, (fold) => -1 * fold.followCount)
|
|
|
|
setFolds(sorted)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-04 00:13:04 +00:00
|
|
|
export async function getFollowedFolds(userId: string) {
|
|
|
|
const snapshot = await getDocs(
|
|
|
|
query(collectionGroup(db, 'followers'), where('userId', '==', userId))
|
|
|
|
)
|
|
|
|
const foldIds = snapshot.docs.map(
|
|
|
|
(doc) => doc.ref.parent.parent?.id as string
|
|
|
|
)
|
|
|
|
return foldIds
|
|
|
|
}
|
2022-02-21 05:12:35 +00:00
|
|
|
|
|
|
|
export function listenForFollowedFolds(
|
|
|
|
userId: string,
|
|
|
|
setFoldIds: (foldIds: string[]) => void
|
|
|
|
) {
|
|
|
|
return onSnapshot(
|
|
|
|
query(collectionGroup(db, 'followers'), where('userId', '==', userId)),
|
|
|
|
(snapshot) => {
|
|
|
|
if (snapshot.metadata.fromCache) return
|
|
|
|
|
|
|
|
const foldIds = snapshot.docs.map(
|
|
|
|
(doc) => doc.ref.parent.parent?.id as string
|
|
|
|
)
|
|
|
|
setFoldIds(foldIds)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|