diff --git a/packages/api/src/events/highlight_subscriber.ts b/packages/api/src/events/highlight_subscriber.ts deleted file mode 100644 index f5725bd3b..000000000 --- a/packages/api/src/events/highlight_subscriber.ts +++ /dev/null @@ -1,103 +0,0 @@ -import { - EntityManager, - EntitySubscriberInterface, - EventSubscriber, - InsertEvent, - ObjectLiteral, - RemoveEvent, - UpdateEvent, -} from 'typeorm' -import { Highlight } from '../entity/highlight' -import { Label } from '../entity/label' -import { LibraryItem } from '../entity/library_item' -import { createPubSubClient, EntityType } from '../pubsub' - -@EventSubscriber() -export class HighlightSubscriber - implements EntitySubscriberInterface -{ - private readonly pubsubClient = createPubSubClient() - - async updateLibraryItem(manager: EntityManager, libraryItemId: string) { - // get all the highlights belonging to the library_item - const highlights = await manager.getRepository(Highlight).find({ - where: { libraryItem: { id: libraryItemId } }, - relations: { - labels: true, - }, - }) - - const highlightLabels: string[] = [] - const highlightAnnotations: string[] = [] - - // for each highlight, add the lowercased label names to highlight_labels - // and the annotation to highlight_annotations - highlights.forEach((highlight) => { - highlight.labels && - highlightLabels.push( - ...highlight.labels.map((label) => label.name.toLowerCase()) - ) - highlightAnnotations.push(highlight.annotation || '') - }) - - // update highlight_labels and highlight_annotations on library_item - await manager.update(LibraryItem, libraryItemId, { - highlightAnnotations, - highlightLabels, - }) - } - - listenTo() { - return Highlight - } - - async afterInsert(event: InsertEvent): Promise { - await this.updateLibraryItem(event.manager, event.entity.libraryItem.id) - - await this.pubsubClient.entityCreated( - EntityType.HIGHLIGHT, - event.entity, - event.entity.libraryItem.user.id - ) - } - - async afterUpdate(event: UpdateEvent): Promise { - if (event.entity) { - await this.updateLibraryItem( - event.manager, - event.databaseEntity.libraryItem.id - ) - - // publish update event - await this.pubsubClient.entityUpdated( - EntityType.HIGHLIGHT, - { ...event.entity, libraryItem: event.databaseEntity.libraryItem }, - event.databaseEntity.libraryItem.user.id - ) - - // publish label added event if a label was added - if (event.entity.labels) { - await this.pubsubClient.entityCreated