forked from Sofie-Automation/sofie-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtriggersContext.ts
More file actions
229 lines (203 loc) · 7.24 KB
/
triggersContext.ts
File metadata and controls
229 lines (203 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import {
TriggersAsyncCollection,
TriggersContext,
TriggerTrackerComputation,
} from '@sofie-automation/meteor-lib/dist/triggers/triggersContext'
import { SINGLE_USE_TOKEN_SALT } from '@sofie-automation/meteor-lib/dist/api/userActions'
import { assertNever, getHash } from '@sofie-automation/corelib/dist/lib'
import type { Time } from '@sofie-automation/shared-lib/dist/lib/lib'
import { ProtectedString, protectString } from '@sofie-automation/corelib/dist/protectedString'
import { getCurrentTime } from '../../lib/lib'
import { MeteorCall } from '../methods'
import { ClientAPI } from '@sofie-automation/meteor-lib/dist/api/client'
import { UserAction } from '@sofie-automation/meteor-lib/dist/userAction'
import { TFunction } from 'i18next'
import { logger } from '../../logging'
import { IBaseFilterLink, IRundownPlaylistFilterLink } from '@sofie-automation/blueprints-integration'
import { PartId, StudioId } from '@sofie-automation/corelib/dist/dataModel/Ids'
import { DummyReactiveVar } from '@sofie-automation/meteor-lib/dist/triggers/reactive-var'
import { ReactivePlaylistActionContext } from '@sofie-automation/meteor-lib/dist/triggers/actionFactory'
import { FindOneOptions, FindOptions, MongoQuery } from '@sofie-automation/corelib/dist/mongo'
import {
DBRundownPlaylist,
SelectedPartInstance,
} from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist/RundownPlaylist'
import {
AdLibActions,
AdLibPieces,
PartInstances,
Parts,
RundownBaselineAdLibActions,
RundownBaselineAdLibPieces,
RundownPlaylists,
Rundowns,
Segments,
} from '../../collections'
import { DBPart } from '@sofie-automation/corelib/dist/dataModel/Part'
import { DBPartInstance } from '@sofie-automation/corelib/dist/dataModel/PartInstance'
import { AsyncOnlyReadOnlyMongoCollection } from '../../collections/collection'
export function hashSingleUseToken(token: string): string {
return getHash(SINGLE_USE_TOKEN_SALT + token)
}
class MeteorTriggersCollectionWrapper<
DBInterface extends { _id: ProtectedString<any> },
> implements TriggersAsyncCollection<DBInterface> {
readonly #collection: AsyncOnlyReadOnlyMongoCollection<DBInterface>
constructor(collection: AsyncOnlyReadOnlyMongoCollection<DBInterface>) {
this.#collection = collection
}
async findFetchAsync(
_computation: TriggerTrackerComputation | null,
selector: MongoQuery<DBInterface>,
options?: FindOptions<DBInterface>
): Promise<Array<DBInterface>> {
// Note: the _computation is not used, since we are not using Tracker server-side
return this.#collection.findFetchAsync(selector, options)
}
async findOneAsync(
_computation: TriggerTrackerComputation | null,
selector: MongoQuery<DBInterface> | DBInterface['_id'],
options?: FindOneOptions<DBInterface>
): Promise<DBInterface | undefined> {
// Note: the _computation is not used, since we are not using Tracker server-side
return this.#collection.findOneAsync(selector, options)
}
}
export const MeteorTriggersContext: TriggersContext = {
MeteorCall,
logger,
isClient: false,
AdLibActions: new MeteorTriggersCollectionWrapper(AdLibActions),
AdLibPieces: new MeteorTriggersCollectionWrapper(AdLibPieces),
Parts: new MeteorTriggersCollectionWrapper(Parts),
RundownBaselineAdLibActions: new MeteorTriggersCollectionWrapper(RundownBaselineAdLibActions),
RundownBaselineAdLibPieces: new MeteorTriggersCollectionWrapper(RundownBaselineAdLibPieces),
RundownPlaylists: new MeteorTriggersCollectionWrapper(RundownPlaylists),
Rundowns: new MeteorTriggersCollectionWrapper(Rundowns),
Segments: new MeteorTriggersCollectionWrapper(Segments),
hashSingleUseToken,
doUserAction: <Result>(
_t: TFunction,
userEvent: string,
_action: UserAction,
fcn: (event: string, timeStamp: Time) => Promise<ClientAPI.ClientResponse<Result>>,
callback?: (err: any, res?: Result) => void | boolean,
_okMessage?: string
): void => {
fcn(userEvent, getCurrentTime()).then(
(value) =>
typeof callback === 'function' &&
(ClientAPI.isClientResponseSuccess(value) ? callback(undefined, value.result) : callback(value)),
(reason) => typeof callback === 'function' && callback(reason)
)
},
withComputation: async (_computation, func) => {
// Note: the _computation is not used, since we are not using Tracker server-side
return func()
},
memoizedIsolatedAutorun: async <TArgs extends any[], TRes>(
computation: TriggerTrackerComputation | null,
fnc: (computation: TriggerTrackerComputation | null, ...args: TArgs) => Promise<TRes>,
_functionName: string,
...params: TArgs
): Promise<TRes> => {
return fnc(computation, ...params)
},
createContextForRundownPlaylistChain,
}
async function createContextForRundownPlaylistChain(
studioId: StudioId,
filterChain: IBaseFilterLink[]
): Promise<ReactivePlaylistActionContext | undefined> {
const playlist = await rundownPlaylistFilter(
studioId,
filterChain.filter((link) => link.object === 'rundownPlaylist') as IRundownPlaylistFilterLink[]
)
if (!playlist) return undefined
const [currentPartInfo, nextPartInfo] = await Promise.all([
fetchInfoForSelectedPart(playlist.currentPartInfo),
fetchInfoForSelectedPart(playlist.nextPartInfo),
])
return {
studioId: new DummyReactiveVar(studioId),
rundownPlaylistId: new DummyReactiveVar(playlist?._id),
rundownPlaylist: new DummyReactiveVar(playlist),
currentRundownId: new DummyReactiveVar(
playlist.currentPartInfo?.rundownId ?? playlist.rundownIdsInOrder[0] ?? null
),
currentPartId: new DummyReactiveVar(currentPartInfo?.partId ?? null),
currentSegmentPartIds: new DummyReactiveVar(currentPartInfo?.segmentPartIds ?? []),
nextPartId: new DummyReactiveVar(nextPartInfo?.partId ?? null),
nextSegmentPartIds: new DummyReactiveVar(nextPartInfo?.segmentPartIds ?? []),
currentPartInstanceId: new DummyReactiveVar(playlist.currentPartInfo?.partInstanceId ?? null),
}
}
async function fetchInfoForSelectedPart(partInfo: SelectedPartInstance | null): Promise<{
partId: PartId
segmentPartIds: PartId[]
} | null> {
if (!partInfo) return null
const partInstance = (await PartInstances.findOneAsync(partInfo.partInstanceId, {
projection: {
// @ts-expect-error deep property
'part._id': 1,
segmentId: 1,
},
})) as (Pick<DBPartInstance, 'segmentId'> & { part: Pick<DBPart, '_id'> }) | null
if (!partInstance) return null
const partId = partInstance.part._id
const segmentPartIds = await Parts.findFetchAsync(
{
segmentId: partInstance.segmentId,
},
{
projection: {
_id: 1,
},
}
).then((parts) => parts.map((part) => part._id))
return {
partId,
segmentPartIds,
}
}
async function rundownPlaylistFilter(
studioId: StudioId,
filterChain: IRundownPlaylistFilterLink[]
): Promise<DBRundownPlaylist | undefined> {
const selector: MongoQuery<DBRundownPlaylist> = {
$and: [
{
studioId,
},
],
}
filterChain.forEach((link) => {
switch (link.field) {
case 'activationId':
selector['activationId'] = {
$exists: link.value,
}
break
case 'name':
selector['name'] = {
$regex: link.value,
}
break
case 'studioId':
selector['$and']?.push({
studioId: {
$eq: protectString(link.value),
},
})
break
case 'rehearsal':
selector['rehearsal'] = link.value
break
default:
assertNever(link)
break
}
})
return RundownPlaylists.findOneAsync(selector)
}