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
|
|
|
}) {
|
2023-01-10 13:22:39 +00:00
|
|
|
const { expanded, isUploading, initialDraft } = $(options)
|
|
|
|
let { draft, isEmpty } = $(options.draftState)
|
2023-01-15 08:38:02 +00:00
|
|
|
const { client } = $(useMasto())
|
2023-01-30 11:20:22 +00:00
|
|
|
const settings = useUserSettings()
|
|
|
|
|
2023-07-29 12:01:17 +00:00
|
|
|
const preferredLanguage = $computed(() => (currentUser.value?.account.source.language || settings.value?.language || 'en').split('-')[0])
|
2023-01-10 13:22:39 +00:00
|
|
|
|
|
|
|
let isSending = $ref(false)
|
|
|
|
const isExpanded = $ref(false)
|
2023-01-17 12:56:51 +00:00
|
|
|
const failedMessages = $ref<string[]>([])
|
2023-01-10 13:22:39 +00:00
|
|
|
|
2023-02-05 17:36:33 +00:00
|
|
|
const publishSpoilerText = $computed({
|
|
|
|
get() {
|
|
|
|
return draft.params.sensitive ? draft.params.spoilerText : ''
|
|
|
|
},
|
|
|
|
set(val) {
|
|
|
|
if (!draft.params.sensitive)
|
|
|
|
return
|
|
|
|
draft.params.spoilerText = val
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-01-10 13:22:39 +00:00
|
|
|
const shouldExpanded = $computed(() => expanded || isExpanded || !isEmpty)
|
|
|
|
const isPublishDisabled = $computed(() => {
|
2023-05-21 19:57:29 +00:00
|
|
|
const firstEmptyInputIndex = draft.params.poll?.options.findIndex(option => option.trim().length === 0)
|
|
|
|
|
2023-05-20 19:23:41 +00:00
|
|
|
return isEmpty
|
|
|
|
|| isUploading
|
|
|
|
|| isSending
|
|
|
|
|| (draft.attachments.length === 0 && !draft.params.status)
|
|
|
|
|| failedMessages.length > 0
|
|
|
|
|| (draft.attachments.length > 0 && draft.params.poll !== null && draft.params.poll !== undefined)
|
2023-05-21 11:37:33 +00:00
|
|
|
|| ((draft.params.poll !== null && draft.params.poll !== undefined)
|
|
|
|
&& (
|
2023-05-21 19:57:29 +00:00
|
|
|
(firstEmptyInputIndex !== -1
|
|
|
|
&& firstEmptyInputIndex !== draft.params.poll.options.length - 1
|
|
|
|
)
|
|
|
|
|| draft.params.poll.options.findLastIndex(option => option.trim().length > 0) + 1 < 2
|
2023-05-21 11:37:33 +00:00
|
|
|
|| (new Set(draft.params.poll.options).size !== draft.params.poll.options.length)
|
|
|
|
|| (currentInstance.value?.configuration?.polls.maxCharactersPerOption !== undefined
|
|
|
|
&& draft.params.poll.options.find(option => option.length > currentInstance.value!.configuration!.polls.maxCharactersPerOption) !== undefined
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2023-01-10 13:22:39 +00:00
|
|
|
})
|
|
|
|
|
2023-01-17 12:56:51 +00:00
|
|
|
watch(() => draft, () => {
|
|
|
|
if (failedMessages.length > 0)
|
|
|
|
failedMessages.length = 0
|
|
|
|
}, { deep: true })
|
|
|
|
|
2023-01-10 13:22:39 +00:00
|
|
|
async function publishDraft() {
|
2023-01-28 22:00:20 +00:00
|
|
|
if (isPublishDisabled)
|
|
|
|
return
|
2023-01-30 11:20:22 +00:00
|
|
|
|
2023-01-13 00:33:04 +00:00
|
|
|
let content = htmlToText(draft.params.status || '')
|
|
|
|
if (draft.mentions?.length)
|
|
|
|
content = `${draft.mentions.map(i => `@${i}`).join(' ')} ${content}`
|
|
|
|
|
2023-05-21 19:57:29 +00:00
|
|
|
let poll
|
|
|
|
|
|
|
|
if (draft.params.poll) {
|
|
|
|
let options = draft.params.poll.options
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
poll = { ...draft.params.poll, options }
|
|
|
|
}
|
|
|
|
|
2023-01-10 13:22:39 +00:00
|
|
|
const payload = {
|
|
|
|
...draft.params,
|
2023-02-05 17:36:33 +00:00
|
|
|
spoilerText: publishSpoilerText,
|
2023-01-13 00:33:04 +00:00
|
|
|
status: content,
|
2023-01-10 13:22:39 +00:00
|
|
|
mediaIds: draft.attachments.map(a => a.id),
|
2023-01-30 11:20:22 +00:00
|
|
|
language: draft.params.language || preferredLanguage,
|
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
|
|
|
|
|
|
|
if (process.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({
|
|
|
|
raw: draft.params.status,
|
|
|
|
...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 {
|
|
|
|
isSending = true
|
|
|
|
|
|
|
|
let status: mastodon.v1.Status
|
2023-08-02 10:31:08 +00:00
|
|
|
if (!draft.editingStatus) {
|
2023-01-15 08:38:02 +00:00
|
|
|
status = await client.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-01-09 08:56:15 +00:00
|
|
|
status = await client.v1.statuses.$select(draft.editingStatus.id).update({
|
2023-08-02 10:31:08 +00:00
|
|
|
...payload,
|
|
|
|
mediaAttributes: draft.attachments.map(media => ({
|
|
|
|
id: media.id,
|
|
|
|
description: media.description,
|
|
|
|
})),
|
2024-01-09 08:56:15 +00:00
|
|
|
})
|
2023-08-02 10:31:08 +00:00
|
|
|
}
|
2023-01-10 13:22:39 +00:00
|
|
|
if (draft.params.inReplyToId)
|
|
|
|
navigateToStatus({ status })
|
|
|
|
|
|
|
|
draft = initialDraft()
|
|
|
|
|
|
|
|
return status
|
|
|
|
}
|
2023-01-12 05:39:22 +00:00
|
|
|
catch (err) {
|
|
|
|
console.error(err)
|
2023-01-17 12:56:51 +00:00
|
|
|
failedMessages.push((err as Error).message)
|
2023-01-12 05:39:22 +00:00
|
|
|
}
|
2023-01-10 13:22:39 +00:00
|
|
|
finally {
|
|
|
|
isSending = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $$({
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export type MediaAttachmentUploadError = [filename: string, message: string]
|
|
|
|
|
2023-01-30 11:20:22 +00:00
|
|
|
export function useUploadMediaAttachment(draftRef: Ref<Draft>) {
|
2023-01-10 13:22:39 +00:00
|
|
|
const draft = $(draftRef)
|
2023-01-15 08:38:02 +00:00
|
|
|
const { client } = $(useMasto())
|
2023-01-10 13:22:39 +00:00
|
|
|
const { t } = useI18n()
|
|
|
|
|
|
|
|
let isUploading = $ref<boolean>(false)
|
|
|
|
let isExceedingAttachmentLimit = $ref<boolean>(false)
|
|
|
|
let failedAttachments = $ref<MediaAttachmentUploadError[]>([])
|
|
|
|
const dropZoneRef = ref<HTMLDivElement>()
|
|
|
|
|
2023-05-21 16:28:28 +00:00
|
|
|
const maxPixels = $computed(() => {
|
|
|
|
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')
|
|
|
|
|
|
|
|
const resizedWidth = canvas.width = Math.round(Math.sqrt(maxPixels * aspectRatio))
|
|
|
|
const resizedHeight = canvas.height = Math.round(Math.sqrt(maxPixels / aspectRatio))
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
if (image.width * image.height > maxPixels)
|
|
|
|
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[]) {
|
|
|
|
isUploading = true
|
|
|
|
failedAttachments = []
|
|
|
|
// 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)) {
|
|
|
|
if (draft.attachments.length < limit) {
|
|
|
|
isExceedingAttachmentLimit = false
|
|
|
|
try {
|
2024-01-09 08:56:15 +00:00
|
|
|
const attachment = await client.v1.media.create({
|
2023-02-28 21:58:19 +00:00
|
|
|
file: await processFile(file),
|
2023-01-10 13:22:39 +00:00
|
|
|
})
|
|
|
|
draft.attachments.push(attachment)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
failedAttachments = [...failedAttachments, [file.name, (e as Error).message]]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
isExceedingAttachmentLimit = true
|
|
|
|
failedAttachments = [...failedAttachments, [file.name, t('state.attachments_limit_error')]]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
isUploading = false
|
|
|
|
}
|
|
|
|
|
|
|
|
async function pickAttachments() {
|
2023-02-06 09:34:50 +00:00
|
|
|
if (process.server)
|
|
|
|
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
|
2023-08-02 10:31:08 +00:00
|
|
|
if (!draft.editingStatus)
|
2024-01-09 08:56:15 +00:00
|
|
|
await client.v1.media.$select(att.id).update({ description: att.description })
|
2023-01-10 13:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeAttachment(index: number) {
|
|
|
|
draft.attachments.splice(index, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function onDrop(files: File[] | null) {
|
|
|
|
if (files)
|
|
|
|
await uploadAttachments(files)
|
|
|
|
}
|
|
|
|
|
|
|
|
const { isOverDropZone } = useDropZone(dropZoneRef, onDrop)
|
|
|
|
|
|
|
|
return $$({
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|