2022-01-11 16:56:26 +00:00
|
|
|
import {
|
|
|
|
collection,
|
|
|
|
collectionGroup,
|
2022-05-05 22:30:30 +00:00
|
|
|
doc,
|
2022-01-11 16:56:26 +00:00
|
|
|
orderBy,
|
2022-05-05 22:30:30 +00:00
|
|
|
query,
|
|
|
|
setDoc,
|
|
|
|
where,
|
2022-01-11 16:56:26 +00:00
|
|
|
} from 'firebase/firestore'
|
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'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { User } from 'common/user'
|
|
|
|
import { Comment } from 'common/comment'
|
2022-05-11 21:11:46 +00:00
|
|
|
import { removeUndefinedProps } from 'common/util/object'
|
2022-06-15 03:00:36 +00:00
|
|
|
import { track } from '@amplitude/analytics-browser'
|
2022-08-06 20:39:52 +00:00
|
|
|
import { JSONContent } from '@tiptap/react'
|
2022-05-05 22:30:30 +00:00
|
|
|
|
2022-01-10 21:07:57 +00:00
|
|
|
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-06-22 16:35:50 +00:00
|
|
|
export async function createCommentOnContract(
|
2022-01-04 07:21:14 +00:00
|
|
|
contractId: string,
|
2022-08-06 20:39:52 +00:00
|
|
|
content: JSONContent,
|
2022-04-21 17:09:06 +00:00
|
|
|
commenter: User,
|
2022-05-03 20:38:40 +00:00
|
|
|
betId?: string,
|
2022-05-11 21:11:46 +00:00
|
|
|
answerOutcome?: string,
|
|
|
|
replyToCommentId?: string
|
2022-01-04 07:21:14 +00:00
|
|
|
) {
|
2022-04-21 17:09:06 +00:00
|
|
|
const ref = betId
|
|
|
|
? doc(getCommentsCollection(contractId), betId)
|
|
|
|
: doc(getCommentsCollection(contractId))
|
2022-05-11 21:11:46 +00:00
|
|
|
const comment: Comment = removeUndefinedProps({
|
2022-02-04 03:04:56 +00:00
|
|
|
id: ref.id,
|
2022-01-04 07:21:14 +00:00
|
|
|
contractId,
|
2022-02-04 03:04:56 +00:00
|
|
|
userId: commenter.id,
|
2022-08-06 20:39:52 +00:00
|
|
|
content: content,
|
2022-01-04 07:21:14 +00:00
|
|
|
createdTime: Date.now(),
|
|
|
|
userName: commenter.name,
|
|
|
|
userUsername: commenter.username,
|
|
|
|
userAvatarUrl: commenter.avatarUrl,
|
2022-05-11 21:11:46 +00:00
|
|
|
betId: betId,
|
|
|
|
answerOutcome: answerOutcome,
|
|
|
|
replyToCommentId: replyToCommentId,
|
|
|
|
})
|
2022-06-15 03:00:36 +00:00
|
|
|
track('comment', {
|
|
|
|
contractId,
|
|
|
|
commentId: ref.id,
|
|
|
|
betId: betId,
|
|
|
|
replyToCommentId: replyToCommentId,
|
|
|
|
})
|
2022-02-04 03:04:56 +00:00
|
|
|
return await setDoc(ref, comment)
|
2022-01-04 07:21:14 +00:00
|
|
|
}
|
2022-06-22 16:35:50 +00:00
|
|
|
export async function createCommentOnGroup(
|
|
|
|
groupId: string,
|
2022-08-06 20:39:52 +00:00
|
|
|
content: JSONContent,
|
2022-06-22 16:35:50 +00:00
|
|
|
user: User,
|
|
|
|
replyToCommentId?: string
|
|
|
|
) {
|
|
|
|
const ref = doc(getCommentsOnGroupCollection(groupId))
|
|
|
|
const comment: Comment = removeUndefinedProps({
|
|
|
|
id: ref.id,
|
|
|
|
groupId,
|
|
|
|
userId: user.id,
|
2022-08-06 20:39:52 +00:00
|
|
|
content: content,
|
2022-06-22 16:35:50 +00:00
|
|
|
createdTime: Date.now(),
|
|
|
|
userName: user.name,
|
|
|
|
userUsername: user.username,
|
|
|
|
userAvatarUrl: user.avatarUrl,
|
|
|
|
replyToCommentId: replyToCommentId,
|
|
|
|
})
|
|
|
|
track('group message', {
|
|
|
|
user,
|
|
|
|
commentId: ref.id,
|
|
|
|
groupId,
|
|
|
|
replyToCommentId: replyToCommentId,
|
|
|
|
})
|
|
|
|
return await setDoc(ref, comment)
|
|
|
|
}
|
2022-01-04 07:21:14 +00:00
|
|
|
|
|
|
|
function getCommentsCollection(contractId: string) {
|
|
|
|
return collection(db, 'contracts', contractId, 'comments')
|
|
|
|
}
|
2022-08-03 21:38:35 +00:00
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
function getCommentsOnGroupCollection(groupId: string) {
|
|
|
|
return collection(db, 'groups', groupId, 'comments')
|
|
|
|
}
|
2022-01-04 07:21:14 +00:00
|
|
|
|
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-08-03 21:38:35 +00:00
|
|
|
export async function listAllCommentsOnGroup(groupId: string) {
|
|
|
|
const comments = await getValues<Comment>(
|
|
|
|
getCommentsOnGroupCollection(groupId)
|
|
|
|
)
|
|
|
|
comments.sort((c1, c2) => c1.createdTime - c2.createdTime)
|
|
|
|
return comments
|
|
|
|
}
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
export function listenForCommentsOnContract(
|
2022-01-04 07:21:14 +00:00
|
|
|
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
|
|
|
}
|
2022-06-22 16:35:50 +00:00
|
|
|
export function listenForCommentsOnGroup(
|
|
|
|
groupId: string,
|
|
|
|
setComments: (comments: Comment[]) => void
|
|
|
|
) {
|
|
|
|
return listenForValues<Comment>(
|
|
|
|
getCommentsOnGroupCollection(groupId),
|
|
|
|
(comments) => {
|
|
|
|
comments.sort((c1, c2) => c1.createdTime - c2.createdTime)
|
|
|
|
setComments(comments)
|
2022-04-21 17:09:06 +00:00
|
|
|
}
|
2022-06-22 16:35:50 +00:00
|
|
|
)
|
2022-01-04 07:21:14 +00:00
|
|
|
}
|
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
|
|
|
|
2022-05-05 22:30:30 +00:00
|
|
|
const getUsersCommentsQuery = (userId: string) =>
|
|
|
|
query(
|
|
|
|
collectionGroup(db, 'comments'),
|
|
|
|
where('userId', '==', userId),
|
|
|
|
orderBy('createdTime', 'desc')
|
|
|
|
)
|
|
|
|
export async function getUsersComments(userId: string) {
|
|
|
|
return await getValues<Comment>(getUsersCommentsQuery(userId))
|
|
|
|
}
|