From 6bc499f6579a9a248430748f9a69f3e5873a5ed3 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 8 Dec 2017 22:57:58 +0900 Subject: #967 --- src/api/endpoints.ts | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/api/endpoints.ts') diff --git a/src/api/endpoints.ts b/src/api/endpoints.ts index 06fb9a64ae..49871c0ce3 100644 --- a/src/api/endpoints.ts +++ b/src/api/endpoints.ts @@ -155,6 +155,14 @@ const endpoints: Endpoint[] = [ name: 'i', withCredential: true }, + { + name: 'i/2fa/register', + withCredential: true + }, + { + name: 'i/2fa/done', + withCredential: true + }, { name: 'i/update', withCredential: true, -- cgit v1.2.3-freya From d0cfe112e11c52acaeebb42c3229a37af23c846a Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 10 Dec 2017 02:45:32 +0900 Subject: #973 --- locales/en.yml | 3 +++ locales/ja.yml | 3 +++ src/api/endpoints.ts | 17 +++++++++++++---- src/api/endpoints/i/2fa/unregister.ts | 28 ++++++++++++++++++++++++++++ src/web/app/desktop/tags/settings.tag | 21 ++++++++++++++++++++- 5 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 src/api/endpoints/i/2fa/unregister.ts (limited to 'src/api/endpoints.ts') diff --git a/locales/en.yml b/locales/en.yml index 4dadcb8065..8392e170c4 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -311,6 +311,9 @@ desktop: url: "https://www.google.com/landing/2step/" caution: "As a caveat, security improves, but you can not sign in to Misskey if you lose a registered device, etc." register: "Register a device" + already-registered: "The setting has already been completed." + unregister: "Disable" + unregistered: "Two-step authentication has been disabled." enter-password: "Enter the password" authenticator: "First, you need install Google Authenticator to your device:" howtoinstall: "How to install" diff --git a/locales/ja.yml b/locales/ja.yml index d38ae682ed..f9d41d9092 100644 --- a/locales/ja.yml +++ b/locales/ja.yml @@ -311,6 +311,9 @@ desktop: url: "https://www.google.co.jp/intl/ja/landing/2step/" caution: "登録したデバイスを紛失するなどした場合、Misskeyにサインインできなくなりますのでご注意ください。" register: "デバイスを登録する" + already-registered: "既に設定は完了しています。" + unregister: "設定を解除" + unregistered: "二段階認証が無効になりました。" enter-password: "パスワードを入力してください" authenticator: "まず、Google Authenticatorをお使いのデバイスにインストールします:" howtoinstall: "インストール方法はこちら" diff --git a/src/api/endpoints.ts b/src/api/endpoints.ts index 49871c0ce3..1138df193b 100644 --- a/src/api/endpoints.ts +++ b/src/api/endpoints.ts @@ -157,11 +157,18 @@ const endpoints: Endpoint[] = [ }, { name: 'i/2fa/register', - withCredential: true + withCredential: true, + secure: true + }, + { + name: 'i/2fa/unregister', + withCredential: true, + secure: true }, { name: 'i/2fa/done', - withCredential: true + withCredential: true, + secure: true }, { name: 'i/update', @@ -179,11 +186,13 @@ const endpoints: Endpoint[] = [ }, { name: 'i/change_password', - withCredential: true + withCredential: true, + secure: true }, { name: 'i/regenerate_token', - withCredential: true + withCredential: true, + secure: true }, { name: 'i/pin', 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/web/app/desktop/tags/settings.tag b/src/web/app/desktop/tags/settings.tag index 9f71da6876..3fa1f50fce 100644 --- a/src/web/app/desktop/tags/settings.tag +++ b/src/web/app/desktop/tags/settings.tag @@ -266,7 +266,11 @@

%i18n:desktop.tags.mk-2fa-setting.intro%%i18n:desktop.tags.mk-2fa-setting.detail%

%fa:exclamation-triangle%%i18n:desktop.tags.mk-2fa-setting.caution%

-

+

+ +

%i18n:desktop.tags.mk-2fa-setting.already-registered%

+ +
  1. %i18n:desktop.tags.mk-2fa-setting.authenticator% %i18n:desktop.tags.mk-2fa-setting.howtoinstall%
  2. @@ -288,6 +292,7 @@ import passwordDialog from '../scripts/password-dialog'; import notify from '../scripts/notify'; + this.mixin('i'); this.mixin('api'); this.register = () => { @@ -302,11 +307,25 @@ }); }; + this.unregister = () => { + passwordDialog('%i18n:desktop.tags.mk-2fa-setting.enter-password%', password => { + this.api('i/2fa/unregister', { + password: password + }).then(data => { + notify('%i18n:desktop.tags.mk-2fa-setting.unregistered%'); + this.I.two_factor_enabled = false; + this.I.update(); + }); + }); + }; + this.submit = () => { this.api('i/2fa/done', { token: this.refs.token.value }).then(() => { notify('%i18n:desktop.tags.mk-2fa-setting.success%'); + this.I.two_factor_enabled = true; + this.I.update(); }).catch(() => { notify('%i18n:desktop.tags.mk-2fa-setting.failed%'); }); -- cgit v1.2.3-freya From a134aa5a81ea2c443153b3723abf662a8069e36a Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 22 Dec 2017 06:03:54 +0900 Subject: wip --- src/api/endpoints.ts | 17 +++++++++ src/api/endpoints/mute/create.ts | 61 +++++++++++++++++++++++++++++++ src/api/endpoints/mute/delete.ts | 63 ++++++++++++++++++++++++++++++++ src/api/endpoints/mute/list.ts | 73 +++++++++++++++++++++++++++++++++++++ src/api/endpoints/posts/timeline.ts | 19 +++++++--- 5 files changed, 228 insertions(+), 5 deletions(-) create mode 100644 src/api/endpoints/mute/create.ts create mode 100644 src/api/endpoints/mute/delete.ts create mode 100644 src/api/endpoints/mute/list.ts (limited to 'src/api/endpoints.ts') diff --git a/src/api/endpoints.ts b/src/api/endpoints.ts index 1138df193b..e846381578 100644 --- a/src/api/endpoints.ts +++ b/src/api/endpoints.ts @@ -222,6 +222,23 @@ const endpoints: Endpoint[] = [ withCredential: true, kind: 'notification-read' }, + + { + name: 'mute/create', + withCredential: true, + kind: 'account/write' + }, + { + name: 'mute/delete', + withCredential: true, + kind: 'account/write' + }, + { + name: 'mute/list', + withCredential: true, + kind: 'account/read' + }, + { name: 'notifications/get_unread_count', withCredential: true, diff --git a/src/api/endpoints/mute/create.ts b/src/api/endpoints/mute/create.ts new file mode 100644 index 0000000000..f44854ab52 --- /dev/null +++ b/src/api/endpoints/mute/create.ts @@ -0,0 +1,61 @@ +/** + * Module dependencies + */ +import $ from 'cafy'; +import User from '../../models/user'; +import Mute from '../../models/mute'; + +/** + * Mute a user + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => new Promise(async (res, rej) => { + const muter = user; + + // Get 'user_id' parameter + const [userId, userIdErr] = $(params.user_id).id().$; + if (userIdErr) return rej('invalid user_id param'); + + // 自分自身 + if (user._id.equals(userId)) { + return rej('mutee is yourself'); + } + + // Get mutee + const mutee = await User.findOne({ + _id: userId + }, { + fields: { + data: false, + profile: false + } + }); + + if (mutee === null) { + return rej('user not found'); + } + + // Check if already muting + const exist = await Mute.findOne({ + muter_id: muter._id, + mutee_id: mutee._id, + deleted_at: { $exists: false } + }); + + if (exist !== null) { + return rej('already muting'); + } + + // Create mute + await Mute.insert({ + created_at: new Date(), + muter_id: muter._id, + mutee_id: mutee._id, + }); + + // Send response + res(); +}); diff --git a/src/api/endpoints/mute/delete.ts b/src/api/endpoints/mute/delete.ts new file mode 100644 index 0000000000..d6bff3353a --- /dev/null +++ b/src/api/endpoints/mute/delete.ts @@ -0,0 +1,63 @@ +/** + * Module dependencies + */ +import $ from 'cafy'; +import User from '../../models/user'; +import Mute from '../../models/mute'; + +/** + * Unmute a user + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => new Promise(async (res, rej) => { + const muter = user; + + // Get 'user_id' parameter + const [userId, userIdErr] = $(params.user_id).id().$; + if (userIdErr) return rej('invalid user_id param'); + + // Check if the mutee is yourself + if (user._id.equals(userId)) { + return rej('mutee is yourself'); + } + + // Get mutee + const mutee = await User.findOne({ + _id: userId + }, { + fields: { + data: false, + profile: false + } + }); + + if (mutee === null) { + return rej('user not found'); + } + + // Check not muting + const exist = await Mute.findOne({ + muter_id: muter._id, + mutee_id: mutee._id, + deleted_at: { $exists: false } + }); + + if (exist === null) { + return rej('already not muting'); + } + + // Delete mute + await Mute.update({ + _id: exist._id + }, { + $set: { + deleted_at: new Date() + } + }); + + // Send response + res(); +}); diff --git a/src/api/endpoints/mute/list.ts b/src/api/endpoints/mute/list.ts new file mode 100644 index 0000000000..740e19f0bb --- /dev/null +++ b/src/api/endpoints/mute/list.ts @@ -0,0 +1,73 @@ +/** + * Module dependencies + */ +import $ from 'cafy'; +import Mute from '../../models/mute'; +import serialize from '../../serializers/user'; +import getFriends from '../../common/get-friends'; + +/** + * Get muted users of a user + * + * @param {any} params + * @param {any} me + * @return {Promise} + */ +module.exports = (params, me) => new Promise(async (res, rej) => { + // Get 'iknow' parameter + const [iknow = false, iknowErr] = $(params.iknow).optional.boolean().$; + if (iknowErr) return rej('invalid iknow param'); + + // Get 'limit' parameter + const [limit = 30, limitErr] = $(params.limit).optional.number().range(1, 100).$; + if (limitErr) return rej('invalid limit param'); + + // Get 'cursor' parameter + const [cursor = null, cursorErr] = $(params.cursor).optional.id().$; + if (cursorErr) return rej('invalid cursor param'); + + // Construct query + const query = { + muter_id: me._id, + deleted_at: { $exists: false } + } as any; + + if (iknow) { + // Get my friends + const myFriends = await getFriends(me._id); + + query.mutee_id = { + $in: myFriends + }; + } + + // カーソルが指定されている場合 + if (cursor) { + query._id = { + $lt: cursor + }; + } + + // Get mutes + const mutes = await Mute + .find(query, { + limit: limit + 1, + sort: { _id: -1 } + }); + + // 「次のページ」があるかどうか + const inStock = mutes.length === limit + 1; + if (inStock) { + mutes.pop(); + } + + // Serialize + const users = await Promise.all(mutes.map(async m => + await serialize(m.mutee_id, me, { detail: true }))); + + // Response + res({ + users: users, + next: inStock ? mutes[mutes.length - 1]._id : null, + }); +}); diff --git a/src/api/endpoints/posts/timeline.ts b/src/api/endpoints/posts/timeline.ts index 6cc7825e64..da7ffd0c14 100644 --- a/src/api/endpoints/posts/timeline.ts +++ b/src/api/endpoints/posts/timeline.ts @@ -4,6 +4,7 @@ import $ from 'cafy'; import rap from '@prezzemolo/rap'; import Post from '../../models/post'; +import Mute from '../../models/mute'; import ChannelWatching from '../../models/channel-watching'; import getFriends from '../../common/get-friends'; import serialize from '../../serializers/post'; @@ -42,15 +43,23 @@ module.exports = async (params, user, app) => { throw 'only one of since_id, until_id, since_date, until_date can be specified'; } - const { followingIds, watchingChannelIds } = await rap({ + const { followingIds, watchingChannelIds, mutedUserIds } = await rap({ // ID list of the user itself and other users who the user follows followingIds: getFriends(user._id), + // Watchしているチャンネルを取得 watchingChannelIds: ChannelWatching.find({ user_id: user._id, // 削除されたドキュメントは除く deleted_at: { $exists: false } - }).then(watches => watches.map(w => w.channel_id)) + }).then(watches => watches.map(w => w.channel_id)), + + // ミュートしているユーザーを取得 + mutedUserIds: Mute.find({ + muter_id: user._id, + // 削除されたドキュメントは除く + deleted_at: { $exists: false } + }).then(ms => ms.map(m => m.mutee_id)) }); //#region Construct query @@ -80,13 +89,13 @@ module.exports = async (params, user, app) => { }], // mute user_id: { - $nin: mutes + $nin: mutedUserIds }, '_reply.user_id': { - $nin: mutes + $nin: mutedUserIds }, '_repost.user_id': { - $nin: mutes + $nin: mutedUserIds }, } as any; -- cgit v1.2.3-freya