Improve custom emoji managemant

This commit is contained in:
syuilo 2020-02-14 01:09:39 +09:00
parent 5ac4c48ad1
commit f5f7654f4b
5 changed files with 86 additions and 81 deletions

View file

@ -393,6 +393,9 @@ noGroups: "グループがありません"
joinOrCreateGroup: "既存のグループに招待してもらうか、新しくグループを作成してください。" joinOrCreateGroup: "既存のグループに招待してもらうか、新しくグループを作成してください。"
noHistory: "履歴はありません" noHistory: "履歴はありません"
disableAnimatedMfm: "動きのあるMFMを無効にする" disableAnimatedMfm: "動きのあるMFMを無効にする"
doing: "やっています"
category: "カテゴリ"
tags: "タグ"
_ago: _ago:
unknown: "謎" unknown: "謎"

View file

@ -2,10 +2,10 @@
<div class="mk-instance-emojis"> <div class="mk-instance-emojis">
<portal to="icon"><fa :icon="faLaugh"/></portal> <portal to="icon"><fa :icon="faLaugh"/></portal>
<portal to="title">{{ $t('customEmojis') }}</portal> <portal to="title">{{ $t('customEmojis') }}</portal>
<section class="_card local"> <section class="_card local">
<div class="_title"><fa :icon="faLaugh"/> {{ $t('customEmojis') }}</div> <div class="_title"><fa :icon="faLaugh"/> {{ $t('customEmojis') }}</div>
<div class="_content"> <div class="_content">
<input ref="file" type="file" style="display: none;" @change="onChangeFile"/>
<mk-pagination :pagination="pagination" class="emojis" ref="emojis"> <mk-pagination :pagination="pagination" class="emojis" ref="emojis">
<template #empty><span>{{ $t('noCustomEmojis') }}</span></template> <template #empty><span>{{ $t('noCustomEmojis') }}</span></template>
<template #default="{items}"> <template #default="{items}">
@ -13,15 +13,25 @@
<img :src="emoji.url" class="img" :alt="emoji.name"/> <img :src="emoji.url" class="img" :alt="emoji.name"/>
<div class="body"> <div class="body">
<span class="name">{{ emoji.name }}</span> <span class="name">{{ emoji.name }}</span>
<span class="info">
<b class="category">{{ emoji.category }}</b>
<span class="aliases">{{ emoji.aliases.join(' ') }}</span>
</span>
</div> </div>
</div> </div>
</template> </template>
</mk-pagination> </mk-pagination>
</div> </div>
<div class="_footer"> <div class="_content" v-if="selected">
<mk-button inline primary @click="add()"><fa :icon="faPlus"/> {{ $t('addEmoji') }}</mk-button> <mk-input v-model="name"><span>{{ $t('name') }}</span></mk-input>
<mk-input v-model="category"><span>{{ $t('category') }}</span></mk-input>
<mk-input v-model="aliases"><span>{{ $t('tags') }}</span></mk-input>
<mk-button inline primary @click="update"><fa :icon="faSave"/> {{ $t('save') }}</mk-button>
<mk-button inline :disabled="selected == null" @click="del()"><fa :icon="faTrashAlt"/> {{ $t('delete') }}</mk-button> <mk-button inline :disabled="selected == null" @click="del()"><fa :icon="faTrashAlt"/> {{ $t('delete') }}</mk-button>
</div> </div>
<div class="_footer">
<mk-button inline primary @click="add"><fa :icon="faPlus"/> {{ $t('addEmoji') }}</mk-button>
</div>
</section> </section>
<section class="_card remote"> <section class="_card remote">
<div class="_title"><fa :icon="faLaugh"/> {{ $t('customEmojisOfRemote') }}</div> <div class="_title"><fa :icon="faLaugh"/> {{ $t('customEmojisOfRemote') }}</div>
@ -34,7 +44,7 @@
<img :src="emoji.url" class="img" :alt="emoji.name"/> <img :src="emoji.url" class="img" :alt="emoji.name"/>
<div class="body"> <div class="body">
<span class="name">{{ emoji.name }}</span> <span class="name">{{ emoji.name }}</span>
<span class="host">{{ emoji.host }}</span> <span class="info">{{ emoji.host }}</span>
</div> </div>
</div> </div>
</template> </template>
@ -49,12 +59,12 @@
<script lang="ts"> <script lang="ts">
import Vue from 'vue'; import Vue from 'vue';
import { faPlus } from '@fortawesome/free-solid-svg-icons'; import { faPlus, faSave } from '@fortawesome/free-solid-svg-icons';
import { faTrashAlt, faLaugh } from '@fortawesome/free-regular-svg-icons'; import { faTrashAlt, faLaugh } from '@fortawesome/free-regular-svg-icons';
import MkButton from '../../components/ui/button.vue'; import MkButton from '../../components/ui/button.vue';
import MkInput from '../../components/ui/input.vue'; import MkInput from '../../components/ui/input.vue';
import MkPagination from '../../components/ui/pagination.vue'; import MkPagination from '../../components/ui/pagination.vue';
import { apiUrl } from '../../config'; import { selectFile } from '../../scripts/select-file';
export default Vue.extend({ export default Vue.extend({
metaInfo() { metaInfo() {
@ -71,9 +81,11 @@ export default Vue.extend({
data() { data() {
return { return {
name: null,
selected: null, selected: null,
selectedRemote: null, selectedRemote: null,
name: null,
category: null,
aliases: null,
host: '', host: '',
pagination: { pagination: {
endpoint: 'admin/emoji/list', endpoint: 'admin/emoji/list',
@ -86,52 +98,38 @@ export default Vue.extend({
host: this.host ? this.host : null host: this.host ? this.host : null
}) })
}, },
faTrashAlt, faPlus, faLaugh faTrashAlt, faPlus, faLaugh, faSave
} }
}, },
watch: { watch: {
host() { host() {
this.$refs.remoteEmojis.reload(); this.$refs.remoteEmojis.reload();
},
selected() {
this.name = this.selected ? this.selected.name : null;
this.category = this.selected ? this.selected.category : null;
this.aliases = this.selected ? this.selected.aliases.join(' ') : null;
} }
}, },
methods: { methods: {
async add() { async add(e) {
const { canceled: canceled, result: name } = await this.$root.dialog({ const files = await selectFile(this, e.currentTarget || e.target, null, true);
title: this.$t('emojiName'),
input: true
});
if (canceled) return;
this.name = name;
(this.$refs.file as any).click();
},
onChangeFile() {
const [file] = Array.from((this.$refs.file as any).files);
if (file == null) return;
const data = new FormData();
data.append('file', file);
data.append('name', this.name);
data.append('i', this.$store.state.i.token);
const dialog = this.$root.dialog({ const dialog = this.$root.dialog({
type: 'waiting', type: 'waiting',
text: this.$t('uploading') + '...', text: this.$t('doing') + '...',
showOkButton: false, showOkButton: false,
showCancelButton: false, showCancelButton: false,
cancelableByBgClick: false cancelableByBgClick: false
}); });
fetch(apiUrl + '/admin/emoji/add', { Promise.all(files.map(file => this.$root.api('admin/emoji/add', {
method: 'POST', fileId: file.id,
body: data })))
}) .then(() => {
.then(response => response.json())
.then(f => {
this.$refs.emojis.reload(); this.$refs.emojis.reload();
this.$root.dialog({ this.$root.dialog({
type: 'success', type: 'success',
@ -143,6 +141,22 @@ export default Vue.extend({
}); });
}, },
async update() {
await this.$root.api('admin/emoji/update', {
id: this.selected.id,
name: this.name,
category: this.category,
aliases: this.aliases.split(' '),
});
this.$root.dialog({
type: 'success',
iconOnly: true, autoClose: true
});
this.$refs.emojis.reload();
},
async del() { async del() {
const { canceled } = await this.$root.dialog({ const { canceled } = await this.$root.dialog({
type: 'warning', type: 'warning',
@ -207,6 +221,18 @@ export default Vue.extend({
> .name { > .name {
display: block; display: block;
} }
> .info {
opacity: 0.5;
> .category {
margin-right: 16px;
}
> .aliases {
font-style: oblique;
}
}
} }
} }
} }
@ -241,7 +267,7 @@ export default Vue.extend({
display: block; display: block;
} }
> .host { > .info {
opacity: 0.5; opacity: 0.5;
} }
} }

View file

@ -1,8 +1,8 @@
import { faUpload, faCloud, faLink } from '@fortawesome/free-solid-svg-icons'; import { faUpload, faCloud } from '@fortawesome/free-solid-svg-icons';
import { selectDriveFile } from './select-drive-file'; import { selectDriveFile } from './select-drive-file';
import { apiUrl } from '../config'; import { apiUrl } from '../config';
export function selectFile(component: any, src: any, label: string, multiple = false) { export function selectFile(component: any, src: any, label: string | null, multiple = false) {
return new Promise((res, rej) => { return new Promise((res, rej) => {
const chooseFileFromPc = () => { const chooseFileFromPc = () => {
const input = document.createElement('input'); const input = document.createElement('input');
@ -56,10 +56,10 @@ export function selectFile(component: any, src: any, label: string, multiple = f
}; };
component.$root.menu({ component.$root.menu({
items: [{ items: [label ? {
text: label, text: label,
type: 'label' type: 'label'
}, { } : undefined, {
text: component.$t('upload'), text: component.$t('upload'),
icon: faUpload, icon: faUpload,
action: chooseFileFromPc action: chooseFileFromPc

View file

@ -1,11 +1,12 @@
import $ from 'cafy'; import $ from 'cafy';
import define from '../../../define'; import define from '../../../define';
import { detectUrlMime } from '../../../../../misc/detect-url-mime'; import { Emojis, DriveFiles } from '../../../../../models';
import { Emojis } from '../../../../../models';
import { genId } from '../../../../../misc/gen-id'; import { genId } from '../../../../../misc/gen-id';
import { getConnection } from 'typeorm'; import { getConnection } from 'typeorm';
import { insertModerationLog } from '../../../../../services/insert-moderation-log'; import { insertModerationLog } from '../../../../../services/insert-moderation-log';
import { ApiError } from '../../../error'; import { ApiError } from '../../../error';
import { ID } from '../../../../../misc/cafy-id';
import rndstr from 'rndstr';
export const meta = { export const meta = {
desc: { desc: {
@ -18,52 +19,36 @@ export const meta = {
requireModerator: true, requireModerator: true,
params: { params: {
name: { fileId: {
validator: $.str.min(1) validator: $.type(ID)
}, },
url: {
validator: $.str.min(1)
},
category: {
validator: $.optional.str
},
aliases: {
validator: $.optional.arr($.str.min(1)),
default: [] as string[]
}
}, },
errors: { errors: {
emojiAlredyExists: { noSuchFile: {
message: 'Emoji already exists.', message: 'No such file.',
code: 'EMOJI_ALREADY_EXISTS', code: 'MO_SUCH_FILE',
id: 'fc46b5a4-6b92-4c33-ac66-b806659bb5cf' id: 'fc46b5a4-6b92-4c33-ac66-b806659bb5cf'
} }
} }
}; };
export default define(meta, async (ps, me) => { export default define(meta, async (ps, me) => {
const type = await detectUrlMime(ps.url); const file = await DriveFiles.findOne(ps.fileId);
const exists = await Emojis.findOne({ if (file == null) throw new ApiError(meta.errors.noSuchFile);
name: ps.name,
host: null
});
if (exists != null) throw new ApiError(meta.errors.emojiAlredyExists); const name = file.name.split('.')[0].match(/^[a-z0-9_]+$/) ? file.name.split('.')[0] : `_${rndstr('a-z0-9', 8)}_`;
const emoji = await Emojis.save({ const emoji = await Emojis.save({
id: genId(), id: genId(),
updatedAt: new Date(), updatedAt: new Date(),
name: ps.name, name: name,
category: ps.category, category: null,
host: null, host: null,
aliases: ps.aliases, aliases: [],
url: ps.url, url: file.url,
type, type: file.type,
}); });
await getConnection().queryResultCache!.remove(['meta_emojis']); await getConnection().queryResultCache!.remove(['meta_emojis']);

View file

@ -1,6 +1,5 @@
import $ from 'cafy'; import $ from 'cafy';
import define from '../../../define'; import define from '../../../define';
import { detectUrlMime } from '../../../../../misc/detect-url-mime';
import { ID } from '../../../../../misc/cafy-id'; import { ID } from '../../../../../misc/cafy-id';
import { Emojis } from '../../../../../models'; import { Emojis } from '../../../../../models';
import { getConnection } from 'typeorm'; import { getConnection } from 'typeorm';
@ -29,10 +28,6 @@ export const meta = {
validator: $.optional.str validator: $.optional.str
}, },
url: {
validator: $.str
},
aliases: { aliases: {
validator: $.arr($.str) validator: $.arr($.str)
} }
@ -52,15 +47,11 @@ export default define(meta, async (ps) => {
if (emoji == null) throw new ApiError(meta.errors.noSuchEmoji); if (emoji == null) throw new ApiError(meta.errors.noSuchEmoji);
const type = await detectUrlMime(ps.url);
await Emojis.update(emoji.id, { await Emojis.update(emoji.id, {
updatedAt: new Date(), updatedAt: new Date(),
name: ps.name, name: ps.name,
category: ps.category, category: ps.category,
aliases: ps.aliases, aliases: ps.aliases,
url: ps.url,
type,
}); });
await getConnection().queryResultCache!.remove(['meta_emojis']); await getConnection().queryResultCache!.remove(['meta_emojis']);