semaphore/src/routes/_actions/media.js

51 lines
1.7 KiB
JavaScript
Raw Normal View History

import { store } from '../_store/store.js'
import { uploadMedia } from '../_api/media.js'
import { toast } from '../_components/toast/toast.js'
import { scheduleIdleTask } from '../_utils/scheduleIdleTask.js'
import { formatIntl } from '../_utils/formatIntl.js'
import { database } from '../_database/database.js'
2018-03-02 05:21:49 +00:00
export async function doMediaUpload (realm, file) {
2019-08-03 21:49:37 +01:00
const { currentInstance, accessToken } = store.get()
store.set({ uploadingMedia: true })
2018-03-02 05:21:49 +00:00
try {
2019-08-03 21:49:37 +01:00
const response = await uploadMedia(currentInstance, accessToken, file)
const composeMedia = store.getComposeData(realm, 'media') || []
if (composeMedia.length === 4) {
throw new Error('Only 4 media max are allowed')
}
await database.setCachedMediaFile(response.id, file)
2018-03-03 22:15:50 +00:00
composeMedia.push({
2018-03-02 05:21:49 +00:00
data: response,
file: { name: file.name },
description: ''
2018-03-02 05:21:49 +00:00
})
2018-03-03 22:51:48 +00:00
store.setComposeData(realm, {
media: composeMedia
2018-03-03 22:51:48 +00:00
})
2018-03-03 19:26:10 +00:00
scheduleIdleTask(() => store.save())
2018-03-02 05:21:49 +00:00
} catch (e) {
console.error(e)
/* no await */ toast.say(formatIntl('intl.failedToUploadMedia', { error: (e.message || '') }))
2018-03-02 05:21:49 +00:00
} finally {
store.set({ uploadingMedia: false })
2018-03-02 05:21:49 +00:00
}
2018-03-03 01:54:38 +00:00
}
2018-03-03 05:55:04 +00:00
export function deleteMedia (realm, i) {
2019-08-03 21:49:37 +01:00
const composeMedia = store.getComposeData(realm, 'media')
composeMedia.splice(i, 1)
2018-03-03 22:51:48 +00:00
store.setComposeData(realm, {
media: composeMedia
2018-03-03 22:51:48 +00:00
})
if (!composeMedia.length) {
const contentWarningShown = store.getComposeData(realm, 'contentWarningShown')
const contentWarning = store.getComposeData(realm, 'contentWarning')
store.setComposeData(realm, {
sensitive: contentWarningShown && contentWarning // reset sensitive if the last media is deleted
})
}
2018-03-03 19:26:10 +00:00
scheduleIdleTask(() => store.save())
2018-03-03 05:55:04 +00:00
}