2023-01-08 15:21:09 +09:00
|
|
|
import type { mastodon } from 'masto'
|
2023-01-10 22:22:39 +09:00
|
|
|
import type { ComputedRef, Ref } from 'vue'
|
2022-11-24 15:54:54 +09:00
|
|
|
import { STORAGE_KEY_DRAFTS } from '~/constants'
|
2022-12-13 23:03:30 +09:00
|
|
|
import type { Draft, DraftMap } from '~/types'
|
2023-01-04 19:21:18 +09:00
|
|
|
import type { Mutable } from '~/types/utils'
|
2022-11-24 15:54:54 +09:00
|
|
|
|
2023-03-19 21:12:20 +09:00
|
|
|
export const currentUserDrafts = (process.server || process.test)
|
|
|
|
? computed<DraftMap>(() => ({}))
|
|
|
|
: useUserLocalStorage<DraftMap>(STORAGE_KEY_DRAFTS, () => ({}))
|
2022-11-24 20:35:26 +09:00
|
|
|
|
2023-01-06 00:42:36 +09:00
|
|
|
export const builtinDraftKeys = [
|
|
|
|
'dialog',
|
|
|
|
'home',
|
|
|
|
]
|
|
|
|
|
2023-04-28 05:41:10 +09:00
|
|
|
const ALL_VISIBILITY = ['public', 'unlisted', 'private', 'direct'] as const
|
|
|
|
|
|
|
|
function getDefaultVisibility(currentVisibility: mastodon.v1.StatusVisibility) {
|
|
|
|
// The default privacy only should be taken into account if it makes
|
|
|
|
// the post more private than the replying to post
|
|
|
|
const preferredVisibility = currentUser.value?.account.source.privacy || 'public'
|
|
|
|
return ALL_VISIBILITY.indexOf(currentVisibility)
|
|
|
|
> ALL_VISIBILITY.indexOf(preferredVisibility)
|
|
|
|
? currentVisibility
|
|
|
|
: preferredVisibility
|
|
|
|
}
|
|
|
|
|
2023-01-08 15:21:09 +09:00
|
|
|
export function getDefaultDraft(options: Partial<Mutable<mastodon.v1.CreateStatusParams> & Omit<Draft, 'params'>> = {}): Draft {
|
2022-11-30 05:45:20 +09:00
|
|
|
const {
|
|
|
|
attachments = [],
|
2022-12-13 07:35:59 +09:00
|
|
|
initialText = '',
|
2023-01-04 19:21:18 +09:00
|
|
|
status,
|
|
|
|
inReplyToId,
|
|
|
|
visibility,
|
|
|
|
sensitive,
|
|
|
|
spoilerText,
|
2023-01-04 18:39:12 +09:00
|
|
|
language,
|
2023-01-13 09:33:04 +09:00
|
|
|
mentions,
|
2022-11-30 05:45:20 +09:00
|
|
|
} = options
|
2022-11-30 13:50:29 +09:00
|
|
|
|
2022-11-24 20:35:26 +09:00
|
|
|
return {
|
2023-01-04 19:21:18 +09:00
|
|
|
attachments,
|
|
|
|
initialText,
|
2022-11-24 23:32:20 +09:00
|
|
|
params: {
|
2023-01-04 19:21:18 +09:00
|
|
|
status: status || '',
|
2022-11-24 23:32:20 +09:00
|
|
|
inReplyToId,
|
2023-04-28 05:41:10 +09:00
|
|
|
visibility: getDefaultVisibility(visibility || 'public'),
|
2023-01-04 19:21:18 +09:00
|
|
|
sensitive: sensitive ?? false,
|
|
|
|
spoilerText: spoilerText || '',
|
2023-01-30 20:20:22 +09:00
|
|
|
language: language || '', // auto inferred from current language on posting
|
2022-11-24 23:32:20 +09:00
|
|
|
},
|
2023-01-13 09:33:04 +09:00
|
|
|
mentions,
|
2023-01-06 00:42:36 +09:00
|
|
|
lastUpdated: Date.now(),
|
2022-11-24 20:35:26 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-08 15:21:09 +09:00
|
|
|
export async function getDraftFromStatus(status: mastodon.v1.Status): Promise<Draft> {
|
2022-11-28 19:23:33 +09:00
|
|
|
return getDefaultDraft({
|
2023-01-02 00:30:21 +09:00
|
|
|
status: await convertMastodonHTML(status.content),
|
2022-11-24 20:35:26 +09:00
|
|
|
mediaIds: status.mediaAttachments.map(att => att.id),
|
|
|
|
visibility: status.visibility,
|
2022-11-28 19:23:33 +09:00
|
|
|
attachments: status.mediaAttachments,
|
2022-12-21 23:21:45 +09:00
|
|
|
sensitive: status.sensitive,
|
|
|
|
spoilerText: status.spoilerText,
|
2023-01-04 18:39:12 +09:00
|
|
|
language: status.language,
|
2023-04-28 12:25:53 +09:00
|
|
|
inReplyToId: status.inReplyToId,
|
2022-11-28 19:23:33 +09:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-08 22:54:12 +09:00
|
|
|
function getAccountsToMention(status: mastodon.v1.Status) {
|
2022-12-13 07:35:59 +09:00
|
|
|
const userId = currentUser.value?.account.id
|
2023-01-13 09:19:24 +09:00
|
|
|
const accountsToMention = new Set<string>()
|
2022-12-13 07:35:59 +09:00
|
|
|
if (status.account.id !== userId)
|
2023-01-13 09:19:24 +09:00
|
|
|
accountsToMention.add(status.account.acct)
|
|
|
|
status.mentions
|
|
|
|
.filter(mention => mention.id !== userId)
|
|
|
|
.map(mention => mention.acct)
|
|
|
|
.forEach(i => accountsToMention.add(i))
|
|
|
|
return Array.from(accountsToMention)
|
2023-01-08 22:54:12 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getReplyDraft(status: mastodon.v1.Status) {
|
|
|
|
const accountsToMention = getAccountsToMention(status)
|
2022-11-28 19:23:33 +09:00
|
|
|
return {
|
|
|
|
key: `reply-${status.id}`,
|
2022-12-13 07:35:59 +09:00
|
|
|
draft: () => {
|
|
|
|
return getDefaultDraft({
|
2023-01-13 09:33:04 +09:00
|
|
|
initialText: '',
|
2022-12-13 07:35:59 +09:00
|
|
|
inReplyToId: status!.id,
|
2023-03-01 07:10:04 +09:00
|
|
|
sensitive: status.sensitive,
|
|
|
|
spoilerText: status.spoilerText,
|
2022-12-13 07:35:59 +09:00
|
|
|
visibility: status.visibility,
|
2023-01-13 09:33:04 +09:00
|
|
|
mentions: accountsToMention,
|
2023-02-05 21:08:01 +09:00
|
|
|
language: status.language,
|
2022-12-13 07:35:59 +09:00
|
|
|
})
|
|
|
|
},
|
2022-11-24 20:35:26 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 04:01:24 +09:00
|
|
|
export function isEmptyDraft(draft: Draft | null | undefined) {
|
2022-11-29 02:46:00 +09:00
|
|
|
if (!draft)
|
|
|
|
return true
|
2022-11-28 19:23:33 +09:00
|
|
|
const { params, attachments } = draft
|
|
|
|
const status = params.status || ''
|
2023-01-12 05:55:47 +09:00
|
|
|
const text = htmlToText(status).trim().replace(/^(@\S+\s?)+/, '').replaceAll(/```/g, '').trim()
|
2023-01-08 22:54:12 +09:00
|
|
|
|
|
|
|
return (text.length === 0)
|
2022-11-28 19:23:33 +09:00
|
|
|
&& attachments.length === 0
|
|
|
|
}
|
|
|
|
|
2023-01-10 22:22:39 +09:00
|
|
|
export interface UseDraft {
|
|
|
|
draft: Ref<Draft>
|
|
|
|
isEmpty: ComputedRef<boolean>
|
|
|
|
}
|
|
|
|
|
2022-11-28 19:23:33 +09:00
|
|
|
export function useDraft(
|
2023-01-06 00:42:36 +09:00
|
|
|
draftKey?: string,
|
2022-11-30 13:50:29 +09:00
|
|
|
initial: () => Draft = () => getDefaultDraft({}),
|
2023-01-10 22:22:39 +09:00
|
|
|
): UseDraft {
|
2023-01-06 00:42:36 +09: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 20:35:26 +09:00
|
|
|
|
2022-11-28 19:23:33 +09:00
|
|
|
const isEmpty = computed(() => isEmptyDraft(draft.value))
|
|
|
|
|
|
|
|
onUnmounted(async () => {
|
|
|
|
// Remove draft if it's empty
|
2023-01-06 00:42:36 +09:00
|
|
|
if (isEmpty.value && draftKey) {
|
2022-11-28 19:23:33 +09:00
|
|
|
await nextTick()
|
|
|
|
delete currentUserDrafts.value[draftKey]
|
|
|
|
}
|
2022-11-24 20:35:26 +09:00
|
|
|
})
|
|
|
|
|
|
|
|
return { draft, isEmpty }
|
|
|
|
}
|
|
|
|
|
2023-01-08 15:21:09 +09:00
|
|
|
export function mentionUser(account: mastodon.v1.Account) {
|
2022-11-28 16:55:57 +09:00
|
|
|
openPublishDialog('dialog', getDefaultDraft({
|
|
|
|
status: `@${account.acct} `,
|
2023-02-25 07:34:17 +09:00
|
|
|
}))
|
2022-11-25 20:39:21 +09:00
|
|
|
}
|
|
|
|
|
2023-01-08 15:21:09 +09:00
|
|
|
export function directMessageUser(account: mastodon.v1.Account) {
|
2022-11-28 16:55:57 +09:00
|
|
|
openPublishDialog('dialog', getDefaultDraft({
|
2022-11-25 20:39:21 +09:00
|
|
|
status: `@${account.acct} `,
|
|
|
|
visibility: 'direct',
|
2023-02-25 07:34:17 +09:00
|
|
|
}))
|
2022-11-25 20:39:21 +09:00
|
|
|
}
|
2023-01-06 00:42:36 +09:00
|
|
|
|
|
|
|
export function clearEmptyDrafts() {
|
|
|
|
for (const key in currentUserDrafts.value) {
|
2023-04-28 05:41:10 +09:00
|
|
|
if (builtinDraftKeys.includes(key) && !isEmptyDraft(currentUserDrafts.value[key]))
|
2023-01-06 00:42:36 +09:00
|
|
|
continue
|
|
|
|
if (!currentUserDrafts.value[key].params || isEmptyDraft(currentUserDrafts.value[key]))
|
|
|
|
delete currentUserDrafts.value[key]
|
|
|
|
}
|
|
|
|
}
|