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

Lectures: Fix embedding of TUM-Live videos in lecture units #8183

Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,36 @@ function isVideoOnlyTumUrl(url: URL): boolean {
return url?.searchParams.get('video_only') === '1';
}

function videoSourceTransformUrlValidator(control: AbstractControl): ValidationErrors | null {
const invalidVideoUrlError = { invalidVideoUrl: true };
function videoSourceTransformUrlValidator(control: AbstractControl): ValidationErrors | undefined {
const urlValue = control.value;
if (!urlValue) return null;
if (!urlValue) {
return undefined;
}
let parsedUrl, url;
try {
return isTumLiveUrl(new URL(urlValue)) || urlParser.parse(urlValue) ? null : invalidVideoUrlError;
url = new URL(urlValue);
parsedUrl = urlParser.parse(urlValue);
rstief marked this conversation as resolved.
Show resolved Hide resolved
} catch {
return invalidVideoUrlError;
//intentionally empty
}
// The URL is valid if it's a TUM-Live URL or if it can be parsed by the js-video-url-parser.
if ((url && isTumLiveUrl(url)) || parsedUrl) {
return undefined;
}
return { invalidVideoUrl: true };
}

function videoSourceUrlValidator(control: AbstractControl): ValidationErrors | null {
const invalidVideoUrlError = { invalidVideoUrl: true };
function videoSourceUrlValidator(control: AbstractControl): ValidationErrors | undefined {
let url;
try {
const urlValue = control.value;
const url = new URL(urlValue);
if (isTumLiveUrl(url)) return isVideoOnlyTumUrl(url) ? null : invalidVideoUrlError;
return urlParser.parse(urlValue) ? invalidVideoUrlError : null;
url = new URL(control.value);
} catch {
return invalidVideoUrlError;
// intentionally empty
}
if (url && !(isTumLiveUrl(url) && !isVideoOnlyTumUrl(url))) {
rstief marked this conversation as resolved.
Show resolved Hide resolved
return undefined;
}
return { invalidVideoUrl: true };
}

@Component({
Expand Down Expand Up @@ -146,16 +155,14 @@ export class VideoUnitFormComponent implements OnInit, OnChanges {

extractEmbeddedUrl(videoUrl: string) {
const url = new URL(videoUrl);
const isTumUrl = isTumLiveUrl(url);
if (isTumUrl && !isVideoOnlyTumUrl(url)) {
if (isTumLiveUrl(url)) {
url.searchParams.set('video_only', '1');
return url.toString();
}
return isTumUrl
? url.toString()
: urlParser.create({
videoInfo: urlParser.parse(videoUrl)!,
format: 'embed',
});
return urlParser.create({
videoInfo: urlParser.parse(videoUrl)!,
format: 'embed',
});
}

cancelForm() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ describe('VideoUnitFormComponent', () => {
});

it('should not submit a form when name is missing', () => {
jest.spyOn(videoUnitFormComponent, 'videoSourceUrlValidator').mockReturnValue(null);
jest.spyOn(videoUnitFormComponent, 'videoSourceTransformUrlValidator').mockReturnValue(null);
jest.spyOn(videoUnitFormComponent, 'videoSourceUrlValidator').mockReturnValue(undefined);
jest.spyOn(videoUnitFormComponent, 'videoSourceTransformUrlValidator').mockReturnValue(undefined);
videoUnitFormComponentFixture.detectChanges();
const exampleDescription = 'lorem ipsum';
videoUnitFormComponent.descriptionControl!.setValue(exampleDescription);
Expand All @@ -67,8 +67,8 @@ describe('VideoUnitFormComponent', () => {
});

it('should not submit a form when source is missing', () => {
jest.spyOn(videoUnitFormComponent, 'videoSourceUrlValidator').mockReturnValue(null);
jest.spyOn(videoUnitFormComponent, 'videoSourceTransformUrlValidator').mockReturnValue(null);
jest.spyOn(videoUnitFormComponent, 'videoSourceUrlValidator').mockReturnValue(undefined);
jest.spyOn(videoUnitFormComponent, 'videoSourceTransformUrlValidator').mockReturnValue(undefined);
videoUnitFormComponentFixture.detectChanges();
const exampleName = 'test';
videoUnitFormComponent.nameControl!.setValue(exampleName);
Expand All @@ -92,8 +92,8 @@ describe('VideoUnitFormComponent', () => {
});

it('should submit valid form', () => {
jest.spyOn(videoUnitFormComponent, 'videoSourceUrlValidator').mockReturnValue(null);
jest.spyOn(videoUnitFormComponent, 'videoSourceTransformUrlValidator').mockReturnValue(null);
jest.spyOn(videoUnitFormComponent, 'videoSourceUrlValidator').mockReturnValue(undefined);
jest.spyOn(videoUnitFormComponent, 'videoSourceTransformUrlValidator').mockReturnValue(undefined);
videoUnitFormComponentFixture.detectChanges();
const exampleName = 'test';
videoUnitFormComponent.nameControl!.setValue(exampleName);
Expand Down Expand Up @@ -129,8 +129,8 @@ describe('VideoUnitFormComponent', () => {

it('should correctly transform YouTube URL into embeddable format', () => {
jest.spyOn(videoUnitFormComponent, 'extractEmbeddedUrl').mockReturnValue(validYouTubeUrlInEmbeddableFormat);
jest.spyOn(videoUnitFormComponent, 'videoSourceUrlValidator').mockReturnValue(null);
jest.spyOn(videoUnitFormComponent, 'videoSourceTransformUrlValidator').mockReturnValue(null);
jest.spyOn(videoUnitFormComponent, 'videoSourceUrlValidator').mockReturnValue(undefined);
jest.spyOn(videoUnitFormComponent, 'videoSourceTransformUrlValidator').mockReturnValue(undefined);

videoUnitFormComponentFixture.detectChanges();

Expand Down
Loading