mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Fix a bug not getting user personalization
This commit is contained in:
parent
83c1930378
commit
19b13d164d
2 changed files with 73 additions and 45 deletions
|
|
@ -83,14 +83,11 @@ export function articleRouter() {
|
|||
}
|
||||
const { uid } = jwt.decode(token) as Claims
|
||||
|
||||
const startTime = Date.now()
|
||||
logger.info(`Get article speech in ${outputFormat} format`, {
|
||||
params: req.params,
|
||||
labels: {
|
||||
userId: uid,
|
||||
source: 'GetArticleSpeechMp3',
|
||||
articleId: id,
|
||||
outputFormat,
|
||||
source: `GetArticleSpeech-${outputFormat}`,
|
||||
},
|
||||
})
|
||||
|
||||
|
|
@ -112,39 +109,45 @@ export function articleRouter() {
|
|||
user: { id: uid },
|
||||
})
|
||||
if (!userPersonalization) {
|
||||
return res.status(200).send('userPersonalization not found')
|
||||
return res.status(404).send('User Personalization not found')
|
||||
}
|
||||
|
||||
const page = await getPageById(id)
|
||||
if (!page) {
|
||||
return res.status(200).send('Page not found')
|
||||
return res.status(404).send('Page not found')
|
||||
}
|
||||
|
||||
const text = parseHTML(page.content).document.documentElement.innerText
|
||||
if (!text) {
|
||||
return res.status(200).send('Page has no text')
|
||||
return res.status(404).send('Page has no text')
|
||||
}
|
||||
|
||||
const speechOutput = await synthesizeTextToSpeech({
|
||||
id,
|
||||
text,
|
||||
languageCode: page.language,
|
||||
voice: userPersonalization.speechVoice,
|
||||
})
|
||||
try {
|
||||
const startTime = Date.now()
|
||||
const speechOutput = await synthesizeTextToSpeech({
|
||||
id,
|
||||
text,
|
||||
languageCode: page.language,
|
||||
voice: userPersonalization.speechVoice,
|
||||
})
|
||||
logger.info('Created speech', {
|
||||
audioUrl: speechOutput.audioUrl,
|
||||
speechMarksUrl: speechOutput.speechMarksUrl,
|
||||
duration: Date.now() - startTime,
|
||||
})
|
||||
|
||||
const speech = await getRepository(Speech).save({
|
||||
elasticPageId: id,
|
||||
audioUrl: speechOutput.audioUrl,
|
||||
speechMarksUrl: speechOutput.speechMarksUrl,
|
||||
user: { id: uid },
|
||||
})
|
||||
const speech = await getRepository(Speech).save({
|
||||
elasticPageId: id,
|
||||
audioUrl: speechOutput.audioUrl,
|
||||
speechMarksUrl: speechOutput.speechMarksUrl,
|
||||
user: { id: uid },
|
||||
})
|
||||
|
||||
logger.info('Created speech', {
|
||||
audioUrl: speech.audioUrl,
|
||||
speechMarksUrl: speech.speechMarksUrl,
|
||||
duration: Date.now() - startTime,
|
||||
})
|
||||
res.redirect(redirectUrl(speech, outputFormat))
|
||||
res.redirect(redirectUrl(speech, outputFormat))
|
||||
} catch (error) {
|
||||
logger.error('Text to speech error', { error })
|
||||
res.status(500).send('Text to speech error')
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
|
|
@ -155,7 +158,7 @@ const redirectUrl = (speech: Speech, outputFormat: string) => {
|
|||
switch (outputFormat) {
|
||||
case 'mp3':
|
||||
return speech.audioUrl
|
||||
case 'json':
|
||||
case 'speech-marks':
|
||||
return speech.speechMarksUrl
|
||||
default:
|
||||
return speech.audioUrl
|
||||
|
|
|
|||
|
|
@ -2,11 +2,14 @@ import express from 'express'
|
|||
import cors from 'cors'
|
||||
import { corsConfig } from '../../utils/corsConfig'
|
||||
import { getRepository } from '../../entity/utils'
|
||||
import { User } from '../../entity/user'
|
||||
import { getPageById } from '../../elastic/pages'
|
||||
import { synthesizeTextToSpeech } from '../../utils/textToSpeech'
|
||||
import { Speech } from '../../entity/speech'
|
||||
import { parseHTML } from 'linkedom'
|
||||
import { UserPersonalization } from '../../entity/user_personalization'
|
||||
import { buildLogger } from '../../utils/logger'
|
||||
|
||||
const logger = buildLogger('app.dispatch')
|
||||
|
||||
export function speechServiceRouter() {
|
||||
const router = express.Router()
|
||||
|
|
@ -23,12 +26,13 @@ export function speechServiceRouter() {
|
|||
return res.status(200).send('Invalid data')
|
||||
}
|
||||
|
||||
const user = await getRepository(User).findOne({
|
||||
where: { id: userId },
|
||||
relations: ['user_personalization'],
|
||||
const userPersonalization = await getRepository(
|
||||
UserPersonalization
|
||||
).findOneBy({
|
||||
user: { id: userId },
|
||||
})
|
||||
if (!user) {
|
||||
return res.status(200).send('User not found')
|
||||
if (!userPersonalization) {
|
||||
return res.status(200).send('User Personalization not found')
|
||||
}
|
||||
|
||||
const page = await getPageById(pageId)
|
||||
|
|
@ -36,26 +40,47 @@ export function speechServiceRouter() {
|
|||
return res.status(200).send('Page not found')
|
||||
}
|
||||
|
||||
const text = parseHTML(page.content).document.documentElement.textContent
|
||||
const text = parseHTML(page.content).document.documentElement.innerText
|
||||
if (!text) {
|
||||
return res.status(200).send('Page has no text')
|
||||
}
|
||||
|
||||
const speech = await synthesizeTextToSpeech({
|
||||
id: pageId,
|
||||
text,
|
||||
languageCode: page.language,
|
||||
voice: user.userPersonalization.speechVoice,
|
||||
logger.info(`Create article speech`, {
|
||||
body: {
|
||||
userId,
|
||||
pageId,
|
||||
},
|
||||
labels: {
|
||||
source: 'CreateArticleSpeech',
|
||||
},
|
||||
})
|
||||
|
||||
await getRepository(Speech).save({
|
||||
elasticPageId: pageId,
|
||||
audioUrl: speech.audioUrl,
|
||||
speechMarks: speech.speechMarksUrl,
|
||||
user,
|
||||
})
|
||||
try {
|
||||
const startTime = Date.now()
|
||||
const speechOutput = await synthesizeTextToSpeech({
|
||||
id: pageId,
|
||||
text,
|
||||
languageCode: page.language,
|
||||
voice: userPersonalization.speechVoice,
|
||||
})
|
||||
logger.info('Created speech', {
|
||||
audioUrl: speechOutput.audioUrl,
|
||||
speechMarksUrl: speechOutput.speechMarksUrl,
|
||||
duration: Date.now() - startTime,
|
||||
})
|
||||
|
||||
res.status(200).send('OK')
|
||||
await getRepository(Speech).save({
|
||||
elasticPageId: pageId,
|
||||
audioUrl: speechOutput.audioUrl,
|
||||
speechMarksUrl: speechOutput.speechMarksUrl,
|
||||
user: { id: userId },
|
||||
})
|
||||
|
||||
res.status(200).send('OK')
|
||||
} catch (error) {
|
||||
logger.error(`Error creating article speech`, { error })
|
||||
res.status(500).send('Error creating article speech')
|
||||
}
|
||||
})
|
||||
|
||||
return router
|
||||
|
|
|
|||
Loading…
Reference in a new issue