2022-05-22 08:36:05 +00:00
|
|
|
import { groupBy, mapValues, maxBy, sortBy } from 'lodash'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { Contract } from 'web/lib/firebase/contracts'
|
2022-08-19 08:06:40 +00:00
|
|
|
import { ContractComment } from 'common/comment'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { Bet } from 'common/bet'
|
2022-03-14 20:29:32 +00:00
|
|
|
|
|
|
|
const MAX_ACTIVE_CONTRACTS = 75
|
|
|
|
|
|
|
|
// This does NOT include comment times, since those aren't part of the contract atm.
|
|
|
|
// TODO: Maybe store last activity time directly in the contract?
|
|
|
|
// Pros: simplifies this code; cons: harder to tweak "activity" definition later
|
|
|
|
function lastActivityTime(contract: Contract) {
|
2022-05-01 16:36:54 +00:00
|
|
|
return Math.max(contract.resolutionTime || 0, contract.createdTime)
|
2022-03-14 20:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Types of activity to surface:
|
|
|
|
// - Comment on a market
|
|
|
|
// - New market created
|
|
|
|
// - Market resolved
|
|
|
|
// - Bet on market
|
|
|
|
export function findActiveContracts(
|
|
|
|
allContracts: Contract[],
|
2022-08-19 08:06:40 +00:00
|
|
|
recentComments: ContractComment[],
|
2022-03-17 07:29:08 +00:00
|
|
|
recentBets: Bet[],
|
|
|
|
seenContracts: { [contractId: string]: number }
|
2022-03-14 20:29:32 +00:00
|
|
|
) {
|
|
|
|
const idToActivityTime = new Map<string, number>()
|
|
|
|
function record(contractId: string, time: number) {
|
|
|
|
// Only record if the time is newer
|
|
|
|
const oldTime = idToActivityTime.get(contractId)
|
|
|
|
idToActivityTime.set(contractId, Math.max(oldTime ?? 0, time))
|
|
|
|
}
|
|
|
|
|
|
|
|
const contractsById = new Map(allContracts.map((c) => [c.id, c]))
|
|
|
|
|
|
|
|
// Record contract activity.
|
|
|
|
for (const contract of allContracts) {
|
|
|
|
record(contract.id, lastActivityTime(contract))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add every contract that had a recent comment, too
|
|
|
|
for (const comment of recentComments) {
|
2022-06-22 16:35:50 +00:00
|
|
|
if (comment.contractId) {
|
|
|
|
const contract = contractsById.get(comment.contractId)
|
|
|
|
if (contract) record(contract.id, comment.createdTime)
|
|
|
|
}
|
2022-03-14 20:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add contracts by last bet time.
|
2022-05-22 08:36:05 +00:00
|
|
|
const contractBets = groupBy(recentBets, (bet) => bet.contractId)
|
|
|
|
const contractMostRecentBet = mapValues(
|
2022-03-14 20:29:32 +00:00
|
|
|
contractBets,
|
2022-05-22 08:36:05 +00:00
|
|
|
(bets) => maxBy(bets, (bet) => bet.createdTime) as Bet
|
2022-03-14 20:29:32 +00:00
|
|
|
)
|
|
|
|
for (const bet of Object.values(contractMostRecentBet)) {
|
|
|
|
const contract = contractsById.get(bet.contractId)
|
|
|
|
if (contract) record(contract.id, bet.createdTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
let activeContracts = allContracts.filter(
|
2022-04-09 23:10:58 +00:00
|
|
|
(contract) =>
|
|
|
|
contract.visibility === 'public' &&
|
|
|
|
!contract.isResolved &&
|
|
|
|
(contract.closeTime ?? Infinity) > Date.now()
|
2022-03-14 20:29:32 +00:00
|
|
|
)
|
2022-05-22 08:36:05 +00:00
|
|
|
activeContracts = sortBy(
|
2022-03-14 20:29:32 +00:00
|
|
|
activeContracts,
|
|
|
|
(c) => -(idToActivityTime.get(c.id) ?? 0)
|
|
|
|
)
|
2022-03-17 07:29:08 +00:00
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const contractComments = groupBy(
|
2022-03-17 07:29:08 +00:00
|
|
|
recentComments,
|
|
|
|
(comment) => comment.contractId
|
|
|
|
)
|
2022-05-22 08:36:05 +00:00
|
|
|
const contractMostRecentComment = mapValues(
|
2022-03-17 07:29:08 +00:00
|
|
|
contractComments,
|
2022-08-19 08:06:40 +00:00
|
|
|
(comments) => maxBy(comments, (c) => c.createdTime) as ContractComment
|
2022-03-17 07:29:08 +00:00
|
|
|
)
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const prioritizedContracts = sortBy(activeContracts, (c) => {
|
2022-03-17 07:29:08 +00:00
|
|
|
const seenTime = seenContracts[c.id]
|
|
|
|
if (!seenTime) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
const lastCommentTime = contractMostRecentComment[c.id]?.createdTime
|
|
|
|
if (lastCommentTime && lastCommentTime > seenTime) {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
const lastBetTime = contractMostRecentBet[c.id]?.createdTime
|
|
|
|
if (lastBetTime && lastBetTime > seenTime) {
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
|
|
|
return seenTime
|
|
|
|
})
|
|
|
|
|
|
|
|
return prioritizedContracts.slice(0, MAX_ACTIVE_CONTRACTS)
|
2022-03-14 20:29:32 +00:00
|
|
|
}
|