Merge pull request #3683 from omnivore-app/fix/allow-yt-transcript-opt-in

Allow opt into the YouTube transcripts feature
This commit is contained in:
Jackson Harper 2024-03-19 09:45:50 +08:00 committed by GitHub
commit 6222cad53a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 9 deletions

View file

@ -6,6 +6,9 @@ import { env } from '../env'
import { getRepository } from '../repository'
import { logger } from '../utils/logger'
const MAX_ULTRA_REALISTIC_USERS = 1500
const MAX_YOUTUBE_TRANSCRIPT_USERS = 100
export enum FeatureName {
AISummaries = 'ai-summaries',
YouTubeTranscripts = 'youtube-transcripts',
@ -20,16 +23,31 @@ export const optInFeature = async (
name: FeatureName,
uid: string
): Promise<Feature | undefined> => {
if (name === FeatureName.UltraRealisticVoice) {
return optInUltraRealisticVoice(uid)
switch (name) {
case FeatureName.UltraRealisticVoice:
return optInLimitedFeature(
FeatureName.UltraRealisticVoice,
uid,
MAX_ULTRA_REALISTIC_USERS
)
case FeatureName.YouTubeTranscripts:
return optInLimitedFeature(
FeatureName.YouTubeTranscripts,
uid,
MAX_YOUTUBE_TRANSCRIPT_USERS
)
}
return undefined
}
const optInUltraRealisticVoice = async (uid: string): Promise<Feature> => {
const optInLimitedFeature = async (
featureName: string,
uid: string,
maxUsers: number
): Promise<Feature> => {
const feature = await getRepository(Feature).findOne({
where: {
name: FeatureName.UltraRealisticVoice,
name: featureName,
grantedAt: Not(IsNull()),
user: { id: uid },
},
@ -41,8 +59,6 @@ const optInUltraRealisticVoice = async (uid: string): Promise<Feature> => {
return feature
}
const MAX_USERS = 1500
// opt in to feature for the first 1500 users
const optedInFeatures = (await appDataSource.query(
`insert into omnivore.features (user_id, name, granted_at)
select $1, $2, $3 from omnivore.features
@ -51,7 +67,7 @@ const optInUltraRealisticVoice = async (uid: string): Promise<Feature> => {
on conflict (user_id, name)
do update set granted_at = $3
returning *, granted_at as "grantedAt", created_at as "createdAt", updated_at as "updatedAt";`,
[uid, FeatureName.UltraRealisticVoice, new Date(), MAX_USERS]
[uid, featureName, new Date(), maxUsers]
)) as Feature[]
// if no new features were created then user has exceeded max users
@ -61,7 +77,7 @@ const optInUltraRealisticVoice = async (uid: string): Promise<Feature> => {
// create/update an opt-in record with null grantedAt
const optInRecord = {
user: { id: uid },
name: FeatureName.UltraRealisticVoice,
name: featureName,
grantedAt: null,
}
const result = await getRepository(Feature).upsert(optInRecord, [

View file

@ -3,6 +3,7 @@ import useSWR from 'swr'
import { publicGqlFetcher } from '../networkHelpers'
type ViewerQueryResponse = {
mutate: () => void
viewerData?: ViewerQueryResponseData
viewerDataError?: unknown
isLoading: boolean
@ -51,9 +52,10 @@ export function useGetViewerQuery(): ViewerQueryResponse {
}
`
const { data, error } = useSWR(query, publicGqlFetcher)
const { data, error, mutate } = useSWR(query, publicGqlFetcher)
return {
mutate,
viewerData: data as ViewerQueryResponseData,
viewerDataError: error, // TODO: figure out error possibilities
isLoading: !error && !data,