refactor: initialise masto outside of functions/handlers

This commit is contained in:
Daniel Roe 2022-12-25 15:04:50 +01:00
parent 03d3775011
commit 720b5114af
No known key found for this signature in database
GPG key ID: 22D5008E4F5D9B55
13 changed files with 44 additions and 30 deletions

View file

@ -11,10 +11,11 @@ const isSelf = $computed(() => currentUser.value?.account.id === account.id)
const enable = $computed(() => !isSelf && currentUser.value) const enable = $computed(() => !isSelf && currentUser.value)
const relationship = $computed(() => props.relationship || useRelationship(account).value) const relationship = $computed(() => props.relationship || useRelationship(account).value)
const masto = useMasto()
async function toggleFollow() { async function toggleFollow() {
relationship!.following = !relationship!.following relationship!.following = !relationship!.following
try { try {
const newRel = await useMasto().accounts[relationship!.following ? 'follow' : 'unfollow'](account.id) const newRel = await masto.accounts[relationship!.following ? 'follow' : 'unfollow'](account.id)
Object.assign(relationship!, newRel) Object.assign(relationship!, newRel)
} }
catch { catch {
@ -26,7 +27,7 @@ async function toggleFollow() {
async function unblock() { async function unblock() {
relationship!.blocking = false relationship!.blocking = false
try { try {
const newRel = await useMasto().accounts.unblock(account.id) const newRel = await masto.accounts.unblock(account.id)
Object.assign(relationship!, newRel) Object.assign(relationship!, newRel)
} }
catch { catch {
@ -38,7 +39,7 @@ async function unblock() {
async function unmute() { async function unmute() {
relationship!.muting = false relationship!.muting = false
try { try {
const newRel = await useMasto().accounts.unmute(account.id) const newRel = await masto.accounts.unmute(account.id)
Object.assign(relationship!, newRel) Object.assign(relationship!, newRel)
} }
catch { catch {

View file

@ -9,29 +9,30 @@ let relationship = $(useRelationship(account))
const isSelf = $computed(() => currentUser.value?.account.id === account.id) const isSelf = $computed(() => currentUser.value?.account.id === account.id)
const masto = useMasto()
const toggleMute = async () => { const toggleMute = async () => {
// TODO: Add confirmation // TODO: Add confirmation
relationship!.muting = !relationship!.muting relationship!.muting = !relationship!.muting
relationship = relationship!.muting relationship = relationship!.muting
? await useMasto().accounts.mute(account.id, { ? await masto.accounts.mute(account.id, {
// TODO support more options // TODO support more options
}) })
: await useMasto().accounts.unmute(account.id) : await masto.accounts.unmute(account.id)
} }
const toggleBlockUser = async () => { const toggleBlockUser = async () => {
// TODO: Add confirmation // TODO: Add confirmation
relationship!.blocking = !relationship!.blocking relationship!.blocking = !relationship!.blocking
relationship = await useMasto().accounts[relationship!.blocking ? 'block' : 'unblock'](account.id) relationship = await masto.accounts[relationship!.blocking ? 'block' : 'unblock'](account.id)
} }
const toggleBlockDomain = async () => { const toggleBlockDomain = async () => {
// TODO: Add confirmation // TODO: Add confirmation
relationship!.domainBlocking = !relationship!.domainBlocking relationship!.domainBlocking = !relationship!.domainBlocking
await useMasto().domainBlocks[relationship!.domainBlocking ? 'block' : 'unblock'](getServerName(account)) await masto.domainBlocks[relationship!.domainBlocking ? 'block' : 'unblock'](getServerName(account))
} }
</script> </script>

View file

@ -94,10 +94,12 @@ async function toggleSensitive() {
draft.params.sensitive = !draft.params.sensitive draft.params.sensitive = !draft.params.sensitive
} }
const masto = useMasto()
async function uploadAttachments(files: File[]) { async function uploadAttachments(files: File[]) {
isUploading = true isUploading = true
for (const file of files) { for (const file of files) {
const attachment = await useMasto().mediaAttachments.create({ const attachment = await masto.mediaAttachments.create({
file, file,
}) })
draft.attachments.push(attachment) draft.attachments.push(attachment)
@ -107,7 +109,7 @@ async function uploadAttachments(files: File[]) {
async function setDescription(att: Attachment, description: string) { async function setDescription(att: Attachment, description: string) {
att.description = description att.description = description
await useMasto().mediaAttachments.update(att.id, { description: att.description }) await masto.mediaAttachments.update(att.id, { description: att.description })
} }
function removeAttachment(index: number) { function removeAttachment(index: number) {
@ -141,9 +143,9 @@ async function publish() {
isSending = true isSending = true
if (!draft.editingStatus) if (!draft.editingStatus)
await useMasto().statuses.create(payload) await masto.statuses.create(payload)
else else
await useMasto().statuses.update(draft.editingStatus.id, payload) await masto.statuses.update(draft.editingStatus.id, payload)
draft = initial() draft = initial()
isPublishDialogOpen.value = false isPublishDialogOpen.value = false

View file

@ -5,7 +5,8 @@ const { status } = defineProps<{
status: Status status: Status
}>() }>()
const { data: statusEdits } = useAsyncData(`status:history:${status.id}`, () => useMasto().statuses.fetchHistory(status.id).then(res => res.reverse())) const masto = useMasto()
const { data: statusEdits } = useAsyncData(`status:history:${status.id}`, () => masto.statuses.fetchHistory(status.id).then(res => res.reverse()))
const showHistory = (edit: StatusEdit) => { const showHistory = (edit: StatusEdit) => {
openEditHistoryDialog(edit) openEditHistoryDialog(edit)

View file

@ -9,6 +9,7 @@ export interface StatusActionsProps {
export function useStatusActions(props: StatusActionsProps) { export function useStatusActions(props: StatusActionsProps) {
let status = $ref<Status>({ ...props.status }) let status = $ref<Status>({ ...props.status })
const masto = useMasto()
watch( watch(
() => props.status, () => props.status,
@ -42,7 +43,7 @@ export function useStatusActions(props: StatusActionsProps) {
} }
const toggleReblog = () => toggleStatusAction( const toggleReblog = () => toggleStatusAction(
'reblogged', 'reblogged',
() => useMasto().statuses[status.reblogged ? 'unreblog' : 'reblog'](status.id).then((res) => { () => masto.statuses[status.reblogged ? 'unreblog' : 'reblog'](status.id).then((res) => {
if (status.reblogged) if (status.reblogged)
// returns the original status // returns the original status
return res.reblog! return res.reblog!
@ -53,18 +54,18 @@ export function useStatusActions(props: StatusActionsProps) {
const toggleFavourite = () => toggleStatusAction( const toggleFavourite = () => toggleStatusAction(
'favourited', 'favourited',
() => useMasto().statuses[status.favourited ? 'unfavourite' : 'favourite'](status.id), () => masto.statuses[status.favourited ? 'unfavourite' : 'favourite'](status.id),
'favouritesCount', 'favouritesCount',
) )
const toggleBookmark = () => toggleStatusAction( const toggleBookmark = () => toggleStatusAction(
'bookmarked', 'bookmarked',
() => useMasto().statuses[status.bookmarked ? 'unbookmark' : 'bookmark'](status.id), () => masto.statuses[status.bookmarked ? 'unbookmark' : 'bookmark'](status.id),
) )
const togglePin = async () => toggleStatusAction( const togglePin = async () => toggleStatusAction(
'pinned', 'pinned',
() => useMasto().statuses[status.pinned ? 'unpin' : 'pin'](status.id), () => masto.statuses[status.pinned ? 'unpin' : 'pin'](status.id),
) )
return { return {

View file

@ -17,7 +17,8 @@ const { data: status, pending, refresh: refreshStatus } = useAsyncData(`status:$
window.history.state?.status as Status | undefined) window.history.state?.status as Status | undefined)
?? await fetchStatus(id), ?? await fetchStatus(id),
) )
const { data: context, pending: pendingContext, refresh: refreshContext } = useAsyncData(`context:${id}`, () => useMasto().statuses.fetchContext(id)) const masto = useMasto()
const { data: context, pending: pendingContext, refresh: refreshContext } = useAsyncData(`context:${id}`, () => masto.statuses.fetchContext(id))
const replyDraft = $computed(() => status.value ? getReplyDraft(status.value) : null) const replyDraft = $computed(() => status.value ? getReplyDraft(status.value) : null)

View file

@ -4,8 +4,9 @@ import { STORAGE_KEY_HIDE_EXPLORE_TAGS_TIPS } from '~~/constants'
const { t } = useI18n() const { t } = useI18n()
const masto = useMasto()
const { data, pending, error } = useLazyAsyncData( const { data, pending, error } = useLazyAsyncData(
() => useMasto().trends.fetchTags({ limit: 20 }), () => masto.trends.fetchTags({ limit: 20 }),
{ immediate: true }, { immediate: true },
) )

View file

@ -2,8 +2,9 @@
const { t } = useI18n() const { t } = useI18n()
// limit: 20 is the default configuration of the official client // limit: 20 is the default configuration of the official client
const masto = useMasto()
const { data, pending, error } = useLazyAsyncData( const { data, pending, error } = useLazyAsyncData(
() => useMasto().suggestions.fetchAll({ limit: 20 }), () => masto.suggestions.fetchAll({ limit: 20 }),
{ immediate: true }, { immediate: true },
) )

View file

@ -2,10 +2,11 @@
const params = useRoute().params const params = useRoute().params
const tagName = $(computedEager(() => params.tag as string)) const tagName = $(computedEager(() => params.tag as string))
const { data: tag, refresh } = $(await useAsyncData(() => useMasto().tags.fetch(tagName))) const masto = useMasto()
const { data: tag, refresh } = $(await useAsyncData(() => masto.tags.fetch(tagName)))
const paginator = useMasto().timelines.iterateHashtag(tagName) const paginator = masto.timelines.iterateHashtag(tagName)
const stream = await useMasto().stream.streamTagTimeline(tagName) const stream = await masto.stream.streamTagTimeline(tagName)
onBeforeUnmount(() => stream.disconnect()) onBeforeUnmount(() => stream.disconnect())
if (tag) { if (tag) {

View file

@ -3,14 +3,15 @@ definePageMeta({
middleware: 'auth', middleware: 'auth',
}) })
const paginator = useMasto().domainBlocks.iterate() const masto = useMasto()
const paginator = masto.domainBlocks.iterate()
useHeadFixed({ useHeadFixed({
title: 'Blocked domains', title: 'Blocked domains',
}) })
const unblock = async (domain: string) => { const unblock = async (domain: string) => {
await useMasto().domainBlocks.unblock(domain) await masto.domainBlocks.unblock(domain)
} }
</script> </script>

View file

@ -11,8 +11,9 @@ if (useRoute().path === '/signin/callback') {
useRouter().push('/home') useRouter().push('/home')
} }
const paginator = useMasto().timelines.iterateHome() const masto = useMasto()
const stream = await useMasto().stream.streamUser() const paginator = masto.timelines.iterateHome()
const stream = await masto.stream.streamUser()
onBeforeUnmount(() => stream.disconnect()) onBeforeUnmount(() => stream.disconnect())
const { t } = useI18n() const { t } = useI18n()

View file

@ -1,13 +1,14 @@
<script setup lang="ts"> <script setup lang="ts">
const { t } = useI18n() const { t } = useI18n()
const masto = useMasto()
// Default limit is 20 notifications, and servers are normally caped to 30 // Default limit is 20 notifications, and servers are normally caped to 30
const paginator = useMasto().notifications.iterate({ limit: 30 }) const paginator = masto.notifications.iterate({ limit: 30 })
const { clearNotifications } = useNotifications() const { clearNotifications } = useNotifications()
onActivated(clearNotifications) onActivated(clearNotifications)
const stream = await useMasto().stream.streamUser() const stream = await masto.stream.streamUser()
useHeadFixed({ useHeadFixed({
title: () => `${t('tab.notifications_all')} | ${t('nav_side.notifications')}`, title: () => `${t('tab.notifications_all')} | ${t('nav_side.notifications')}`,

View file

@ -1,13 +1,14 @@
<script setup lang="ts"> <script setup lang="ts">
const { t } = useI18n() const { t } = useI18n()
const masto = useMasto()
// Default limit is 20 notifications, and servers are normally caped to 30 // Default limit is 20 notifications, and servers are normally caped to 30
const paginator = useMasto().notifications.iterate({ limit: 30, types: ['mention'] }) const paginator = masto.notifications.iterate({ limit: 30, types: ['mention'] })
const { clearNotifications } = useNotifications() const { clearNotifications } = useNotifications()
onActivated(clearNotifications) onActivated(clearNotifications)
const stream = await useMasto().stream.streamUser() const stream = await masto.stream.streamUser()
useHeadFixed({ useHeadFixed({
title: () => `${t('tab.notifications_mention')} | ${t('nav_side.notifications')}`, title: () => `${t('tab.notifications_mention')} | ${t('nav_side.notifications')}`,