2022-08-29 15:06:17 +00:00
|
|
|
import * as admin from 'firebase-admin'
|
|
|
|
|
|
|
|
import { getUser } from './utils'
|
|
|
|
import { slugify } from '../../common/util/slugify'
|
|
|
|
import { randomString } from '../../common/util/random'
|
|
|
|
import { Post, MAX_POST_TITLE_LENGTH } from '../../common/post'
|
|
|
|
import { APIError, newEndpoint, validate } from './api'
|
|
|
|
import { JSONContent } from '@tiptap/core'
|
|
|
|
import { z } from 'zod'
|
2022-09-27 22:30:07 +00:00
|
|
|
import { removeUndefinedProps } from '../../common/util/object'
|
|
|
|
import { createMarketHelper } from './create-market'
|
|
|
|
import { DAY_MS } from '../../common/util/time'
|
2022-08-29 15:06:17 +00:00
|
|
|
|
|
|
|
const contentSchema: z.ZodType<JSONContent> = z.lazy(() =>
|
|
|
|
z.intersection(
|
|
|
|
z.record(z.any()),
|
|
|
|
z.object({
|
|
|
|
type: z.string().optional(),
|
|
|
|
attrs: z.record(z.any()).optional(),
|
|
|
|
content: z.array(contentSchema).optional(),
|
|
|
|
marks: z
|
|
|
|
.array(
|
|
|
|
z.intersection(
|
|
|
|
z.record(z.any()),
|
|
|
|
z.object({
|
|
|
|
type: z.string(),
|
|
|
|
attrs: z.record(z.any()).optional(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.optional(),
|
|
|
|
text: z.string().optional(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
const postSchema = z.object({
|
|
|
|
title: z.string().min(1).max(MAX_POST_TITLE_LENGTH),
|
|
|
|
content: contentSchema,
|
2022-09-23 19:11:50 +00:00
|
|
|
groupId: z.string().optional(),
|
2022-09-27 22:30:07 +00:00
|
|
|
|
|
|
|
// Date doc fields:
|
|
|
|
bounty: z.number().optional(),
|
|
|
|
birthday: z.number().optional(),
|
|
|
|
type: z.string().optional(),
|
|
|
|
question: z.string().optional(),
|
2022-08-29 15:06:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
export const createpost = newEndpoint({}, async (req, auth) => {
|
|
|
|
const firestore = admin.firestore()
|
2022-09-27 22:30:07 +00:00
|
|
|
const { title, content, groupId, question, ...otherProps } = validate(
|
|
|
|
postSchema,
|
|
|
|
req.body
|
|
|
|
)
|
2022-08-29 15:06:17 +00:00
|
|
|
|
|
|
|
const creator = await getUser(auth.uid)
|
|
|
|
if (!creator)
|
|
|
|
throw new APIError(400, 'No user exists with the authenticated user ID.')
|
|
|
|
|
|
|
|
console.log('creating post owned by', creator.username, 'titled', title)
|
|
|
|
|
|
|
|
const slug = await getSlug(title)
|
|
|
|
|
|
|
|
const postRef = firestore.collection('posts').doc()
|
|
|
|
|
2022-09-27 22:30:07 +00:00
|
|
|
// If this is a date doc, create a market for it.
|
|
|
|
let contractSlug
|
|
|
|
if (question) {
|
|
|
|
const closeTime = Date.now() + DAY_MS * 30 * 3
|
|
|
|
|
|
|
|
const result = await createMarketHelper(
|
|
|
|
{
|
|
|
|
question,
|
|
|
|
closeTime,
|
|
|
|
outcomeType: 'BINARY',
|
|
|
|
visibility: 'unlisted',
|
|
|
|
initialProb: 50,
|
2022-09-27 23:10:35 +00:00
|
|
|
// Dating group!
|
|
|
|
groupId: 'j3ZE8fkeqiKmRGumy3O1',
|
2022-09-27 22:30:07 +00:00
|
|
|
},
|
|
|
|
auth
|
|
|
|
)
|
|
|
|
contractSlug = result.slug
|
|
|
|
}
|
|
|
|
|
|
|
|
const post: Post = removeUndefinedProps({
|
|
|
|
...otherProps,
|
2022-08-29 15:06:17 +00:00
|
|
|
id: postRef.id,
|
|
|
|
creatorId: creator.id,
|
|
|
|
slug,
|
|
|
|
title,
|
|
|
|
createdTime: Date.now(),
|
|
|
|
content: content,
|
2022-09-27 22:30:07 +00:00
|
|
|
contractSlug,
|
|
|
|
})
|
2022-08-29 15:06:17 +00:00
|
|
|
|
|
|
|
await postRef.create(post)
|
2022-09-23 19:11:50 +00:00
|
|
|
if (groupId) {
|
|
|
|
const groupRef = firestore.collection('groups').doc(groupId)
|
|
|
|
const group = await groupRef.get()
|
|
|
|
if (group.exists) {
|
|
|
|
const groupData = group.data()
|
|
|
|
if (groupData) {
|
|
|
|
const postIds = groupData.postIds ?? []
|
|
|
|
postIds.push(postRef.id)
|
|
|
|
await groupRef.update({ postIds })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-29 15:06:17 +00:00
|
|
|
|
|
|
|
return { status: 'success', post }
|
|
|
|
})
|
|
|
|
|
|
|
|
export const getSlug = async (title: string) => {
|
|
|
|
const proposedSlug = slugify(title)
|
|
|
|
|
|
|
|
const preexistingPost = await getPostFromSlug(proposedSlug)
|
|
|
|
|
|
|
|
return preexistingPost ? proposedSlug + '-' + randomString() : proposedSlug
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getPostFromSlug(slug: string) {
|
|
|
|
const firestore = admin.firestore()
|
|
|
|
const snap = await firestore
|
|
|
|
.collection('posts')
|
|
|
|
.where('slug', '==', slug)
|
|
|
|
.get()
|
|
|
|
|
|
|
|
return snap.empty ? undefined : (snap.docs[0].data() as Post)
|
|
|
|
}
|