summaryrefslogtreecommitdiff
path: root/src/api/endpoints/i/appdata/set.ts
diff options
context:
space:
mode:
authorsyuilo⭐️ <Syuilotan@yahoo.co.jp>2017-03-03 19:54:40 +0900
committerGitHub <noreply@github.com>2017-03-03 19:54:40 +0900
commit3ce6601f0436da23589384990dfb6c12cec5a5b4 (patch)
treeb7b9cc14d9787f06c72d013bc25690a9470e6bbe /src/api/endpoints/i/appdata/set.ts
parentfix(package): update whatwg-fetch to version 2.0.3 (diff)
parentdone (diff)
downloadmisskey-3ce6601f0436da23589384990dfb6c12cec5a5b4.tar.gz
misskey-3ce6601f0436da23589384990dfb6c12cec5a5b4.tar.bz2
misskey-3ce6601f0436da23589384990dfb6c12cec5a5b4.zip
Merge pull request #232 from syuilo/#226
#226、あとTypeScriptにした
Diffstat (limited to 'src/api/endpoints/i/appdata/set.ts')
-rw-r--r--src/api/endpoints/i/appdata/set.ts61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/api/endpoints/i/appdata/set.ts b/src/api/endpoints/i/appdata/set.ts
new file mode 100644
index 0000000000..57001f4e8b
--- /dev/null
+++ b/src/api/endpoints/i/appdata/set.ts
@@ -0,0 +1,61 @@
+'use strict';
+
+/**
+ * Module dependencies
+ */
+import Appdata from '../../../models/appdata';
+import User from '../../../models/user';
+import serialize from '../../../serializers/user';
+import event from '../../../event';
+
+/**
+ * Set app data
+ *
+ * @param {any} params
+ * @param {any} user
+ * @param {any} app
+ * @param {Boolean} isSecure
+ * @return {Promise<any>}
+ */
+module.exports = (params, user, app, isSecure) =>
+ new Promise(async (res, rej) =>
+{
+ const data = params.data;
+ if (data == null) {
+ return rej('data is required');
+ }
+
+ if (isSecure) {
+ const _user = await User.findOneAndUpdate(user._id, {
+ $set: {
+ data: Object.assign(user.data || {}, JSON.parse(data))
+ }
+ });
+ res(204);
+
+ // Publish i updated event
+ event(user._id, 'i_updated', await serialize(_user, user, {
+ detail: true,
+ includeSecrets: true
+ }));
+ } else {
+ const appdata = await Appdata.findOne({
+ app_id: app._id,
+ user_id: user._id
+ });
+ await Appdata.update({
+ app_id: app._id,
+ user_id: user._id
+ }, Object.assign({
+ app_id: app._id,
+ user_id: user._id
+ }, {
+ $set: {
+ data: Object.assign((appdata || {}).data || {}, JSON.parse(data))
+ }
+ }), {
+ upsert: true
+ });
+ res(204);
+ }
+});