diff --git a/packages/api/src/datalayer/pubsub.ts b/packages/api/src/datalayer/pubsub.ts index 690e9376b..e4f397acd 100644 --- a/packages/api/src/datalayer/pubsub.ts +++ b/packages/api/src/datalayer/pubsub.ts @@ -2,7 +2,6 @@ import { PubSub } from '@google-cloud/pubsub' import { env } from '../env' import { ReportType } from '../generated/graphql' import express from 'express' -import { Page } from '../elastic/types' export const createPubSubClient = (): PubsubClient => { const client = new PubSub() @@ -37,17 +36,35 @@ export const createPubSubClient = (): PubsubClient => { Buffer.from(JSON.stringify({ userId, email, name, username })) ) }, - pageUpdated: (page: Partial, userId: string): Promise => { + entityCreated: ( + type: EntityType, + data: T, + userId: string + ): Promise => { return publish( - 'pageUpdated', - Buffer.from(JSON.stringify({ ...page, userId })) + 'entityCreated', + Buffer.from(JSON.stringify({ type, userId, ...data })) ) }, - pageCreated: (page: Page): Promise => { - return publish('pageCreated', Buffer.from(JSON.stringify(page))) + entityUpdated: ( + type: EntityType, + data: T, + userId: string + ): Promise => { + return publish( + 'entityUpdated', + Buffer.from(JSON.stringify({ type, userId, ...data })) + ) }, - pageDeleted: (id: string, userId: string): Promise => { - return publish('pageDeleted', Buffer.from(JSON.stringify({ id, userId }))) + entityDeleted: ( + type: EntityType, + id: string, + userId: string + ): Promise => { + return publish( + 'entityDeleted', + Buffer.from(JSON.stringify({ type, id, userId })) + ) }, reportSubmitted: ( submitterId: string, @@ -65,6 +82,12 @@ export const createPubSubClient = (): PubsubClient => { } } +export enum EntityType { + PAGE = 'page', + HIGHLIGHT = 'highlight', + LABEL = 'label', +} + export interface PubsubClient { userCreated: ( userId: string, @@ -72,9 +95,9 @@ export interface PubsubClient { name: string, username: string ) => Promise - pageCreated: (page: Page) => Promise - pageUpdated: (page: Partial, userId: string) => Promise - pageDeleted: (id: string, userId: string) => Promise + entityCreated: (type: EntityType, data: T, userId: string) => Promise + entityUpdated: (type: EntityType, data: T, userId: string) => Promise + entityDeleted: (type: EntityType, id: string, userId: string) => Promise reportSubmitted( submitterId: string | undefined, itemUrl: string, diff --git a/packages/api/src/elastic/highlights.ts b/packages/api/src/elastic/highlights.ts index 5a499237e..934563ca3 100644 --- a/packages/api/src/elastic/highlights.ts +++ b/packages/api/src/elastic/highlights.ts @@ -8,6 +8,7 @@ import { import { ResponseError } from '@elastic/elasticsearch/lib/errors' import { client, INDEX_ALIAS } from './index' import { SortBy, SortOrder, SortParams } from '../utils/search' +import { EntityType } from '../datalayer/pubsub' export const addHighlightToPage = async ( id: string, @@ -35,7 +36,15 @@ export const addHighlightToPage = async ( retry_on_conflict: 3, }) - return body.result === 'updated' + if (body.result !== 'updated') return false + + await ctx.pubsub.entityCreated( + EntityType.HIGHLIGHT, + highlight, + ctx.uid + ) + + return true } catch (e) { if ( e instanceof ResponseError && @@ -125,7 +134,11 @@ export const deleteHighlight = async ( refresh: ctx.refresh, }) - return !!body.updated + if (body.result !== 'updated') return false + + await ctx.pubsub.entityDeleted(EntityType.HIGHLIGHT, highlightId, ctx.uid) + + return true } catch (e) { console.error('failed to delete a highlight in elastic', e) @@ -266,7 +279,15 @@ export const updateHighlight = async ( refresh: ctx.refresh, }) - return !!body.updated + if (body.result !== 'updated') return false + + await ctx.pubsub.entityUpdated( + EntityType.HIGHLIGHT, + highlight, + ctx.uid + ) + + return true } catch (e) { if ( e instanceof ResponseError && diff --git a/packages/api/src/elastic/labels.ts b/packages/api/src/elastic/labels.ts index ec1ac5188..2bb00bba8 100644 --- a/packages/api/src/elastic/labels.ts +++ b/packages/api/src/elastic/labels.ts @@ -1,5 +1,6 @@ import { Label, PageContext } from './types' import { client, INDEX_ALIAS } from './index' +import { EntityType } from '../datalayer/pubsub' export const addLabelInPage = async ( id: string, @@ -27,9 +28,17 @@ export const addLabelInPage = async ( retry_on_conflict: 3, }) - return body.result === 'updated' + if (body.result !== 'updated') return false + + await ctx.pubsub.entityCreated