Skip to content

Performance fixes on groups #444

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

Merged
merged 1 commit into from
Dec 22, 2021
Merged
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
29 changes: 7 additions & 22 deletions src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -820,36 +820,21 @@ async function validateChallengeTerms (terms = []) {
*/
async function _filterChallengesByGroupsAccess (currentUser, challenges) {
const res = []
let userGroups
const needToCheckForGroupAccess = !currentUser ? true : !currentUser.isMachine && !hasAdminRole(currentUser)
const subGroupsMap = {}
if(!needToCheckForGroupAccess) return challenges

let userGroups

for (const challenge of challenges) {
challenge.groups = _.filter(challenge.groups, g => !_.includes(['null', 'undefined'], _.toString(g).toLowerCase()))
let expandedGroups = []
if (!challenge.groups || _.get(challenge, 'groups.length', 0) === 0 || !needToCheckForGroupAccess) {
res.push(challenge)
} else if (currentUser) {
// get user groups if not yet
if (_.isNil(userGroups)) {
userGroups = await getUserGroups(currentUser.userId)
}
// Expand challenge groups by subGroups
// results are being saved on a hashmap for efficiency
for (const group of challenge.groups) {
let subGroups
if (subGroupsMap[group]) {
subGroups = subGroupsMap[group]
} else {
subGroups = await expandWithSubGroups(group)
subGroupsMap[group] = subGroups
}
expandedGroups = [
..._.concat(expandedGroups, subGroups)
]
userGroups = await getCompleteUserGroupTreeIds(currentUser.userId)
}
// check if there is matched group
// logger.debug('Groups', challenge.groups, userGroups)
if (_.find(expandedGroups, (group) => !!_.find(userGroups, (ug) => ug.id === group))) {
// get user groups if not yet
if (_.find(challenge.groups, (group) => !!_.find(userGroups, (ug) => ug.id === group))) {
res.push(challenge)
}
}
Expand Down