-
Notifications
You must be signed in to change notification settings - Fork 716
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8716 from rtibbles/i_think_im_a_clone_now
Handle duplicate resources
- Loading branch information
Showing
12 changed files
with
175 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import groupBy from 'lodash/groupBy'; | ||
import sortBy from 'lodash/sortBy'; | ||
import { currentLanguage } from 'kolibri.utils.i18n'; | ||
|
||
export function deduplicateResources(contentNodes) { | ||
const grouped = groupBy(contentNodes, 'content_id'); | ||
return Object.keys(grouped).map(key => { | ||
const groupedNodes = grouped[key]; | ||
if (groupedNodes.length === 1) { | ||
return groupedNodes[0]; | ||
} | ||
const langCode = currentLanguage.split('-')[0]; | ||
const sortedNodes = sortBy(groupedNodes, n => { | ||
if (n.lang && n.lang.id === currentLanguage) { | ||
// Best language match return 0 to sort first | ||
return 0; | ||
} | ||
if (n.lang && n.lang.lang_code === langCode) { | ||
// lang_code match, so next best | ||
return 1; | ||
} | ||
// Everything else | ||
return 2; | ||
}); | ||
const primaryNode = sortedNodes[0]; | ||
// Include self in copies | ||
primaryNode.copies = sortedNodes; | ||
return primaryNode; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.