manifold/web/lib/firebase/comments.ts
Austin Chen 5b431226d4
Show all recent activity on a central feed (#24)
* Tracks all market activity on a single page

* Support both global and per-contract feeds

* UI tweaks

* Include contract description in activity feed

* Show activity feed on Create page
2022-01-11 11:56:26 -05:00

82 lines
1.9 KiB
TypeScript

import {
doc,
collection,
onSnapshot,
setDoc,
query,
collectionGroup,
getDocs,
where,
orderBy,
} from 'firebase/firestore'
import { listenForValues } from './utils'
import { db } from './init'
import { User } from '../../../common/user'
import { Comment } from '../../../common/comment'
export type { Comment }
export async function createComment(
contractId: string,
betId: string,
text: string,
commenter: User
) {
const ref = doc(getCommentsCollection(contractId), betId)
return await setDoc(ref, {
contractId,
betId,
text,
createdTime: Date.now(),
userName: commenter.name,
userUsername: commenter.username,
userAvatarUrl: commenter.avatarUrl,
})
}
function getCommentsCollection(contractId: string) {
return collection(db, 'contracts', contractId, 'comments')
}
export function listenForComments(
contractId: string,
setComments: (comments: Comment[]) => void
) {
return onSnapshot(getCommentsCollection(contractId), (snap) => {
const comments = snap.docs.map((doc) => doc.data() as Comment)
comments.sort((c1, c2) => c1.createdTime - c2.createdTime)
setComments(comments)
})
}
// Return a map of betId -> comment
export function mapCommentsByBetId(comments: Comment[]) {
const map: Record<string, Comment> = {}
for (const comment of comments) {
map[comment.betId] = comment
}
return map
}
const DAY_IN_MS = 24 * 60 * 60 * 1000
// Define "recent" as "<3 days ago" for now
const recentCommentsQuery = query(
collectionGroup(db, 'comments'),
where('createdTime', '>', Date.now() - 3 * DAY_IN_MS),
orderBy('createdTime', 'desc')
)
export async function getRecentComments() {
const snapshot = await getDocs(recentCommentsQuery)
const comments = snapshot.docs.map((doc) => doc.data() as Comment)
return comments
}
export function listenForRecentComments(
setComments: (comments: Comment[]) => void
) {
return listenForValues<Comment>(recentCommentsQuery, setComments)
}