Add function to test if a page should be transcribed

This commit is contained in:
Hongbo Wu 2022-08-23 22:36:27 +08:00
parent 5622154e6c
commit 9b0093e3d2
2 changed files with 38 additions and 0 deletions

View file

@ -1,5 +1,8 @@
import { getRepository } from '../entity/utils'
import { Speech, SpeechState } from '../entity/speech'
import { searchPages } from '../elastic/pages'
import { Page } from '../elastic/types'
import { SortBy, SortOrder } from '../utils/search'
export const setSpeechFailure = async (id: string) => {
// update state
@ -7,3 +10,37 @@ export const setSpeechFailure = async (id: string) => {
state: SpeechState.FAILED,
})
}
/*
* We should not transcribe the page when:
** 1. User has no recent listens the last 30 days
** 2. User has a recent listen but the page was saved after the listen
*/
export const shouldTranscribe = async (
userId: string,
page: Page
): Promise<boolean> => {
const [recentListenedPage, count] = (await searchPages(
{
dateFilters: [
{
field: 'listenedAt',
startDate: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000),
},
],
sort: {
by: SortBy.LISTENED,
order: SortOrder.DESCENDING,
},
size: 1,
},
userId
)) || [[], 0]
if (count === 0) {
return false
}
return (
!!recentListenedPage[0].listenedAt &&
page.savedAt < recentListenedPage[0].listenedAt
)
}

View file

@ -63,6 +63,7 @@ export enum SortBy {
SCORE = '_score',
PUBLISHED = 'publishedAt',
READ = 'readAt',
LISTENED = 'listenedAt',
}
export enum SortOrder {