06b7e49e98
* Store view counts & last viewed time * Schedule updating user recommendations. Compute using tf-idf. * Update contract's lastBetTime and lastCommentTime on new bets and comments. * Remove contract's lastUpdatedTime * Remove folds activity feed * Implement getFeed cloud function * Hook up client to use getFeed * Script to cache viewCounts and lastViewTime * Batched wait all userRecommendations * Cache view script runs on all users * Update user feed each hour and get feed from cache doc. * Delete view cache script * Update feed script * Tweak feed algorithm * Compute recommendation scores from updateUserFeed * Disable lastViewedScore factor * Update lastCommentTime script * Comment out console.log * Fix timeout issue by calling new cloud functions with part of the work. * Listen for contract updates to feed. * Handle new user: use default feed of top markets this week * Track lastUpdatedTime * Tweak logic of calling cloud functions in batches * Tweak cloud function batching
61 lines
1.6 KiB
TypeScript
61 lines
1.6 KiB
TypeScript
import _ from 'lodash'
|
|
import { useState, useEffect } from 'react'
|
|
import { Bet } from '../../common/bet'
|
|
import { Comment } from '../../common/comment'
|
|
import { Contract } from '../../common/contract'
|
|
import { useTimeSinceFirstRender } from './use-time-since-first-render'
|
|
import { trackLatency } from '../lib/firebase/tracking'
|
|
import { User } from '../../common/user'
|
|
import { getUserFeed } from '../lib/firebase/users'
|
|
import { useUpdatedContracts } from './use-contracts'
|
|
import {
|
|
getRecentBetsAndComments,
|
|
getTopWeeklyContracts,
|
|
} from '../lib/firebase/contracts'
|
|
|
|
type feed = {
|
|
contract: Contract
|
|
recentBets: Bet[]
|
|
recentComments: Comment[]
|
|
}[]
|
|
|
|
export const useAlgoFeed = (user: User | null | undefined) => {
|
|
const [feed, setFeed] = useState<feed>()
|
|
|
|
const getTime = useTimeSinceFirstRender()
|
|
|
|
useEffect(() => {
|
|
if (user) {
|
|
getUserFeed(user.id).then((feed) => {
|
|
if (feed.length === 0) {
|
|
getDefaultFeed().then((feed) => setFeed(feed))
|
|
} else setFeed(feed)
|
|
|
|
trackLatency('feed', getTime())
|
|
console.log('feed load time', getTime())
|
|
})
|
|
}
|
|
}, [user?.id])
|
|
|
|
return useUpdateFeed(feed)
|
|
}
|
|
|
|
const useUpdateFeed = (feed: feed | undefined) => {
|
|
const contracts = useUpdatedContracts(feed?.map((item) => item.contract))
|
|
|
|
return feed && contracts
|
|
? feed.map(({ contract, ...other }, i) => ({
|
|
...other,
|
|
contract: contracts[i],
|
|
}))
|
|
: undefined
|
|
}
|
|
|
|
const getDefaultFeed = async () => {
|
|
const contracts = await getTopWeeklyContracts()
|
|
const feed = await Promise.all(
|
|
contracts.map((c) => getRecentBetsAndComments(c))
|
|
)
|
|
return feed
|
|
}
|