summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/i
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/api/endpoints/i')
-rw-r--r--src/server/api/endpoints/i/2fa/done.ts37
-rw-r--r--src/server/api/endpoints/i/2fa/register.ts48
-rw-r--r--src/server/api/endpoints/i/2fa/unregister.ts28
-rw-r--r--src/server/api/endpoints/i/authorized_apps.ts43
-rw-r--r--src/server/api/endpoints/i/change_password.ts42
-rw-r--r--src/server/api/endpoints/i/favorites.ts44
-rw-r--r--src/server/api/endpoints/i/notifications.ts106
-rw-r--r--src/server/api/endpoints/i/pin.ts44
-rw-r--r--src/server/api/endpoints/i/regenerate_token.ts42
-rw-r--r--src/server/api/endpoints/i/signin_history.ts61
-rw-r--r--src/server/api/endpoints/i/update.ts77
-rw-r--r--src/server/api/endpoints/i/update_client_setting.ts43
-rw-r--r--src/server/api/endpoints/i/update_home.ts60
-rw-r--r--src/server/api/endpoints/i/update_mobile_home.ts59
14 files changed, 734 insertions, 0 deletions
diff --git a/src/server/api/endpoints/i/2fa/done.ts b/src/server/api/endpoints/i/2fa/done.ts
new file mode 100644
index 0000000000..3e824feffd
--- /dev/null
+++ b/src/server/api/endpoints/i/2fa/done.ts
@@ -0,0 +1,37 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import * as speakeasy from 'speakeasy';
+import User from '../../../../../models/user';
+
+module.exports = async (params, user) => new Promise(async (res, rej) => {
+ // Get 'token' parameter
+ const [token, tokenErr] = $(params.token).string().$;
+ if (tokenErr) return rej('invalid token param');
+
+ const _token = token.replace(/\s/g, '');
+
+ if (user.twoFactorTempSecret == null) {
+ return rej('二段階認証の設定が開始されていません');
+ }
+
+ const verified = (speakeasy as any).totp.verify({
+ secret: user.twoFactorTempSecret,
+ encoding: 'base32',
+ token: _token
+ });
+
+ if (!verified) {
+ return rej('not verified');
+ }
+
+ await User.update(user._id, {
+ $set: {
+ 'twoFactorSecret': user.twoFactorTempSecret,
+ 'twoFactorEnabled': true
+ }
+ });
+
+ res();
+});
diff --git a/src/server/api/endpoints/i/2fa/register.ts b/src/server/api/endpoints/i/2fa/register.ts
new file mode 100644
index 0000000000..bed64a2545
--- /dev/null
+++ b/src/server/api/endpoints/i/2fa/register.ts
@@ -0,0 +1,48 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import * as bcrypt from 'bcryptjs';
+import * as speakeasy from 'speakeasy';
+import * as QRCode from 'qrcode';
+import User from '../../../../../models/user';
+import config from '../../../../../config';
+
+module.exports = async (params, user) => new Promise(async (res, rej) => {
+ // Get 'password' parameter
+ const [password, passwordErr] = $(params.password).string().$;
+ if (passwordErr) return rej('invalid password param');
+
+ // Compare password
+ const same = await bcrypt.compare(password, user.password);
+
+ if (!same) {
+ return rej('incorrect password');
+ }
+
+ // Generate user's secret key
+ const secret = speakeasy.generateSecret({
+ length: 32
+ });
+
+ await User.update(user._id, {
+ $set: {
+ twoFactorTempSecret: secret.base32
+ }
+ });
+
+ // Get the data URL of the authenticator URL
+ QRCode.toDataURL(speakeasy.otpauthURL({
+ secret: secret.base32,
+ encoding: 'base32',
+ label: user.username,
+ issuer: config.host
+ }), (err, data_url) => {
+ res({
+ qr: data_url,
+ secret: secret.base32,
+ label: user.username,
+ issuer: config.host
+ });
+ });
+});
diff --git a/src/server/api/endpoints/i/2fa/unregister.ts b/src/server/api/endpoints/i/2fa/unregister.ts
new file mode 100644
index 0000000000..f9d7a25f53
--- /dev/null
+++ b/src/server/api/endpoints/i/2fa/unregister.ts
@@ -0,0 +1,28 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import * as bcrypt from 'bcryptjs';
+import User from '../../../../../models/user';
+
+module.exports = async (params, user) => new Promise(async (res, rej) => {
+ // Get 'password' parameter
+ const [password, passwordErr] = $(params.password).string().$;
+ if (passwordErr) return rej('invalid password param');
+
+ // Compare password
+ const same = await bcrypt.compare(password, user.password);
+
+ if (!same) {
+ return rej('incorrect password');
+ }
+
+ await User.update(user._id, {
+ $set: {
+ 'twoFactorSecret': null,
+ 'twoFactorEnabled': false
+ }
+ });
+
+ res();
+});
diff --git a/src/server/api/endpoints/i/authorized_apps.ts b/src/server/api/endpoints/i/authorized_apps.ts
new file mode 100644
index 0000000000..82fd2d2516
--- /dev/null
+++ b/src/server/api/endpoints/i/authorized_apps.ts
@@ -0,0 +1,43 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import AccessToken from '../../../../models/access-token';
+import { pack } from '../../../../models/app';
+
+/**
+ * Get authorized apps of my account
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'limit' parameter
+ const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
+ if (limitErr) return rej('invalid limit param');
+
+ // Get 'offset' parameter
+ const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$;
+ if (offsetErr) return rej('invalid offset param');
+
+ // Get 'sort' parameter
+ const [sort = 'desc', sortError] = $(params.sort).optional.string().or('desc asc').$;
+ if (sortError) return rej('invalid sort param');
+
+ // Get tokens
+ const tokens = await AccessToken
+ .find({
+ userId: user._id
+ }, {
+ limit: limit,
+ skip: offset,
+ sort: {
+ _id: sort == 'asc' ? 1 : -1
+ }
+ });
+
+ // Serialize
+ res(await Promise.all(tokens.map(async token =>
+ await pack(token.appId))));
+});
diff --git a/src/server/api/endpoints/i/change_password.ts b/src/server/api/endpoints/i/change_password.ts
new file mode 100644
index 0000000000..57415083f1
--- /dev/null
+++ b/src/server/api/endpoints/i/change_password.ts
@@ -0,0 +1,42 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import * as bcrypt from 'bcryptjs';
+import User from '../../../../models/user';
+
+/**
+ * Change password
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = async (params, user) => new Promise(async (res, rej) => {
+ // Get 'currentPasword' parameter
+ const [currentPassword, currentPasswordErr] = $(params.currentPasword).string().$;
+ if (currentPasswordErr) return rej('invalid currentPasword param');
+
+ // Get 'newPassword' parameter
+ const [newPassword, newPasswordErr] = $(params.newPassword).string().$;
+ if (newPasswordErr) return rej('invalid newPassword param');
+
+ // Compare password
+ const same = await bcrypt.compare(currentPassword, user.password);
+
+ if (!same) {
+ return rej('incorrect password');
+ }
+
+ // Generate hash of password
+ const salt = await bcrypt.genSalt(8);
+ const hash = await bcrypt.hash(newPassword, salt);
+
+ await User.update(user._id, {
+ $set: {
+ 'password': hash
+ }
+ });
+
+ res();
+});
diff --git a/src/server/api/endpoints/i/favorites.ts b/src/server/api/endpoints/i/favorites.ts
new file mode 100644
index 0000000000..b40f2b3887
--- /dev/null
+++ b/src/server/api/endpoints/i/favorites.ts
@@ -0,0 +1,44 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import Favorite from '../../../../models/favorite';
+import { pack } from '../../../../models/note';
+
+/**
+ * Get followers of a user
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'limit' parameter
+ const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
+ if (limitErr) return rej('invalid limit param');
+
+ // Get 'offset' parameter
+ const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$;
+ if (offsetErr) return rej('invalid offset param');
+
+ // Get 'sort' parameter
+ const [sort = 'desc', sortError] = $(params.sort).optional.string().or('desc asc').$;
+ if (sortError) return rej('invalid sort param');
+
+ // Get favorites
+ const favorites = await Favorite
+ .find({
+ userId: user._id
+ }, {
+ limit: limit,
+ skip: offset,
+ sort: {
+ _id: sort == 'asc' ? 1 : -1
+ }
+ });
+
+ // Serialize
+ res(await Promise.all(favorites.map(async favorite =>
+ await pack(favorite.noteId)
+ )));
+});
diff --git a/src/server/api/endpoints/i/notifications.ts b/src/server/api/endpoints/i/notifications.ts
new file mode 100644
index 0000000000..3b4899682d
--- /dev/null
+++ b/src/server/api/endpoints/i/notifications.ts
@@ -0,0 +1,106 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import Notification from '../../../../models/notification';
+import Mute from '../../../../models/mute';
+import { pack } from '../../../../models/notification';
+import getFriends from '../../common/get-friends';
+import read from '../../common/read-notification';
+
+/**
+ * Get notifications
+ */
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'following' parameter
+ const [following = false, followingError] =
+ $(params.following).optional.boolean().$;
+ if (followingError) return rej('invalid following param');
+
+ // Get 'markAsRead' parameter
+ const [markAsRead = true, markAsReadErr] = $(params.markAsRead).optional.boolean().$;
+ if (markAsReadErr) return rej('invalid markAsRead param');
+
+ // Get 'type' parameter
+ const [type, typeErr] = $(params.type).optional.array('string').unique().$;
+ if (typeErr) return rej('invalid type param');
+
+ // Get 'limit' parameter
+ const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
+ if (limitErr) return rej('invalid limit param');
+
+ // Get 'sinceId' parameter
+ const [sinceId, sinceIdErr] = $(params.sinceId).optional.id().$;
+ if (sinceIdErr) return rej('invalid sinceId param');
+
+ // Get 'untilId' parameter
+ const [untilId, untilIdErr] = $(params.untilId).optional.id().$;
+ if (untilIdErr) return rej('invalid untilId param');
+
+ // Check if both of sinceId and untilId is specified
+ if (sinceId && untilId) {
+ return rej('cannot set sinceId and untilId');
+ }
+
+ const mute = await Mute.find({
+ muterId: user._id,
+ deletedAt: { $exists: false }
+ });
+
+ const query = {
+ notifieeId: user._id,
+ $and: [{
+ notifierId: {
+ $nin: mute.map(m => m.muteeId)
+ }
+ }]
+ } as any;
+
+ const sort = {
+ _id: -1
+ };
+
+ if (following) {
+ // ID list of the user itself and other users who the user follows
+ const followingIds = await getFriends(user._id);
+
+ query.$and.push({
+ notifierId: {
+ $in: followingIds
+ }
+ });
+ }
+
+ if (type) {
+ query.type = {
+ $in: type
+ };
+ }
+
+ if (sinceId) {
+ sort._id = 1;
+ query._id = {
+ $gt: sinceId
+ };
+ } else if (untilId) {
+ query._id = {
+ $lt: untilId
+ };
+ }
+
+ // Issue query
+ const notifications = await Notification
+ .find(query, {
+ limit: limit,
+ sort: sort
+ });
+
+ // Serialize
+ res(await Promise.all(notifications.map(async notification =>
+ await pack(notification))));
+
+ // Mark as read all
+ if (notifications.length > 0 && markAsRead) {
+ read(user._id, notifications);
+ }
+});
diff --git a/src/server/api/endpoints/i/pin.ts b/src/server/api/endpoints/i/pin.ts
new file mode 100644
index 0000000000..909a6fdbde
--- /dev/null
+++ b/src/server/api/endpoints/i/pin.ts
@@ -0,0 +1,44 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import User from '../../../../models/user';
+import Note from '../../../../models/note';
+import { pack } from '../../../../models/user';
+
+/**
+ * Pin note
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = async (params, user) => new Promise(async (res, rej) => {
+ // Get 'noteId' parameter
+ const [noteId, noteIdErr] = $(params.noteId).id().$;
+ if (noteIdErr) return rej('invalid noteId param');
+
+ // Fetch pinee
+ const note = await Note.findOne({
+ _id: noteId,
+ userId: user._id
+ });
+
+ if (note === null) {
+ return rej('note not found');
+ }
+
+ await User.update(user._id, {
+ $set: {
+ pinnedNoteId: note._id
+ }
+ });
+
+ // Serialize
+ const iObj = await pack(user, user, {
+ detail: true
+ });
+
+ // Send response
+ res(iObj);
+});
diff --git a/src/server/api/endpoints/i/regenerate_token.ts b/src/server/api/endpoints/i/regenerate_token.ts
new file mode 100644
index 0000000000..f9e92c1797
--- /dev/null
+++ b/src/server/api/endpoints/i/regenerate_token.ts
@@ -0,0 +1,42 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import * as bcrypt from 'bcryptjs';
+import User from '../../../../models/user';
+import event from '../../../../publishers/stream';
+import generateUserToken from '../../common/generate-native-user-token';
+
+/**
+ * Regenerate native token
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = async (params, user) => new Promise(async (res, rej) => {
+ // Get 'password' parameter
+ const [password, passwordErr] = $(params.password).string().$;
+ if (passwordErr) return rej('invalid password param');
+
+ // Compare password
+ const same = await bcrypt.compare(password, user.password);
+
+ if (!same) {
+ return rej('incorrect password');
+ }
+
+ // Generate secret
+ const secret = generateUserToken();
+
+ await User.update(user._id, {
+ $set: {
+ 'token': secret
+ }
+ });
+
+ res();
+
+ // Publish event
+ event(user._id, 'my_token_regenerated');
+});
diff --git a/src/server/api/endpoints/i/signin_history.ts b/src/server/api/endpoints/i/signin_history.ts
new file mode 100644
index 0000000000..931b9e2252
--- /dev/null
+++ b/src/server/api/endpoints/i/signin_history.ts
@@ -0,0 +1,61 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import Signin, { pack } from '../../../../models/signin';
+
+/**
+ * Get signin history of my account
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'limit' parameter
+ const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
+ if (limitErr) return rej('invalid limit param');
+
+ // Get 'sinceId' parameter
+ const [sinceId, sinceIdErr] = $(params.sinceId).optional.id().$;
+ if (sinceIdErr) return rej('invalid sinceId param');
+
+ // Get 'untilId' parameter
+ const [untilId, untilIdErr] = $(params.untilId).optional.id().$;
+ if (untilIdErr) return rej('invalid untilId param');
+
+ // Check if both of sinceId and untilId is specified
+ if (sinceId && untilId) {
+ return rej('cannot set sinceId and untilId');
+ }
+
+ const query = {
+ userId: user._id
+ } as any;
+
+ const sort = {
+ _id: -1
+ };
+
+ if (sinceId) {
+ sort._id = 1;
+ query._id = {
+ $gt: sinceId
+ };
+ } else if (untilId) {
+ query._id = {
+ $lt: untilId
+ };
+ }
+
+ // Issue query
+ const history = await Signin
+ .find(query, {
+ limit: limit,
+ sort: sort
+ });
+
+ // Serialize
+ res(await Promise.all(history.map(async record =>
+ await pack(record))));
+});
diff --git a/src/server/api/endpoints/i/update.ts b/src/server/api/endpoints/i/update.ts
new file mode 100644
index 0000000000..f3c9d777b5
--- /dev/null
+++ b/src/server/api/endpoints/i/update.ts
@@ -0,0 +1,77 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import User, { isValidName, isValidDescription, isValidLocation, isValidBirthday, pack } from '../../../../models/user';
+import event from '../../../../publishers/stream';
+
+/**
+ * Update myself
+ */
+module.exports = async (params, user, app) => new Promise(async (res, rej) => {
+ const isSecure = user != null && app == null;
+
+ // Get 'name' parameter
+ const [name, nameErr] = $(params.name).optional.nullable.string().pipe(isValidName).$;
+ if (nameErr) return rej('invalid name param');
+ if (name) user.name = name;
+
+ // Get 'description' parameter
+ const [description, descriptionErr] = $(params.description).optional.nullable.string().pipe(isValidDescription).$;
+ if (descriptionErr) return rej('invalid description param');
+ if (description !== undefined) user.description = description;
+
+ // Get 'location' parameter
+ const [location, locationErr] = $(params.location).optional.nullable.string().pipe(isValidLocation).$;
+ if (locationErr) return rej('invalid location param');
+ if (location !== undefined) user.profile.location = location;
+
+ // Get 'birthday' parameter
+ const [birthday, birthdayErr] = $(params.birthday).optional.nullable.string().pipe(isValidBirthday).$;
+ if (birthdayErr) return rej('invalid birthday param');
+ if (birthday !== undefined) user.profile.birthday = birthday;
+
+ // Get 'avatarId' parameter
+ const [avatarId, avatarIdErr] = $(params.avatarId).optional.id().$;
+ if (avatarIdErr) return rej('invalid avatarId param');
+ if (avatarId) user.avatarId = avatarId;
+
+ // Get 'bannerId' parameter
+ const [bannerId, bannerIdErr] = $(params.bannerId).optional.id().$;
+ if (bannerIdErr) return rej('invalid bannerId param');
+ if (bannerId) user.bannerId = bannerId;
+
+ // Get 'isBot' parameter
+ const [isBot, isBotErr] = $(params.isBot).optional.boolean().$;
+ if (isBotErr) return rej('invalid isBot param');
+ if (isBot != null) user.isBot = isBot;
+
+ // Get 'autoWatch' parameter
+ const [autoWatch, autoWatchErr] = $(params.autoWatch).optional.boolean().$;
+ if (autoWatchErr) return rej('invalid autoWatch param');
+ if (autoWatch != null) user.settings.autoWatch = autoWatch;
+
+ await User.update(user._id, {
+ $set: {
+ name: user.name,
+ description: user.description,
+ avatarId: user.avatarId,
+ bannerId: user.bannerId,
+ profile: user.profile,
+ isBot: user.isBot,
+ settings: user.settings
+ }
+ });
+
+ // Serialize
+ const iObj = await pack(user, user, {
+ detail: true,
+ includeSecrets: isSecure
+ });
+
+ // Send response
+ res(iObj);
+
+ // Publish i updated event
+ event(user._id, 'i_updated', iObj);
+});
diff --git a/src/server/api/endpoints/i/update_client_setting.ts b/src/server/api/endpoints/i/update_client_setting.ts
new file mode 100644
index 0000000000..b0d5db5ec2
--- /dev/null
+++ b/src/server/api/endpoints/i/update_client_setting.ts
@@ -0,0 +1,43 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import User, { pack } from '../../../../models/user';
+import event from '../../../../publishers/stream';
+
+/**
+ * Update myself
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = async (params, user) => new Promise(async (res, rej) => {
+ // Get 'name' parameter
+ const [name, nameErr] = $(params.name).string().$;
+ if (nameErr) return rej('invalid name param');
+
+ // Get 'value' parameter
+ const [value, valueErr] = $(params.value).nullable.any().$;
+ if (valueErr) return rej('invalid value param');
+
+ const x = {};
+ x[`clientSettings.${name}`] = value;
+
+ await User.update(user._id, {
+ $set: x
+ });
+
+ // Serialize
+ user.clientSettings[name] = value;
+ const iObj = await pack(user, user, {
+ detail: true,
+ includeSecrets: true
+ });
+
+ // Send response
+ res(iObj);
+
+ // Publish i updated event
+ event(user._id, 'i_updated', iObj);
+});
diff --git a/src/server/api/endpoints/i/update_home.ts b/src/server/api/endpoints/i/update_home.ts
new file mode 100644
index 0000000000..ce7661ede0
--- /dev/null
+++ b/src/server/api/endpoints/i/update_home.ts
@@ -0,0 +1,60 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import User from '../../../../models/user';
+import event from '../../../../publishers/stream';
+
+module.exports = async (params, user) => 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: {
+ 'clientSettings.home': home
+ }
+ });
+
+ res();
+
+ event(user._id, 'home_updated', {
+ home
+ });
+ } else {
+ if (id == null && data == null) return rej('you need to set id and data params if home param unset');
+
+ const _home = user.clientSettings.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: {
+ 'clientSettings.home': _home
+ }
+ });
+
+ res();
+
+ event(user._id, 'home_updated', {
+ id, data
+ });
+ }
+});
diff --git a/src/server/api/endpoints/i/update_mobile_home.ts b/src/server/api/endpoints/i/update_mobile_home.ts
new file mode 100644
index 0000000000..b710e2f330
--- /dev/null
+++ b/src/server/api/endpoints/i/update_mobile_home.ts
@@ -0,0 +1,59 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import User from '../../../../models/user';
+import event from '../../../../publishers/stream';
+
+module.exports = async (params, user) => 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('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: {
+ 'clientSettings.mobileHome': home
+ }
+ });
+
+ res();
+
+ event(user._id, 'mobile_home_updated', {
+ home
+ });
+ } else {
+ if (id == null && data == null) return rej('you need to set id and data params if home param unset');
+
+ const _home = user.clientSettings.mobileHome || [];
+ 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: {
+ 'clientSettings.mobileHome': _home
+ }
+ });
+
+ res();
+
+ event(user._id, 'mobile_home_updated', {
+ id, data
+ });
+ }
+});