summaryrefslogtreecommitdiff
path: root/src/api/endpoints/i
diff options
context:
space:
mode:
authorこぴなたみぽ <Syuilotan@yahoo.co.jp>2017-11-06 19:10:10 +0900
committerGitHub <noreply@github.com>2017-11-06 19:10:10 +0900
commit03e86ec05216d49f9a1af5dd92f0fa12f2da4825 (patch)
tree9257b5d2d0abc626187a9ba9640097bc5624d158 /src/api/endpoints/i
parentchore(package): update @types/serve-favicon to version 2.2.29 (diff)
parentMerge pull request #870 from syuilo/greenkeeper/@types/multer-1.3.5 (diff)
downloadmisskey-03e86ec05216d49f9a1af5dd92f0fa12f2da4825.tar.gz
misskey-03e86ec05216d49f9a1af5dd92f0fa12f2da4825.tar.bz2
misskey-03e86ec05216d49f9a1af5dd92f0fa12f2da4825.zip
Merge branch 'master' into greenkeeper/@types/serve-favicon-2.2.29
Diffstat (limited to 'src/api/endpoints/i')
-rw-r--r--src/api/endpoints/i/appdata/set.ts2
-rw-r--r--src/api/endpoints/i/change_password.ts42
-rw-r--r--src/api/endpoints/i/notifications.ts14
-rw-r--r--src/api/endpoints/i/pin.ts44
-rw-r--r--src/api/endpoints/i/regenerate_token.ts42
5 files changed, 131 insertions, 13 deletions
diff --git a/src/api/endpoints/i/appdata/set.ts b/src/api/endpoints/i/appdata/set.ts
index 24f192de6b..9c3dbe185b 100644
--- a/src/api/endpoints/i/appdata/set.ts
+++ b/src/api/endpoints/i/appdata/set.ts
@@ -21,7 +21,7 @@ module.exports = (params, user, app, isSecure) => new Promise(async (res, rej) =
const [data, dataError] = $(params.data).optional.object()
.pipe(obj => {
const hasInvalidData = Object.entries(obj).some(([k, v]) =>
- $(k).string().match(/^[a-z_]+$/).isNg() && $(v).string().isNg());
+ $(k).string().match(/^[a-z_]+$/).nok() && $(v).string().nok());
return !hasInvalidData;
}).$;
if (dataError) return rej('invalid data param');
diff --git a/src/api/endpoints/i/change_password.ts b/src/api/endpoints/i/change_password.ts
new file mode 100644
index 0000000000..faceded29d
--- /dev/null
+++ b/src/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 'current_password' parameter
+ const [currentPassword, currentPasswordErr] = $(params.current_password).string().$;
+ if (currentPasswordErr) return rej('invalid current_password param');
+
+ // Get 'new_password' parameter
+ const [newPassword, newPasswordErr] = $(params.new_password).string().$;
+ if (newPasswordErr) return rej('invalid new_password param');
+
+ // Compare password
+ const same = bcrypt.compareSync(currentPassword, user.password);
+
+ if (!same) {
+ return rej('incorrect password');
+ }
+
+ // Generate hash of password
+ const salt = bcrypt.genSaltSync(8);
+ const hash = bcrypt.hashSync(newPassword, salt);
+
+ await User.update(user._id, {
+ $set: {
+ password: hash
+ }
+ });
+
+ res();
+});
diff --git a/src/api/endpoints/i/notifications.ts b/src/api/endpoints/i/notifications.ts
index 5575fb7412..607e0768a4 100644
--- a/src/api/endpoints/i/notifications.ts
+++ b/src/api/endpoints/i/notifications.ts
@@ -5,6 +5,7 @@ import $ from 'cafy';
import Notification from '../../models/notification';
import serialize from '../../serializers/notification';
import getFriends from '../../common/get-friends';
+import read from '../../common/read-notification';
/**
* Get notifications
@@ -91,17 +92,6 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
// Mark as read all
if (notifications.length > 0 && markAsRead) {
- const ids = notifications
- .filter(x => x.is_read == false)
- .map(x => x._id);
-
- // Update documents
- await Notification.update({
- _id: { $in: ids }
- }, {
- $set: { is_read: true }
- }, {
- multi: true
- });
+ read(user._id, notifications);
}
});
diff --git a/src/api/endpoints/i/pin.ts b/src/api/endpoints/i/pin.ts
new file mode 100644
index 0000000000..a94950d22b
--- /dev/null
+++ b/src/api/endpoints/i/pin.ts
@@ -0,0 +1,44 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import User from '../../models/user';
+import Post from '../../models/post';
+import serialize from '../../serializers/user';
+
+/**
+ * Pin post
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = async (params, user) => new Promise(async (res, rej) => {
+ // Get 'post_id' parameter
+ const [postId, postIdErr] = $(params.post_id).id().$;
+ if (postIdErr) return rej('invalid post_id param');
+
+ // Fetch pinee
+ const post = await Post.findOne({
+ _id: postId,
+ user_id: user._id
+ });
+
+ if (post === null) {
+ return rej('post not found');
+ }
+
+ await User.update(user._id, {
+ $set: {
+ pinned_post_id: post._id
+ }
+ });
+
+ // Serialize
+ const iObj = await serialize(user, user, {
+ detail: true
+ });
+
+ // Send response
+ res(iObj);
+});
diff --git a/src/api/endpoints/i/regenerate_token.ts b/src/api/endpoints/i/regenerate_token.ts
new file mode 100644
index 0000000000..f96d10ebfc
--- /dev/null
+++ b/src/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 '../../event';
+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 = bcrypt.compareSync(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');
+});