From e8e220b4108766ab89f5fbaff35f1574030a298d Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 19 Mar 2017 15:47:36 +0900 Subject: [PATCH] [API] Fix bug, rename param set -> data (BREAKING) --- src/api/endpoints/i/appdata/set.ts | 28 ++++++++++++++-------------- src/web/app/boot.js | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/api/endpoints/i/appdata/set.ts b/src/api/endpoints/i/appdata/set.ts index 6ba91cd80..eea17fd0d 100644 --- a/src/api/endpoints/i/appdata/set.ts +++ b/src/api/endpoints/i/appdata/set.ts @@ -17,16 +17,14 @@ import event from '../../../event'; * @return {Promise} */ module.exports = (params, user, app, isSecure) => new Promise(async (res, rej) => { - // Get 'set' parameter - const [set, setError] = $(params.set).optional.object() + // Get 'data' parameter + const [data, dataError] = $(params.data).optional.object() .pipe(obj => { - return Object.entries(obj).some(kv => { - const k = kv[0]; - const v = kv[1]; - return $(k).string().match(/[a-z_]+/).isNg() && $(v).string().isNg(); - }); + const hasInvalidData = Object.entries(obj).some(([k, v]) => + $(k).string().match(/^[a-z_]+$/).isNg() && $(v).string().isNg()); + return !hasInvalidData; }).$; - if (setError) return rej('invalid set param'); + if (dataError) return rej('invalid data param'); // Get 'key' parameter const [key, keyError] = $(params.key).optional.string().match(/[a-z_]+/).$; @@ -36,16 +34,18 @@ module.exports = (params, user, app, isSecure) => new Promise(async (res, rej) = const [value, valueError] = $(params.value).optional.string().$; if (valueError) return rej('invalid value param'); - let data = {}; - if (set) { - data = set; + let set = {}; + if (data) { + Object.entries(data).forEach(([k, v]) => { + set['data.' + k] = v; + }); } else { - data[key] = value; + set['data.' + key] = value; } if (isSecure) { const _user = await User.findOneAndUpdate(user._id, { - $set: { data } + $set: set }); res(204); @@ -63,7 +63,7 @@ module.exports = (params, user, app, isSecure) => new Promise(async (res, rej) = app_id: app._id, user_id: user._id }, { - $set: { data } + $set: set }), { upsert: true }); diff --git a/src/web/app/boot.js b/src/web/app/boot.js index cdedcd500..8a65e5973 100644 --- a/src/web/app/boot.js +++ b/src/web/app/boot.js @@ -157,7 +157,7 @@ function fetchme(token, cb) { function init() { const data = generateDefaultUserdata(); api(token, 'i/appdata/set', { - set: data + data }).then(() => { me.data = data; done();