Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PATCH update calls to Media Server #407

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/lib/api/mission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ export default class {
async subscriptionRoles(
name: string,
opts?: Static<typeof MissionOptions>
): Promise<TAKList<Array<Static<typeof MissionSubscriber>>>> {
): Promise<TAKList<Static<typeof MissionSubscriber>>> {
const url = this.#isGUID(name)
? new URL(`/Marti/api/missions/guid/${encodeURIComponent(name)}/subscriptions/roles`, this.api.url)
: new URL(`/Marti/api/missions/${this.#encodeName(name)}/subscriptions/roles`, this.api.url);
Expand Down
59 changes: 57 additions & 2 deletions api/lib/control/video-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Err from '@openaddresses/batch-error';
import Config from '../config.js';
import { Type, Static } from '@sinclair/typebox';
import { VideoLeaseResponse } from '../types.js';
import moment from 'moment';
import fetch from '../fetch.js';

export const Protocols = Type.Object({
Expand Down Expand Up @@ -279,7 +280,6 @@ export default class VideoServiceControl {
proxy?: string;
}): Promise<Static<typeof VideoLeaseResponse>> {
const video = await this.settings();

if (!video.configured) throw new Err(400, null, 'Media Integration is not configured');

const headers = this.headers(video.username, video.password);
Expand Down Expand Up @@ -349,6 +349,57 @@ export default class VideoServiceControl {
return lease;
}

async commit(leaseid: string, body: { name?: string, duration?: number }, opts: {
username: string;
admin: boolean;
}): Promise<Static<typeof VideoLeaseResponse>> {
const video = await this.settings();
if (!video.configured) throw new Err(400, null, 'Media Integration is not configured');

let lease = await this.config.models.VideoLease.from(leaseid);

if (opts.admin) {
lease = await this.config.models.VideoLease.commit(leaseid, {
...body,
expiration: body.duration ? moment().add(body.duration, 'seconds').toISOString() : lease.expiration,
});
} else if (lease.username === opts.username) {
lease = await this.config.models.VideoLease.commit(leaseid, {
...body,
expiration: body.duration ? moment().add(body.duration, 'seconds').toISOString() : lease.expiration,
});
} else {
throw new Err(400, null, 'You can only update a lease you created');
}

try {
await this.path(lease.path);
} catch (err) {
if (err instanceof Err && err.status === 404) {
const url = new URL(`/v3/config/paths/add/${lease.path}`, video.url);
url.port = '9997';

const headers = this.headers(video.username, video.password);
headers.append('Content-Type', 'application/json');

const res = await fetch(url, {
method: 'POST',
headers,
body: JSON.stringify({
name: lease.path
}),
})

if (!res.ok) throw new Err(500, null, await res.text())
} else {
throw err;
}
}


return lease;
}

async path(pathid: string): Promise<Static<typeof PathConfig>> {
const video = await this.settings();
if (!video.configured) throw new Err(400, null, 'Media Integration is not configured');
Expand All @@ -363,7 +414,11 @@ export default class VideoServiceControl {
headers,
});

return await res.typed(PathConfig);
if (res.ok) {
return await res.typed(PathConfig);
} else {
throw new Err(res.status, new Error(await res.text()), 'Media Server Error');
}
}

async delete(leaseid: string): Promise<void> {
Expand Down
21 changes: 4 additions & 17 deletions api/routes/video-lease.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,23 +165,10 @@ export default async function router(schema: Schema, config: Config) {
throw new Err(400, null, 'Only Administrators can request a lease > 16 hours')
}

let lease = await config.models.VideoLease.from(req.params.lease);

if (user.access === AuthUserAccess.ADMIN) {
lease = await config.models.VideoLease.commit(req.params.lease, {
...req.body,
expiration: req.body.duration ? moment().add(req.body.duration, 'seconds').toISOString() : lease.expiration,
});
} else {
if (lease.username === user.email) {
lease = await config.models.VideoLease.commit(req.params.lease, {
...req.body,
expiration: req.body.duration ? moment().add(req.body.duration, 'seconds').toISOString() : lease.expiration,
});
} else {
throw new Err(400, null, 'You can only update a lease you created');
}
}
const lease = await videoControl.commit(req.params.lease, req.body, {
username: user.email,
admin: user.access === AuthUserAccess.ADMIN
});

res.json({
lease,
Expand Down
Loading