Add popular reads to new users

This commit is contained in:
Hongbo Wu 2022-10-11 15:44:53 +08:00
parent 9725f505b7
commit 606183829a
3 changed files with 102 additions and 0 deletions

View file

@ -10,6 +10,7 @@ import { IntercomClient } from '../../utils/intercom'
import { createPubSubClient } from '../../datalayer/pubsub'
import { env } from '../../env'
import { analytics } from '../../utils/analytics'
import { addPopularReadsForNewUser } from '../../services/popular_reads'
@EventSubscriber()
export class FollowOmnivoreUser implements EntitySubscriberInterface<Profile> {
@ -114,3 +115,17 @@ export class PublishNewUserEvent implements EntitySubscriberInterface<Profile> {
)
}
}
@EventSubscriber()
export class AddPopularReadsToNewUser
implements EntitySubscriberInterface<Profile>
{
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
listenTo() {
return Profile
}
async afterInsert(event: InsertEvent<Profile>): Promise<void> {
await addPopularReadsForNewUser(event.entity.user.id)
}
}

View file

@ -19,6 +19,12 @@ type PopularRead = {
originalHtml: string
}
interface AddPopularReadResult {
pageId?: string
name: string
status: ArticleSavingRequestStatus
}
const popularRead = (key: string): PopularRead | undefined => {
const metadata = popularReads.find((pr) => pr.key === key)
if (!metadata) {
@ -88,6 +94,35 @@ export const addPopularRead = async (
return pageId
}
const addPopularReads = async (
userId: string,
...names: string[]
): Promise<AddPopularReadResult[]> => {
const results: AddPopularReadResult[] = []
for (const name of names) {
const pageId = await addPopularRead(userId, name)
results.push({
pageId,
name,
status: pageId
? ArticleSavingRequestStatus.Succeeded
: ArticleSavingRequestStatus.Failed,
})
}
return results
}
export const addPopularReadsForNewUser = async (
userId: string
): Promise<void> => {
await addPopularReads(
userId,
'omnivore_get_started',
'power_read_it_later',
'omnivore_organize'
)
}
const popularReads = [
{
key: 'omnivore_get_started',

View file

@ -14,6 +14,8 @@ import {
} from '../../src/utils/auth'
import sinonChai from 'sinon-chai'
import chai, { expect } from 'chai'
import { searchPages } from '../../src/elastic/pages'
import { createPendingUserToken } from '../../src/routers/auth/jwt_helpers'
chai.use(sinonChai)
@ -546,4 +548,54 @@ describe('auth router', () => {
})
})
})
describe('create account', () => {
const createAccountRequest = (
bio: string,
name: string,
username: string,
pendingUserAuth: string
): supertest.Test => {
return request
.post(`${route}/create-account`)
.set('Cookie', [`pendingUserAuth=${pendingUserAuth}`])
.send({
name,
bio,
username,
})
}
context('when inputs are valid and user not exists', () => {
let name = 'test_user'
let username = 'test_user'
after(async () => {
await deleteTestUser(username)
})
it('adds popular reads to the library', async () => {
const pendingUserToken = await createPendingUserToken({
sourceUserId: 'test_source_user_id',
email: 'test_user@omnivore.app',
provider: 'APPLE',
name,
username,
})
await createAccountRequest(
'',
name,
username,
pendingUserToken!
).expect(200)
const user = await getRepository(User).findOneBy({ name })
const [popularReads, count] = (await searchPages({}, user?.id!)) || [
[],
0,
]
expect(count).to.eql(3)
})
})
})
})