This commit is contained in:
Phillip Thelen
2024-02-05 15:14:16 +01:00
committed by Sabe Jones
parent 93011f182f
commit 2dfe5585eb
3 changed files with 10 additions and 13 deletions

View File

@@ -34,7 +34,7 @@ describe('contentLib', () => {
});
it('generates a hash for a filter', () => {
const hash = contentLib.hashForFilter(['backgroundsFlat', 'gear.flat']);
const hash = contentLib.hashForFilter('backgroundsFlat,gear.flat');
expect(hash).to.equal('-1791877526');
});
@@ -46,7 +46,7 @@ describe('contentLib', () => {
it('serves filtered content', () => {
const resSpy = generateRes();
contentLib.serveContent(resSpy, 'en', ['backgroundsFlat', 'gear.flat'], false);
contentLib.serveContent(resSpy, 'en', 'backgroundsFlat,gear.flat', false);
expect(resSpy.send).to.have.been.calledOnce;
});
@@ -81,7 +81,7 @@ describe('contentLib', () => {
});
it('caches filtered requests', async () => {
const filter = ['backgroundsFlat', 'gear.flat'];
const filter = 'backgroundsFlat,gear.flat';
const hash = contentLib.hashForFilter(filter);
expect(fs.existsSync(`${contentLib.CONTENT_CACHE_PATH}en${hash}.json`)).to.be.false;
contentLib.serveContent(resSpy, 'en', filter, true);
@@ -89,7 +89,7 @@ describe('contentLib', () => {
});
it('serves filtered cached requests', async () => {
const filter = ['backgroundsFlat', 'gear.flat'];
const filter = 'backgroundsFlat,gear.flat';
const hash = contentLib.hashForFilter(filter);
fs.writeFileSync(
`${contentLib.CONTENT_CACHE_PATH}en${hash}.json`,

View File

@@ -12,8 +12,8 @@ const MOBILE_FILTER = ['achievements', 'questSeriesAchievements', 'animalColorAc
'premiumHatchingPotions', 'wackyHatchingPotions', 'backgroundsFlat', 'questsByLevel', 'gear.tree', 'tasksByCategory',
'userDefaults', 'timeTravelStable', 'gearTypes', 'cardTypes'];
const ANDROID_FILTER = [...MOBILE_FILTER, 'appearances.background'];
const IOS_FILTER = [...MOBILE_FILTER, 'backgrounds'];
const ANDROID_FILTER = [...MOBILE_FILTER, 'appearances.background'].join(',');
const IOS_FILTER = [...MOBILE_FILTER, 'backgrounds'].join(',');
/**
* @api {get} /api/v3/content Get all available content objects
@@ -74,20 +74,17 @@ api.getContent = {
language = proposedLang;
}
let filterList = [];
const filter = req.query.filter || '';
// apply defaults for mobile clients
if (filter === '') {
if (req.headers['x-client'] === 'habitica-android') {
filterList = ANDROID_FILTER;
filter = ANDROID_FILTER;
} else if (req.headers['x-client'] === 'habitica-ios') {
filterList = IOS_FILTER;
filter = IOS_FILTER;
}
} else {
filterList = filter.split(',');
}
serveContent(res, language, filterList, IS_PROD);
serveContent(res, language, filter, IS_PROD);
},
};

View File

@@ -55,7 +55,7 @@ export function hashForFilter (filter) {
export function serveContent (res, language, filter, isProd) {
// Build usable filter object
const filterObj = {};
filter.forEach(item => {
filter.split(',').forEach(item => {
if (item.includes('.')) {
const [key, subkey] = item.split('.');
if (!filterObj[key]) {