9a4e5763f5
* basic market categories * use tags to store market category * display category in market * display full category * category selector component on feed * Move feed data fetching to new file * Decrease batch size for updating feed to prevent out-of-memory error * Compute and update category feeds! * Show feeds based on category tabs * Add react-query package! * Use react query to cache contracts * Remove 'other' category * Add back personal / friends to feed categories * Show scrollbar temporarily for categories * Remove 5 categories, change geopolitics to world * finance => economics * Show categories on two lines on larger screens Co-authored-by: James Grugett <jahooma@gmail.com>
72 lines
2.0 KiB
TypeScript
72 lines
2.0 KiB
TypeScript
import * as admin from 'firebase-admin'
|
|
import { Bet } from '../../common/bet'
|
|
import { Contract } from '../../common/contract'
|
|
import { DAY_MS } from '../../common/util/time'
|
|
import { getValues } from './utils'
|
|
|
|
const firestore = admin.firestore()
|
|
|
|
export async function getFeedContracts() {
|
|
// Get contracts bet on or created in last week.
|
|
const [activeContracts, inactiveContracts] = await Promise.all([
|
|
getValues<Contract>(
|
|
firestore
|
|
.collection('contracts')
|
|
.where('isResolved', '==', false)
|
|
.where('volume7Days', '>', 0)
|
|
),
|
|
|
|
getValues<Contract>(
|
|
firestore
|
|
.collection('contracts')
|
|
.where('isResolved', '==', false)
|
|
.where('createdTime', '>', Date.now() - DAY_MS * 7)
|
|
.where('volume7Days', '==', 0)
|
|
),
|
|
])
|
|
|
|
const combined = [...activeContracts, ...inactiveContracts]
|
|
// Remove closed contracts.
|
|
return combined.filter((c) => (c.closeTime ?? Infinity) > Date.now())
|
|
}
|
|
|
|
export async function getTaggedContracts(tag: string) {
|
|
const taggedContracts = await getValues<Contract>(
|
|
firestore
|
|
.collection('contracts')
|
|
.where('isResolved', '==', false)
|
|
.where('lowercaseTags', 'array-contains', tag.toLowerCase())
|
|
)
|
|
|
|
// Remove closed contracts.
|
|
return taggedContracts.filter((c) => (c.closeTime ?? Infinity) > Date.now())
|
|
}
|
|
|
|
export async function getRecentBetsAndComments(contract: Contract) {
|
|
const contractDoc = firestore.collection('contracts').doc(contract.id)
|
|
|
|
const [recentBets, recentComments] = await Promise.all([
|
|
getValues<Bet>(
|
|
contractDoc
|
|
.collection('bets')
|
|
.where('createdTime', '>', Date.now() - DAY_MS)
|
|
.orderBy('createdTime', 'desc')
|
|
.limit(1)
|
|
),
|
|
|
|
getValues<Comment>(
|
|
contractDoc
|
|
.collection('comments')
|
|
.where('createdTime', '>', Date.now() - 3 * DAY_MS)
|
|
.orderBy('createdTime', 'desc')
|
|
.limit(3)
|
|
),
|
|
])
|
|
|
|
return {
|
|
contract,
|
|
recentBets,
|
|
recentComments,
|
|
}
|
|
}
|