2022-01-11 16:56:26 +00:00
|
|
|
import {
|
|
|
|
doc,
|
|
|
|
collection,
|
|
|
|
setDoc,
|
|
|
|
query,
|
|
|
|
collectionGroup,
|
|
|
|
where,
|
|
|
|
orderBy,
|
|
|
|
} from 'firebase/firestore'
|
2022-03-03 20:59:12 +00:00
|
|
|
import _ from 'lodash'
|
2022-02-04 03:04:56 +00:00
|
|
|
|
2022-01-15 00:16:25 +00:00
|
|
|
import { getValues, listenForValues } from './utils'
|
2022-01-10 21:07:57 +00:00
|
|
|
import { db } from './init'
|
|
|
|
import { User } from '../../../common/user'
|
|
|
|
import { Comment } from '../../../common/comment'
|
|
|
|
export type { Comment }
|
2022-01-04 07:21:14 +00:00
|
|
|
|
2022-03-02 22:09:53 +00:00
|
|
|
export const MAX_COMMENT_LENGTH = 10000
|
|
|
|
|
2022-01-04 07:21:14 +00:00
|
|
|
export async function createComment(
|
|
|
|
contractId: string,
|
|
|
|
betId: string,
|
|
|
|
text: string,
|
|
|
|
commenter: User
|
|
|
|
) {
|
|
|
|
const ref = doc(getCommentsCollection(contractId), betId)
|
2022-02-04 03:04:56 +00:00
|
|
|
|
|
|
|
const comment: Comment = {
|
|
|
|
id: ref.id,
|
2022-01-04 07:21:14 +00:00
|
|
|
contractId,
|
|
|
|
betId,
|
2022-02-04 03:04:56 +00:00
|
|
|
userId: commenter.id,
|
2022-03-02 22:09:53 +00:00
|
|
|
text: text.slice(0, MAX_COMMENT_LENGTH),
|
2022-01-04 07:21:14 +00:00
|
|
|
createdTime: Date.now(),
|
|
|
|
userName: commenter.name,
|
|
|
|
userUsername: commenter.username,
|
|
|
|
userAvatarUrl: commenter.avatarUrl,
|
2022-02-04 03:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return await setDoc(ref, comment)
|
2022-01-04 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getCommentsCollection(contractId: string) {
|
|
|
|
return collection(db, 'contracts', contractId, 'comments')
|
|
|
|
}
|
|
|
|
|
2022-01-15 00:16:25 +00:00
|
|
|
export async function listAllComments(contractId: string) {
|
|
|
|
const comments = await getValues<Comment>(getCommentsCollection(contractId))
|
|
|
|
comments.sort((c1, c2) => c1.createdTime - c2.createdTime)
|
|
|
|
return comments
|
|
|
|
}
|
|
|
|
|
2022-01-04 07:21:14 +00:00
|
|
|
export function listenForComments(
|
|
|
|
contractId: string,
|
|
|
|
setComments: (comments: Comment[]) => void
|
|
|
|
) {
|
2022-02-04 00:13:04 +00:00
|
|
|
return listenForValues<Comment>(
|
|
|
|
getCommentsCollection(contractId),
|
|
|
|
(comments) => {
|
|
|
|
comments.sort((c1, c2) => c1.createdTime - c2.createdTime)
|
|
|
|
setComments(comments)
|
|
|
|
}
|
|
|
|
)
|
2022-01-04 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2022-01-11 16:56:26 +00:00
|
|
|
|
|
|
|
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() {
|
2022-01-21 23:21:46 +00:00
|
|
|
return getValues<Comment>(recentCommentsQuery)
|
2022-01-11 16:56:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function listenForRecentComments(
|
|
|
|
setComments: (comments: Comment[]) => void
|
|
|
|
) {
|
|
|
|
return listenForValues<Comment>(recentCommentsQuery, setComments)
|
|
|
|
}
|
2022-03-03 20:59:12 +00:00
|
|
|
|
|
|
|
const getCommentsQuery = (startTime: number, endTime: number) =>
|
|
|
|
query(
|
|
|
|
collectionGroup(db, 'comments'),
|
|
|
|
where('createdTime', '>=', startTime),
|
|
|
|
where('createdTime', '<', endTime),
|
|
|
|
orderBy('createdTime', 'asc')
|
|
|
|
)
|
|
|
|
|
|
|
|
export async function getDailyComments(
|
|
|
|
startTime: number,
|
|
|
|
numberOfDays: number
|
|
|
|
) {
|
|
|
|
const query = getCommentsQuery(
|
|
|
|
startTime,
|
|
|
|
startTime + DAY_IN_MS * numberOfDays
|
|
|
|
)
|
|
|
|
const comments = await getValues<Comment>(query)
|
|
|
|
|
|
|
|
const commentsByDay = _.range(0, numberOfDays).map(() => [] as Comment[])
|
|
|
|
for (const comment of comments) {
|
|
|
|
const dayIndex = Math.floor((comment.createdTime - startTime) / DAY_IN_MS)
|
|
|
|
commentsByDay[dayIndex].push(comment)
|
|
|
|
}
|
|
|
|
|
|
|
|
return commentsByDay
|
|
|
|
}
|