f26ba1c4a2
* Award badges for market creation, betting streaks, proven correct * Styling * Add minimum unique bettors for proven correct * Add name, refactor * Add notifications for badge awards * Correct styling * Need at least 3 unique bettors for market maker badge * Lint * Switch to badges_awarded * Don't include n/a resolutions in market creator badge * Add badges by rarities to profile * Show badges on profile, soon on market page * Add achievements to new user * Backfill all users badges
128 lines
3.8 KiB
TypeScript
128 lines
3.8 KiB
TypeScript
import * as admin from 'firebase-admin'
|
|
import { z } from 'zod'
|
|
|
|
import { PrivateUser, User } from '../../common/user'
|
|
import { getUser, getUserByUsername, getValues } from './utils'
|
|
import { randomString } from '../../common/util/random'
|
|
import {
|
|
cleanDisplayName,
|
|
cleanUsername,
|
|
} from '../../common/util/clean-username'
|
|
import { isWhitelisted } from '../../common/envs/constants'
|
|
import {
|
|
CATEGORIES_GROUP_SLUG_POSTFIX,
|
|
DEFAULT_CATEGORIES,
|
|
} from '../../common/categories'
|
|
|
|
import { track } from './analytics'
|
|
import { APIError, newEndpoint, validate } from './api'
|
|
import { Group } from '../../common/group'
|
|
import { SUS_STARTING_BALANCE, STARTING_BALANCE } from '../../common/economy'
|
|
import { getDefaultNotificationPreferences } from '../../common/user-notification-preferences'
|
|
|
|
const bodySchema = z.object({
|
|
deviceToken: z.string().optional(),
|
|
})
|
|
|
|
const opts = { secrets: ['MAILGUN_KEY'] }
|
|
|
|
export const createuser = newEndpoint(opts, async (req, auth) => {
|
|
const { deviceToken } = validate(bodySchema, req.body)
|
|
const preexistingUser = await getUser(auth.uid)
|
|
if (preexistingUser)
|
|
throw new APIError(400, 'User already exists', { user: preexistingUser })
|
|
|
|
const fbUser = await admin.auth().getUser(auth.uid)
|
|
|
|
const email = fbUser.email
|
|
if (!isWhitelisted(email)) {
|
|
throw new APIError(400, `${email} is not whitelisted`)
|
|
}
|
|
const emailName = email?.replace(/@.*$/, '')
|
|
|
|
const rawName = fbUser.displayName || emailName || 'User' + randomString(4)
|
|
const name = cleanDisplayName(rawName)
|
|
let username = cleanUsername(name)
|
|
|
|
const sameNameUser = await getUserByUsername(username)
|
|
if (sameNameUser) {
|
|
username += randomString(4)
|
|
}
|
|
|
|
const avatarUrl = fbUser.photoURL
|
|
const deviceUsedBefore =
|
|
!deviceToken || (await isPrivateUserWithDeviceToken(deviceToken))
|
|
|
|
const balance = deviceUsedBefore ? SUS_STARTING_BALANCE : STARTING_BALANCE
|
|
|
|
const user: User = {
|
|
id: auth.uid,
|
|
name,
|
|
username,
|
|
avatarUrl,
|
|
balance,
|
|
totalDeposits: balance,
|
|
createdTime: Date.now(),
|
|
profitCached: { daily: 0, weekly: 0, monthly: 0, allTime: 0 },
|
|
creatorVolumeCached: { daily: 0, weekly: 0, monthly: 0, allTime: 0 },
|
|
nextLoanCached: 0,
|
|
followerCountCached: 0,
|
|
followedCategories: DEFAULT_CATEGORIES,
|
|
shouldShowWelcome: true,
|
|
fractionResolvedCorrectly: 1,
|
|
achievements: {},
|
|
}
|
|
|
|
await firestore.collection('users').doc(auth.uid).create(user)
|
|
console.log('created user', username, 'firebase id:', auth.uid)
|
|
|
|
const privateUser: PrivateUser = {
|
|
id: auth.uid,
|
|
username,
|
|
email,
|
|
initialIpAddress: req.ip,
|
|
initialDeviceToken: deviceToken,
|
|
notificationPreferences: getDefaultNotificationPreferences(auth.uid),
|
|
}
|
|
|
|
await firestore.collection('private-users').doc(auth.uid).create(privateUser)
|
|
await addUserToDefaultGroups(user)
|
|
|
|
await track(auth.uid, 'create user', { username }, { ip: req.ip })
|
|
|
|
return { user, privateUser }
|
|
})
|
|
|
|
const firestore = admin.firestore()
|
|
|
|
const isPrivateUserWithDeviceToken = async (deviceToken: string) => {
|
|
const snap = await firestore
|
|
.collection('private-users')
|
|
.where('initialDeviceToken', '==', deviceToken)
|
|
.get()
|
|
|
|
return !snap.empty
|
|
}
|
|
|
|
export const numberUsersWithIp = async (ipAddress: string) => {
|
|
const snap = await firestore
|
|
.collection('private-users')
|
|
.where('initialIpAddress', '==', ipAddress)
|
|
.get()
|
|
|
|
return snap.docs.length
|
|
}
|
|
|
|
const addUserToDefaultGroups = async (user: User) => {
|
|
for (const category of Object.values(DEFAULT_CATEGORIES)) {
|
|
const slug = category.toLowerCase() + CATEGORIES_GROUP_SLUG_POSTFIX
|
|
const groups = await getValues<Group>(
|
|
firestore.collection('groups').where('slug', '==', slug)
|
|
)
|
|
await firestore
|
|
.collection(`groups/${groups[0].id}/groupMembers`)
|
|
.doc(user.id)
|
|
.set({ userId: user.id, createdTime: Date.now() })
|
|
}
|
|
}
|