2023-01-08 06:21:09 +00:00
|
|
|
import type { mastodon } from 'masto'
|
2023-01-10 13:22:39 +00:00
|
|
|
import type { ComputedRef, Ref } from 'vue'
|
2022-11-24 06:54:54 +00:00
|
|
|
import { STORAGE_KEY_DRAFTS } from '~/constants'
|
2022-12-13 14:03:30 +00:00
|
|
|
import type { Draft, DraftMap } from '~/types'
|
2023-01-04 10:21:18 +00:00
|
|
|
import type { Mutable } from '~/types/utils'
|
2022-11-24 06:54:54 +00:00
|
|
|
|
2022-12-17 16:55:29 +00:00
|
|
|
export const currentUserDrafts = process.server ? computed<DraftMap>(() => ({})) : useUserLocalStorage<DraftMap>(STORAGE_KEY_DRAFTS, () => ({}))
|
2022-11-24 11:35:26 +00:00
|
|
|
|
2023-01-05 15:42:36 +00:00
|
|
|
export const builtinDraftKeys = [
|
|
|
|
'dialog',
|
|
|
|
'home',
|
|
|
|
]
|
|
|
|
|
2023-01-08 06:21:09 +00:00
|
|
|
export function getDefaultDraft(options: Partial<Mutable<mastodon.v1.CreateStatusParams> & Omit<Draft, 'params'>> = {}): Draft {
|
2022-11-29 20:45:20 +00:00
|
|
|
const {
|
|
|
|
attachments = [],
|
2022-12-12 22:35:59 +00:00
|
|
|
initialText = '',
|
2023-01-04 10:21:18 +00:00
|
|
|
|
|
|
|
status,
|
|
|
|
inReplyToId,
|
|
|
|
visibility,
|
|
|
|
sensitive,
|
|
|
|
spoilerText,
|
2023-01-04 09:39:12 +00:00
|
|
|
language,
|
2022-11-29 20:45:20 +00:00
|
|
|
} = options
|
2022-11-30 04:50:29 +00:00
|
|
|
|
2022-11-24 11:35:26 +00:00
|
|
|
return {
|
2023-01-04 10:21:18 +00:00
|
|
|
attachments,
|
|
|
|
initialText,
|
2022-11-24 14:32:20 +00:00
|
|
|
params: {
|
2023-01-04 10:21:18 +00:00
|
|
|
status: status || '',
|
2022-11-24 14:32:20 +00:00
|
|
|
inReplyToId,
|
2023-01-04 10:21:18 +00:00
|
|
|
visibility: visibility || 'public',
|
|
|
|
sensitive: sensitive ?? false,
|
|
|
|
spoilerText: spoilerText || '',
|
|
|
|
language: language || 'en',
|
2022-11-24 14:32:20 +00:00
|
|
|
},
|
2023-01-05 15:42:36 +00:00
|
|
|
lastUpdated: Date.now(),
|
2022-11-24 11:35:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-08 06:21:09 +00:00
|
|
|
export async function getDraftFromStatus(status: mastodon.v1.Status): Promise<Draft> {
|
2022-11-28 10:23:33 +00:00
|
|
|
return getDefaultDraft({
|
2023-01-01 15:30:21 +00:00
|
|
|
status: await convertMastodonHTML(status.content),
|
2022-11-24 11:35:26 +00:00
|
|
|
mediaIds: status.mediaAttachments.map(att => att.id),
|
|
|
|
visibility: status.visibility,
|
2022-11-28 10:23:33 +00:00
|
|
|
attachments: status.mediaAttachments,
|
2022-12-21 14:21:45 +00:00
|
|
|
sensitive: status.sensitive,
|
|
|
|
spoilerText: status.spoilerText,
|
2023-01-04 09:39:12 +00:00
|
|
|
language: status.language,
|
2022-11-28 10:23:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-12 22:35:59 +00:00
|
|
|
function mentionHTML(acct: string) {
|
|
|
|
return `<span data-type="mention" data-id="${acct}" contenteditable="false">@${acct}</span>`
|
|
|
|
}
|
|
|
|
|
2023-01-08 13:54:12 +00:00
|
|
|
function getAccountsToMention(status: mastodon.v1.Status) {
|
2022-12-12 22:35:59 +00:00
|
|
|
const userId = currentUser.value?.account.id
|
2023-01-08 13:54:12 +00:00
|
|
|
const accountsToMention: string[] = []
|
2022-12-12 22:35:59 +00:00
|
|
|
if (status.account.id !== userId)
|
2022-12-13 13:49:22 +00:00
|
|
|
accountsToMention.push(status.account.acct)
|
|
|
|
accountsToMention.push(...(status.mentions.filter(mention => mention.id !== userId).map(mention => mention.acct)))
|
2023-01-08 13:54:12 +00:00
|
|
|
return accountsToMention
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getReplyDraft(status: mastodon.v1.Status) {
|
|
|
|
const accountsToMention = getAccountsToMention(status)
|
2022-11-28 10:23:33 +00:00
|
|
|
return {
|
|
|
|
key: `reply-${status.id}`,
|
2022-12-12 22:35:59 +00:00
|
|
|
draft: () => {
|
|
|
|
return getDefaultDraft({
|
2022-12-13 13:49:22 +00:00
|
|
|
initialText: accountsToMention.map(acct => mentionHTML(acct)).join(' '),
|
2022-12-12 22:35:59 +00:00
|
|
|
inReplyToId: status!.id,
|
|
|
|
visibility: status.visibility,
|
|
|
|
})
|
|
|
|
},
|
2022-11-24 11:35:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-28 17:46:00 +00:00
|
|
|
export const isEmptyDraft = (draft: Draft | null | undefined) => {
|
|
|
|
if (!draft)
|
|
|
|
return true
|
2022-11-28 10:23:33 +00:00
|
|
|
const { params, attachments } = draft
|
|
|
|
const status = params.status || ''
|
2023-01-08 13:54:12 +00:00
|
|
|
const text = htmlToText(status).trim().replace(/^(@\S+\s?)+/, '').trim()
|
|
|
|
|
|
|
|
return (text.length === 0)
|
2022-11-28 10:23:33 +00:00
|
|
|
&& attachments.length === 0
|
|
|
|
&& (params.spoilerText || '').length === 0
|
|
|
|
}
|
|
|
|
|
2023-01-10 13:22:39 +00:00
|
|
|
export interface UseDraft {
|
|
|
|
draft: Ref<Draft>
|
|
|
|
isEmpty: ComputedRef<boolean>
|
|
|
|
}
|
|
|
|
|
2022-11-28 10:23:33 +00:00
|
|
|
export function useDraft(
|
2023-01-05 15:42:36 +00:00
|
|
|
draftKey?: string,
|
2022-11-30 04:50:29 +00:00
|
|
|
initial: () => Draft = () => getDefaultDraft({}),
|
2023-01-10 13:22:39 +00:00
|
|
|
): UseDraft {
|
2023-01-05 15:42:36 +00:00
|
|
|
const draft = draftKey
|
|
|
|
? computed({
|
|
|
|
get() {
|
|
|
|
if (!currentUserDrafts.value[draftKey])
|
|
|
|
currentUserDrafts.value[draftKey] = initial()
|
|
|
|
return currentUserDrafts.value[draftKey]
|
|
|
|
},
|
|
|
|
set(val) {
|
|
|
|
currentUserDrafts.value[draftKey] = val
|
|
|
|
},
|
|
|
|
})
|
|
|
|
: ref(initial())
|
2022-11-24 11:35:26 +00:00
|
|
|
|
2022-11-28 10:23:33 +00:00
|
|
|
const isEmpty = computed(() => isEmptyDraft(draft.value))
|
|
|
|
|
|
|
|
onUnmounted(async () => {
|
|
|
|
// Remove draft if it's empty
|
2023-01-05 15:42:36 +00:00
|
|
|
if (isEmpty.value && draftKey) {
|
2022-11-28 10:23:33 +00:00
|
|
|
await nextTick()
|
|
|
|
delete currentUserDrafts.value[draftKey]
|
|
|
|
}
|
2022-11-24 11:35:26 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return { draft, isEmpty }
|
|
|
|
}
|
|
|
|
|
2023-01-08 06:21:09 +00:00
|
|
|
export function mentionUser(account: mastodon.v1.Account) {
|
2022-11-28 07:55:57 +00:00
|
|
|
openPublishDialog('dialog', getDefaultDraft({
|
|
|
|
status: `@${account.acct} `,
|
2022-11-28 10:23:33 +00:00
|
|
|
}), true)
|
2022-11-25 11:39:21 +00:00
|
|
|
}
|
|
|
|
|
2023-01-08 06:21:09 +00:00
|
|
|
export function directMessageUser(account: mastodon.v1.Account) {
|
2022-11-28 07:55:57 +00:00
|
|
|
openPublishDialog('dialog', getDefaultDraft({
|
2022-11-25 11:39:21 +00:00
|
|
|
status: `@${account.acct} `,
|
|
|
|
visibility: 'direct',
|
2022-11-28 10:23:33 +00:00
|
|
|
}), true)
|
2022-11-25 11:39:21 +00:00
|
|
|
}
|
2023-01-05 15:42:36 +00:00
|
|
|
|
|
|
|
export function clearEmptyDrafts() {
|
|
|
|
for (const key in currentUserDrafts.value) {
|
|
|
|
if (builtinDraftKeys.includes(key))
|
|
|
|
continue
|
|
|
|
if (!currentUserDrafts.value[key].params || isEmptyDraft(currentUserDrafts.value[key]))
|
|
|
|
delete currentUserDrafts.value[key]
|
|
|
|
}
|
|
|
|
}
|