pinafore/routes/_actions/media.js

52 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-03-02 05:21:49 +00:00
import { store } from '../_store/store'
import { uploadMedia } from '../_api/media'
import { toast } from '../_utils/toast'
2018-03-03 19:26:10 +00:00
import { scheduleIdleTask } from '../_utils/scheduleIdleTask'
2018-03-02 05:21:49 +00:00
export async function doMediaUpload (realm, file) {
2018-03-02 05:21:49 +00:00
let instanceName = store.get('currentInstance')
let accessToken = store.get('accessToken')
store.set({uploadingMedia: true})
try {
let response = await uploadMedia(instanceName, accessToken, file)
let uploadedMedia = store.get('uploadedMedia')
uploadedMedia[instanceName] = uploadedMedia[instanceName] || {}
uploadedMedia[instanceName][realm] = uploadedMedia[instanceName][realm] || []
uploadedMedia[instanceName][realm].push({
2018-03-02 05:21:49 +00:00
data: response,
2018-03-03 19:26:10 +00:00
file: {
name: file.name
}
2018-03-02 05:21:49 +00:00
})
2018-03-03 19:26:10 +00:00
let rawComposeText = store.get('rawComposeText') || ''
rawComposeText += ' ' + response.text_url
store.set({
uploadedMedia,
rawComposeText
})
scheduleIdleTask(() => store.save())
2018-03-02 05:21:49 +00:00
} catch (e) {
console.error(e)
toast.say('Failed to upload media: ' + (e.message || ''))
} finally {
store.set({uploadingMedia: false})
}
2018-03-03 01:54:38 +00:00
}
2018-03-03 05:55:04 +00:00
export function deleteMedia (realm, i) {
2018-03-03 05:55:04 +00:00
let uploadedMedia = store.get('uploadedMedia')
let instanceName = store.get('currentInstance')
let uploadedMediaArray = uploadedMedia[instanceName][realm]
2018-03-03 19:26:10 +00:00
let deletedMedia = uploadedMediaArray.splice(i, 1)[0]
let rawComposeText = store.get('rawComposeText') || ''
rawComposeText = rawComposeText.replace(' ' + deletedMedia.data.text_url, '')
2018-03-03 19:26:10 +00:00
store.set({
uploadedMedia,
rawComposeText
})
scheduleIdleTask(() => store.save())
2018-03-03 05:55:04 +00:00
}