diff options
Diffstat (limited to 'src/api/endpoints/i')
| -rw-r--r-- | src/api/endpoints/i/appdata/get.ts | 45 | ||||
| -rw-r--r-- | src/api/endpoints/i/appdata/set.ts | 41 | ||||
| -rw-r--r-- | src/api/endpoints/i/change_password.ts | 6 | ||||
| -rw-r--r-- | src/api/endpoints/i/regenerate_token.ts | 2 | ||||
| -rw-r--r-- | src/api/endpoints/i/update.ts | 8 | ||||
| -rw-r--r-- | src/api/endpoints/i/update_home.ts | 60 |
6 files changed, 101 insertions, 61 deletions
diff --git a/src/api/endpoints/i/appdata/get.ts b/src/api/endpoints/i/appdata/get.ts index a1a57fa13a..571208d46c 100644 --- a/src/api/endpoints/i/appdata/get.ts +++ b/src/api/endpoints/i/appdata/get.ts @@ -13,38 +13,27 @@ import Appdata from '../../../models/appdata'; * @param {Boolean} isSecure * @return {Promise<any>} */ -module.exports = (params, user, app, isSecure) => new Promise(async (res, rej) => { +module.exports = (params, user, app) => new Promise(async (res, rej) => { + if (app == null) return rej('このAPIはサードパーティAppからのみ利用できます'); + // Get 'key' parameter const [key = null, keyError] = $(params.key).optional.nullable.string().match(/[a-z_]+/).$; if (keyError) return rej('invalid key param'); - if (isSecure) { - if (!user.data) { - return res(); - } - if (key !== null) { - const data = {}; - data[key] = user.data[key]; - res(data); - } else { - res(user.data); - } - } else { - const select = {}; - if (key !== null) { - select[`data.${key}`] = true; - } - const appdata = await Appdata.findOne({ - app_id: app._id, - user_id: user._id - }, { - fields: select - }); + const select = {}; + if (key !== null) { + select[`data.${key}`] = true; + } + const appdata = await Appdata.findOne({ + app_id: app._id, + user_id: user._id + }, { + fields: select + }); - if (appdata) { - res(appdata.data); - } else { - res(); - } + if (appdata) { + res(appdata.data); + } else { + res(); } }); diff --git a/src/api/endpoints/i/appdata/set.ts b/src/api/endpoints/i/appdata/set.ts index 9c3dbe185b..2804a14cb3 100644 --- a/src/api/endpoints/i/appdata/set.ts +++ b/src/api/endpoints/i/appdata/set.ts @@ -3,9 +3,6 @@ */ import $ from 'cafy'; import Appdata from '../../../models/appdata'; -import User from '../../../models/user'; -import serialize from '../../../serializers/user'; -import event from '../../../event'; /** * Set app data @@ -16,7 +13,9 @@ import event from '../../../event'; * @param {Boolean} isSecure * @return {Promise<any>} */ -module.exports = (params, user, app, isSecure) => new Promise(async (res, rej) => { +module.exports = (params, user, app) => new Promise(async (res, rej) => { + if (app == null) return rej('このAPIはサードパーティAppからのみ利用できます'); + // Get 'data' parameter const [data, dataError] = $(params.data).optional.object() .pipe(obj => { @@ -43,31 +42,17 @@ module.exports = (params, user, app, isSecure) => new Promise(async (res, rej) = set[`data.${key}`] = value; } - if (isSecure) { - const _user = await User.findOneAndUpdate(user._id, { + await Appdata.update({ + app_id: app._id, + user_id: user._id + }, Object.assign({ + app_id: app._id, + user_id: user._id + }, { $set: set + }), { + upsert: true }); - res(204); - - // Publish i updated event - event(user._id, 'i_updated', await serialize(_user, user, { - detail: true, - includeSecrets: true - })); - } else { - await Appdata.update({ - app_id: app._id, - user_id: user._id - }, Object.assign({ - app_id: app._id, - user_id: user._id - }, { - $set: set - }), { - upsert: true - }); - - res(204); - } + res(204); }); diff --git a/src/api/endpoints/i/change_password.ts b/src/api/endpoints/i/change_password.ts index faceded29d..16f1a2e4ec 100644 --- a/src/api/endpoints/i/change_password.ts +++ b/src/api/endpoints/i/change_password.ts @@ -22,15 +22,15 @@ module.exports = async (params, user) => new Promise(async (res, rej) => { if (newPasswordErr) return rej('invalid new_password param'); // Compare password - const same = bcrypt.compareSync(currentPassword, user.password); + const same = await bcrypt.compare(currentPassword, user.password); if (!same) { return rej('incorrect password'); } // Generate hash of password - const salt = bcrypt.genSaltSync(8); - const hash = bcrypt.hashSync(newPassword, salt); + const salt = await bcrypt.genSalt(8); + const hash = await bcrypt.hash(newPassword, salt); await User.update(user._id, { $set: { diff --git a/src/api/endpoints/i/regenerate_token.ts b/src/api/endpoints/i/regenerate_token.ts index f96d10ebfc..653468330f 100644 --- a/src/api/endpoints/i/regenerate_token.ts +++ b/src/api/endpoints/i/regenerate_token.ts @@ -20,7 +20,7 @@ module.exports = async (params, user) => new Promise(async (res, rej) => { if (passwordErr) return rej('invalid password param'); // Compare password - const same = bcrypt.compareSync(password, user.password); + const same = await bcrypt.compare(password, user.password); if (!same) { return rej('incorrect password'); diff --git a/src/api/endpoints/i/update.ts b/src/api/endpoints/i/update.ts index 111a4b1909..c484c51a96 100644 --- a/src/api/endpoints/i/update.ts +++ b/src/api/endpoints/i/update.ts @@ -48,13 +48,19 @@ module.exports = async (params, user, _, isSecure) => new Promise(async (res, re if (bannerIdErr) return rej('invalid banner_id param'); if (bannerId) user.banner_id = bannerId; + // Get 'show_donation' parameter + const [showDonation, showDonationErr] = $(params.show_donation).optional.boolean().$; + if (showDonationErr) return rej('invalid show_donation param'); + if (showDonation) user.client_settings.show_donation = showDonation; + await User.update(user._id, { $set: { name: user.name, description: user.description, avatar_id: user.avatar_id, banner_id: user.banner_id, - profile: user.profile + profile: user.profile, + 'client_settings.show_donation': user.client_settings.show_donation } }); diff --git a/src/api/endpoints/i/update_home.ts b/src/api/endpoints/i/update_home.ts new file mode 100644 index 0000000000..429e88529a --- /dev/null +++ b/src/api/endpoints/i/update_home.ts @@ -0,0 +1,60 @@ +/** + * Module dependencies + */ +import $ from 'cafy'; +import User from '../../models/user'; + +/** + * Update myself + * + * @param {any} params + * @param {any} user + * @param {any} _ + * @param {boolean} isSecure + * @return {Promise<any>} + */ +module.exports = async (params, user, _, isSecure) => new Promise(async (res, rej) => { + // Get 'home' parameter + const [home, homeErr] = $(params.home).optional.array().each( + $().strict.object() + .have('name', $().string()) + .have('id', $().string()) + .have('place', $().string()) + .have('data', $().object())).$; + if (homeErr) return rej('invalid home param'); + + // Get 'id' parameter + const [id, idErr] = $(params.id).optional.string().$; + if (idErr) return rej('invalid id param'); + + // Get 'data' parameter + const [data, dataErr] = $(params.data).optional.object().$; + if (dataErr) return rej('invalid data param'); + + if (home) { + await User.update(user._id, { + $set: { + 'client_settings.home': home + } + }); + + res(); + } else { + if (id == null && data == null) return rej('you need to set id and data params if home param unset'); + + const _home = user.client_settings.home; + const widget = _home.find(w => w.id == id); + + if (widget == null) return rej('widget not found'); + + widget.data = data; + + await User.update(user._id, { + $set: { + 'client_settings.home': _home + } + }); + + res(); + } +}); |