1
0
Fork 1
mirror of https://github.com/elk-zone/elk.git synced 2024-11-08 18:09:58 +00:00
elk/composables/masto/statusDrafts.ts

182 lines
5.2 KiB
TypeScript
Raw Normal View History

2023-01-08 06:21:09 +00:00
import type { mastodon } from 'masto'
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
2024-02-24 16:46:14 +00:00
export const currentUserDrafts = (import.meta.server || process.test)
2023-03-19 12:12:20 +00:00
? 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',
]
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
}
2024-01-09 08:56:15 +00:00
export function getDefaultDraft(options: Partial<Mutable<mastodon.rest.v1.CreateStatusParams> & Omit<Draft, 'params'>> = {}): Draft {
2022-11-29 20:45:20 +00:00
const {
attachments = [],
initialText = '',
2023-01-04 10:21:18 +00:00
status,
inReplyToId,
visibility,
sensitive,
spoilerText,
language,
mentions,
2023-05-20 20:23:41 +01:00
poll,
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 || '',
2023-05-20 20:23:41 +01:00
poll,
2022-11-24 14:32:20 +00:00
inReplyToId,
visibility: getDefaultVisibility(visibility || 'public'),
2023-01-04 10:21:18 +00:00
sensitive: sensitive ?? false,
spoilerText: spoilerText || '',
language: language || '', // auto inferred from current language on posting
2022-11-24 14:32:20 +00:00
},
mentions,
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> {
2023-05-20 20:23:41 +01:00
const info = {
2023-01-01 15:30:21 +00:00
status: await convertMastodonHTML(status.content),
2022-11-24 11:35:26 +00:00
visibility: status.visibility,
attachments: status.mediaAttachments,
sensitive: status.sensitive,
spoilerText: status.spoilerText,
language: status.language,
inReplyToId: status.inReplyToId,
2023-05-20 20:23:41 +01:00
}
return getDefaultDraft((status.mediaAttachments !== undefined && status.mediaAttachments.length > 0)
? { ...info, mediaIds: status.mediaAttachments.map(att => att.id) }
: {
...info,
poll: status.poll
? {
expiresIn: Math.abs(new Date().getTime() - new Date(status.poll.expiresAt!).getTime()) / 1000,
options: [...status.poll.options.map(({ title }) => title), ''],
multiple: status.poll.multiple,
hideTotals: status.poll.options[0].votesCount === null,
}
: undefined,
})
}
function getAccountsToMention(status: mastodon.v1.Status) {
const userId = currentUser.value?.account.id
2023-01-13 00:19:24 +00:00
const accountsToMention = new Set<string>()
if (status.account.id !== userId)
2023-01-13 00:19:24 +00: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)
}
export function getReplyDraft(status: mastodon.v1.Status) {
const accountsToMention = getAccountsToMention(status)
return {
key: `reply-${status.id}`,
draft: () => {
return getDefaultDraft({
initialText: '',
inReplyToId: status!.id,
sensitive: status.sensitive,
spoilerText: status.spoilerText,
visibility: status.visibility,
mentions: accountsToMention,
language: status.language,
})
},
2022-11-24 11:35:26 +00:00
}
}
export function isEmptyDraft(draft: Draft | null | undefined) {
2022-11-28 17:46:00 +00:00
if (!draft)
return true
const { params, attachments } = draft
const status = params.status || ''
2023-01-11 20:55:47 +00:00
const text = htmlToText(status).trim().replace(/^(@\S+\s?)+/, '').replaceAll(/```/g, '').trim()
return (text.length === 0)
&& attachments.length === 0
}
export interface UseDraft {
draft: Ref<Draft>
isEmpty: ComputedRef<boolean>
}
export function useDraft(
2023-01-05 15:42:36 +00:00
draftKey?: string,
2022-11-30 04:50:29 +00:00
initial: () => Draft = () => getDefaultDraft({}),
): 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
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) {
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) {
openPublishDialog('dialog', getDefaultDraft({
status: `@${account.acct} `,
}))
2022-11-25 11:39:21 +00:00
}
2023-01-08 06:21:09 +00:00
export function directMessageUser(account: mastodon.v1.Account) {
openPublishDialog('dialog', getDefaultDraft({
2022-11-25 11:39:21 +00:00
status: `@${account.acct} `,
visibility: 'direct',
}))
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) && !isEmptyDraft(currentUserDrafts.value[key]))
2023-01-05 15:42:36 +00:00
continue
if (!currentUserDrafts.value[key].params || isEmptyDraft(currentUserDrafts.value[key]))
delete currentUserDrafts.value[key]
}
}