Update emoji APIs for adding, updating and removing emoji files

This commit is contained in:
Angelina Filippova 2020-03-31 03:18:20 +03:00
parent 1f488a18be
commit 0e876da852
5 changed files with 128 additions and 126 deletions

View file

@ -2,8 +2,6 @@ import request from '@/utils/request'
import { getToken } from '@/utils/auth' import { getToken } from '@/utils/auth'
import { baseName } from './utils' import { baseName } from './utils'
import _ from 'lodash'
export async function deletePack(host, token, name) { export async function deletePack(host, token, name) {
return await request({ return await request({
baseURL: baseName(host), baseURL: baseName(host),
@ -35,7 +33,7 @@ export async function createPack(host, token, name) {
return await request({ return await request({
baseURL: baseName(host), baseURL: baseName(host),
url: `/api/pleroma/emoji/packs/${name}`, url: `/api/pleroma/emoji/packs/${name}`,
method: 'put', method: 'post',
headers: authHeaders(token) 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({ return await request({
baseURL: baseName(host), baseURL: baseName(host),
url: `/api/pleroma/emoji/packs/${name}/update_metadata`, url: `/api/pleroma/emoji/packs/${name}`,
method: 'post', method: 'patch',
headers: authHeaders(token), headers: authHeaders(token),
data: { name, new_data }, data: { metadata },
timeout: 0 // This might take a long time timeout: 0 // This might take a long time
}) })
} }
function fileUpdateFormData(d) { export async function addNewEmojiFile(packName, file, shortcode, filename, host, token) {
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
return await request({ return await request({
baseURL: baseName(host), baseURL: baseName(host),
url: `/api/pleroma/emoji/packs/${packName}/update_file`, url: `/api/pleroma/emoji/packs/${packName}/files`,
method: 'post', method: 'post',
headers: authHeaders(token), headers: authHeaders(token),
data: data, data: { file, shortcode, filename: filename || null }
timeout: 0 })
}
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 }
}) })
} }

View file

@ -1,13 +1,16 @@
import { import {
addNewEmojiFile,
createPack,
deleteEmojiFile,
deletePack,
downloadFrom,
importFromFS,
listPacks, listPacks,
listRemotePacks, listRemotePacks,
downloadFrom,
reloadEmoji, reloadEmoji,
createPack,
deletePack,
savePackMetadata, savePackMetadata,
importFromFS, updateEmojiFile
updatePackFile } from '@/api/emojiPacks' } from '@/api/emojiPacks'
import i18n from '@/lang' import i18n from '@/lang'
import { Message } from 'element-ui' import { Message } from 'element-ui'
@ -49,6 +52,36 @@ const packs = {
} }
}, },
actions: { 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 }) { async CreatePack({ getters }, { name }) {
await createPack(getters.authHost, getters.token, name) await createPack(getters.authHost, getters.token, name)
}, },
@ -119,20 +152,20 @@ const packs = {
SetRemoteInstance({ commit }, instance) { SetRemoteInstance({ commit }, instance) {
commit('SET_REMOTE_INSTANCE', instance) commit('SET_REMOTE_INSTANCE', instance)
}, },
async UpdateAndSavePackFile({ commit, getters }, args) { async UpdateEmojiFile({ commit, getters }, { packName, shortcode, newShortcode, newFilename, force }) {
const result = await updatePackFile(getters.authHost, getters.token, args) let result
try {
if (result.status === 200) { result = await updateEmojiFile(packName, shortcode, newShortcode, newFilename, force, getters.authHost, getters.token)
const { packName } = args } 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 })
} }
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) { async UpdateLocalPackVal({ commit }, args) {
commit('UPDATE_LOCAL_PACK_VAL', args) commit('UPDATE_LOCAL_PACK_VAL', args)

View file

@ -96,9 +96,9 @@ export default {
if (this.isMobile) { if (this.isMobile) {
return '90px' return '90px'
} else if (this.isTablet) { } else if (this.isTablet) {
return '120px' return '155px'
} else { } else {
return '120px' return '155px'
} }
}, },
share: { share: {

View file

@ -52,20 +52,22 @@ export default {
} }
}, },
methods: { methods: {
uploadEmoji({ file }) { async uploadEmoji({ file }) {
this.$store.dispatch('UpdateAndSavePackFile', { try {
action: 'add', this.$store.dispatch('AddNewEmojiFile', {
packName: this.packName, packName: this.packName,
shortcode: this.shortcode, file: file || this.imageUploadURL,
file: file || this.imageUploadURL, shortcode: this.shortcode,
fileName: this.customFileName filename: this.customFileName
}).then(() => { })
this.shortcode = '' } catch (e) {
this.imageUploadURL = '' return
this.customFileName = '' }
this.shortcode = ''
this.imageUploadURL = ''
this.customFileName = ''
this.$store.dispatch('ReloadEmoji') this.$store.dispatch('ReloadEmoji')
})
} }
} }
} }

View file

@ -106,19 +106,22 @@ export default {
} }
}, },
methods: { methods: {
update() { async update() {
this.$store.dispatch('UpdateAndSavePackFile', { try {
action: 'update', this.$store.dispatch('UpdateEmojiFile', {
packName: this.packName, packName: this.packName,
oldName: this.name, shortcode: this.name,
newName: this.emojiName, newShortcode: this.emojiName,
newFilename: this.emojiFile newFilename: this.emojiFile,
}).then(() => { force: true
this.newName = null })
this.newFile = null } catch (e) {
return
}
this.newName = null
this.newFile = null
this.$store.dispatch('ReloadEmoji') this.$store.dispatch('ReloadEmoji')
})
}, },
remove() { remove() {
this.$confirm('This will delete the emoji, are you sure?', 'Warning', { this.$confirm('This will delete the emoji, are you sure?', 'Warning', {
@ -126,10 +129,9 @@ export default {
cancelButtonText: 'No, leave it be', cancelButtonText: 'No, leave it be',
type: 'warning' type: 'warning'
}).then(() => { }).then(() => {
this.$store.dispatch('UpdateAndSavePackFile', { this.$store.dispatch('DeleteEmojiFile', {
action: 'remove',
packName: this.packName, packName: this.packName,
name: this.name shortcode: this.name
}).then(() => { }).then(() => {
this.newName = null this.newName = null
this.newFile = null this.newFile = null
@ -139,20 +141,22 @@ export default {
}) })
}, },
copyToLocal() { copyToLocal() {
this.$store.dispatch('UpdateAndSavePackFile', { try {
action: 'add', this.$store.dispatch('AddNewEmojiFile', {
packName: this.copyToLocalPackName, packName: this.copyToLocalPackName,
shortcode: this.copyToShortcode.trim() !== '' ? this.copyToShortcode.trim() : this.name, file: this.addressOfEmojiInPack(this.host, this.packName, this.file),
fileName: this.copyToFilename.trim() !== '' ? this.copyToFilename.trim() : this.file, shortcode: this.copyToShortcode.trim() !== '' ? this.copyToShortcode.trim() : this.name,
file: this.addressOfEmojiInPack(this.host, this.packName, this.file) filename: this.copyToFilename.trim() !== '' ? this.copyToFilename.trim() : this.file
}).then(() => { })
this.copyToLocalPackName = null } catch (e) {
this.copyToLocalVisible = false return
this.copyToShortcode = '' }
this.copyToFilename = '' this.copyToLocalPackName = null
this.copyToLocalVisible = false
this.copyToShortcode = ''
this.copyToFilename = ''
this.$store.dispatch('ReloadEmoji') this.$store.dispatch('ReloadEmoji')
})
}, },
addressOfEmojiInPack addressOfEmojiInPack
} }