This commit is contained in:
Hongbo Wu
2023-10-09 22:06:35 +08:00
parent 0b7da62aca
commit ac0d295cf3
4 changed files with 74 additions and 97 deletions

View File

@ -1,37 +0,0 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { PubSub } from '@google-cloud/pubsub'
import {
BaseEntity,
EntitySubscriberInterface,
EventSubscriber,
InsertEvent,
} from 'typeorm'
import { env } from '../env'
import { logger } from '../utils/logger'
const TOPIC_NAME = 'EntityCreated'
@EventSubscriber()
export class PublishEntitySubscriber implements EntitySubscriberInterface {
async afterInsert(event: InsertEvent<BaseEntity>): Promise<void> {
const client = new PubSub()
const msg = JSON.stringify({
type: 'EntityCreated',
entity: event.entity,
entityClass: event.entity?.constructor?.name,
})
if (env.dev.isLocal) {
logger.info('PublishEntitySubscriber', msg)
return
}
await client
.topic(TOPIC_NAME)
.publishMessage({ data: Buffer.from(msg) })
.catch((err) => {
logger.error('PublishEntitySubscriber error publishing event', err)
})
}
}

View File

@ -1,54 +1,54 @@
import {
EntitySubscriberInterface,
EventSubscriber,
InsertEvent,
} from 'typeorm'
import { Profile } from '../../entity/profile'
import { createPubSubClient } from '../../pubsub'
import { addPopularReadsForNewUser } from '../../services/popular_reads'
import { IntercomClient } from '../../utils/intercom'
// import {
// EntitySubscriberInterface,
// EventSubscriber,
// InsertEvent,
// } from 'typeorm'
// import { Profile } from '../../entity/profile'
// import { createPubSubClient } from '../../pubsub'
// import { addPopularReadsForNewUser } from '../../services/popular_reads'
// import { IntercomClient } from '../../utils/intercom'
@EventSubscriber()
export class CreateIntercomAccount
implements EntitySubscriberInterface<Profile>
{
listenTo() {
return Profile
}
// @EventSubscriber()
// export class CreateIntercomAccount
// implements EntitySubscriberInterface<Profile>
// {
// listenTo() {
// return Profile
// }
async afterInsert(event: InsertEvent<Profile>): Promise<void> {
const profile = event.entity
// async afterInsert(event: InsertEvent<Profile>): Promise<void> {
// const profile = event.entity
const customAttributes: { source_user_id: string } = {
source_user_id: profile.user.sourceUserId,
}
await IntercomClient?.contacts.createUser({
email: profile.user.email,
externalId: profile.user.id,
name: profile.user.name,
avatar: profile.pictureUrl || undefined,
customAttributes: customAttributes,
signedUpAt: Math.floor(Date.now() / 1000),
})
}
}
// const customAttributes: { source_user_id: string } = {
// source_user_id: profile.user.sourceUserId,
// }
// await IntercomClient?.contacts.createUser({
// email: profile.user.email,
// externalId: profile.user.id,
// name: profile.user.name,
// avatar: profile.pictureUrl || undefined,
// customAttributes: customAttributes,
// signedUpAt: Math.floor(Date.now() / 1000),
// })
// }
// }
@EventSubscriber()
export class PublishNewUserEvent implements EntitySubscriberInterface<Profile> {
listenTo() {
return Profile
}
// @EventSubscriber()
// export class PublishNewUserEvent implements EntitySubscriberInterface<Profile> {
// listenTo() {
// return Profile
// }
async afterInsert(event: InsertEvent<Profile>): Promise<void> {
const client = createPubSubClient()
await client.userCreated(
event.entity.user.id,
event.entity.user.email,
event.entity.user.name,
event.entity.username
)
}
}
// async afterInsert(event: InsertEvent<Profile>): Promise<void> {
// const client = createPubSubClient()
// await client.userCreated(
// event.entity.user.id,
// event.entity.user.email,
// event.entity.user.name,
// event.entity.username
// )
// }
// }
// @EventSubscriber()
// export class AddPopularReadsToNewUser

View File

@ -1,29 +1,22 @@
import { EntityManager } from 'typeorm'
import { Filter } from '../entity/filter'
import { GroupMembership } from '../entity/groups/group_membership'
import { Invite } from '../entity/groups/invite'
import { Profile } from '../entity/profile'
import { StatusType, User } from '../entity/user'
import { env } from '../env'
import { SignupErrorCode } from '../generated/graphql'
import { createPubSubClient } from '../pubsub'
import { authTrx, entityManager, getRepository } from '../repository'
import { userRepository } from '../repository/user'
import { AuthProvider } from '../routers/auth/auth_types'
import { analytics } from '../utils/analytics'
import { IntercomClient } from '../utils/intercom'
import { logger } from '../utils/logger'
import { validateUsername } from '../utils/usernamePolicy'
import { addPopularReadsForNewUser } from './popular_reads'
import { sendConfirmationEmail } from './send_emails'
import { Filter } from '../entity/filter'
import { analytics } from '../utils/analytics'
import { env } from '../env'
const TOP_USERS = [
'jacksonh',
'nat',
'luis',
'satindar',
'malandrina',
'patrick',
'alexgutjahr',
'hongbowu',
]
export const MAX_RECORDS_LIMIT = 1000
export const createUser = async (input: {
@ -111,6 +104,7 @@ export const createUser = async (input: {
}
await createDefaultFiltersForUser(t)(user.id)
await addPopularReadsForNewUser(user.id, t)
return [user, profile]
}
@ -122,6 +116,26 @@ export const createUser = async (input: {
}
}
const customAttributes: { source_user_id: string } = {
source_user_id: user.sourceUserId,
}
await IntercomClient?.contacts.createUser({
email: user.email,
externalId: user.id,
name: user.name,
avatar: profile.pictureUrl || undefined,
customAttributes: customAttributes,
signedUpAt: Math.floor(Date.now() / 1000),
})
const pubsubClient = createPubSubClient()
await pubsubClient.userCreated(
user.id,
user.email,
user.name,
profile.username
)
analytics.track({
userId: user.id,
event: 'create_user',

View File

@ -26,7 +26,7 @@ export const sendEmail = async (msg: MailDataRequired): Promise<boolean> => {
const client = new MailService()
if (!process.env.SENDGRID_MSGS_API_KEY) {
if (env.dev.isLocal) {
logger.error('SendGrid API key not set.\nSending email:', msg)
logger.info('SendGrid API key not set.\nSending email:', msg)
return true
}