-
Notifications
You must be signed in to change notification settings - Fork 4k
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
allow clients to filter content api call #15115
Closed
Closed
Changes from 2 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
7365f60
allow clients to filter content api call
phillipthelen b42c991
Add tests for content filtering
phillipthelen 986a01a
change mobile filter defaults to a list
phillipthelen d6173da
Fix lint and test
phillipthelen 3f7c203
fix
phillipthelen 3f4727b
typo
phillipthelen ade0d46
Fix sort/search on member selection modal (#15066)
leonardochappuis 5d33d88
fix(birthday): correct birthday robe ownership check
196e44f
feat(content): add February items (#15090)
CuriousMagpie 0cf2f18
5.17.0
c8795ca
Translated using Weblate (Ukrainian)
weblate 6ef1a71
fix(content): fix February background release date
CuriousMagpie fa99bbf
5.17.1
097634c
chore(repo): remove duplicate file
d447aaa
chore(migrations): move various files to archive
c87fe56
Translated using Weblate (Portuguese)
weblate 38ddff1
Merge branch 'release' into phillip/content-filter
625b083
Merge branch 'develop' of https://github.com/HabitRPG/habitica into p…
phillipthelen e9934b9
fix test error
phillipthelen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,27 +1,96 @@ | ||
import _ from 'lodash'; | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
import common from '../../common'; | ||
import packageInfo from '../../../package.json'; | ||
|
||
export const CONTENT_CACHE_PATH = path.join(__dirname, '/../../../content_cache/'); | ||
|
||
function walkContent (obj, lang) { | ||
const CACHED_HASHES = [ | ||
|
||
]; | ||
|
||
function walkContent (obj, lang, removedKeys = {}) { | ||
_.each(obj, (item, key, source) => { | ||
if (key in removedKeys && removedKeys[key] === true) { | ||
delete source[key]; | ||
return; | ||
} | ||
if (_.isPlainObject(item) || _.isArray(item)) { | ||
walkContent(item, lang); | ||
if (key in removedKeys && _.isPlainObject(removedKeys[key])) { | ||
walkContent(item, lang, removedKeys[key]); | ||
} else { | ||
walkContent(item, lang); | ||
} | ||
} else if (_.isFunction(item) && item.i18nLangFunc) { | ||
source[key] = item(lang); | ||
} | ||
}); | ||
} | ||
|
||
export function localizeContentData (data, langCode) { | ||
export function localizeContentData (data, langCode, removedKeys = {}) { | ||
const dataClone = _.cloneDeep(data); | ||
walkContent(dataClone, langCode); | ||
walkContent(dataClone, langCode, removedKeys); | ||
return dataClone; | ||
} | ||
|
||
export function getLocalizedContentResponse (langCode) { | ||
const localizedContent = localizeContentData(common.content, langCode); | ||
export function getLocalizedContentResponse (langCode, removedKeys = {}) { | ||
const localizedContent = localizeContentData(common.content, langCode, removedKeys); | ||
return `{"success": true, "data": ${JSON.stringify(localizedContent)}, "appVersion": "${packageInfo.version}"}`; | ||
} | ||
|
||
export function hashForFilter (filter) { | ||
let hash = 0; | ||
let i; let | ||
chr; | ||
if (filter.length === 0) return ''; | ||
for (i = 0; i < filter.length; i++) { // eslint-disable-line | ||
chr = filter.charCodeAt(i); | ||
hash = ((hash << 5) - hash) + chr; // eslint-disable-line | ||
hash |= 0; // eslint-disable-line | ||
} | ||
return String(hash); | ||
} | ||
|
||
export function serveContent (res, language, filter, isProd) { | ||
// Build usable filter object | ||
const filterObj = {}; | ||
filter.split(',').forEach(item => { | ||
if (item.includes('.')) { | ||
const [key, subkey] = item.split('.'); | ||
if (!filterObj[key]) { | ||
filterObj[key] = {}; | ||
} | ||
filterObj[key][subkey.trim()] = true; | ||
} else { | ||
filterObj[item.trim()] = true; | ||
} | ||
}); | ||
|
||
if (isProd) { | ||
const filterHash = language + hashForFilter(filter); | ||
if (CACHED_HASHES.includes(filterHash)) { | ||
// Content is already cached, so just send it. | ||
res.sendFile(`${CONTENT_CACHE_PATH}${filterHash}.json`); | ||
} else { | ||
// Content is not cached, so cache it and send it. | ||
res.set({ | ||
'Content-Type': 'application/json', | ||
}); | ||
const jsonResString = getLocalizedContentResponse(language, filterObj); | ||
fs.writeFileSync( | ||
`${CONTENT_CACHE_PATH}${filterHash}.json`, | ||
jsonResString, | ||
'utf8', | ||
); | ||
CACHED_HASHES.push(filterHash); | ||
res.status(200).send(jsonResString); | ||
} | ||
} else { | ||
res.set({ | ||
'Content-Type': 'application/json', | ||
}); | ||
const jsonResString = getLocalizedContentResponse(language, filterObj); | ||
res.status(200).send(jsonResString); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be easier to maintain if this would use an array as source which then at the end used
.join(',')
to get the filter complete