2023-01-10 22:22:39 +09:00
|
|
|
import { fileOpen } from 'browser-fs-access'
|
|
|
|
import type { Ref } from 'vue'
|
|
|
|
import type { mastodon } from 'masto'
|
2024-04-08 18:53:26 +09:00
|
|
|
import type { DraftItem } from '~~/types'
|
2023-01-10 22:22:39 +09:00
|
|
|
|
2023-01-30 20:20:22 +09:00
|
|
|
export function usePublish(options: {
|
2024-04-08 18:53:26 +09:00
|
|
|
draftItem: Ref<DraftItem>
|
2023-01-10 22:22:39 +09:00
|
|
|
expanded: Ref<boolean>
|
|
|
|
isUploading: Ref<boolean>
|
2024-04-08 18:53:26 +09:00
|
|
|
isPartOfThread: boolean
|
|
|
|
initialDraft: () => DraftItem
|
2023-01-30 20:20:22 +09:00
|
|
|
}) {
|
2024-04-08 18:53:26 +09:00
|
|
|
const { draftItem } = options
|
|
|
|
|
|
|
|
const isEmpty = computed(() => isEmptyDraft([draftItem.value]))
|
|
|
|
|
2024-02-22 00:20:08 +09:00
|
|
|
const { client } = useMasto()
|
2023-01-30 20:20:22 +09:00
|
|
|
const settings = useUserSettings()
|
|
|
|
|
2024-02-22 00:20:08 +09:00
|
|
|
const preferredLanguage = computed(() => (currentUser.value?.account.source.language || settings.value?.language || 'en').split('-')[0])
|
2023-01-10 22:22:39 +09:00
|
|
|
|
2024-02-22 00:20:08 +09:00
|
|
|
const isSending = ref(false)
|
|
|
|
const isExpanded = ref(false)
|
|
|
|
const failedMessages = ref<string[]>([])
|
2023-01-10 22:22:39 +09:00
|
|
|
|
2024-02-22 00:20:08 +09:00
|
|
|
const publishSpoilerText = computed({
|
2023-02-06 02:36:33 +09:00
|
|
|
get() {
|
2024-04-08 18:53:26 +09:00
|
|
|
return draftItem.value.params.sensitive ? draftItem.value.params.spoilerText : ''
|
2023-02-06 02:36:33 +09:00
|
|
|
},
|
|
|
|
set(val) {
|
2024-04-08 18:53:26 +09:00
|
|
|
if (!draftItem.value.params.sensitive)
|
2023-02-06 02:36:33 +09:00
|
|
|
return
|
2024-04-08 18:53:26 +09:00
|
|
|
draftItem.value.params.spoilerText = val
|
2023-02-06 02:36:33 +09:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-02-22 00:20:08 +09:00
|
|
|
const shouldExpanded = computed(() => options.expanded.value || isExpanded.value || !isEmpty.value)
|
|
|
|
const isPublishDisabled = computed(() => {
|
2024-04-08 18:53:26 +09:00
|
|
|
const { params, attachments } = draftItem.value
|
2024-02-22 00:20:08 +09:00
|
|
|
const firstEmptyInputIndex = params.poll?.options.findIndex(option => option.trim().length === 0)
|
|
|
|
return isEmpty.value
|
2024-03-05 23:48:58 +09: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 22:22:39 +09:00
|
|
|
})
|
|
|
|
|
2024-04-08 18:53:26 +09:00
|
|
|
watch(draftItem, () => {
|
2024-02-22 00:20:08 +09:00
|
|
|
if (failedMessages.value.length > 0)
|
|
|
|
failedMessages.value.length = 0
|
2023-01-17 21:56:51 +09:00
|
|
|
}, { deep: true })
|
|
|
|
|
2023-01-10 22:22:39 +09:00
|
|
|
async function publishDraft() {
|
2024-02-22 00:20:08 +09:00
|
|
|
if (isPublishDisabled.value)
|
2023-01-29 07:00:20 +09:00
|
|
|
return
|
2023-01-30 20:20:22 +09:00
|
|
|
|
2024-04-08 18:53:26 +09: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 09:33:04 +09:00
|
|
|
|
2023-05-22 04:57:29 +09:00
|
|
|
let poll
|
|
|
|
|
2024-04-08 18:53:26 +09:00
|
|
|
if (draftItem.value.params.poll) {
|
|
|
|
let options = draftItem.value.params.poll.options
|
2023-05-22 04:57:29 +09:00
|
|
|
|
|
|
|
if (currentInstance.value?.configuration !== undefined
|
|
|
|
&& (
|
|
|
|
options.length < currentInstance.value.configuration.polls.maxOptions
|
|
|
|
|| options[options.length - 1].trim().length === 0
|
|
|
|
)
|
|
|
|
)
|
|
|
|
options = options.slice(0, options.length - 1)
|
|
|
|
|
2024-04-08 18:53:26 +09:00
|
|
|
poll = { ...draftItem.value.params.poll, options }
|
2023-05-22 04:57:29 +09:00
|
|
|
}
|
|
|
|
|
2023-01-10 22:22:39 +09:00
|
|
|
const payload = {
|
2024-04-08 18:53:26 +09:00
|
|
|
...draftItem.value.params,
|
2024-02-22 00:20:08 +09:00
|
|
|
spoilerText: publishSpoilerText.value,
|
2023-01-13 09:33:04 +09:00
|
|
|
status: content,
|
2024-04-08 18:53:26 +09:00
|
|
|
mediaIds: draftItem.value.attachments.map(a => a.id),
|
|
|
|
language: draftItem.value.params.language || preferredLanguage.value,
|
2023-05-22 04:57:29 +09:00
|
|
|
poll,
|
2023-01-10 22:22:39 +09:00
|
|
|
...(isGlitchEdition.value ? { 'content-type': 'text/markdown' } : {}),
|
2024-01-09 17:56:15 +09:00
|
|
|
} as mastodon.rest.v1.CreateStatusParams
|
2023-01-10 22:22:39 +09:00
|
|
|
|
2024-02-25 01:46:14 +09:00
|
|
|
if (import.meta.dev) {
|
2023-01-30 20:20:22 +09:00
|
|
|
// eslint-disable-next-line no-console
|
2023-01-10 22:22:39 +09:00
|
|
|
console.info({
|
2024-04-08 18:53:26 +09:00
|
|
|
raw: draftItem.value.params.status,
|
2023-01-10 22:22:39 +09: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-22 00:20:08 +09:00
|
|
|
isSending.value = true
|
2023-01-10 22:22:39 +09:00
|
|
|
|
|
|
|
let status: mastodon.v1.Status
|
2024-04-08 18:53:26 +09:00
|
|
|
if (!draftItem.value.editingStatus) {
|
2024-02-22 00:20:08 +09:00
|
|
|
status = await client.value.v1.statuses.create(payload)
|
2023-08-02 19:31:08 +09:00
|
|
|
}
|
2023-01-30 20:20:22 +09:00
|
|
|
|
2023-08-02 19:31:08 +09:00
|
|
|
else {
|
2024-04-08 18:53:26 +09:00
|
|
|
status = await client.value.v1.statuses.$select(draftItem.value.editingStatus.id).update({
|
2023-08-02 19:31:08 +09:00
|
|
|
...payload,
|
2024-04-08 18:53:26 +09:00
|
|
|
mediaAttributes: draftItem.value.attachments.map(media => ({
|
2023-08-02 19:31:08 +09:00
|
|
|
id: media.id,
|
|
|
|
description: media.description,
|
|
|
|
})),
|
2024-01-09 17:56:15 +09:00
|
|
|
})
|
2023-08-02 19:31:08 +09:00
|
|
|
}
|
2024-04-08 18:53:26 +09:00
|
|
|
if (draftItem.value.params.inReplyToId && !options.isPartOfThread)
|
2023-01-10 22:22:39 +09:00
|
|
|
navigateToStatus({ status })
|
|
|
|
|
2024-04-08 18:53:26 +09:00
|
|
|
draftItem.value = options.initialDraft()
|
2023-01-10 22:22:39 +09:00
|
|
|
|
|
|
|
return status
|
|
|
|
}
|
2023-01-12 14:39:22 +09:00
|
|
|
catch (err) {
|
|
|
|
console.error(err)
|
2024-02-22 00:20:08 +09:00
|
|
|
failedMessages.value.push((err as Error).message)
|
2023-01-12 14:39:22 +09:00
|
|
|
}
|
2023-01-10 22:22:39 +09:00
|
|
|
finally {
|
2024-02-22 00:20:08 +09:00
|
|
|
isSending.value = false
|
2023-01-10 22:22:39 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-22 00:20:08 +09:00
|
|
|
return {
|
2023-01-10 22:22:39 +09:00
|
|
|
isSending,
|
|
|
|
isExpanded,
|
|
|
|
shouldExpanded,
|
|
|
|
isPublishDisabled,
|
2023-01-17 21:56:51 +09:00
|
|
|
failedMessages,
|
2023-01-30 20:20:22 +09:00
|
|
|
preferredLanguage,
|
2023-02-06 02:36:33 +09:00
|
|
|
publishSpoilerText,
|
2023-01-10 22:22:39 +09:00
|
|
|
publishDraft,
|
2024-02-22 00:20:08 +09:00
|
|
|
}
|
2023-01-10 22:22:39 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export type MediaAttachmentUploadError = [filename: string, message: string]
|
|
|
|
|
2024-04-08 18:53:26 +09:00
|
|
|
export function useUploadMediaAttachment(draft: Ref<DraftItem>) {
|
2024-02-22 00:20:08 +09:00
|
|
|
const { client } = useMasto()
|
2023-01-10 22:22:39 +09:00
|
|
|
const { t } = useI18n()
|
2024-04-04 19:28:18 +09:00
|
|
|
const { formatFileSize } = useFileSizeFormatter()
|
2023-01-10 22:22:39 +09:00
|
|
|
|
2024-02-22 00:20:08 +09:00
|
|
|
const isUploading = ref<boolean>(false)
|
|
|
|
const isExceedingAttachmentLimit = ref<boolean>(false)
|
|
|
|
const failedAttachments = ref<MediaAttachmentUploadError[]>([])
|
2023-01-10 22:22:39 +09:00
|
|
|
const dropZoneRef = ref<HTMLDivElement>()
|
|
|
|
|
2024-02-22 00:20:08 +09:00
|
|
|
const maxPixels = computed(() => {
|
2023-05-22 01:28:28 +09:00
|
|
|
return currentInstance.value?.configuration?.mediaAttachments?.imageMatrixLimit
|
2024-03-05 23:48:58 +09:00
|
|
|
?? 4096 ** 2
|
2023-05-22 01:28:28 +09:00
|
|
|
})
|
2023-03-01 06:58:19 +09: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-03 01:34:39 +09:00
|
|
|
function resizeImage(img: HTMLImageElement, type = 'image/png'): Promise<Blob | null> {
|
2023-03-01 06:58:19 +09:00
|
|
|
const { width, height } = img
|
|
|
|
|
|
|
|
const aspectRatio = (width as number) / (height as number)
|
|
|
|
|
|
|
|
const canvas = document.createElement('canvas')
|
|
|
|
|
2024-02-22 00:20:08 +09: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 06:58:19 +09: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-22 00:20:08 +09:00
|
|
|
if (image.width * image.height > maxPixels.value)
|
2023-03-01 06:58:19 +09: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 22:22:39 +09:00
|
|
|
async function uploadAttachments(files: File[]) {
|
2024-02-22 00:20:08 +09:00
|
|
|
isUploading.value = true
|
|
|
|
failedAttachments.value = []
|
2023-01-10 22:22:39 +09:00
|
|
|
// TODO: display some kind of message if too many media are selected
|
|
|
|
// DONE
|
2023-01-15 17:38:02 +09:00
|
|
|
const limit = currentInstance.value!.configuration?.statuses.maxMediaAttachments || 4
|
2024-04-04 19:28:18 +09:00
|
|
|
const maxVideoSize = currentInstance.value!.configuration?.mediaAttachments.videoSizeLimit || 0
|
|
|
|
const maxImageSize = currentInstance.value!.configuration?.mediaAttachments.imageSizeLimit || 0
|
2023-01-10 22:22:39 +09:00
|
|
|
for (const file of files.slice(0, limit)) {
|
2024-02-22 00:20:08 +09:00
|
|
|
if (draft.value.attachments.length < limit) {
|
2024-04-04 19:28:18 +09: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-22 00:20:08 +09:00
|
|
|
isExceedingAttachmentLimit.value = false
|
2023-01-10 22:22:39 +09:00
|
|
|
try {
|
2024-02-22 00:20:08 +09:00
|
|
|
const attachment = await client.value.v1.media.create({
|
2023-03-01 06:58:19 +09:00
|
|
|
file: await processFile(file),
|
2023-01-10 22:22:39 +09:00
|
|
|
})
|
2024-02-22 00:20:08 +09:00
|
|
|
draft.value.attachments.push(attachment)
|
2023-01-10 22:22:39 +09:00
|
|
|
}
|
|
|
|
catch (e) {
|
2023-01-30 20:20:22 +09:00
|
|
|
// TODO: add some human-readable error message, problem is that masto api will not return response code
|
2023-01-10 22:22:39 +09:00
|
|
|
console.error(e)
|
2024-02-22 00:20:08 +09:00
|
|
|
failedAttachments.value = [...failedAttachments.value, [file.name, (e as Error).message]]
|
2023-01-10 22:22:39 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2024-02-22 00:20:08 +09:00
|
|
|
isExceedingAttachmentLimit.value = true
|
|
|
|
failedAttachments.value = [...failedAttachments.value, [file.name, t('state.attachments_limit_error')]]
|
2023-01-10 22:22:39 +09:00
|
|
|
}
|
|
|
|
}
|
2024-02-22 00:20:08 +09:00
|
|
|
isUploading.value = false
|
2023-01-10 22:22:39 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
async function pickAttachments() {
|
2024-02-25 01:46:14 +09:00
|
|
|
if (import.meta.server)
|
2023-02-06 18:34:50 +09:00
|
|
|
return
|
2023-01-15 17:38:02 +09:00
|
|
|
const mimeTypes = currentInstance.value!.configuration?.mediaAttachments.supportedMimeTypes
|
2023-01-10 22:22:39 +09: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-22 00:20:08 +09:00
|
|
|
if (!draft.value.editingStatus)
|
|
|
|
await client.value.v1.media.$select(att.id).update({ description: att.description })
|
2023-01-10 22:22:39 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeAttachment(index: number) {
|
2024-02-22 00:20:08 +09:00
|
|
|
draft.value.attachments.splice(index, 1)
|
2023-01-10 22:22:39 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
async function onDrop(files: File[] | null) {
|
|
|
|
if (files)
|
|
|
|
await uploadAttachments(files)
|
|
|
|
}
|
|
|
|
|
|
|
|
const { isOverDropZone } = useDropZone(dropZoneRef, onDrop)
|
|
|
|
|
2024-02-22 00:20:08 +09:00
|
|
|
return {
|
2023-01-10 22:22:39 +09:00
|
|
|
isUploading,
|
|
|
|
isExceedingAttachmentLimit,
|
2023-01-30 20:20:22 +09:00
|
|
|
isOverDropZone,
|
|
|
|
|
2023-01-10 22:22:39 +09:00
|
|
|
failedAttachments,
|
2023-01-11 07:51:25 +09:00
|
|
|
dropZoneRef,
|
2023-01-10 22:22:39 +09:00
|
|
|
|
|
|
|
uploadAttachments,
|
|
|
|
pickAttachments,
|
|
|
|
setDescription,
|
|
|
|
removeAttachment,
|
2024-02-22 00:20:08 +09:00
|
|
|
}
|
2023-01-10 22:22:39 +09:00
|
|
|
}
|