From 56d571c0f0f525263ea6257a5d5a2e7a9085e203 Mon Sep 17 00:00:00 2001
From: syuilo
Date: Thu, 15 Nov 2018 04:15:42 +0900
Subject: [PATCH] Moderator system
Closes #2357
---
locales/ja-JP.yml | 7 +++
src/client/app/admin/views/index.vue | 9 ++-
src/client/app/admin/views/moderators.vue | 61 +++++++++++++++++++
.../app/common/views/components/note-menu.vue | 2 +-
.../views/components/ui.header.account.vue | 2 +-
.../app/mobile/views/components/ui.nav.vue | 2 +-
src/models/user.ts | 2 +
src/server/api/call.ts | 4 ++
src/server/api/endpoints.ts | 5 ++
src/server/api/endpoints/admin/emoji/add.ts | 2 +-
src/server/api/endpoints/admin/emoji/list.ts | 2 +-
.../api/endpoints/admin/emoji/remove.ts | 2 +-
.../api/endpoints/admin/emoji/update.ts | 2 +-
src/server/api/endpoints/admin/invite.ts | 2 +-
.../api/endpoints/admin/moderators/add.ts | 45 ++++++++++++++
.../api/endpoints/admin/moderators/remove.ts | 45 ++++++++++++++
.../api/endpoints/admin/suspend-user.ts | 2 +-
.../api/endpoints/admin/unsuspend-user.ts | 2 +-
.../api/endpoints/admin/unverify-user.ts | 2 +-
src/server/api/endpoints/admin/update-meta.ts | 2 +-
src/server/api/endpoints/admin/verify-user.ts | 2 +-
src/server/api/endpoints/meta.ts | 2 +-
src/server/api/endpoints/notes/delete.ts | 2 +-
23 files changed, 191 insertions(+), 17 deletions(-)
create mode 100644 src/client/app/admin/views/moderators.vue
create mode 100644 src/server/api/endpoints/admin/moderators/add.ts
create mode 100644 src/server/api/endpoints/admin/moderators/remove.ts
diff --git a/locales/ja-JP.yml b/locales/ja-JP.yml
index 7d716f6ca3..32cf7d075c 100644
--- a/locales/ja-JP.yml
+++ b/locales/ja-JP.yml
@@ -1034,6 +1034,7 @@ admin/views/index.vue:
dashboard: "ダッシュボード"
instance: "インスタンス"
emoji: "カスタム絵文字"
+ moderators: "モデレーター"
users: "ユーザー"
update: "更新"
announcements: "お知らせ"
@@ -1133,6 +1134,12 @@ admin/views/users.vue:
unverify: "公式アカウントを解除する"
unverified: "公式アカウントを解除しました"
+admin/views/moderators.vue:
+ add-moderator:
+ title: "モデレーターの登録"
+ add: "登録"
+ added: "モデレーターを登録しました"
+
admin/views/emoji.vue:
add-emoji:
title: "絵文字の登録"
diff --git a/src/client/app/admin/views/index.vue b/src/client/app/admin/views/index.vue
index a5ffb2098e..116d794b91 100644
--- a/src/client/app/admin/views/index.vue
+++ b/src/client/app/admin/views/index.vue
@@ -20,6 +20,7 @@
- {{ $t('dashboard') }}
- {{ $t('instance') }}
+ - {{ $t('moderators') }}
- {{ $t('users') }}
- {{ $t('emoji') }}
- {{ $t('announcements') }}
@@ -38,6 +39,7 @@
+
@@ -54,11 +56,12 @@ import i18n from '../../i18n';
import { version } from '../../config';
import XDashboard from "./dashboard.vue";
import XInstance from "./instance.vue";
+import XModerators from "./moderators.vue";
import XEmoji from "./emoji.vue";
import XAnnouncements from "./announcements.vue";
import XHashtags from "./hashtags.vue";
import XUsers from "./users.vue";
-import { faArrowLeft } from '@fortawesome/free-solid-svg-icons';
+import { faHeadset, faArrowLeft } from '@fortawesome/free-solid-svg-icons';
import { faGrin } from '@fortawesome/free-regular-svg-icons';
// Detect the user agent
@@ -70,6 +73,7 @@ export default Vue.extend({
components: {
XDashboard,
XInstance,
+ XModerators,
XEmoji,
XAnnouncements,
XHashtags,
@@ -85,7 +89,8 @@ export default Vue.extend({
isMobile,
navOpend: !isMobile,
faGrin,
- faArrowLeft
+ faArrowLeft,
+ faHeadset
};
},
methods: {
diff --git a/src/client/app/admin/views/moderators.vue b/src/client/app/admin/views/moderators.vue
new file mode 100644
index 0000000000..ebf20c12fd
--- /dev/null
+++ b/src/client/app/admin/views/moderators.vue
@@ -0,0 +1,61 @@
+
+
+
+ {{ $t('add-moderator.title') }}
+
+
+ @
+
+ {{ $t('add-moderator.add') }}
+
+
+
+
+
+
+
+
diff --git a/src/client/app/common/views/components/note-menu.vue b/src/client/app/common/views/components/note-menu.vue
index d848cb765d..7d15b4ed7f 100644
--- a/src/client/app/common/views/components/note-menu.vue
+++ b/src/client/app/common/views/components/note-menu.vue
@@ -55,7 +55,7 @@ export default Vue.extend({
}
] : []
], [
- this.note.userId == this.$store.state.i.id || this.$store.state.i.isAdmin ? [{
+ this.note.userId == this.$store.state.i.id || this.$store.state.i.isAdmin || this.$store.state.i.isModerator ? [{
icon: ['far', 'trash-alt'],
text: this.$t('delete'),
action: this.del
diff --git a/src/client/app/desktop/views/components/ui.header.account.vue b/src/client/app/desktop/views/components/ui.header.account.vue
index 79410273aa..a16f164556 100644
--- a/src/client/app/desktop/views/components/ui.header.account.vue
+++ b/src/client/app/desktop/views/components/ui.header.account.vue
@@ -58,7 +58,7 @@
-
+
{{ $t('admin') }}
diff --git a/src/client/app/mobile/views/components/ui.nav.vue b/src/client/app/mobile/views/components/ui.nav.vue
index c529ae2617..4f085a5e6d 100644
--- a/src/client/app/mobile/views/components/ui.nav.vue
+++ b/src/client/app/mobile/views/components/ui.nav.vue
@@ -30,7 +30,7 @@
diff --git a/src/models/user.ts b/src/models/user.ts
index 2ca1917dc9..a5863de8da 100644
--- a/src/models/user.ts
+++ b/src/models/user.ts
@@ -99,6 +99,7 @@ export interface ILocalUser extends IUserBase {
lastUsedAt: Date;
isCat: boolean;
isAdmin?: boolean;
+ isModerator?: boolean;
isVerified?: boolean;
twoFactorSecret: string;
twoFactorEnabled: boolean;
@@ -125,6 +126,7 @@ export interface IRemoteUser extends IUserBase {
};
updatedAt: Date;
isAdmin: false;
+ isModerator: false;
}
export type IUser = ILocalUser | IRemoteUser;
diff --git a/src/server/api/call.ts b/src/server/api/call.ts
index 6252537c0e..a3953d0439 100644
--- a/src/server/api/call.ts
+++ b/src/server/api/call.ts
@@ -29,6 +29,10 @@ export default (endpoint: string, user: IUser, app: IApp, data: any, file?: any)
return rej('YOU_ARE_NOT_ADMIN');
}
+ if (ep.meta.requireModerator && !user.isAdmin && !user.isModerator) {
+ return rej('YOU_ARE_NOT_MODERATOR');
+ }
+
if (app && ep.meta.kind && !app.permission.some(p => p === ep.meta.kind)) {
return rej('PERMISSION_DENIED');
}
diff --git a/src/server/api/endpoints.ts b/src/server/api/endpoints.ts
index e764ac2e95..6765a63e9f 100644
--- a/src/server/api/endpoints.ts
+++ b/src/server/api/endpoints.ts
@@ -30,6 +30,11 @@ export interface IEndpointMeta {
*/
requireAdmin?: boolean;
+ /**
+ * 管理者またはモデレーターのみ使えるエンドポイントか否か
+ */
+ requireModerator?: boolean;
+
/**
* エンドポイントのリミテーションに関するやつ
* 省略した場合はリミテーションは無いものとして解釈されます。
diff --git a/src/server/api/endpoints/admin/emoji/add.ts b/src/server/api/endpoints/admin/emoji/add.ts
index 4f9a84e67e..91b5ff62d7 100644
--- a/src/server/api/endpoints/admin/emoji/add.ts
+++ b/src/server/api/endpoints/admin/emoji/add.ts
@@ -8,7 +8,7 @@ export const meta = {
},
requireCredential: true,
- requireAdmin: true,
+ requireModerator: true,
params: {
name: {
diff --git a/src/server/api/endpoints/admin/emoji/list.ts b/src/server/api/endpoints/admin/emoji/list.ts
index fd69fb0b29..428b274fed 100644
--- a/src/server/api/endpoints/admin/emoji/list.ts
+++ b/src/server/api/endpoints/admin/emoji/list.ts
@@ -8,7 +8,7 @@ export const meta = {
},
requireCredential: true,
- requireAdmin: true,
+ requireModerator: true,
params: {
host: {
diff --git a/src/server/api/endpoints/admin/emoji/remove.ts b/src/server/api/endpoints/admin/emoji/remove.ts
index 32f1ced0c8..1d6ed11b63 100644
--- a/src/server/api/endpoints/admin/emoji/remove.ts
+++ b/src/server/api/endpoints/admin/emoji/remove.ts
@@ -9,7 +9,7 @@ export const meta = {
},
requireCredential: true,
- requireAdmin: true,
+ requireModerator: true,
params: {
id: {
diff --git a/src/server/api/endpoints/admin/emoji/update.ts b/src/server/api/endpoints/admin/emoji/update.ts
index d0c2e6dafc..cbcc07fd48 100644
--- a/src/server/api/endpoints/admin/emoji/update.ts
+++ b/src/server/api/endpoints/admin/emoji/update.ts
@@ -9,7 +9,7 @@ export const meta = {
},
requireCredential: true,
- requireAdmin: true,
+ requireModerator: true,
params: {
id: {
diff --git a/src/server/api/endpoints/admin/invite.ts b/src/server/api/endpoints/admin/invite.ts
index 056cb8aa75..ebfcb84452 100644
--- a/src/server/api/endpoints/admin/invite.ts
+++ b/src/server/api/endpoints/admin/invite.ts
@@ -8,7 +8,7 @@ export const meta = {
},
requireCredential: true,
- requireAdmin: true,
+ requireModerator: true,
params: {}
};
diff --git a/src/server/api/endpoints/admin/moderators/add.ts b/src/server/api/endpoints/admin/moderators/add.ts
new file mode 100644
index 0000000000..4b4675c566
--- /dev/null
+++ b/src/server/api/endpoints/admin/moderators/add.ts
@@ -0,0 +1,45 @@
+import $ from 'cafy';
+import ID, { transform } from '../../../../../misc/cafy-id';
+import define from '../../../define';
+import User from '../../../../../models/user';
+
+export const meta = {
+ desc: {
+ 'ja-JP': '指定したユーザーをモデレーターにします。',
+ 'en-US': 'Mark a user as moderator.'
+ },
+
+ requireCredential: true,
+ requireAdmin: true,
+
+ params: {
+ userId: {
+ validator: $.type(ID),
+ transform: transform,
+ desc: {
+ 'ja-JP': '対象のユーザーID',
+ 'en-US': 'The user ID'
+ }
+ },
+ }
+};
+
+export default define(meta, (ps) => new Promise(async (res, rej) => {
+ const user = await User.findOne({
+ _id: ps.userId
+ });
+
+ if (user == null) {
+ return rej('user not found');
+ }
+
+ await User.update({
+ _id: user._id
+ }, {
+ $set: {
+ isModerator: true
+ }
+ });
+
+ res();
+}));
diff --git a/src/server/api/endpoints/admin/moderators/remove.ts b/src/server/api/endpoints/admin/moderators/remove.ts
new file mode 100644
index 0000000000..2b9da61bb5
--- /dev/null
+++ b/src/server/api/endpoints/admin/moderators/remove.ts
@@ -0,0 +1,45 @@
+import $ from 'cafy';
+import ID, { transform } from '../../../../../misc/cafy-id';
+import define from '../../../define';
+import User from '../../../../../models/user';
+
+export const meta = {
+ desc: {
+ 'ja-JP': '指定したユーザーをモデレーター解除します。',
+ 'en-US': 'Unmark a user as moderator.'
+ },
+
+ requireCredential: true,
+ requireAdmin: true,
+
+ params: {
+ userId: {
+ validator: $.type(ID),
+ transform: transform,
+ desc: {
+ 'ja-JP': '対象のユーザーID',
+ 'en-US': 'The user ID'
+ }
+ },
+ }
+};
+
+export default define(meta, (ps) => new Promise(async (res, rej) => {
+ const user = await User.findOne({
+ _id: ps.userId
+ });
+
+ if (user == null) {
+ return rej('user not found');
+ }
+
+ await User.update({
+ _id: user._id
+ }, {
+ $set: {
+ isModerator: false
+ }
+ });
+
+ res();
+}));
diff --git a/src/server/api/endpoints/admin/suspend-user.ts b/src/server/api/endpoints/admin/suspend-user.ts
index 0ad0aab74c..5bbd387a20 100644
--- a/src/server/api/endpoints/admin/suspend-user.ts
+++ b/src/server/api/endpoints/admin/suspend-user.ts
@@ -10,7 +10,7 @@ export const meta = {
},
requireCredential: true,
- requireAdmin: true,
+ requireModerator: true,
params: {
userId: {
diff --git a/src/server/api/endpoints/admin/unsuspend-user.ts b/src/server/api/endpoints/admin/unsuspend-user.ts
index 7c5eedee46..4b53246264 100644
--- a/src/server/api/endpoints/admin/unsuspend-user.ts
+++ b/src/server/api/endpoints/admin/unsuspend-user.ts
@@ -10,7 +10,7 @@ export const meta = {
},
requireCredential: true,
- requireAdmin: true,
+ requireModerator: true,
params: {
userId: {
diff --git a/src/server/api/endpoints/admin/unverify-user.ts b/src/server/api/endpoints/admin/unverify-user.ts
index d749e002e3..3e044ffed7 100644
--- a/src/server/api/endpoints/admin/unverify-user.ts
+++ b/src/server/api/endpoints/admin/unverify-user.ts
@@ -10,7 +10,7 @@ export const meta = {
},
requireCredential: true,
- requireAdmin: true,
+ requireModerator: true,
params: {
userId: {
diff --git a/src/server/api/endpoints/admin/update-meta.ts b/src/server/api/endpoints/admin/update-meta.ts
index 39d7ef86b9..1e4ff959d9 100644
--- a/src/server/api/endpoints/admin/update-meta.ts
+++ b/src/server/api/endpoints/admin/update-meta.ts
@@ -8,7 +8,7 @@ export const meta = {
},
requireCredential: true,
- requireAdmin: true,
+ requireModerator: true,
params: {
broadcasts: {
diff --git a/src/server/api/endpoints/admin/verify-user.ts b/src/server/api/endpoints/admin/verify-user.ts
index 09efc2e803..996e044d18 100644
--- a/src/server/api/endpoints/admin/verify-user.ts
+++ b/src/server/api/endpoints/admin/verify-user.ts
@@ -10,7 +10,7 @@ export const meta = {
},
requireCredential: true,
- requireAdmin: true,
+ requireModerator: true,
params: {
userId: {
diff --git a/src/server/api/endpoints/meta.ts b/src/server/api/endpoints/meta.ts
index f7c3179909..b324b113c8 100644
--- a/src/server/api/endpoints/meta.ts
+++ b/src/server/api/endpoints/meta.ts
@@ -84,7 +84,7 @@ export default define(meta, (ps, me) => new Promise(async (res, rej) => {
};
}
- if (me && me.isAdmin) {
+ if (me && (me.isAdmin || me.isModerator)) {
response.hidedTags = instance.hidedTags;
response.recaptchaSecretKey = instance.recaptchaSecretKey;
response.proxyAccount = instance.proxyAccount;
diff --git a/src/server/api/endpoints/notes/delete.ts b/src/server/api/endpoints/notes/delete.ts
index a8f22ad405..aa11f7bf19 100644
--- a/src/server/api/endpoints/notes/delete.ts
+++ b/src/server/api/endpoints/notes/delete.ts
@@ -38,7 +38,7 @@ export default define(meta, (ps, user) => new Promise(async (res, rej) => {
return rej('note not found');
}
- if (!user.isAdmin && !note.userId.equals(user._id)) {
+ if (!user.isAdmin && !user.isModerator && !note.userId.equals(user._id)) {
return rej('access denied');
}