2023-01-10 22:22:39 +09: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'
|
|
|
|
|
|
|
|
export const usePublish = (options: {
|
|
|
|
draftState: UseDraft
|
|
|
|
expanded: Ref<boolean>
|
|
|
|
isUploading: Ref<boolean>
|
|
|
|
initialDraft: Ref<() => Draft>
|
|
|
|
}) => {
|
|
|
|
const { expanded, isUploading, initialDraft } = $(options)
|
|
|
|
let { draft, isEmpty } = $(options.draftState)
|
2023-01-15 17:38:02 +09:00
|
|
|
const { client } = $(useMasto())
|
2023-01-10 22:22:39 +09:00
|
|
|
|
|
|
|
let isSending = $ref(false)
|
|
|
|
const isExpanded = $ref(false)
|
|
|
|
|
|
|
|
const shouldExpanded = $computed(() => expanded || isExpanded || !isEmpty)
|
|
|
|
const isPublishDisabled = $computed(() => {
|
|
|
|
return isEmpty || isUploading || isSending || (draft.attachments.length === 0 && !draft.params.status)
|
|
|
|
})
|
|
|
|
|
|
|
|
async function publishDraft() {
|
2023-01-13 09:33:04 +09:00
|
|
|
let content = htmlToText(draft.params.status || '')
|
|
|
|
if (draft.mentions?.length)
|
|
|
|
content = `${draft.mentions.map(i => `@${i}`).join(' ')} ${content}`
|
|
|
|
|
2023-01-10 22:22:39 +09:00
|
|
|
const payload = {
|
|
|
|
...draft.params,
|
2023-01-13 09:33:04 +09:00
|
|
|
status: content,
|
2023-01-10 22:22:39 +09:00
|
|
|
mediaIds: draft.attachments.map(a => a.id),
|
|
|
|
...(isGlitchEdition.value ? { 'content-type': 'text/markdown' } : {}),
|
|
|
|
} as mastodon.v1.CreateStatusParams
|
|
|
|
|
|
|
|
if (process.dev) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
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
|
|
|
|
if (!draft.editingStatus)
|
2023-01-15 17:38:02 +09:00
|
|
|
status = await client.v1.statuses.create(payload)
|
2023-01-10 22:22:39 +09:00
|
|
|
else
|
2023-01-15 17:38:02 +09:00
|
|
|
status = await client.v1.statuses.update(draft.editingStatus.id, payload)
|
2023-01-10 22:22:39 +09:00
|
|
|
if (draft.params.inReplyToId)
|
|
|
|
navigateToStatus({ status })
|
|
|
|
|
|
|
|
draft = initialDraft()
|
|
|
|
|
|
|
|
return status
|
|
|
|
}
|
2023-01-12 14:39:22 +09:00
|
|
|
catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
}
|
2023-01-10 22:22:39 +09:00
|
|
|
finally {
|
|
|
|
isSending = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $$({
|
|
|
|
isSending,
|
|
|
|
isExpanded,
|
|
|
|
shouldExpanded,
|
|
|
|
isPublishDisabled,
|
|
|
|
|
|
|
|
publishDraft,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export type MediaAttachmentUploadError = [filename: string, message: string]
|
|
|
|
|
|
|
|
export const useUploadMediaAttachment = (draftRef: Ref<Draft>) => {
|
|
|
|
const draft = $(draftRef)
|
2023-01-15 17:38:02 +09:00
|
|
|
const { client } = $(useMasto())
|
2023-01-10 22:22:39 +09:00
|
|
|
const { t } = useI18n()
|
|
|
|
|
|
|
|
let isUploading = $ref<boolean>(false)
|
|
|
|
let isExceedingAttachmentLimit = $ref<boolean>(false)
|
|
|
|
let failedAttachments = $ref<MediaAttachmentUploadError[]>([])
|
|
|
|
const dropZoneRef = ref<HTMLDivElement>()
|
|
|
|
|
|
|
|
async function uploadAttachments(files: File[]) {
|
|
|
|
isUploading = true
|
|
|
|
failedAttachments = []
|
|
|
|
// 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
|
2023-01-10 22:22:39 +09:00
|
|
|
for (const file of files.slice(0, limit)) {
|
|
|
|
if (draft.attachments.length < limit) {
|
|
|
|
isExceedingAttachmentLimit = false
|
|
|
|
try {
|
2023-01-15 17:38:02 +09:00
|
|
|
const attachment = await client.v1.mediaAttachments.create({
|
2023-01-10 22:22:39 +09:00
|
|
|
file,
|
|
|
|
})
|
|
|
|
draft.attachments.push(attachment)
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
// TODO: add some human-readable error message, problem is that masto api will not return response code
|
|
|
|
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-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
|
2023-01-15 17:38:02 +09:00
|
|
|
await client.v1.mediaAttachments.update(att.id, { description: att.description })
|
2023-01-10 22:22:39 +09: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,
|
|
|
|
failedAttachments,
|
2023-01-11 07:51:25 +09:00
|
|
|
dropZoneRef,
|
2023-01-10 22:22:39 +09:00
|
|
|
isOverDropZone,
|
|
|
|
|
|
|
|
uploadAttachments,
|
|
|
|
pickAttachments,
|
|
|
|
setDescription,
|
|
|
|
removeAttachment,
|
|
|
|
})
|
|
|
|
}
|