|
| 1 | +import { Meteor } from 'meteor/meteor' |
| 2 | +import { SnapshotId } from '@sofie-automation/corelib/dist/dataModel/Ids' |
| 3 | +import { check } from 'meteor/check' |
| 4 | +import { APIFactory, APIRegisterHook, ServerAPIContext } from './types' |
| 5 | +import { logger } from '../../../logging' |
| 6 | +import { storeRundownPlaylistSnapshot, storeSystemSnapshot } from '../../snapshot' |
| 7 | +import { makeIdempotent, makeRateLimited } from './middlewares' |
| 8 | +import { protectString } from '@sofie-automation/corelib/dist/protectedString' |
| 9 | +import { playlistSnapshotOptionsFrom, systemSnapshotOptionsFrom } from './typeConversion' |
| 10 | +import { |
| 11 | + APIPlaylistSnapshotOptions, |
| 12 | + APISnapshotType, |
| 13 | + APISystemSnapshotOptions, |
| 14 | + SnapshotsRestAPI, |
| 15 | +} from '../../../lib/rest/v1' |
| 16 | +import { ClientAPI } from '@sofie-automation/meteor-lib/dist/api/client' |
| 17 | +import { checkAccessToPlaylist } from '../../../security/check' |
| 18 | + |
| 19 | +export class SnapshotsServerAPI implements SnapshotsRestAPI { |
| 20 | + constructor(private context: ServerAPIContext) {} |
| 21 | + |
| 22 | + async storeSystemSnapshot( |
| 23 | + connection: Meteor.Connection, |
| 24 | + _event: string, |
| 25 | + options: APISystemSnapshotOptions |
| 26 | + ): Promise<ClientAPI.ClientResponse<SnapshotId>> { |
| 27 | + check(options.reason, String) |
| 28 | + return ClientAPI.responseSuccess( |
| 29 | + await storeSystemSnapshot( |
| 30 | + this.context.getMethodContext(connection), |
| 31 | + systemSnapshotOptionsFrom(options), |
| 32 | + options.reason |
| 33 | + ) |
| 34 | + ) |
| 35 | + } |
| 36 | + |
| 37 | + async storePlaylistSnapshot( |
| 38 | + connection: Meteor.Connection, |
| 39 | + _event: string, |
| 40 | + options: APIPlaylistSnapshotOptions |
| 41 | + ): Promise<ClientAPI.ClientResponse<SnapshotId>> { |
| 42 | + const playlistId = protectString(options.rundownPlaylistId) |
| 43 | + check(playlistId, String) |
| 44 | + check(options.reason, String) |
| 45 | + const access = await checkAccessToPlaylist(connection, playlistId) |
| 46 | + return ClientAPI.responseSuccess( |
| 47 | + await storeRundownPlaylistSnapshot(access, playlistSnapshotOptionsFrom(options), options.reason) |
| 48 | + ) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +class SnapshotsAPIFactory implements APIFactory<SnapshotsRestAPI> { |
| 53 | + createServerAPI(context: ServerAPIContext): SnapshotsRestAPI { |
| 54 | + return new SnapshotsServerAPI(context) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +const SNAPSHOT_RESOURCE = 'snapshot' |
| 59 | + |
| 60 | +export function registerRoutes(registerRoute: APIRegisterHook<SnapshotsRestAPI>): void { |
| 61 | + const snapshotsApiFactory = new SnapshotsAPIFactory() |
| 62 | + |
| 63 | + registerRoute<never, APISystemSnapshotOptions | APIPlaylistSnapshotOptions, SnapshotId>( |
| 64 | + 'post', |
| 65 | + '/snapshots', |
| 66 | + new Map(), |
| 67 | + snapshotsApiFactory, |
| 68 | + makeRateLimited( |
| 69 | + makeIdempotent(async (serverAPI, connection, event, _params, body) => { |
| 70 | + if (body.snapshotType === APISnapshotType.SYSTEM) { |
| 71 | + logger.info(`API POST: Store System Snapshot`) |
| 72 | + return await serverAPI.storeSystemSnapshot(connection, event, body) |
| 73 | + } else if (body.snapshotType === APISnapshotType.PLAYLIST) { |
| 74 | + logger.info(`API POST: Store Playlist Snapshot`) |
| 75 | + return await serverAPI.storePlaylistSnapshot(connection, event, body) |
| 76 | + } |
| 77 | + throw new Meteor.Error(400, `Invalid snapshot type`) |
| 78 | + }), |
| 79 | + SNAPSHOT_RESOURCE |
| 80 | + ) |
| 81 | + ) |
| 82 | +} |
0 commit comments