From bd8adaac93d1a1ac9ed69b8ef73aeb87405e3104 Mon Sep 17 00:00:00 2001 From: ascariandrea Date: Mon, 19 Sep 2022 12:46:53 +0200 Subject: [PATCH] fix(yttrex): codec configuration for metadata logger ui --- platforms/yttrex/extension/src/app/index.ts | 36 ++++++++- .../yttrex/extension/src/models/HubEvent.ts | 79 +++++++++++++++---- 2 files changed, 98 insertions(+), 17 deletions(-) diff --git a/platforms/yttrex/extension/src/app/index.ts b/platforms/yttrex/extension/src/app/index.ts index 7a5f20773..87f5a2888 100644 --- a/platforms/yttrex/extension/src/app/index.ts +++ b/platforms/yttrex/extension/src/app/index.ts @@ -3,10 +3,12 @@ import { boot } from '@shared/extension/app'; import { bo } from '@shared/extension/utils/browser.utils'; -import { youtubeDomainRegExp } from '@yttrex/shared/parsers/selectors'; import { getParser } from '@yttrex/shared/parsers/parser/html'; +import { youtubeDomainRegExp } from '@yttrex/shared/parsers/selectors'; +import * as E from 'fp-ts/lib/Either'; import * as hubHandlers from '../handlers/events'; import ytHub from '../handlers/hub'; +import { ContributionEvent, NEW_VIDEO_EVENT } from '../models/HubEvent'; import { onLocationChange, watchedPaths, ytLogger, ytTrexActions } from './app'; bo.runtime.sendMessage({ type: 'chromeConfig' }, (config) => { @@ -38,7 +40,37 @@ bo.runtime.sendMessage({ type: 'chromeConfig' }, (config) => { }, onAuthenticated: ytTrexActions, ui: { - getParser, + metadataLogger: { + getParser: (db) => { + return getParser( + db, + { contribution: 'contributions', metadata: 'metadata' }, + (contribution) => ({ + ...contribution, + jsdom: new DOMParser().parseFromString( + contribution.html.html, + 'text/html' + ), + }) + ); + }, + mapEvent: (e: any) => { + if (e.type === NEW_VIDEO_EVENT.value) { + return { + ...e, + }; + } + return e; + }, + decode: (e: any) => { + // return E.right(null) for events that don't need parsing + if (['APIEvent', 'SyncResponse', 'WindowUnload'].includes(e.type)) { + return E.right(null); + } + + return ContributionEvent.decode(e); + }, + }, }, }); } catch (e) { diff --git a/platforms/yttrex/extension/src/models/HubEvent.ts b/platforms/yttrex/extension/src/models/HubEvent.ts index c9d0d4d1d..a53785166 100644 --- a/platforms/yttrex/extension/src/models/HubEvent.ts +++ b/platforms/yttrex/extension/src/models/HubEvent.ts @@ -1,19 +1,68 @@ -import { HubEvent, HubEventBase } from '@shared/extension/models/HubEvent'; +import { HubEvent } from '@shared/extension/models/HubEvent'; +import * as t from 'io-ts'; -export interface NewVideoEvent extends HubEventBase { - type: 'NewVideo'; - payload: { - type: number | undefined; - element: string; - size: number; - href: string; - randomUUID: string; - }; -} +export const NEW_VIDEO_EVENT = t.literal('NewVideo'); +export type NEW_VIDEO_EVENT = t.TypeOf; +export const NewVideoEvent = t.type( + { + type: NEW_VIDEO_EVENT, + payload: t.type( + { + type: t.string, + element: t.string, + size: t.number, + href: t.string, + html: t.string, + randomUUID: t.string, + feedCounter: t.number, + videoCounter: t.number, + rect: t.type( + { + height: t.number, + width: t.number, + x: t.number, + y: t.number, + bottom: t.union([t.number, t.undefined]), + left: t.union([t.number, t.undefined]), + right: t.union([t.number, t.undefined]), + top: t.union([t.number, t.undefined]), + }, + 'DOMRect' + ), + }, + 'NewVideoPayload' + ), + }, + 'NewVideoEvent' +); +export type NewVideoEvent = t.TypeOf; -export interface NewLeafEvent extends HubEventBase { - type: 'leaf'; - payload: {}; -} +export const LEAF_EVENT = t.literal('leaf'); +export type LEAF_EVENT = t.TypeOf; +export const NewLeafEvent = t.type( + { + type: LEAF_EVENT, + payload: t.type( + { + type: t.string, + element: t.string, + size: t.number, + href: t.string, + html: t.string, + randomUUID: t.string, + feedCounter: t.number, + videoCounter: t.number, + }, + 'NewVideoPayload' + ), + }, + 'NewLeafEvent' +); +export type NewLeafEvent = t.TypeOf; + +export const ContributionEvent = t.union( + [NewVideoEvent, NewLeafEvent], + 'ContributionEvent' +); export type YTHubEvent = HubEvent | NewVideoEvent | NewLeafEvent;