forked from Sofie-Automation/sofie-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.ts
More file actions
215 lines (197 loc) · 6.86 KB
/
api.ts
File metadata and controls
215 lines (197 loc) · 6.86 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
import { Meteor } from 'meteor/meteor'
import { check } from '../../lib/check'
import { registerClassToMeteorMethods } from '../../methods'
import { NewStudiosAPI, StudiosAPIMethods } from '@sofie-automation/meteor-lib/dist/api/studios'
import { DBStudio } from '@sofie-automation/corelib/dist/dataModel/Studio'
import { literal, getRandomId, protectString } from '../../lib/tempLib'
import { lazyIgnore } from '../../lib/lib'
import { stringifyError } from '@sofie-automation/shared-lib/dist/lib/stringifyError'
import {
ExpectedPackages,
ExpectedPackageWorkStatuses,
ExternalMessageQueue,
MediaObjects,
Notifications,
PackageContainerPackageStatuses,
PackageInfos,
PeripheralDevices,
RundownPlaylists,
Rundowns,
Studios,
Timeline,
} from '../../collections'
import { MethodContextAPI, MethodContext } from '../methodContext'
import { wrapDefaultObject } from '@sofie-automation/corelib/dist/settings/objectWithOverrides'
import { OrganizationId, PeripheralDeviceId, StudioId } from '@sofie-automation/corelib/dist/dataModel/Ids'
import { logger } from '../../logging'
import { DEFAULT_MINIMUM_TAKE_SPAN } from '@sofie-automation/shared-lib/dist/core/constants'
import { UserPermissions } from '@sofie-automation/meteor-lib/dist/userPermissions'
import { assertConnectionHasOneOfPermissions } from '../../security/auth'
const PERMISSIONS_FOR_MANAGE_STUDIOS: Array<keyof UserPermissions> = ['configure']
async function insertStudio(context: MethodContext, newId?: StudioId): Promise<StudioId> {
if (newId) check(newId, String)
assertConnectionHasOneOfPermissions(context.connection, ...PERMISSIONS_FOR_MANAGE_STUDIOS)
return insertStudioInner(null, newId)
}
export async function insertStudioInner(organizationId: OrganizationId | null, newId?: StudioId): Promise<StudioId> {
const studioCount = await Studios.countDocuments()
if (studioCount > 0) {
throw new Meteor.Error(
400,
`Only one studio is supported per installation (there are currently ${studioCount})`
)
}
return Studios.insertAsync(
literal<DBStudio>({
_id: newId || getRandomId(),
name: 'New Studio',
organizationId: organizationId,
// blueprintId?: BlueprintId
mappingsWithOverrides: wrapDefaultObject({}),
supportedShowStyleBase: [],
blueprintConfigWithOverrides: wrapDefaultObject({}),
// testToolsConfig?: ITestToolsConfig
settingsWithOverrides: wrapDefaultObject({
frameRate: 25,
mediaPreviewsUrl: '',
minimumTakeSpan: DEFAULT_MINIMUM_TAKE_SPAN,
allowHold: false,
allowPieceDirectPlay: false,
enableBuckets: true,
enableEvaluationForm: true,
}),
_rundownVersionHash: '',
routeSetsWithOverrides: wrapDefaultObject({}),
routeSetExclusivityGroupsWithOverrides: wrapDefaultObject({}),
packageContainersWithOverrides: wrapDefaultObject({}),
thumbnailContainerIds: [],
previewContainerIds: [],
peripheralDeviceSettings: {
deviceSettings: wrapDefaultObject({}),
playoutDevices: wrapDefaultObject({}),
ingestDevices: wrapDefaultObject({}),
inputDevices: wrapDefaultObject({}),
},
lastBlueprintConfig: undefined,
lastBlueprintFixUpHash: undefined,
})
)
}
async function removeStudio(context: MethodContext, studioId: StudioId): Promise<void> {
check(studioId, String)
assertConnectionHasOneOfPermissions(context.connection, ...PERMISSIONS_FOR_MANAGE_STUDIOS)
const studioCount = await Studios.countDocuments()
if (studioCount === 1) {
throw new Meteor.Error(
400,
`The last studio in the system cannot be deleted (there must be at least one studio)`
)
}
const studio = await Studios.findOneAsync(studioId)
if (!studio) throw new Meteor.Error(404, `Studio "${studioId}" not found`)
// allowed to remove?
const rundown = await Rundowns.findOneAsync({ studioId: studio._id }, { projection: { _id: 1 } })
if (rundown)
throw new Meteor.Error(404, `Can't remove studio "${studioId}", because the rundown "${rundown._id}" is in it.`)
const playlist = await RundownPlaylists.findOneAsync({ studioId: studio._id }, { projection: { _id: 1 } })
if (playlist)
throw new Meteor.Error(
404,
`Can't remove studio "${studioId}", because the rundownPlaylist "${playlist._id}" is in it.`
)
const peripheralDevice = await PeripheralDevices.findOneAsync(
{ 'studioAndConfigId.studioId': studio._id },
{ projection: { _id: 1 } }
)
if (peripheralDevice)
throw new Meteor.Error(
404,
`Can't remoce studio "${studioId}", because the peripheralDevice "${peripheralDevice._id}" is in it.`
)
// This is allowed to mutate the job-worker 'owned' collections, as at this point the thread for that studio is about to be destroyed
await Promise.all([
Studios.removeAsync(studio._id),
// Studios.remove({ studioId: studio._id }) // TODO - what was this supposed to be?
ExternalMessageQueue.removeAsync({ studioId: studio._id }),
MediaObjects.removeAsync({ studioId: studio._id }),
Timeline.mutableCollection.removeAsync({ studioId: studio._id }),
ExpectedPackages.mutableCollection.removeAsync({ studioId: studio._id }),
ExpectedPackageWorkStatuses.removeAsync({ studioId: studio._id }),
PackageInfos.removeAsync({ studioId: studio._id }),
PackageContainerPackageStatuses.removeAsync({ studioId: studio._id }),
Notifications.removeAsync({ 'relatedTo.studioId': studio._id }),
])
}
class ServerStudiosAPI extends MethodContextAPI implements NewStudiosAPI {
async insertStudio() {
return insertStudio(this)
}
async removeStudio(studioId: StudioId) {
return removeStudio(this, studioId)
}
async assignConfigToPeripheralDevice(studioId: StudioId, configId: string, deviceId: PeripheralDeviceId | null) {
assertConnectionHasOneOfPermissions(this.connection, ...PERMISSIONS_FOR_MANAGE_STUDIOS)
// Unassign other uses
await PeripheralDevices.updateAsync(
{
studioAndConfigId: {
studioId,
configId,
},
_id: { $ne: deviceId ?? protectString('') },
},
{
$unset: {
studioAndConfigId: 1,
},
},
{
multi: true,
}
)
if (deviceId) {
// Set for the new one
await PeripheralDevices.updateAsync(deviceId, {
$set: {
studioAndConfigId: {
studioId,
configId,
},
},
})
}
}
}
registerClassToMeteorMethods(StudiosAPIMethods, ServerStudiosAPI, false)
// Set up a watcher for updating the mappingsHash whenever a mapping or route is changed:
function triggerUpdateStudioMappingsHash(studioId: StudioId) {
lazyIgnore(
`triggerUpdateStudio_${studioId}`,
() => {
Studios.updateAsync(studioId, {
$set: {
mappingsHash: getRandomId(),
},
}).catch((e) => {
logger.error(`triggerUpdateStudioMappingsHash: ${stringifyError(e)}`)
})
},
10
)
}
Meteor.startup(async () => {
await Studios.observeChanges(
{},
{
added: triggerUpdateStudioMappingsHash,
changed: triggerUpdateStudioMappingsHash,
removed: triggerUpdateStudioMappingsHash,
},
{
projection: {
mappingsWithOverrides: 1,
routeSetsWithOverrides: 1,
},
}
)
})