Skip to content

Commit

Permalink
fix(ContentTypeCache): localContentType is converted to HubContentType (
Browse files Browse the repository at this point in the history
fixes #308) (#313)

* convert HubContentType to interface

* use .canInstallLibrary instead of .canBeInstalledBy

* test(ContentTypeCache): remove .canBeInstalledBy test

* fix(ContentTypeCache): remove deprecated conversion to LocalFormat
  • Loading branch information
JPSchellenberg authored and sr258 committed Dec 6, 2019
1 parent 69fb6ed commit f37052e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 57 deletions.
14 changes: 7 additions & 7 deletions src/ContentTypeCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { crc32 } from 'crc';
import * as merge from 'merge';
import * as qs from 'qs';

import HubContentType from './HubContentType';
import {
IEditorConfig,
IHubContentType,
IKeyValueStorage,
IRegistrationData,
IUsageStatistics
Expand Down Expand Up @@ -48,9 +48,9 @@ export default class ContentTypeCache {
* @param entry the entry as received from H5P Hub
* @returns the local content type object
*/
private static convertCacheEntryToLocalFormat(entry: any): HubContentType {
private static convertCacheEntryToLocalFormat(entry: any): IHubContentType {
log.debug(`converting Cache Entry to local format`);
return new HubContentType({
return {
categories: entry.categories || [],
createdAt: Date.parse(entry.createdAt),
description: entry.description,
Expand All @@ -72,7 +72,7 @@ export default class ContentTypeCache {
title: entry.title,
tutorial: entry.tutorial || '',
updatedAt: Date.parse(entry.updatedAt)
});
};
}

/**
Expand Down Expand Up @@ -148,9 +148,9 @@ export default class ContentTypeCache {
/**
* Returns the cache data.
* @param {string[]} machineNames (optional) The method only returns content type cache data for these machine names.
* @returns {Promise<HubContentType[]>} Cached hub data in a format in which the version objects are flattened into the main object,
* @returns {Promise<IHubContentType[]>} Cached hub data in a format in which the version objects are flattened into the main object,
*/
public async get(...machineNames: string[]): Promise<HubContentType[]> {
public async get(...machineNames: string[]): Promise<IHubContentType[]> {
log.info(`getting content types`);

let cache = await this.storage.load('contentTypeCache');
Expand All @@ -172,7 +172,7 @@ export default class ContentTypeCache {
if (!machineNames || machineNames.length === 0) {
return cache;
}
return cache.filter((contentType: HubContentType) =>
return cache.filter((contentType: IHubContentType) =>
machineNames.some(
machineName => machineName === contentType.machineName
)
Expand Down
6 changes: 3 additions & 3 deletions src/ContentTypeInformationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { withFile } from 'tmp-promise';

import ContentTypeCache from './ContentTypeCache';
import H5pError from './helpers/H5pError';
import HubContentType from './HubContentType';
import LibraryManager from './LibraryManager';
import PackageImporter from './PackageImporter';
import {
IEditorConfig,
IHubContentType,
IInstalledLibrary,
IKeyValueStorage,
ITranslationService,
Expand Down Expand Up @@ -105,7 +105,7 @@ export default class ContentTypeInformationRepository {
}

// Reject installation of content types that the user has no permission to
if (!localContentType[0].canBeInstalledBy(user)) {
if (!this.canInstallLibrary(localContentType[0], user)) {
log.warn(
`rejecting installation of content type ${machineName}: user has no permission`
);
Expand Down Expand Up @@ -268,7 +268,7 @@ export default class ContentTypeInformationRepository {
* Checks if users can install library due to their rights.
* @param {HubContentType} library
*/
private canInstallLibrary(library: HubContentType, user: IUser): boolean {
private canInstallLibrary(library: IHubContentType, user: IUser): boolean {
log.verbose(
`checking if user can install library ${library.machineName}`
);
Expand Down
45 changes: 0 additions & 45 deletions src/HubContentType.ts

This file was deleted.

28 changes: 28 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1030,3 +1030,31 @@ export type ILibraryFileUrlResolver = (
library: ILibraryName,
filename: string
) => string;

/**
* This objects carries information about content types that the user can select
* from the H5P Hub.
*/
export interface IHubContentType {
categories: string[];
createdAt: number;
description: string;
example: string;
h5pMajorVersion: number;
h5pMinorVersion: number;
icon: string;
isRecommended: boolean;
keywords: string[];
license: any;
machineName: string;
majorVersion: number;
minorVersion: number;
owner: string;
patchVersion: number;
popularity: number;
screenshots: any;
summary: string;
title: string;
tutorial: string;
updatedAt: number;
}
2 changes: 0 additions & 2 deletions test/ContentTypeCache.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import axios from 'axios';
// tslint:disable-next-line: no-implicit-dependencies
import axiosMockAdapter from 'axios-mock-adapter';
// tslint:disable-next-line: no-implicit-dependencies
import mockdate from 'mockdate';

import ContentTypeCache from '../src/ContentTypeCache';
Expand Down

0 comments on commit f37052e

Please sign in to comment.