2023-01-10 16:22:39 +03:00
|
|
|
import { fileOpen } from 'browser-fs-access'
|
|
|
|
import type { Ref } from 'vue'
|
|
|
|
import type { mastodon } from 'masto'
|
2024-04-08 12:53:26 +03:00
|
|
|
import type { DraftItem } from '~~/types'
|
2023-01-10 16:22:39 +03:00
|
|
|
|
2023-01-30 14:20:22 +03:00
|
|
|
export function usePublish(options: {
|
2024-04-08 12:53:26 +03:00
|
|
|
draftItem: Ref<DraftItem>
|
2023-01-10 16:22:39 +03:00
|
|
|
expanded: Ref<boolean>
|
|
|
|
isUploading: Ref<boolean>
|
2024-04-08 12:53:26 +03:00
|
|
|
isPartOfThread: boolean
|
|
|
|
initialDraft: () => DraftItem
|
2023-01-30 14:20:22 +03:00
|
|
|
}) {
|
2024-04-08 12:53:26 +03:00
|
|
|
const { draftItem } = options
|
|
|
|
|
|
|
|
const isEmpty = computed(() => isEmptyDraft([draftItem.value]))
|
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
const { client } = useMasto()
|
2023-01-30 14:20:22 +03:00
|
|
|
const settings = useUserSettings()
|
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
const preferredLanguage = computed(() => (currentUser.value?.account.source.language || settings.value?.language || 'en').split('-')[0])
|
2023-01-10 16:22:39 +03:00
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
const isSending = ref(false)
|
|
|
|
const isExpanded = ref(false)
|
|
|
|
const failedMessages = ref<string[]>([])
|
2023-01-10 16:22:39 +03:00
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
const publishSpoilerText = computed({
|
2023-02-05 20:36:33 +03:00
|
|
|
get() {
|
2024-04-08 12:53:26 +03:00
|
|
|
return draftItem.value.params.sensitive ? draftItem.value.params.spoilerText : ''
|
2023-02-05 20:36:33 +03:00
|
|
|
},
|
|
|
|
set(val) {
|
2024-04-08 12:53:26 +03:00
|
|
|
if (!draftItem.value.params.sensitive)
|
2023-02-05 20:36:33 +03:00
|
|
|
return
|
2024-04-08 12:53:26 +03:00
|
|
|
draftItem.value.params.spoilerText = val
|
2023-02-05 20:36:33 +03:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
const shouldExpanded = computed(() => options.expanded.value || isExpanded.value || !isEmpty.value)
|
|
|
|
const isPublishDisabled = computed(() => {
|
2024-04-08 12:53:26 +03:00
|
|
|
const { params, attachments } = draftItem.value
|
2024-02-21 18:20:08 +03:00
|
|
|
const firstEmptyInputIndex = params.poll?.options.findIndex(option => option.trim().length === 0)
|
|
|
|
return isEmpty.value
|
2024-03-05 17:48:58 +03:00
|
|
|
|| options.isUploading.value
|
|
|
|
|| isSending.value
|
|
|
|
|| (attachments.length === 0 && !params.status)
|
|
|
|
|| failedMessages.value.length > 0
|
|
|
|
|| (attachments.length > 0 && params.poll !== null && params.poll !== undefined)
|
|
|
|
|| ((params.poll !== null && params.poll !== undefined)
|
|
|
|
&& (
|
|
|
|
(firstEmptyInputIndex !== -1
|
|
|
|
&& firstEmptyInputIndex !== params.poll.options.length - 1
|
|
|
|
)
|
|
|
|
|| params.poll.options.findLastIndex(option => option.trim().length > 0) + 1 < 2
|
|
|
|
|| (new Set(params.poll.options).size !== params.poll.options.length)
|
|
|
|
|| (currentInstance.value?.configuration?.polls.maxCharactersPerOption !== undefined
|
|
|
|
&& params.poll.options.find(option => option.length > currentInstance.value!.configuration!.polls.maxCharactersPerOption) !== undefined
|
|
|
|
)
|
|
|
|
))
|
2023-01-10 16:22:39 +03:00
|
|
|
})
|
|
|
|
|
2024-04-08 12:53:26 +03:00
|
|
|
watch(draftItem, () => {
|
2024-02-21 18:20:08 +03:00
|
|
|
if (failedMessages.value.length > 0)
|
|
|
|
failedMessages.value.length = 0
|
2023-01-17 15:56:51 +03:00
|
|
|
}, { deep: true })
|
|
|
|
|
2023-01-10 16:22:39 +03:00
|
|
|
async function publishDraft() {
|
2024-02-21 18:20:08 +03:00
|
|
|
if (isPublishDisabled.value)
|
2023-01-29 01:00:20 +03:00
|
|
|
return
|
2023-01-30 14:20:22 +03:00
|
|
|
|
2024-04-08 12:53:26 +03:00
|
|
|
let content = htmlToText(draftItem.value.params.status || '')
|
|
|
|
if (draftItem.value.mentions?.length)
|
|
|
|
content = `${draftItem.value.mentions.map(i => `@${i}`).join(' ')} ${content}`
|
2023-01-13 03:33:04 +03:00
|
|
|
|
2023-05-21 22:57:29 +03:00
|
|
|
let poll
|
|
|
|
|
2024-04-08 12:53:26 +03:00
|
|
|
if (draftItem.value.params.poll) {
|
|
|
|
let options = draftItem.value.params.poll.options
|
2023-05-21 22:57:29 +03:00
|
|
|
|
|
|
|
if (currentInstance.value?.configuration !== undefined
|
|
|
|
&& (
|
|
|
|
options.length < currentInstance.value.configuration.polls.maxOptions
|
|
|
|
|| options[options.length - 1].trim().length === 0
|
|
|
|
)
|
2024-08-16 17:52:08 +03:00
|
|
|
) {
|
2023-05-21 22:57:29 +03:00
|
|
|
options = options.slice(0, options.length - 1)
|
2024-08-16 17:52:08 +03:00
|
|
|
}
|
2023-05-21 22:57:29 +03:00
|
|
|
|
2024-04-08 12:53:26 +03:00
|
|
|
poll = { ...draftItem.value.params.poll, options }
|
2023-05-21 22:57:29 +03:00
|
|
|
}
|
|
|
|
|
2023-01-10 16:22:39 +03:00
|
|
|
const payload = {
|
2024-04-08 12:53:26 +03:00
|
|
|
...draftItem.value.params,
|
2024-02-21 18:20:08 +03:00
|
|
|
spoilerText: publishSpoilerText.value,
|
2023-01-13 03:33:04 +03:00
|
|
|
status: content,
|
2024-04-08 12:53:26 +03:00
|
|
|
mediaIds: draftItem.value.attachments.map(a => a.id),
|
|
|
|
language: draftItem.value.params.language || preferredLanguage.value,
|
2023-05-21 22:57:29 +03:00
|
|
|
poll,
|
2023-01-10 16:22:39 +03:00
|
|
|
...(isGlitchEdition.value ? { 'content-type': 'text/markdown' } : {}),
|
2024-01-09 11:56:15 +03:00
|
|
|
} as mastodon.rest.v1.CreateStatusParams
|
2023-01-10 16:22:39 +03:00
|
|
|
|
2024-02-24 19:46:14 +03:00
|
|
|
if (import.meta.dev) {
|
2023-01-30 14:20:22 +03:00
|
|
|
// eslint-disable-next-line no-console
|
2023-01-10 16:22:39 +03:00
|
|
|
console.info({
|
2024-04-08 12:53:26 +03:00
|
|
|
raw: draftItem.value.params.status,
|
2023-01-10 16:22:39 +03:00
|
|
|
...payload,
|
|
|
|
})
|
|
|
|
// eslint-disable-next-line no-alert
|
|
|
|
const result = confirm('[DEV] Payload logged to console, do you want to publish it?')
|
|
|
|
if (!result)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2024-02-21 18:20:08 +03:00
|
|
|
isSending.value = true
|
2023-01-10 16:22:39 +03:00
|
|
|
|
|
|
|
let status: mastodon.v1.Status
|
2024-04-08 12:53:26 +03:00
|
|
|
if (!draftItem.value.editingStatus) {
|
2024-02-21 18:20:08 +03:00
|
|
|
status = await client.value.v1.statuses.create(payload)
|
2023-08-02 13:31:08 +03:00
|
|
|
}
|
2023-01-30 14:20:22 +03:00
|
|
|
|
2023-08-02 13:31:08 +03:00
|
|
|
else {
|
2024-04-08 12:53:26 +03:00
|
|
|
status = await client.value.v1.statuses.$select(draftItem.value.editingStatus.id).update({
|
2023-08-02 13:31:08 +03:00
|
|
|
...payload,
|
2024-04-08 12:53:26 +03:00
|
|
|
mediaAttributes: draftItem.value.attachments.map(media => ({
|
2023-08-02 13:31:08 +03:00
|
|
|
id: media.id,
|
|
|
|
description: media.description,
|
|
|
|
})),
|
2024-01-09 11:56:15 +03:00
|
|
|
})
|
2023-08-02 13:31:08 +03:00
|
|
|
}
|
2024-04-08 12:53:26 +03:00
|
|
|
if (draftItem.value.params.inReplyToId && !options.isPartOfThread)
|
2023-01-10 16:22:39 +03:00
|
|
|
navigateToStatus({ status })
|
|
|
|
|
2024-04-08 12:53:26 +03:00
|
|
|
draftItem.value = options.initialDraft()
|
2023-01-10 16:22:39 +03:00
|
|
|
|
|
|
|
return status
|
|
|
|
}
|
2023-01-12 08:39:22 +03:00
|
|
|
catch (err) {
|
|
|
|
console.error(err)
|
2024-02-21 18:20:08 +03:00
|
|
|
failedMessages.value.push((err as Error).message)
|
2023-01-12 08:39:22 +03:00
|
|
|
}
|
2023-01-10 16:22:39 +03:00
|
|
|
finally {
|
2024-02-21 18:20:08 +03:00
|
|
|
isSending.value = false
|
2023-01-10 16:22:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
return {
|
2023-01-10 16:22:39 +03:00
|
|
|
isSending,
|
|
|
|
isExpanded,
|
|
|
|
shouldExpanded,
|
|
|
|
isPublishDisabled,
|
2023-01-17 15:56:51 +03:00
|
|
|
failedMessages,
|
2023-01-30 14:20:22 +03:00
|
|
|
preferredLanguage,
|
2023-02-05 20:36:33 +03:00
|
|
|
publishSpoilerText,
|
2023-01-10 16:22:39 +03:00
|
|
|
publishDraft,
|
2024-02-21 18:20:08 +03:00
|
|
|
}
|
2023-01-10 16:22:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export type MediaAttachmentUploadError = [filename: string, message: string]
|
|
|
|
|
2024-04-08 12:53:26 +03:00
|
|
|
export function useUploadMediaAttachment(draft: Ref<DraftItem>) {
|
2024-02-21 18:20:08 +03:00
|
|
|
const { client } = useMasto()
|
2023-01-10 16:22:39 +03:00
|
|
|
const { t } = useI18n()
|
2024-04-04 13:28:18 +03:00
|
|
|
const { formatFileSize } = useFileSizeFormatter()
|
2023-01-10 16:22:39 +03:00
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
const isUploading = ref<boolean>(false)
|
|
|
|
const isExceedingAttachmentLimit = ref<boolean>(false)
|
|
|
|
const failedAttachments = ref<MediaAttachmentUploadError[]>([])
|
2023-01-10 16:22:39 +03:00
|
|
|
const dropZoneRef = ref<HTMLDivElement>()
|
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
const maxPixels = computed(() => {
|
2023-05-21 19:28:28 +03:00
|
|
|
return currentInstance.value?.configuration?.mediaAttachments?.imageMatrixLimit
|
2024-03-05 17:48:58 +03:00
|
|
|
?? 4096 ** 2
|
2023-05-21 19:28:28 +03:00
|
|
|
})
|
2023-03-01 00:58:19 +03:00
|
|
|
|
|
|
|
const loadImage = (inputFile: Blob) => new Promise<HTMLImageElement>((resolve, reject) => {
|
|
|
|
const url = URL.createObjectURL(inputFile)
|
|
|
|
const img = new Image()
|
|
|
|
|
|
|
|
img.onerror = err => reject(err)
|
|
|
|
img.onload = () => resolve(img)
|
|
|
|
|
|
|
|
img.src = url
|
|
|
|
})
|
|
|
|
|
2023-07-02 19:34:39 +03:00
|
|
|
function resizeImage(img: HTMLImageElement, type = 'image/png'): Promise<Blob | null> {
|
2023-03-01 00:58:19 +03:00
|
|
|
const { width, height } = img
|
|
|
|
|
|
|
|
const aspectRatio = (width as number) / (height as number)
|
|
|
|
|
|
|
|
const canvas = document.createElement('canvas')
|
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
const resizedWidth = canvas.width = Math.round(Math.sqrt(maxPixels.value * aspectRatio))
|
|
|
|
const resizedHeight = canvas.height = Math.round(Math.sqrt(maxPixels.value / aspectRatio))
|
2023-03-01 00:58:19 +03:00
|
|
|
|
|
|
|
const context = canvas.getContext('2d')
|
|
|
|
|
|
|
|
context?.drawImage(img, 0, 0, resizedWidth, resizedHeight)
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
canvas.toBlob(resolve, type)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function processImageFile(file: File) {
|
|
|
|
try {
|
|
|
|
const image = await loadImage(file) as HTMLImageElement
|
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
if (image.width * image.height > maxPixels.value)
|
2023-03-01 00:58:19 +03:00
|
|
|
file = await resizeImage(image, file.type) as File
|
|
|
|
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
// Resize failed, just use the original file
|
|
|
|
console.error(e)
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function processFile(file: File) {
|
|
|
|
if (file.type.startsWith('image/'))
|
|
|
|
return await processImageFile(file)
|
|
|
|
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
|
2023-01-10 16:22:39 +03:00
|
|
|
async function uploadAttachments(files: File[]) {
|
2024-02-21 18:20:08 +03:00
|
|
|
isUploading.value = true
|
|
|
|
failedAttachments.value = []
|
2023-01-10 16:22:39 +03:00
|
|
|
// TODO: display some kind of message if too many media are selected
|
|
|
|
// DONE
|
2023-01-15 11:38:02 +03:00
|
|
|
const limit = currentInstance.value!.configuration?.statuses.maxMediaAttachments || 4
|
2024-04-04 13:28:18 +03:00
|
|
|
const maxVideoSize = currentInstance.value!.configuration?.mediaAttachments.videoSizeLimit || 0
|
|
|
|
const maxImageSize = currentInstance.value!.configuration?.mediaAttachments.imageSizeLimit || 0
|
2023-01-10 16:22:39 +03:00
|
|
|
for (const file of files.slice(0, limit)) {
|
2024-02-21 18:20:08 +03:00
|
|
|
if (draft.value.attachments.length < limit) {
|
2024-04-04 13:28:18 +03:00
|
|
|
if (file.type.startsWith('image/')) {
|
|
|
|
if (maxImageSize > 0 && file.size > maxImageSize) {
|
|
|
|
failedAttachments.value = [...failedAttachments.value, [file.name, t('state.attachments_limit_image_error', [formatFileSize(maxImageSize)])]]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (maxVideoSize > 0 && file.size > maxVideoSize) {
|
|
|
|
const key
|
|
|
|
= file.type.startsWith('audio/')
|
|
|
|
? 'state.attachments_limit_audio_error'
|
|
|
|
: file.type.startsWith('video/')
|
|
|
|
? 'state.attachments_limit_video_error'
|
|
|
|
: 'state.attachments_limit_unknown_error'
|
|
|
|
const errorMessage = t(key, [formatFileSize(maxVideoSize)])
|
|
|
|
failedAttachments.value = [
|
|
|
|
...failedAttachments.value,
|
|
|
|
[file.name, errorMessage],
|
|
|
|
]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2024-02-21 18:20:08 +03:00
|
|
|
isExceedingAttachmentLimit.value = false
|
2023-01-10 16:22:39 +03:00
|
|
|
try {
|
2024-02-21 18:20:08 +03:00
|
|
|
const attachment = await client.value.v1.media.create({
|
2023-03-01 00:58:19 +03:00
|
|
|
file: await processFile(file),
|
2023-01-10 16:22:39 +03:00
|
|
|
})
|
2024-02-21 18:20:08 +03:00
|
|
|
draft.value.attachments.push(attachment)
|
2023-01-10 16:22:39 +03:00
|
|
|
}
|
|
|
|
catch (e) {
|
2023-01-30 14:20:22 +03:00
|
|
|
// TODO: add some human-readable error message, problem is that masto api will not return response code
|
2023-01-10 16:22:39 +03:00
|
|
|
console.error(e)
|
2024-02-21 18:20:08 +03:00
|
|
|
failedAttachments.value = [...failedAttachments.value, [file.name, (e as Error).message]]
|
2023-01-10 16:22:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2024-02-21 18:20:08 +03:00
|
|
|
isExceedingAttachmentLimit.value = true
|
|
|
|
failedAttachments.value = [...failedAttachments.value, [file.name, t('state.attachments_limit_error')]]
|
2023-01-10 16:22:39 +03:00
|
|
|
}
|
|
|
|
}
|
2024-02-21 18:20:08 +03:00
|
|
|
isUploading.value = false
|
2023-01-10 16:22:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async function pickAttachments() {
|
2024-02-24 19:46:14 +03:00
|
|
|
if (import.meta.server)
|
2023-02-06 12:34:50 +03:00
|
|
|
return
|
2023-01-15 11:38:02 +03:00
|
|
|
const mimeTypes = currentInstance.value!.configuration?.mediaAttachments.supportedMimeTypes
|
2023-01-10 16:22:39 +03:00
|
|
|
const files = await fileOpen({
|
|
|
|
description: 'Attachments',
|
|
|
|
multiple: true,
|
|
|
|
mimeTypes,
|
|
|
|
})
|
|
|
|
await uploadAttachments(files)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setDescription(att: mastodon.v1.MediaAttachment, description: string) {
|
|
|
|
att.description = description
|
2024-02-21 18:20:08 +03:00
|
|
|
if (!draft.value.editingStatus)
|
|
|
|
await client.value.v1.media.$select(att.id).update({ description: att.description })
|
2023-01-10 16:22:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeAttachment(index: number) {
|
2024-02-21 18:20:08 +03:00
|
|
|
draft.value.attachments.splice(index, 1)
|
2023-01-10 16:22:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async function onDrop(files: File[] | null) {
|
|
|
|
if (files)
|
|
|
|
await uploadAttachments(files)
|
|
|
|
}
|
|
|
|
|
|
|
|
const { isOverDropZone } = useDropZone(dropZoneRef, onDrop)
|
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
return {
|
2023-01-10 16:22:39 +03:00
|
|
|
isUploading,
|
|
|
|
isExceedingAttachmentLimit,
|
2023-01-30 14:20:22 +03:00
|
|
|
isOverDropZone,
|
|
|
|
|
2023-01-10 16:22:39 +03:00
|
|
|
failedAttachments,
|
2023-01-11 01:51:25 +03:00
|
|
|
dropZoneRef,
|
2023-01-10 16:22:39 +03:00
|
|
|
|
|
|
|
uploadAttachments,
|
|
|
|
pickAttachments,
|
|
|
|
setDescription,
|
|
|
|
removeAttachment,
|
2024-02-21 18:20:08 +03:00
|
|
|
}
|
2023-01-10 16:22:39 +03:00
|
|
|
}
|