summaryrefslogtreecommitdiff
path: root/src/api/endpoints/i
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-02-05 03:59:29 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-02-05 03:59:29 +0900
commitdbd3cdb308d2edf600b20a8b632045c6163ae326 (patch)
treede3630065fcddeb1916668ef3b0b43a219340e2e /src/api/endpoints/i
parentFix (diff)
parentMerge pull request #1097 from syuilo/refactor (diff)
downloadmisskey-dbd3cdb308d2edf600b20a8b632045c6163ae326.tar.gz
misskey-dbd3cdb308d2edf600b20a8b632045c6163ae326.tar.bz2
misskey-dbd3cdb308d2edf600b20a8b632045c6163ae326.zip
Merge remote-tracking branch 'refs/remotes/origin/master' into vue-#972
Diffstat (limited to 'src/api/endpoints/i')
-rw-r--r--src/api/endpoints/i/2fa/done.ts37
-rw-r--r--src/api/endpoints/i/2fa/register.ts48
-rw-r--r--src/api/endpoints/i/2fa/unregister.ts28
-rw-r--r--src/api/endpoints/i/authorized_apps.ts4
-rw-r--r--src/api/endpoints/i/favorites.ts4
-rw-r--r--src/api/endpoints/i/notifications.ts43
-rw-r--r--src/api/endpoints/i/pin.ts4
-rw-r--r--src/api/endpoints/i/signin_history.ts21
-rw-r--r--src/api/endpoints/i/update.ts6
9 files changed, 159 insertions, 36 deletions
diff --git a/src/api/endpoints/i/2fa/done.ts b/src/api/endpoints/i/2fa/done.ts
new file mode 100644
index 0000000000..0b36033bb6
--- /dev/null
+++ b/src/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.two_factor_temp_secret == null) {
+ return rej('二段階認証の設定が開始されていません');
+ }
+
+ const verified = (speakeasy as any).totp.verify({
+ secret: user.two_factor_temp_secret,
+ encoding: 'base32',
+ token: _token
+ });
+
+ if (!verified) {
+ return rej('not verified');
+ }
+
+ await User.update(user._id, {
+ $set: {
+ two_factor_secret: user.two_factor_temp_secret,
+ two_factor_enabled: true
+ }
+ });
+
+ res();
+});
diff --git a/src/api/endpoints/i/2fa/register.ts b/src/api/endpoints/i/2fa/register.ts
new file mode 100644
index 0000000000..c2b5037a29
--- /dev/null
+++ b/src/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 '../../../../conf';
+
+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: {
+ two_factor_temp_secret: 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/api/endpoints/i/2fa/unregister.ts b/src/api/endpoints/i/2fa/unregister.ts
new file mode 100644
index 0000000000..6bee6a26f2
--- /dev/null
+++ b/src/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: {
+ two_factor_secret: null,
+ two_factor_enabled: false
+ }
+ });
+
+ res();
+});
diff --git a/src/api/endpoints/i/authorized_apps.ts b/src/api/endpoints/i/authorized_apps.ts
index 807ca5b1e7..40ce7a68c8 100644
--- a/src/api/endpoints/i/authorized_apps.ts
+++ b/src/api/endpoints/i/authorized_apps.ts
@@ -3,7 +3,7 @@
*/
import $ from 'cafy';
import AccessToken from '../../models/access-token';
-import serialize from '../../serializers/app';
+import { pack } from '../../models/app';
/**
* Get authorized apps of my account
@@ -39,5 +39,5 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
// Serialize
res(await Promise.all(tokens.map(async token =>
- await serialize(token.app_id))));
+ await pack(token.app_id))));
});
diff --git a/src/api/endpoints/i/favorites.ts b/src/api/endpoints/i/favorites.ts
index a66eaa7546..eb464cf0f0 100644
--- a/src/api/endpoints/i/favorites.ts
+++ b/src/api/endpoints/i/favorites.ts
@@ -3,7 +3,7 @@
*/
import $ from 'cafy';
import Favorite from '../../models/favorite';
-import serialize from '../../serializers/post';
+import { pack } from '../../models/post';
/**
* Get followers of a user
@@ -39,6 +39,6 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
// Serialize
res(await Promise.all(favorites.map(async favorite =>
- await serialize(favorite.post)
+ await pack(favorite.post)
)));
});
diff --git a/src/api/endpoints/i/notifications.ts b/src/api/endpoints/i/notifications.ts
index 607e0768a4..688039a0dd 100644
--- a/src/api/endpoints/i/notifications.ts
+++ b/src/api/endpoints/i/notifications.ts
@@ -3,7 +3,8 @@
*/
import $ from 'cafy';
import Notification from '../../models/notification';
-import serialize from '../../serializers/notification';
+import Mute from '../../models/mute';
+import { pack } from '../../models/notification';
import getFriends from '../../common/get-friends';
import read from '../../common/read-notification';
@@ -36,17 +37,27 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$;
if (sinceIdErr) return rej('invalid since_id param');
- // Get 'max_id' parameter
- const [maxId, maxIdErr] = $(params.max_id).optional.id().$;
- if (maxIdErr) return rej('invalid max_id param');
+ // Get 'until_id' parameter
+ const [untilId, untilIdErr] = $(params.until_id).optional.id().$;
+ if (untilIdErr) return rej('invalid until_id param');
- // Check if both of since_id and max_id is specified
- if (sinceId && maxId) {
- return rej('cannot set since_id and max_id');
+ // Check if both of since_id and until_id is specified
+ if (sinceId && untilId) {
+ return rej('cannot set since_id and until_id');
}
+ const mute = await Mute.find({
+ muter_id: user._id,
+ deleted_at: { $exists: false }
+ });
+
const query = {
- notifiee_id: user._id
+ notifiee_id: user._id,
+ $and: [{
+ notifier_id: {
+ $nin: mute.map(m => m.mutee_id)
+ }
+ }]
} as any;
const sort = {
@@ -54,12 +65,14 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
};
if (following) {
- // ID list of the user $self and other users who the user follows
+ // ID list of the user itself and other users who the user follows
const followingIds = await getFriends(user._id);
- query.notifier_id = {
- $in: followingIds
- };
+ query.$and.push({
+ notifier_id: {
+ $in: followingIds
+ }
+ });
}
if (type) {
@@ -73,9 +86,9 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
query._id = {
$gt: sinceId
};
- } else if (maxId) {
+ } else if (untilId) {
query._id = {
- $lt: maxId
+ $lt: untilId
};
}
@@ -88,7 +101,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
// Serialize
res(await Promise.all(notifications.map(async notification =>
- await serialize(notification))));
+ await pack(notification))));
// Mark as read all
if (notifications.length > 0 && markAsRead) {
diff --git a/src/api/endpoints/i/pin.ts b/src/api/endpoints/i/pin.ts
index a94950d22b..ff546fc2bd 100644
--- a/src/api/endpoints/i/pin.ts
+++ b/src/api/endpoints/i/pin.ts
@@ -4,7 +4,7 @@
import $ from 'cafy';
import User from '../../models/user';
import Post from '../../models/post';
-import serialize from '../../serializers/user';
+import { pack } from '../../models/user';
/**
* Pin post
@@ -35,7 +35,7 @@ module.exports = async (params, user) => new Promise(async (res, rej) => {
});
// Serialize
- const iObj = await serialize(user, user, {
+ const iObj = await pack(user, user, {
detail: true
});
diff --git a/src/api/endpoints/i/signin_history.ts b/src/api/endpoints/i/signin_history.ts
index 1a6e50c7c8..859e81653d 100644
--- a/src/api/endpoints/i/signin_history.ts
+++ b/src/api/endpoints/i/signin_history.ts
@@ -2,8 +2,7 @@
* Module dependencies
*/
import $ from 'cafy';
-import Signin from '../../models/signin';
-import serialize from '../../serializers/signin';
+import Signin, { pack } from '../../models/signin';
/**
* Get signin history of my account
@@ -21,13 +20,13 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$;
if (sinceIdErr) return rej('invalid since_id param');
- // Get 'max_id' parameter
- const [maxId, maxIdErr] = $(params.max_id).optional.id().$;
- if (maxIdErr) return rej('invalid max_id param');
+ // Get 'until_id' parameter
+ const [untilId, untilIdErr] = $(params.until_id).optional.id().$;
+ if (untilIdErr) return rej('invalid until_id param');
- // Check if both of since_id and max_id is specified
- if (sinceId && maxId) {
- return rej('cannot set since_id and max_id');
+ // Check if both of since_id and until_id is specified
+ if (sinceId && untilId) {
+ return rej('cannot set since_id and until_id');
}
const query = {
@@ -43,9 +42,9 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
query._id = {
$gt: sinceId
};
- } else if (maxId) {
+ } else if (untilId) {
query._id = {
- $lt: maxId
+ $lt: untilId
};
}
@@ -58,5 +57,5 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
// Serialize
res(await Promise.all(history.map(async record =>
- await serialize(record))));
+ await pack(record))));
});
diff --git a/src/api/endpoints/i/update.ts b/src/api/endpoints/i/update.ts
index c484c51a96..7bbbf95900 100644
--- a/src/api/endpoints/i/update.ts
+++ b/src/api/endpoints/i/update.ts
@@ -2,9 +2,7 @@
* Module dependencies
*/
import $ from 'cafy';
-import User from '../../models/user';
-import { isValidName, isValidDescription, isValidLocation, isValidBirthday } from '../../models/user';
-import serialize from '../../serializers/user';
+import User, { isValidName, isValidDescription, isValidLocation, isValidBirthday, pack } from '../../models/user';
import event from '../../event';
import config from '../../../conf';
@@ -65,7 +63,7 @@ module.exports = async (params, user, _, isSecure) => new Promise(async (res, re
});
// Serialize
- const iObj = await serialize(user, user, {
+ const iObj = await pack(user, user, {
detail: true,
includeSecrets: isSecure
});