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