@@ -26,11 +26,12 @@ import { MountedAdLibTriggerType } from '../api/MountedTriggers.js'
2626import { assertNever , generateTranslation } from '@sofie-automation/corelib/dist/lib'
2727import { FindOptions } from '../collections/lib.js'
2828import { TriggersContext , TriggerTrackerComputation } from './triggersContext.js'
29+ import { unprotectString } from '@sofie-automation/corelib/dist/protectedString'
2930
3031export type AdLibFilterChainLink = IRundownPlaylistFilterLink | IGUIContextFilterLink | IAdLibFilterLink
3132
3233/** This is a compiled Filter type, targetting a particular MongoCollection */
33- type CompiledFilter < T > = {
34+ type CompiledAdLibFilter < T > = {
3435 selector : MongoQuery < T >
3536 options : FindOptions < T >
3637 pick : number | undefined
@@ -306,7 +307,7 @@ type AdLibActionType = RundownBaselineAdLibAction | AdLibAction
306307function compileAdLibActionFilter (
307308 filterChain : IAdLibFilterLink [ ] ,
308309 sourceLayers : SourceLayers
309- ) : CompiledFilter < AdLibActionType > {
310+ ) : CompiledAdLibFilter < AdLibActionType > {
310311 const selector : MongoQuery < AdLibActionType > = { }
311312 const options : FindOptions < AdLibActionType > = { }
312313 let pick : number | undefined = undefined
@@ -405,7 +406,7 @@ type AdLibPieceType =
405406function compileAdLibPieceFilter (
406407 filterChain : IAdLibFilterLink [ ] ,
407408 sourceLayers : SourceLayers
408- ) : CompiledFilter < AdLibPieceType > {
409+ ) : CompiledAdLibFilter < AdLibPieceType > {
409410 const selector : MongoQuery < AdLibPieceType > = { }
410411 const options : FindOptions < AdLibPieceType > = { }
411412 let pick : number | undefined = undefined
@@ -496,6 +497,61 @@ function compileAdLibPieceFilter(
496497 }
497498}
498499
500+ type RundownSelector = {
501+ activationId : boolean | undefined
502+ name : RegExp | undefined
503+ studioId : string | undefined
504+ rehearsal : boolean | undefined
505+ }
506+
507+ function compileRundownPlaylistFilter ( filterChain : IRundownPlaylistFilterLink [ ] ) : {
508+ selector : RundownSelector
509+ /**
510+ * The query compiler has determined that this filter will always match
511+ * it's safe to skip it entirely.
512+ */
513+ matchAll ?: true
514+ } {
515+ const selector : RundownSelector = {
516+ activationId : undefined ,
517+ name : undefined ,
518+ studioId : undefined ,
519+ rehearsal : undefined ,
520+ }
521+
522+ if ( filterChain . length === 0 ) {
523+ // no filter, accept all
524+ return {
525+ selector,
526+ matchAll : true ,
527+ }
528+ }
529+
530+ filterChain . forEach ( ( link ) => {
531+ switch ( link . field ) {
532+ case 'activationId' :
533+ selector . activationId = link . value
534+ return
535+ case 'name' :
536+ selector . name = new RegExp ( link . value )
537+ return
538+ case 'studioId' :
539+ selector . studioId = link . value
540+ return
541+ case 'rehearsal' :
542+ selector . rehearsal = link . value
543+ return
544+ default :
545+ assertNever ( link )
546+ return
547+ }
548+ } )
549+
550+ return {
551+ selector,
552+ }
553+ }
554+
499555/**
500556 * Compile the filter chain and return a reactive function that will return the result set for this adLib filter
501557 * @param filterChain
@@ -508,6 +564,13 @@ export function compileAdLibFilter(
508564 sourceLayers : SourceLayers
509565) : ( context : ReactivePlaylistActionContext , computation : TriggerTrackerComputation | null ) => Promise < IWrappedAdLib [ ] > {
510566 const onlyAdLibLinks = filterChain . filter ( ( link ) => link . object === 'adLib' ) as IAdLibFilterLink [ ]
567+ const onlyRundownPlaylistLinks = filterChain . filter (
568+ ( link ) => link . object === 'rundownPlaylist'
569+ ) as IRundownPlaylistFilterLink [ ]
570+
571+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
572+ // @ts -ignore ignore unused
573+ const rundownPlaylistFilter = compileRundownPlaylistFilter ( onlyRundownPlaylistLinks )
511574 const adLibPieceTypeFilter = compileAdLibPieceFilter ( onlyAdLibLinks , sourceLayers )
512575 const adLibActionTypeFilter = compileAdLibActionFilter ( onlyAdLibLinks , sourceLayers )
513576
@@ -556,6 +619,35 @@ export function compileAdLibFilter(
556619 }
557620 }
558621
622+ {
623+ const matchAll = rundownPlaylistFilter . matchAll
624+ const currentRundownPlaylist = context . rundownPlaylist . get ( computation )
625+
626+ const activationStateMatches =
627+ rundownPlaylistFilter . selector . activationId !== undefined
628+ ? ( currentRundownPlaylist ?. activationId !== undefined ) ===
629+ rundownPlaylistFilter . selector . activationId
630+ : true
631+ const nameMatches =
632+ rundownPlaylistFilter . selector . name !== undefined
633+ ? currentRundownPlaylist ?. name . match ( rundownPlaylistFilter . selector . name ) !== null
634+ : true
635+ const studioMatches =
636+ rundownPlaylistFilter . selector . studioId !== undefined
637+ ? unprotectString ( currentRundownPlaylist ?. studioId ) === rundownPlaylistFilter . selector . studioId
638+ : true
639+ const rehearsalMatches =
640+ rundownPlaylistFilter . selector . rehearsal !== undefined
641+ ? currentRundownPlaylist ?. rehearsal === rundownPlaylistFilter . selector . rehearsal
642+ : true
643+
644+ if ( ! matchAll ) {
645+ if ( ! activationStateMatches || ! nameMatches || ! studioMatches || ! rehearsalMatches ) {
646+ return [ ]
647+ }
648+ }
649+ }
650+
559651 {
560652 let skip = adLibPieceTypeFilter . skip
561653 const currentNextOverride : MongoQuery < AdLibPieceType > = { }
0 commit comments