From 0e876da85289fbbb2ab15d8e2ce97bdf58539643 Mon Sep 17 00:00:00 2001 From: Angelina Filippova Date: Tue, 31 Mar 2020 03:18:20 +0300 Subject: [PATCH] Update emoji APIs for adding, updating and removing emoji files --- src/api/emojiPacks.js | 93 ++++++------------- src/store/modules/emojiPacks.js | 69 ++++++++++---- .../emojiPacks/components/LocalEmojiPack.vue | 4 +- .../components/NewEmojiUploader.vue | 28 +++--- .../components/SingleEmojiEditor.vue | 60 ++++++------ 5 files changed, 128 insertions(+), 126 deletions(-) diff --git a/src/api/emojiPacks.js b/src/api/emojiPacks.js index 22d8f7ad..b45dfb11 100644 --- a/src/api/emojiPacks.js +++ b/src/api/emojiPacks.js @@ -2,8 +2,6 @@ import request from '@/utils/request' import { getToken } from '@/utils/auth' import { baseName } from './utils' -import _ from 'lodash' - export async function deletePack(host, token, name) { return await request({ baseURL: baseName(host), @@ -35,7 +33,7 @@ export async function createPack(host, token, name) { return await request({ baseURL: baseName(host), url: `/api/pleroma/emoji/packs/${name}`, - method: 'put', + method: 'post', headers: authHeaders(token) }) } @@ -72,79 +70,44 @@ export async function downloadFrom(host, instance, pack_name, as, token) { }) } -export async function savePackMetadata(host, token, name, new_data) { +export async function savePackMetadata(host, token, name, metadata) { return await request({ baseURL: baseName(host), - url: `/api/pleroma/emoji/packs/${name}/update_metadata`, - method: 'post', + url: `/api/pleroma/emoji/packs/${name}`, + method: 'patch', headers: authHeaders(token), - data: { name, new_data }, + data: { metadata }, timeout: 0 // This might take a long time }) } -function fileUpdateFormData(d) { - const data = new FormData() - - _.each(d, (v, k) => { - data.set(k, v) - }) - - return data -} - -export async function updatePackFile(host, token, args) { - let data = null - - switch (args.action) { - case 'add': { - const { shortcode, file, fileName } = args - - data = fileUpdateFormData({ - action: 'add', - shortcode: shortcode, - file: file - }) - if (fileName.trim() !== '') { - data.set('filename', fileName) - } - - break - } - - case 'update': { - const { oldName, newName, newFilename } = args - - data = fileUpdateFormData({ - action: 'update', - shortcode: oldName, - new_shortcode: newName, - new_filename: newFilename - }) - - break - } - - case 'remove': { - const { name } = args - data = fileUpdateFormData({ - action: 'remove', - shortcode: name - }) - - break - } - } - - const { packName } = args - +export async function addNewEmojiFile(packName, file, shortcode, filename, host, token) { return await request({ baseURL: baseName(host), - url: `/api/pleroma/emoji/packs/${packName}/update_file`, + url: `/api/pleroma/emoji/packs/${packName}/files`, method: 'post', headers: authHeaders(token), - data: data, - timeout: 0 + data: { file, shortcode, filename: filename || null } + }) +} + +export async function updateEmojiFile(packName, shortcode, newShortcode, newFilename, force, host, token) { + return await request({ + baseURL: baseName(host), + url: `/api/pleroma/emoji/packs/${packName}/files`, + method: 'patch', + headers: authHeaders(token), + data: { shortcode, new_shortcode: newShortcode, new_filename: newFilename, force } + }) +} + +export async function deleteEmojiFile(packName, shortcode, host, token) { + return await request({ + baseURL: baseName(host), + url: `/api/pleroma/emoji/packs/${packName}/files`, + method: 'delete', + headers: authHeaders(token), + data: { shortcode } }) } diff --git a/src/store/modules/emojiPacks.js b/src/store/modules/emojiPacks.js index 3b376769..e4748c26 100644 --- a/src/store/modules/emojiPacks.js +++ b/src/store/modules/emojiPacks.js @@ -1,13 +1,16 @@ import { + addNewEmojiFile, + createPack, + deleteEmojiFile, + deletePack, + downloadFrom, + importFromFS, listPacks, listRemotePacks, - downloadFrom, reloadEmoji, - createPack, - deletePack, savePackMetadata, - importFromFS, - updatePackFile } from '@/api/emojiPacks' + updateEmojiFile +} from '@/api/emojiPacks' import i18n from '@/lang' import { Message } from 'element-ui' @@ -49,6 +52,36 @@ const packs = { } }, actions: { + async AddNewEmojiFile({ commit, getters }, { packName, file, shortcode, filename }) { + let result + try { + result = await addNewEmojiFile(packName, file, shortcode, filename, getters.authHost, getters.token) + } catch (_e) { + return + } + Message({ + message: `${i18n.t('settings.successfullyUpdated')} ${packName} ${i18n.t('settings.metadatLowerCase')}`, + type: 'success', + duration: 5 * 1000 + }) + + commit('UPDATE_LOCAL_PACK_FILES', { name: packName, files: result.data }) + }, + async DeleteEmojiFile({ commit, getters }, { packName, shortcode }) { + let result + try { + result = await deleteEmojiFile(packName, shortcode, getters.authHost, getters.token) + } catch (_e) { + return + } + Message({ + message: `${i18n.t('settings.successfullyUpdated')} ${packName} ${i18n.t('settings.metadatLowerCase')}`, + type: 'success', + duration: 5 * 1000 + }) + + commit('UPDATE_LOCAL_PACK_FILES', { name: packName, files: result.data }) + }, async CreatePack({ getters }, { name }) { await createPack(getters.authHost, getters.token, name) }, @@ -119,20 +152,20 @@ const packs = { SetRemoteInstance({ commit }, instance) { commit('SET_REMOTE_INSTANCE', instance) }, - async UpdateAndSavePackFile({ commit, getters }, args) { - const result = await updatePackFile(getters.authHost, getters.token, args) - - if (result.status === 200) { - const { packName } = args - - Message({ - message: `${i18n.t('settings.successfullyUpdated')} ${packName} ${i18n.t('settings.metadatLowerCase')}`, - type: 'success', - duration: 5 * 1000 - }) - - commit('UPDATE_LOCAL_PACK_FILES', { name: packName, files: result.data }) + async UpdateEmojiFile({ commit, getters }, { packName, shortcode, newShortcode, newFilename, force }) { + let result + try { + result = await updateEmojiFile(packName, shortcode, newShortcode, newFilename, force, getters.authHost, getters.token) + } catch (_e) { + return } + Message({ + message: `${i18n.t('settings.successfullyUpdated')} ${packName} ${i18n.t('settings.metadatLowerCase')}`, + type: 'success', + duration: 5 * 1000 + }) + + commit('UPDATE_LOCAL_PACK_FILES', { name: packName, files: result.data }) }, async UpdateLocalPackVal({ commit }, args) { commit('UPDATE_LOCAL_PACK_VAL', args) diff --git a/src/views/emojiPacks/components/LocalEmojiPack.vue b/src/views/emojiPacks/components/LocalEmojiPack.vue index 917ff4dd..7d997e31 100644 --- a/src/views/emojiPacks/components/LocalEmojiPack.vue +++ b/src/views/emojiPacks/components/LocalEmojiPack.vue @@ -96,9 +96,9 @@ export default { if (this.isMobile) { return '90px' } else if (this.isTablet) { - return '120px' + return '155px' } else { - return '120px' + return '155px' } }, share: { diff --git a/src/views/emojiPacks/components/NewEmojiUploader.vue b/src/views/emojiPacks/components/NewEmojiUploader.vue index f364b49e..8e86f243 100644 --- a/src/views/emojiPacks/components/NewEmojiUploader.vue +++ b/src/views/emojiPacks/components/NewEmojiUploader.vue @@ -52,20 +52,22 @@ export default { } }, methods: { - uploadEmoji({ file }) { - this.$store.dispatch('UpdateAndSavePackFile', { - action: 'add', - packName: this.packName, - shortcode: this.shortcode, - file: file || this.imageUploadURL, - fileName: this.customFileName - }).then(() => { - this.shortcode = '' - this.imageUploadURL = '' - this.customFileName = '' + async uploadEmoji({ file }) { + try { + this.$store.dispatch('AddNewEmojiFile', { + packName: this.packName, + file: file || this.imageUploadURL, + shortcode: this.shortcode, + filename: this.customFileName + }) + } catch (e) { + return + } + this.shortcode = '' + this.imageUploadURL = '' + this.customFileName = '' - this.$store.dispatch('ReloadEmoji') - }) + this.$store.dispatch('ReloadEmoji') } } } diff --git a/src/views/emojiPacks/components/SingleEmojiEditor.vue b/src/views/emojiPacks/components/SingleEmojiEditor.vue index cd0eeb7f..d2237e82 100644 --- a/src/views/emojiPacks/components/SingleEmojiEditor.vue +++ b/src/views/emojiPacks/components/SingleEmojiEditor.vue @@ -106,19 +106,22 @@ export default { } }, methods: { - update() { - this.$store.dispatch('UpdateAndSavePackFile', { - action: 'update', - packName: this.packName, - oldName: this.name, - newName: this.emojiName, - newFilename: this.emojiFile - }).then(() => { - this.newName = null - this.newFile = null + async update() { + try { + this.$store.dispatch('UpdateEmojiFile', { + packName: this.packName, + shortcode: this.name, + newShortcode: this.emojiName, + newFilename: this.emojiFile, + force: true + }) + } catch (e) { + return + } + this.newName = null + this.newFile = null - this.$store.dispatch('ReloadEmoji') - }) + this.$store.dispatch('ReloadEmoji') }, remove() { this.$confirm('This will delete the emoji, are you sure?', 'Warning', { @@ -126,10 +129,9 @@ export default { cancelButtonText: 'No, leave it be', type: 'warning' }).then(() => { - this.$store.dispatch('UpdateAndSavePackFile', { - action: 'remove', + this.$store.dispatch('DeleteEmojiFile', { packName: this.packName, - name: this.name + shortcode: this.name }).then(() => { this.newName = null this.newFile = null @@ -139,20 +141,22 @@ export default { }) }, copyToLocal() { - this.$store.dispatch('UpdateAndSavePackFile', { - action: 'add', - packName: this.copyToLocalPackName, - shortcode: this.copyToShortcode.trim() !== '' ? this.copyToShortcode.trim() : this.name, - fileName: this.copyToFilename.trim() !== '' ? this.copyToFilename.trim() : this.file, - file: this.addressOfEmojiInPack(this.host, this.packName, this.file) - }).then(() => { - this.copyToLocalPackName = null - this.copyToLocalVisible = false - this.copyToShortcode = '' - this.copyToFilename = '' + try { + this.$store.dispatch('AddNewEmojiFile', { + packName: this.copyToLocalPackName, + file: this.addressOfEmojiInPack(this.host, this.packName, this.file), + shortcode: this.copyToShortcode.trim() !== '' ? this.copyToShortcode.trim() : this.name, + filename: this.copyToFilename.trim() !== '' ? this.copyToFilename.trim() : this.file + }) + } catch (e) { + return + } + this.copyToLocalPackName = null + this.copyToLocalVisible = false + this.copyToShortcode = '' + this.copyToFilename = '' - this.$store.dispatch('ReloadEmoji') - }) + this.$store.dispatch('ReloadEmoji') }, addressOfEmojiInPack }