upload highlight to gcs

This commit is contained in:
Hongbo Wu
2022-04-13 21:18:58 +08:00
parent 686b00bc4d
commit a806f55233
4 changed files with 45 additions and 10 deletions

View File

@ -2,7 +2,7 @@ import { PubSub } from '@google-cloud/pubsub'
import { env } from '../env'
import { ReportType } from '../generated/graphql'
import express from 'express'
import { Page } from '../elastic/types'
import { Highlight, Page } from '../elastic/types'
export const createPubSubClient = (): PubsubClient => {
const client = new PubSub()
@ -49,6 +49,24 @@ export const createPubSubClient = (): PubsubClient => {
pageDeleted: (id: string, userId: string): Promise<void> => {
return publish('pageDeleted', Buffer.from(JSON.stringify({ id, userId })))
},
highlightCreated: (highlight: Highlight): Promise<void> => {
return publish('highlightCreated', Buffer.from(JSON.stringify(highlight)))
},
highlightUpdated: (
highlight: Partial<Highlight>,
userId: string
): Promise<void> => {
return publish(
'highlightUpdated',
Buffer.from(JSON.stringify({ ...highlight, userId }))
)
},
highlightDeleted: (id: string, userId: string): Promise<void> => {
return publish(
'highlightDeleted',
Buffer.from(JSON.stringify({ id, userId }))
)
},
reportSubmitted: (
submitterId: string,
itemUrl: string,
@ -75,6 +93,12 @@ export interface PubsubClient {
pageCreated: (page: Page) => Promise<void>
pageUpdated: (page: Partial<Page>, userId: string) => Promise<void>
pageDeleted: (id: string, userId: string) => Promise<void>
highlightCreated: (highlight: Highlight) => Promise<void>
highlightUpdated: (
highlight: Partial<Highlight>,
userId: string
) => Promise<void>
highlightDeleted: (id: string, userId: string) => Promise<void>
reportSubmitted(
submitterId: string | undefined,
itemUrl: string,

View File

@ -35,7 +35,11 @@ export const addHighlightToPage = async (
retry_on_conflict: 3,
})
return body.result === 'updated'
if (body.result !== 'updated') return false
await ctx.pubsub.highlightCreated(highlight)
return true
} catch (e) {
if (
e instanceof ResponseError &&
@ -125,7 +129,11 @@ export const deleteHighlight = async (
refresh: ctx.refresh,
})
return !!body.updated
if (body.result !== 'updated') return false
await ctx.pubsub.highlightDeleted(highlightId, ctx.uid)
return true
} catch (e) {
console.error('failed to delete a highlight in elastic', e)
@ -266,7 +274,11 @@ export const updateHighlight = async (
refresh: ctx.refresh,
})
return !!body.updated
if (body.result !== 'updated') return false
await ctx.pubsub.highlightUpdated(highlight, ctx.uid)
return true
} catch (e) {
if (
e instanceof ResponseError &&

View File

@ -6,14 +6,13 @@ import { readPushSubscription } from '../../datalayer/pubsub'
import { generateUploadSignedUrl, uploadToSignedUrl } from '../../utils/uploads'
import { v4 as uuidv4 } from 'uuid'
import { env } from '../../env'
import { Page } from '../../elastic/types'
import { DateTime } from 'luxon'
export function pageServiceRouter() {
export function uploadServiceRouter() {
const router = express.Router()
router.post('/upload/:folder', async (req, res) => {
console.log('upload page data req', req.params.folder)
console.log('upload data req', req.params.folder)
const { message: msgStr, expired } = readPushSubscription(req)
if (!msgStr) {
@ -28,7 +27,7 @@ export function pageServiceRouter() {
}
try {
const data: Partial<Page> = JSON.parse(msgStr)
const data: { userId: string } = JSON.parse(msgStr)
if (!data.userId) {
console.log('No userId found in message')
res.status(400).send('Bad Request')

View File

@ -40,7 +40,7 @@ import { ApolloServer } from 'apollo-server-express'
import { pdfAttachmentsRouter } from './routers/svc/pdf_attachments'
import { corsConfig } from './utils/corsConfig'
import { initElasticsearch } from './elastic'
import { pageServiceRouter } from './routers/svc/pages'
import { uploadServiceRouter } from './routers/svc/upload'
const PORT = process.env.PORT || 4000
@ -98,7 +98,7 @@ export const createApp = (): {
app.use('/svc/pubsub/links', linkServiceRouter())
app.use('/svc/pubsub/newsletters', newsletterServiceRouter())
app.use('/svc/pubsub/emails', emailsServiceRouter())
app.use('/svc/pubsub/pages', pageServiceRouter())
app.use('/svc/pubsub/upload', uploadServiceRouter())
app.use('/svc/reminders', remindersServiceRouter())
app.use('/svc/pdf-attachments', pdfAttachmentsRouter())