diff --git a/functions/src/scripts/update-feed.ts b/functions/src/scripts/update-feed.ts new file mode 100644 index 00000000..e748e137 --- /dev/null +++ b/functions/src/scripts/update-feed.ts @@ -0,0 +1,38 @@ +import * as admin from 'firebase-admin' +import * as _ from 'lodash' + +import { initAdmin } from './script-init' +initAdmin() + +import { getValues } from '../utils' +import { User } from '../../../common/user' +import { batchedWaitAll } from '../../../common/util/promise' +import { Contract } from '../../../common/contract' +import { updateUserRecommendations } from '../update-recommendations' +import { + getFeedContracts, + updateFeed as updateUserFeed, +} from '../update-user-feed' + +const firestore = admin.firestore() + +async function updateFeed() { + console.log('Updating feed') + + const contracts = await getValues(firestore.collection('contracts')) + const feedContracts = await getFeedContracts() + const users = await getValues(firestore.collection('users')) + + await batchedWaitAll( + users.map((user) => async () => { + console.log('Updating recs for', user.username) + await updateUserRecommendations(user, contracts) + console.log('Updating feed for', user.username) + await updateUserFeed(user, feedContracts) + }) + ) +} + +if (require.main === module) { + updateFeed().then(() => process.exit()) +} diff --git a/functions/src/update-user-feed.ts b/functions/src/update-user-feed.ts index cfb0510a..7764d633 100644 --- a/functions/src/update-user-feed.ts +++ b/functions/src/update-user-feed.ts @@ -23,34 +23,39 @@ const MAX_FEED_CONTRACTS = 60 export const updateUserFeed = functions.pubsub .schedule('every 60 minutes') .onRun(async () => { - // Get contracts bet on or created in last week. - const contracts = await Promise.all([ - getValues( - firestore - .collection('contracts') - .where('isResolved', '==', false) - .where('volume7Days', '>', 0) - ), - - getValues( - firestore - .collection('contracts') - .where('isResolved', '==', false) - .where('createdTime', '>', Date.now() - DAY_MS * 7) - .where('volume7Days', '==', 0) - ), - ]).then(([activeContracts, inactiveContracts]) => { - const combined = [...activeContracts, ...inactiveContracts] - // Remove closed contracts. - return combined.filter((c) => (c.closeTime ?? Infinity) > Date.now()) - }) - + const contracts = await getFeedContracts() const users = await getValues(firestore.collection('users')) await batchedWaitAll(users.map((user) => () => updateFeed(user, contracts))) }) -const updateFeed = async (user: User, contracts: Contract[]) => { +export async function getFeedContracts() { + // Get contracts bet on or created in last week. + const contracts = await Promise.all([ + getValues( + firestore + .collection('contracts') + .where('isResolved', '==', false) + .where('volume7Days', '>', 0) + ), + + getValues( + firestore + .collection('contracts') + .where('isResolved', '==', false) + .where('createdTime', '>', Date.now() - DAY_MS * 7) + .where('volume7Days', '==', 0) + ), + ]).then(([activeContracts, inactiveContracts]) => { + const combined = [...activeContracts, ...inactiveContracts] + // Remove closed contracts. + return combined.filter((c) => (c.closeTime ?? Infinity) > Date.now()) + }) + + return contracts +} + +export const updateFeed = async (user: User, contracts: Contract[]) => { const userCacheCollection = firestore.collection( `private-users/${user.id}/cache` ) @@ -95,7 +100,7 @@ const updateFeed = async (user: User, contracts: Contract[]) => { feedContracts.map((contract) => getRecentBetsAndComments(contract)) ) - await userCacheCollection.doc('feed').set(feed) + await userCacheCollection.doc('feed').set({ feed }) } function scoreContract(