Handle new user: use default feed of top markets this week

This commit is contained in:
James Grugett 2022-05-01 10:35:05 -04:00
parent 21692a7b75
commit 3d96b11c6d
5 changed files with 68 additions and 4 deletions

View File

@ -5,6 +5,8 @@ import { ClickEvent } from './tracking'
import { filterDefined } from './util/array' import { filterDefined } from './util/array'
import { addObjects } from './util/object' import { addObjects } from './util/object'
export const MAX_FEED_CONTRACTS = 75
export const getRecommendedContracts = ( export const getRecommendedContracts = (
contractsById: { [contractId: string]: Contract }, contractsById: { [contractId: string]: Contract },
yourBetOnContractIds: string[] yourBetOnContractIds: string[]
@ -146,6 +148,8 @@ export function getContractScore(
contract: Contract, contract: Contract,
wordScores: { [word: string]: number } wordScores: { [word: string]: number }
) { ) {
if (Object.keys(wordScores).length === 0) return 1
const wordFrequency = contractToWordFrequency(contract) const wordFrequency = contractToWordFrequency(contract)
const score = _.sumBy(Object.keys(wordFrequency), (word) => { const score = _.sumBy(Object.keys(wordFrequency), (word) => {
const wordFreq = wordFrequency[word] ?? 0 const wordFreq = wordFrequency[word] ?? 0

View File

@ -14,13 +14,14 @@ import {
import { Bet } from '../../common/bet' import { Bet } from '../../common/bet'
import { Comment } from '../../common/comment' import { Comment } from '../../common/comment'
import { User } from '../../common/user' import { User } from '../../common/user'
import { getContractScore } from '../../common/recommended-contracts' import {
getContractScore,
MAX_FEED_CONTRACTS,
} from '../../common/recommended-contracts'
import { callCloudFunction } from './call-cloud-function' import { callCloudFunction } from './call-cloud-function'
const firestore = admin.firestore() const firestore = admin.firestore()
const MAX_FEED_CONTRACTS = 75
export const updateFeed = functions.pubsub export const updateFeed = functions.pubsub
.schedule('every 60 minutes') .schedule('every 60 minutes')
.onRun(async () => { .onRun(async () => {

View File

@ -8,6 +8,10 @@ import { trackLatency } from '../lib/firebase/tracking'
import { User } from '../../common/user' import { User } from '../../common/user'
import { getUserFeed } from '../lib/firebase/users' import { getUserFeed } from '../lib/firebase/users'
import { useUpdatedContracts } from './use-contracts' import { useUpdatedContracts } from './use-contracts'
import {
getRecentBetsAndComments,
getTopWeeklyContracts,
} from '../lib/firebase/contracts'
type feed = { type feed = {
contract: Contract contract: Contract
@ -23,7 +27,9 @@ export const useAlgoFeed = (user: User | null | undefined) => {
useEffect(() => { useEffect(() => {
if (user) { if (user) {
getUserFeed(user.id).then((feed) => { getUserFeed(user.id).then((feed) => {
setFeed(feed) if (feed.length === 0) {
getDefaultFeed().then((feed) => setFeed(feed))
} else setFeed(feed)
trackLatency('feed', getTime()) trackLatency('feed', getTime())
console.log('feed load time', getTime()) console.log('feed load time', getTime())
@ -44,3 +50,11 @@ const useUpdateFeed = (feed: feed | undefined) => {
})) }))
: undefined : undefined
} }
const getDefaultFeed = async () => {
const contracts = await getTopWeeklyContracts()
const feed = await Promise.all(
contracts.map((c) => getRecentBetsAndComments(c))
)
return feed
}

View File

@ -94,6 +94,8 @@ export const useUpdatedContracts = (contracts: Contract[] | undefined) => {
}) })
}) })
triggerUpdate((n) => n + 1)
return () => { return () => {
disposes.forEach((dispose) => dispose()) disposes.forEach((dispose) => dispose())
} }

View File

@ -23,6 +23,9 @@ import { createRNG, shuffle } from '../../../common/util/random'
import { getCpmmProbability } from '../../../common/calculate-cpmm' import { getCpmmProbability } from '../../../common/calculate-cpmm'
import { formatMoney, formatPercent } from '../../../common/util/format' import { formatMoney, formatPercent } from '../../../common/util/format'
import { DAY_MS } from '../../../common/util/time' import { DAY_MS } from '../../../common/util/time'
import { MAX_FEED_CONTRACTS } from '../../../common/recommended-contracts'
import { Bet } from '../../../common/bet'
import { Comment } from '../../../common/comment'
export type { Contract } export type { Contract }
export function contractPath(contract: Contract) { export function contractPath(contract: Contract) {
@ -231,6 +234,16 @@ export async function getHotContracts() {
) )
} }
const topWeeklyQuery = query(
contractCollection,
where('isResolved', '==', false),
orderBy('volume7Days', 'desc'),
limit(MAX_FEED_CONTRACTS)
)
export async function getTopWeeklyContracts() {
return await getValues<Contract>(topWeeklyQuery)
}
const closingSoonQuery = query( const closingSoonQuery = query(
contractCollection, contractCollection,
where('isResolved', '==', false), where('isResolved', '==', false),
@ -276,3 +289,33 @@ export async function getDailyContracts(
return contractsByDay return contractsByDay
} }
export async function getRecentBetsAndComments(contract: Contract) {
const contractDoc = doc(db, 'contracts', contract.id)
const [recentBets, recentComments] = await Promise.all([
getValues<Bet>(
query(
collection(contractDoc, 'bets'),
where('createdTime', '>', Date.now() - DAY_MS),
orderBy('createdTime', 'desc'),
limit(1)
)
),
getValues<Comment>(
query(
collection(contractDoc, 'comments'),
where('createdTime', '>', Date.now() - 3 * DAY_MS),
orderBy('createdTime', 'desc'),
limit(3)
)
),
])
return {
contract,
recentBets,
recentComments,
}
}