2022-06-08 14:43:24 +00:00
|
|
|
import * as functions from 'firebase-functions'
|
2022-08-17 22:36:52 +00:00
|
|
|
|
2022-10-10 20:32:29 +00:00
|
|
|
import { getUser, getValues } from './utils'
|
|
|
|
import {
|
|
|
|
createBadgeAwardedNotification,
|
|
|
|
createNewContractNotification,
|
|
|
|
} from './create-notification'
|
2022-06-08 14:43:24 +00:00
|
|
|
import { Contract } from '../../common/contract'
|
2022-08-04 22:35:55 +00:00
|
|
|
import { parseMentions, richTextToString } from '../../common/util/parse'
|
Rich content (#620)
* Add TipTap editor and renderer components
* Change market description editor to rich text
* Type description as JSON, fix string-based logic
- Delete make-predictions.tsx
- Delete feed logic that showed descriptions
* wip Fix API validation
* fix type error
* fix extension import (backend)
In firebase, typescript compiles imports into common js imports
like `const StarterKit = require("@tiptap/starter-kit")`
Even though StarterKit is exported from the cjs file, it gets imported
as undefined. But it magically works if we import *
If you're reading this in the future, consider replacing StarterKit with
the entire list of extensions.
* Stop load on fail create market, improve warning
* Refactor editor as hook / fix infinite submit bug
Move state of editor back up to parent
We have to do this later anyways to allow parent to edit
* Add images - display, paste + uploading
* add uploading state of image
* Fix placeholder, misc styling
min height, quote
* Fix appending to description
* code review fixes: rename, refactor, chop carets
* Add hint & upload button on new lines
- bump to Tailwind 3.1 for arbitrary variants
* clean up, run prettier
* rename FileButton to FileUploadButton
* add image extension as functions dependency
2022-07-13 18:58:22 +00:00
|
|
|
import { JSONContent } from '@tiptap/core'
|
2022-08-24 16:49:53 +00:00
|
|
|
import { addUserToContractFollowers } from './follow-market'
|
2022-10-10 20:32:29 +00:00
|
|
|
import { User } from '../../common/user'
|
|
|
|
import * as admin from 'firebase-admin'
|
|
|
|
import {
|
|
|
|
MarketCreatorBadge,
|
|
|
|
marketCreatorBadgeRarityThresholds,
|
|
|
|
} from '../../common/badge'
|
2022-06-08 14:43:24 +00:00
|
|
|
|
2022-08-17 22:36:52 +00:00
|
|
|
export const onCreateContract = functions
|
|
|
|
.runWith({ secrets: ['MAILGUN_KEY'] })
|
|
|
|
.firestore.document('contracts/{contractId}')
|
2022-06-08 14:43:24 +00:00
|
|
|
.onCreate(async (snapshot, context) => {
|
|
|
|
const contract = snapshot.data() as Contract
|
|
|
|
const { eventId } = context
|
|
|
|
|
|
|
|
const contractCreator = await getUser(contract.creatorId)
|
|
|
|
if (!contractCreator) throw new Error('Could not find contract creator')
|
|
|
|
|
2022-08-04 22:35:55 +00:00
|
|
|
const desc = contract.description as JSONContent
|
|
|
|
const mentioned = parseMentions(desc)
|
2022-08-24 16:49:53 +00:00
|
|
|
await addUserToContractFollowers(contract.id, contractCreator.id)
|
2022-08-04 22:35:55 +00:00
|
|
|
|
2022-09-13 15:54:51 +00:00
|
|
|
await createNewContractNotification(
|
2022-06-08 14:43:24 +00:00
|
|
|
contractCreator,
|
2022-09-13 15:54:51 +00:00
|
|
|
contract,
|
2022-06-08 14:43:24 +00:00
|
|
|
eventId,
|
2022-08-04 22:35:55 +00:00
|
|
|
richTextToString(desc),
|
2022-09-13 15:54:51 +00:00
|
|
|
mentioned
|
2022-06-08 14:43:24 +00:00
|
|
|
)
|
2022-10-10 20:32:29 +00:00
|
|
|
await handleMarketCreatorBadgeAward(contractCreator)
|
2022-06-08 14:43:24 +00:00
|
|
|
})
|
2022-10-10 20:32:29 +00:00
|
|
|
|
|
|
|
const firestore = admin.firestore()
|
|
|
|
|
|
|
|
async function handleMarketCreatorBadgeAward(contractCreator: User) {
|
|
|
|
// get all contracts by user and calculate size of array
|
|
|
|
const contracts = await getValues<Contract>(
|
|
|
|
firestore
|
|
|
|
.collection(`contracts`)
|
|
|
|
.where('creatorId', '==', contractCreator.id)
|
|
|
|
.where('resolution', '!=', 'CANCEL')
|
|
|
|
)
|
2022-10-11 14:35:59 +00:00
|
|
|
if (marketCreatorBadgeRarityThresholds.includes(contracts.length)) {
|
2022-10-10 20:32:29 +00:00
|
|
|
const badge = {
|
|
|
|
type: 'MARKET_CREATOR',
|
|
|
|
name: 'Market Creator',
|
|
|
|
data: {
|
|
|
|
totalContractsCreated: contracts.length,
|
|
|
|
},
|
|
|
|
createdTime: Date.now(),
|
|
|
|
} as MarketCreatorBadge
|
|
|
|
// update user
|
|
|
|
await firestore
|
|
|
|
.collection('users')
|
|
|
|
.doc(contractCreator.id)
|
|
|
|
.update({
|
|
|
|
achievements: {
|
|
|
|
...contractCreator.achievements,
|
|
|
|
marketCreator: {
|
|
|
|
badges: [
|
|
|
|
...(contractCreator.achievements?.marketCreator?.badges ?? []),
|
|
|
|
badge,
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
await createBadgeAwardedNotification(contractCreator, badge)
|
|
|
|
}
|
|
|
|
}
|