Skip to content

Commit ad12f89

Browse files
authored
Merge pull request #4185 from dpalou/MOBILE-4337
MOBILE-4337 h5p: Don't install editor libraries
2 parents 1842a55 + 035ba7c commit ad12f89

File tree

2 files changed

+78
-13
lines changed

2 files changed

+78
-13
lines changed

src/core/features/h5p/classes/storage.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,7 @@ export class CoreH5PStorage {
130130
if (libraryData.dynamicDependencies !== undefined) {
131131
promises.push(this.h5pFramework.saveLibraryDependencies(libraryData, libraryData.dynamicDependencies, 'dynamic'));
132132
}
133-
if (libraryData.editorDependencies !== undefined) {
134-
promises.push(this.h5pFramework.saveLibraryDependencies(libraryData, libraryData.editorDependencies, 'editor'));
135-
}
133+
// Don't save editor dependencies, they are not used in the app.
136134

137135
await Promise.all(promises);
138136
}));

src/core/features/h5p/classes/validator.ts

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,73 @@ export class CoreH5PValidator {
2929
constructor(public h5pFramework: CoreH5PFramework) {
3030
}
3131

32+
/**
33+
* Get all editor dependencies of the package.
34+
*
35+
* @param mainJsonData Contents of h5p.json file.
36+
* @param librariesJsonData JSON data about each library.
37+
* @returns Set with the editor dependencies names.
38+
*/
39+
protected getAllEditorDependencies(
40+
mainJsonData: CoreH5PMainJSONData,
41+
librariesJsonData: CoreH5PLibrariesJsonData,
42+
): Set<string> {
43+
const editorDependencies = new Set<string>();
44+
45+
// Function to add or remove an editor dependency to the Set.
46+
const addOrRemoveEditorDependency = (library: CoreH5PLibraryBasicData, add = true) => {
47+
const libString = CoreH5PCore.libraryToString(library);
48+
if ((add && editorDependencies.has(libString)) || (!add && !editorDependencies.has(libString))) {
49+
// Already treated, ignore.
50+
return;
51+
}
52+
53+
if (add) {
54+
editorDependencies.add(libString);
55+
} else {
56+
editorDependencies.delete(libString);
57+
}
58+
59+
const libraryData = librariesJsonData[libString];
60+
(libraryData?.preloadedDependencies ?? []).concat(libraryData?.dynamicDependencies ?? []).forEach((dependency) => {
61+
if (!add) {
62+
// Remove all dependencies too.
63+
addOrRemoveEditorDependency(dependency, add);
64+
} else if (dependency.machineName.startsWith('H5PEditor.')) {
65+
// Consider all dependencies that begin with H5PEditor as editor dependencies.
66+
// It might be safe to consider all dependencies of an editor dependency as editor dependency too,
67+
// but for now we decided to take a less aggressive approach.
68+
addOrRemoveEditorDependency(dependency, add);
69+
}
70+
});
71+
};
72+
73+
// First add all editor dependencies and some of their dependencies to the list.
74+
Object.values(librariesJsonData).forEach((libraryData) => {
75+
libraryData.editorDependencies?.forEach(library => {
76+
addOrRemoveEditorDependency(library, true);
77+
});
78+
});
79+
80+
// Now remove from the Set all the libraries that are listed as a preloaded/dynamic dependency of a non-editor library.
81+
mainJsonData.preloadedDependencies?.forEach((dependency) => {
82+
addOrRemoveEditorDependency(dependency, false);
83+
});
84+
85+
Object.keys(librariesJsonData).forEach((libString) => {
86+
if (editorDependencies.has(libString)) {
87+
return;
88+
}
89+
90+
const libraryData = librariesJsonData[libString];
91+
(libraryData?.preloadedDependencies ?? []).concat(libraryData?.dynamicDependencies ?? []).forEach((dependency) => {
92+
addOrRemoveEditorDependency(dependency, false);
93+
});
94+
});
95+
96+
return editorDependencies;
97+
}
98+
3299
/**
33100
* Get library data.
34101
* This function won't validate most things because it should've been done by the server already.
@@ -71,9 +138,7 @@ export class CoreH5PValidator {
71138
if (library.dynamicDependencies !== undefined) {
72139
Object.assign(missing, this.getMissingDependencies(library.dynamicDependencies, library, libraries));
73140
}
74-
if (library.editorDependencies !== undefined) {
75-
Object.assign(missing, this.getMissingDependencies(library.editorDependencies, library, libraries));
76-
}
141+
// No need to check editorDependencies, they are not used in the app.
77142
});
78143

79144
return missing;
@@ -173,14 +238,20 @@ export class CoreH5PValidator {
173238
): Promise<CoreH5PMainJSONFilesData> {
174239

175240
// Read the needed files.
176-
const results = await Promise.all([
241+
const [mainJsonData, contentJsonData, librariesJsonData] = await Promise.all([
177242
this.readH5PJsonFile(packagePath),
178243
this.readH5PContentJsonFile(packagePath),
179244
this.getPackageLibrariesData(packagePath, entries),
180245
]);
181246

247+
// Remove editor dependencies from the list of libraries to install.
248+
const editorDependencies = this.getAllEditorDependencies(mainJsonData, librariesJsonData);
249+
editorDependencies.forEach((libString) => {
250+
delete librariesJsonData[libString];
251+
});
252+
182253
// Check if there are missing libraries.
183-
const missingLibraries = this.getMissingLibraries(results[2]);
254+
const missingLibraries = this.getMissingLibraries(librariesJsonData);
184255

185256
// Check if the missing libraries are already installed in the app.
186257
await Promise.all(Object.keys(missingLibraries).map(async (libString) => {
@@ -204,11 +275,7 @@ export class CoreH5PValidator {
204275
} }));
205276
}
206277

207-
return {
208-
librariesJsonData: results[2],
209-
mainJsonData: results[0],
210-
contentJsonData: results[1],
211-
};
278+
return { librariesJsonData, mainJsonData, contentJsonData };
212279

213280
}
214281

0 commit comments

Comments
 (0)