Merge branch 'main' into show-comments-on-profile

This commit is contained in:
Ian Philips 2022-05-05 09:59:05 -04:00
commit 9d8b27f42d
8 changed files with 180 additions and 12 deletions

View File

@ -9,7 +9,7 @@ export type Answer = {
userId: string userId: string
username: string username: string
name: string name: string
avatarUrl?: string avatarUrl: string
text: string text: string
} }

View File

@ -13,5 +13,5 @@ export type Comment = {
// Denormalized, for rendering comments // Denormalized, for rendering comments
userName: string userName: string
userUsername: string userUsername: string
userAvatarUrl?: string userAvatarUrl: string
} }

View File

@ -11,7 +11,7 @@ export type FullContract<
creatorId: string creatorId: string
creatorName: string creatorName: string
creatorUsername: string creatorUsername: string
creatorAvatarUrl?: string // Start requiring after 2022-03-01 creatorAvatarUrl: string
question: string question: string
description: string // More info about what the contract is about description: string // More info about what the contract is about

View File

@ -4,7 +4,7 @@ export type User = {
name: string name: string
username: string username: string
avatarUrl?: string avatarUrl: string
// For their user page // For their user page
bio?: string bio?: string

View File

@ -0,0 +1,125 @@
// Script for lining up users and contracts/comments to make sure the denormalized avatar URLs in the contracts and
// comments match the user avatar URLs.
import * as admin from 'firebase-admin'
import { initAdmin } from './script-init'
import {
DocumentCorrespondence,
findDiffs,
describeDiff,
applyDiff,
} from './denormalize'
import { DocumentSnapshot, Transaction } from 'firebase-admin/firestore'
initAdmin()
const firestore = admin.firestore()
async function getUsersById(transaction: Transaction) {
const results = new Map<string, DocumentSnapshot>()
const users = await transaction.get(firestore.collection('users'))
users.forEach((doc) => {
results.set(doc.get('id'), doc)
})
console.log(`Found ${results.size} unique users.`)
return results
}
async function getContractsByUserId(transaction: Transaction) {
let n = 0
const results = new Map<string, DocumentSnapshot[]>()
const contracts = await transaction.get(firestore.collection('contracts'))
contracts.forEach((doc) => {
const creatorId = doc.get('creatorId')
const creatorContracts = results.get(creatorId) || []
creatorContracts.push(doc)
results.set(creatorId, creatorContracts)
n++
})
console.log(`Found ${n} contracts from ${results.size} unique users.`)
return results
}
async function getCommentsByUserId(transaction: Transaction) {
let n = 0
const results = new Map<string, DocumentSnapshot[]>()
const comments = await transaction.get(firestore.collectionGroup('comments'))
comments.forEach((doc) => {
const userId = doc.get('userId')
const userComments = results.get(userId) || []
userComments.push(doc)
results.set(userId, userComments)
n++
})
console.log(`Found ${n} comments from ${results.size} unique users.`)
return results
}
async function getAnswersByUserId(transaction: Transaction) {
let n = 0
const results = new Map<string, DocumentSnapshot[]>()
const answers = await transaction.get(firestore.collectionGroup('answers'))
answers.forEach((doc) => {
const userId = doc.get('userId')
const userAnswers = results.get(userId) || []
userAnswers.push(doc)
results.set(userId, userAnswers)
n++
})
console.log(`Found ${n} answers from ${results.size} unique users.`)
return results
}
if (require.main === module) {
admin.firestore().runTransaction(async (transaction) => {
const [usersById, contractsByUserId, commentsByUserId, answersByUserId] =
await Promise.all([
getUsersById(transaction),
getContractsByUserId(transaction),
getCommentsByUserId(transaction),
getAnswersByUserId(transaction),
])
const usersContracts = Array.from(
usersById.entries(),
([id, doc]): DocumentCorrespondence => {
return [doc, contractsByUserId.get(id) || []]
}
)
const contractDiffs = findDiffs(
usersContracts,
'avatarUrl',
'creatorAvatarUrl'
)
console.log(`Found ${contractDiffs.length} contracts with mismatches.`)
contractDiffs.forEach((d) => {
console.log(describeDiff(d))
applyDiff(transaction, d)
})
const usersComments = Array.from(
usersById.entries(),
([id, doc]): DocumentCorrespondence => {
return [doc, commentsByUserId.get(id) || []]
}
)
const commentDiffs = findDiffs(usersComments, 'avatarUrl', 'userAvatarUrl')
console.log(`Found ${commentDiffs.length} comments with mismatches.`)
commentDiffs.forEach((d) => {
console.log(describeDiff(d))
applyDiff(transaction, d)
})
const usersAnswers = Array.from(
usersById.entries(),
([id, doc]): DocumentCorrespondence => {
return [doc, answersByUserId.get(id) || []]
}
)
const answerDiffs = findDiffs(usersAnswers, 'avatarUrl', 'avatarUrl')
console.log(`Found ${answerDiffs.length} answers with mismatches.`)
answerDiffs.forEach((d) => {
console.log(describeDiff(d))
applyDiff(transaction, d)
})
})
}

View File

@ -0,0 +1,48 @@
// Helper functions for maintaining the relationship between fields in one set of documents and denormalized copies in
// another set of documents.
import { DocumentSnapshot, Transaction } from 'firebase-admin/firestore'
export type DocumentValue = {
doc: DocumentSnapshot
field: string
val: any
}
export type DocumentCorrespondence = [DocumentSnapshot, DocumentSnapshot[]]
export type DocumentDiff = {
src: DocumentValue
dest: DocumentValue
}
export function findDiffs(
docs: DocumentCorrespondence[],
srcPath: string,
destPath: string
) {
const diffs: DocumentDiff[] = []
for (let [srcDoc, destDocs] of docs) {
const srcVal = srcDoc.get(srcPath)
for (let destDoc of destDocs) {
const destVal = destDoc.get(destPath)
if (destVal !== srcVal) {
diffs.push({
src: { doc: srcDoc, field: srcPath, val: srcVal },
dest: { doc: destDoc, field: destPath, val: destVal },
})
}
}
}
return diffs
}
export function describeDiff(diff: DocumentDiff) {
function describeDocVal(x: DocumentValue): string {
return `${x.doc.ref.path}.${x.field}: ${x.val}`
}
return `${describeDocVal(diff.src)} -> ${describeDocVal(diff.dest)}`
}
export function applyDiff(transaction: Transaction, diff: DocumentDiff) {
const { src, dest } = diff
transaction.update(dest.doc.ref, dest.field, src.val)
}

View File

@ -6,7 +6,7 @@ export type OgCardProps = {
metadata: string metadata: string
creatorName: string creatorName: string
creatorUsername: string creatorUsername: string
creatorAvatarUrl?: string creatorAvatarUrl: string
} }
function buildCardUrl(props: OgCardProps) { function buildCardUrl(props: OgCardProps) {
@ -14,11 +14,6 @@ function buildCardUrl(props: OgCardProps) {
props.probability === undefined props.probability === undefined
? '' ? ''
: `&probability=${encodeURIComponent(props.probability ?? '')}` : `&probability=${encodeURIComponent(props.probability ?? '')}`
const creatorAvatarUrlParam =
props.creatorAvatarUrl === undefined
? ''
: `&creatorAvatarUrl=${encodeURIComponent(props.creatorAvatarUrl ?? '')}`
// URL encode each of the props, then add them as query params // URL encode each of the props, then add them as query params
return ( return (
`https://manifold-og-image.vercel.app/m.png` + `https://manifold-og-image.vercel.app/m.png` +
@ -26,7 +21,7 @@ function buildCardUrl(props: OgCardProps) {
probabilityParam + probabilityParam +
`&metadata=${encodeURIComponent(props.metadata)}` + `&metadata=${encodeURIComponent(props.metadata)}` +
`&creatorName=${encodeURIComponent(props.creatorName)}` + `&creatorName=${encodeURIComponent(props.creatorName)}` +
creatorAvatarUrlParam + `&creatorAvatarUrl=${encodeURIComponent(props.creatorAvatarUrl)}` +
`&creatorUsername=${encodeURIComponent(props.creatorUsername)}` `&creatorUsername=${encodeURIComponent(props.creatorUsername)}`
) )
} }

View File

@ -12,7 +12,7 @@ export type LiteMarket = {
creatorUsername: string creatorUsername: string
creatorName: string creatorName: string
createdTime: number createdTime: number
creatorAvatarUrl?: string creatorAvatarUrl: string
// Market attributes. All times are in milliseconds since epoch // Market attributes. All times are in milliseconds since epoch
closeTime?: number closeTime?: number