2023-01-08 09:21:09 +03:00
|
|
|
import type { mastodon } from 'masto'
|
2023-01-10 16:22:39 +03:00
|
|
|
import type { ComputedRef, Ref } from 'vue'
|
2024-04-08 12:53:26 +03:00
|
|
|
import type { DraftItem, DraftMap } from '~/types'
|
2023-01-04 13:21:18 +03:00
|
|
|
import type { Mutable } from '~/types/utils'
|
2024-12-09 06:50:20 +03:00
|
|
|
import { STORAGE_KEY_DRAFTS } from '~/constants'
|
2022-11-24 09:54:54 +03:00
|
|
|
|
2024-02-24 19:46:14 +03:00
|
|
|
export const currentUserDrafts = (import.meta.server || process.test)
|
2023-03-19 15:12:20 +03:00
|
|
|
? computed<DraftMap>(() => ({}))
|
|
|
|
: useUserLocalStorage<DraftMap>(STORAGE_KEY_DRAFTS, () => ({}))
|
2022-11-24 14:35:26 +03:00
|
|
|
|
2023-01-05 18:42:36 +03:00
|
|
|
export const builtinDraftKeys = [
|
|
|
|
'dialog',
|
|
|
|
'home',
|
|
|
|
]
|
|
|
|
|
2023-04-27 23:41:10 +03: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)
|
2024-03-05 17:48:58 +03:00
|
|
|
> ALL_VISIBILITY.indexOf(preferredVisibility)
|
2023-04-27 23:41:10 +03:00
|
|
|
? currentVisibility
|
|
|
|
: preferredVisibility
|
|
|
|
}
|
|
|
|
|
2024-04-08 12:53:26 +03:00
|
|
|
export function getDefaultDraftItem(options: Partial<Mutable<mastodon.rest.v1.CreateStatusParams> & Omit<DraftItem, 'params'>> = {}): DraftItem {
|
2022-11-29 23:45:20 +03:00
|
|
|
const {
|
|
|
|
attachments = [],
|
2022-12-13 01:35:59 +03:00
|
|
|
initialText = '',
|
2023-01-04 13:21:18 +03:00
|
|
|
status,
|
|
|
|
inReplyToId,
|
|
|
|
visibility,
|
|
|
|
sensitive,
|
|
|
|
spoilerText,
|
2023-01-04 12:39:12 +03:00
|
|
|
language,
|
2023-01-13 03:33:04 +03:00
|
|
|
mentions,
|
2023-05-20 22:23:41 +03:00
|
|
|
poll,
|
2022-11-29 23:45:20 +03:00
|
|
|
} = options
|
2022-11-30 07:50:29 +03:00
|
|
|
|
2022-11-24 14:35:26 +03:00
|
|
|
return {
|
2023-01-04 13:21:18 +03:00
|
|
|
attachments,
|
|
|
|
initialText,
|
2022-11-24 17:32:20 +03:00
|
|
|
params: {
|
2023-01-04 13:21:18 +03:00
|
|
|
status: status || '',
|
2023-05-20 22:23:41 +03:00
|
|
|
poll,
|
2022-11-24 17:32:20 +03:00
|
|
|
inReplyToId,
|
2023-04-27 23:41:10 +03:00
|
|
|
visibility: getDefaultVisibility(visibility || 'public'),
|
2023-01-04 13:21:18 +03:00
|
|
|
sensitive: sensitive ?? false,
|
|
|
|
spoilerText: spoilerText || '',
|
2023-01-30 14:20:22 +03:00
|
|
|
language: language || '', // auto inferred from current language on posting
|
2022-11-24 17:32:20 +03:00
|
|
|
},
|
2023-01-13 03:33:04 +03:00
|
|
|
mentions,
|
2023-01-05 18:42:36 +03:00
|
|
|
lastUpdated: Date.now(),
|
2022-11-24 14:35:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-08 12:53:26 +03:00
|
|
|
export async function getDraftFromStatus(status: mastodon.v1.Status): Promise<DraftItem> {
|
2023-05-20 22:23:41 +03:00
|
|
|
const info = {
|
2023-01-01 18:30:21 +03:00
|
|
|
status: await convertMastodonHTML(status.content),
|
2022-11-24 14:35:26 +03:00
|
|
|
visibility: status.visibility,
|
2022-11-28 13:23:33 +03:00
|
|
|
attachments: status.mediaAttachments,
|
2022-12-21 17:21:45 +03:00
|
|
|
sensitive: status.sensitive,
|
|
|
|
spoilerText: status.spoilerText,
|
2023-01-04 12:39:12 +03:00
|
|
|
language: status.language,
|
2023-04-28 06:25:53 +03:00
|
|
|
inReplyToId: status.inReplyToId,
|
2023-05-20 22:23:41 +03:00
|
|
|
}
|
|
|
|
|
2024-04-08 12:53:26 +03:00
|
|
|
return getDefaultDraftItem((status.mediaAttachments !== undefined && status.mediaAttachments.length > 0)
|
2023-05-20 22:23:41 +03:00
|
|
|
? { ...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,
|
|
|
|
})
|
2022-11-28 13:23:33 +03:00
|
|
|
}
|
|
|
|
|
2023-01-08 16:54:12 +03:00
|
|
|
function getAccountsToMention(status: mastodon.v1.Status) {
|
2022-12-13 01:35:59 +03:00
|
|
|
const userId = currentUser.value?.account.id
|
2023-01-13 03:19:24 +03:00
|
|
|
const accountsToMention = new Set<string>()
|
2022-12-13 01:35:59 +03:00
|
|
|
if (status.account.id !== userId)
|
2023-01-13 03:19:24 +03: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 16:54:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getReplyDraft(status: mastodon.v1.Status) {
|
|
|
|
const accountsToMention = getAccountsToMention(status)
|
2022-11-28 13:23:33 +03:00
|
|
|
return {
|
|
|
|
key: `reply-${status.id}`,
|
2022-12-13 01:35:59 +03:00
|
|
|
draft: () => {
|
2024-04-08 12:53:26 +03:00
|
|
|
return getDefaultDraftItem({
|
2023-01-13 03:33:04 +03:00
|
|
|
initialText: '',
|
2022-12-13 01:35:59 +03:00
|
|
|
inReplyToId: status!.id,
|
2023-03-01 01:10:04 +03:00
|
|
|
sensitive: status.sensitive,
|
|
|
|
spoilerText: status.spoilerText,
|
2022-12-13 01:35:59 +03:00
|
|
|
visibility: status.visibility,
|
2023-01-13 03:33:04 +03:00
|
|
|
mentions: accountsToMention,
|
2023-02-05 15:08:01 +03:00
|
|
|
language: status.language,
|
2022-12-13 01:35:59 +03:00
|
|
|
})
|
|
|
|
},
|
2022-11-24 14:35:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-08 12:53:26 +03:00
|
|
|
export function isEmptyDraft(drafts: Array<DraftItem> | DraftItem | null | undefined) {
|
|
|
|
if (!drafts)
|
2022-11-28 20:46:00 +03:00
|
|
|
return true
|
2023-01-08 16:54:12 +03:00
|
|
|
|
2024-04-08 12:53:26 +03:00
|
|
|
const draftsArray: Array<DraftItem> = Array.isArray(drafts) ? drafts : [drafts]
|
|
|
|
|
|
|
|
if (draftsArray.length === 0)
|
|
|
|
return true
|
|
|
|
|
|
|
|
const anyDraftHasContent = draftsArray.some((draft) => {
|
|
|
|
const { params, attachments } = draft
|
|
|
|
const status = params.status ?? ''
|
|
|
|
const text = htmlToText(status).trim().replace(/^(@\S+\s?)+/, '').replaceAll(/```/g, '').trim()
|
|
|
|
|
|
|
|
return (text.length > 0)
|
|
|
|
|| (attachments.length > 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
return !anyDraftHasContent
|
2022-11-28 13:23:33 +03:00
|
|
|
}
|
|
|
|
|
2023-01-10 16:22:39 +03:00
|
|
|
export interface UseDraft {
|
2024-04-08 12:53:26 +03:00
|
|
|
draftItems: Ref<Array<DraftItem>>
|
|
|
|
isEmpty: ComputedRef<boolean> | Ref<boolean>
|
2023-01-10 16:22:39 +03:00
|
|
|
}
|
|
|
|
|
2022-11-28 13:23:33 +03:00
|
|
|
export function useDraft(
|
2024-04-08 12:53:26 +03:00
|
|
|
draftKey: string,
|
|
|
|
initial: () => DraftItem = () => getDefaultDraftItem({}),
|
2023-01-10 16:22:39 +03:00
|
|
|
): UseDraft {
|
2024-04-08 12:53:26 +03:00
|
|
|
const draftItems = computed({
|
|
|
|
get() {
|
|
|
|
if (!currentUserDrafts.value[draftKey])
|
|
|
|
currentUserDrafts.value[draftKey] = [initial()]
|
|
|
|
const drafts = currentUserDrafts.value[draftKey]
|
|
|
|
if (Array.isArray(drafts))
|
|
|
|
return drafts
|
|
|
|
return [drafts]
|
|
|
|
},
|
|
|
|
set(val) {
|
|
|
|
currentUserDrafts.value[draftKey] = val
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const isEmpty = computed(() => isEmptyDraft(draftItems.value))
|
2022-11-28 13:23:33 +03:00
|
|
|
|
|
|
|
onUnmounted(async () => {
|
|
|
|
// Remove draft if it's empty
|
2023-01-05 18:42:36 +03:00
|
|
|
if (isEmpty.value && draftKey) {
|
2022-11-28 13:23:33 +03:00
|
|
|
await nextTick()
|
|
|
|
delete currentUserDrafts.value[draftKey]
|
|
|
|
}
|
2022-11-24 14:35:26 +03:00
|
|
|
})
|
|
|
|
|
2024-04-08 12:53:26 +03:00
|
|
|
return { draftItems, isEmpty }
|
2022-11-24 14:35:26 +03:00
|
|
|
}
|
|
|
|
|
2023-01-08 09:21:09 +03:00
|
|
|
export function mentionUser(account: mastodon.v1.Account) {
|
2024-04-08 12:53:26 +03:00
|
|
|
openPublishDialog('dialog', getDefaultDraftItem({
|
2022-11-28 10:55:57 +03:00
|
|
|
status: `@${account.acct} `,
|
2023-02-25 01:34:17 +03:00
|
|
|
}))
|
2022-11-25 14:39:21 +03:00
|
|
|
}
|
|
|
|
|
2023-01-08 09:21:09 +03:00
|
|
|
export function directMessageUser(account: mastodon.v1.Account) {
|
2024-04-08 12:53:26 +03:00
|
|
|
openPublishDialog('dialog', getDefaultDraftItem({
|
2022-11-25 14:39:21 +03:00
|
|
|
status: `@${account.acct} `,
|
|
|
|
visibility: 'direct',
|
2023-02-25 01:34:17 +03:00
|
|
|
}))
|
2022-11-25 14:39:21 +03:00
|
|
|
}
|
2023-01-05 18:42:36 +03:00
|
|
|
|
|
|
|
export function clearEmptyDrafts() {
|
|
|
|
for (const key in currentUserDrafts.value) {
|
2023-04-27 23:41:10 +03:00
|
|
|
if (builtinDraftKeys.includes(key) && !isEmptyDraft(currentUserDrafts.value[key]))
|
2023-01-05 18:42:36 +03:00
|
|
|
continue
|
2024-04-08 12:53:26 +03:00
|
|
|
if (isEmptyDraft(currentUserDrafts.value[key]))
|
2023-01-05 18:42:36 +03:00
|
|
|
delete currentUserDrafts.value[key]
|
|
|
|
}
|
|
|
|
}
|