2022-01-18 07:18:38 +00:00
|
|
|
import * as functions from 'firebase-functions'
|
|
|
|
import * as admin from 'firebase-admin'
|
|
|
|
|
2022-01-19 03:36:46 +00:00
|
|
|
import {
|
|
|
|
PrivateUser,
|
|
|
|
STARTING_BALANCE,
|
|
|
|
SUS_STARTING_BALANCE,
|
|
|
|
User,
|
2022-05-15 17:39:42 +00:00
|
|
|
} from '../../common/user'
|
2022-01-18 07:18:38 +00:00
|
|
|
import { getUser, getUserByUsername } from './utils'
|
2022-05-15 17:39:42 +00:00
|
|
|
import { randomString } from '../../common/util/random'
|
|
|
|
import {
|
|
|
|
cleanDisplayName,
|
|
|
|
cleanUsername,
|
|
|
|
} from '../../common/util/clean-username'
|
2022-02-08 11:26:33 +00:00
|
|
|
import { sendWelcomeEmail } from './emails'
|
2022-05-15 17:39:42 +00:00
|
|
|
import { isWhitelisted } from '../../common/envs/constants'
|
2022-06-21 15:01:41 +00:00
|
|
|
import { DEFAULT_CATEGORIES } from '../../common/categories'
|
|
|
|
|
2022-06-16 15:45:44 +00:00
|
|
|
import { track } from './analytics'
|
2022-01-18 07:18:38 +00:00
|
|
|
|
|
|
|
export const createUser = functions
|
2022-06-04 21:39:25 +00:00
|
|
|
.runWith({ minInstances: 1, secrets: ['MAILGUN_KEY'] })
|
2022-01-19 03:36:46 +00:00
|
|
|
.https.onCall(async (data: { deviceToken?: string }, context) => {
|
2022-01-18 07:18:38 +00:00
|
|
|
const userId = context?.auth?.uid
|
|
|
|
if (!userId) return { status: 'error', message: 'Not authorized' }
|
|
|
|
|
|
|
|
const preexistingUser = await getUser(userId)
|
|
|
|
if (preexistingUser)
|
|
|
|
return {
|
|
|
|
status: 'error',
|
|
|
|
message: 'User already created',
|
|
|
|
user: preexistingUser,
|
|
|
|
}
|
|
|
|
|
|
|
|
const fbUser = await admin.auth().getUser(userId)
|
|
|
|
|
|
|
|
const email = fbUser.email
|
2022-02-27 07:29:33 +00:00
|
|
|
if (!isWhitelisted(email)) {
|
|
|
|
return { status: 'error', message: `${email} is not whitelisted` }
|
|
|
|
}
|
2022-01-18 07:18:38 +00:00
|
|
|
const emailName = email?.replace(/@.*$/, '')
|
|
|
|
|
2022-02-04 03:04:56 +00:00
|
|
|
const rawName = fbUser.displayName || emailName || 'User' + randomString(4)
|
|
|
|
const name = cleanDisplayName(rawName)
|
2022-01-18 07:18:38 +00:00
|
|
|
let username = cleanUsername(name)
|
|
|
|
|
2022-05-17 00:26:51 +00:00
|
|
|
const sameNameUser = await getUserByUsername(username)
|
|
|
|
if (sameNameUser) {
|
2022-01-18 07:18:38 +00:00
|
|
|
username += randomString(4)
|
|
|
|
}
|
|
|
|
|
|
|
|
const avatarUrl = fbUser.photoURL
|
|
|
|
|
2022-01-19 03:36:46 +00:00
|
|
|
const { deviceToken } = data
|
|
|
|
const deviceUsedBefore =
|
|
|
|
!deviceToken || (await isPrivateUserWithDeviceToken(deviceToken))
|
|
|
|
|
|
|
|
const ipAddress = context.rawRequest.ip
|
|
|
|
const ipCount = ipAddress ? await numberUsersWithIp(ipAddress) : 0
|
|
|
|
|
|
|
|
const balance =
|
|
|
|
deviceUsedBefore || ipCount > 2 ? SUS_STARTING_BALANCE : STARTING_BALANCE
|
|
|
|
|
2022-01-18 07:18:38 +00:00
|
|
|
const user: User = {
|
|
|
|
id: userId,
|
|
|
|
name,
|
|
|
|
username,
|
|
|
|
avatarUrl,
|
2022-01-19 03:36:46 +00:00
|
|
|
balance,
|
|
|
|
totalDeposits: balance,
|
2022-01-18 07:18:38 +00:00
|
|
|
createdTime: Date.now(),
|
2022-06-22 20:29:40 +00:00
|
|
|
profitCached: { daily: 0, weekly: 0, monthly: 0, allTime: 0 },
|
|
|
|
creatorVolumeCached: { daily: 0, weekly: 0, monthly: 0, allTime: 0 },
|
2022-06-22 16:05:54 +00:00
|
|
|
followerCountCached: 0,
|
2022-06-21 15:01:41 +00:00
|
|
|
followedCategories: DEFAULT_CATEGORIES,
|
2022-01-18 07:18:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await firestore.collection('users').doc(userId).create(user)
|
|
|
|
console.log('created user', username, 'firebase id:', userId)
|
|
|
|
|
2022-01-19 03:36:46 +00:00
|
|
|
const privateUser: PrivateUser = {
|
|
|
|
id: userId,
|
|
|
|
username,
|
|
|
|
email,
|
|
|
|
initialIpAddress: ipAddress,
|
|
|
|
initialDeviceToken: deviceToken,
|
|
|
|
}
|
|
|
|
|
|
|
|
await firestore.collection('private-users').doc(userId).create(privateUser)
|
|
|
|
|
2022-02-08 11:26:33 +00:00
|
|
|
await sendWelcomeEmail(user, privateUser)
|
|
|
|
|
2022-06-16 15:45:44 +00:00
|
|
|
await track(userId, 'create user', { username }, { ip: ipAddress })
|
|
|
|
|
2022-01-18 07:18:38 +00:00
|
|
|
return { status: 'success', user }
|
|
|
|
})
|
|
|
|
|
|
|
|
const firestore = admin.firestore()
|
2022-01-19 03:36:46 +00:00
|
|
|
|
|
|
|
const isPrivateUserWithDeviceToken = async (deviceToken: string) => {
|
|
|
|
const snap = await firestore
|
|
|
|
.collection('private-users')
|
|
|
|
.where('initialDeviceToken', '==', deviceToken)
|
|
|
|
.get()
|
|
|
|
|
|
|
|
return !snap.empty
|
|
|
|
}
|
|
|
|
|
|
|
|
const numberUsersWithIp = async (ipAddress: string) => {
|
|
|
|
const snap = await firestore
|
|
|
|
.collection('private-users')
|
|
|
|
.where('initialIpAddress', '==', ipAddress)
|
|
|
|
.get()
|
|
|
|
|
|
|
|
return snap.docs.length
|
|
|
|
}
|