mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
send feedback api
This commit is contained in:
parent
1edb3419f8
commit
e37bcd7408
2 changed files with 76 additions and 0 deletions
|
|
@ -5,6 +5,7 @@ import { CREATE_DIGEST_JOB } from '../jobs/create_digest'
|
|||
import { createJobId, getJob, jobStateToTaskState } from '../queue-processor'
|
||||
import { redisDataSource } from '../redis_data_source'
|
||||
import { findActiveUser } from '../services/user'
|
||||
import { analytics } from '../utils/analytics'
|
||||
import { getClaimsByToken, getTokenByRequest } from '../utils/auth'
|
||||
import { corsConfig } from '../utils/corsConfig'
|
||||
import { enqueueCreateDigest } from '../utils/createTask'
|
||||
|
|
@ -23,6 +24,24 @@ interface Chapter {
|
|||
title: string
|
||||
}
|
||||
|
||||
interface Feedback {
|
||||
digestRating: number
|
||||
rankingRating: number
|
||||
summaryRating: number
|
||||
voiceRating: number
|
||||
musicRating: number
|
||||
}
|
||||
|
||||
const isFeedback = (data: any): data is Feedback => {
|
||||
return (
|
||||
'digestRating' in data &&
|
||||
'rankingRating' in data &&
|
||||
'summaryRating' in data &&
|
||||
'voiceRating' in data &&
|
||||
'musicRating' in data
|
||||
)
|
||||
}
|
||||
|
||||
export function digestRouter() {
|
||||
const router = express.Router()
|
||||
|
||||
|
|
@ -137,5 +156,60 @@ export function digestRouter() {
|
|||
}
|
||||
})
|
||||
|
||||
// v1 version of sending feedback api
|
||||
router.post(
|
||||
'/v1/feedback',
|
||||
cors<express.Request>(corsConfig),
|
||||
async (req, res) => {
|
||||
const token = getTokenByRequest(req)
|
||||
|
||||
let userId: string
|
||||
try {
|
||||
// get claims from token
|
||||
const claims = await getClaimsByToken(token)
|
||||
if (!claims) {
|
||||
logger.info('Token not found')
|
||||
return res.sendStatus(401)
|
||||
}
|
||||
|
||||
// get user by uid from claims
|
||||
userId = claims.uid
|
||||
} catch (error) {
|
||||
logger.info('Error while getting claims from token', error)
|
||||
return res.sendStatus(401)
|
||||
}
|
||||
|
||||
try {
|
||||
const user = await findActiveUser(userId)
|
||||
if (!user) {
|
||||
logger.info(`User not found: ${userId}`)
|
||||
return res.sendStatus(401)
|
||||
}
|
||||
|
||||
// get feedback from request body
|
||||
if (!isFeedback(req.body)) {
|
||||
logger.info('Invalid feedback format')
|
||||
return res.sendStatus(400)
|
||||
}
|
||||
|
||||
const feedback = req.body
|
||||
// send feedback to analytics
|
||||
logger.info(`Sending feedback: ${JSON.stringify(feedback)}`)
|
||||
|
||||
analytics.capture({
|
||||
distinctId: userId,
|
||||
event: 'digest_feedback',
|
||||
properties: feedback,
|
||||
})
|
||||
|
||||
// return success
|
||||
return res.sendStatus(200)
|
||||
} catch (error) {
|
||||
logger.error('Error while saving feedback', error)
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
return router
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ import { aiSummariesRouter } from './routers/ai_summary_router'
|
|||
import { articleRouter } from './routers/article_router'
|
||||
import { authRouter } from './routers/auth/auth_router'
|
||||
import { mobileAuthRouter } from './routers/auth/mobile/mobile_auth_router'
|
||||
import { digestRouter } from './routers/digest_router'
|
||||
import { integrationRouter } from './routers/integration_router'
|
||||
import { localDebugRouter } from './routers/local_debug_router'
|
||||
import { notificationRouter } from './routers/notification_router'
|
||||
|
|
@ -89,6 +90,7 @@ export const createApp = (): Express => {
|
|||
app.use('/api/notification', notificationRouter())
|
||||
app.use('/api/integration', integrationRouter())
|
||||
app.use('/api/tasks', taskRouter())
|
||||
app.use('/api/digest', digestRouter)
|
||||
app.use('/svc/pubsub/content', contentServiceRouter())
|
||||
app.use('/svc/pubsub/links', linkServiceRouter())
|
||||
app.use('/svc/pubsub/newsletters', newsletterServiceRouter())
|
||||
|
|
|
|||
Loading…
Reference in a new issue