refactor: inject masto instance via nuxt app (#134)
This commit is contained in:
parent
5c60497421
commit
39b005899e
26 changed files with 67 additions and 48 deletions
|
@ -11,7 +11,7 @@ let relationship = $(useRelationship(account))
|
||||||
async function toggleFollow() {
|
async function toggleFollow() {
|
||||||
relationship!.following = !relationship!.following
|
relationship!.following = !relationship!.following
|
||||||
try {
|
try {
|
||||||
relationship = await masto.accounts[relationship!.following ? 'follow' : 'unfollow'](account.id)
|
relationship = await useMasto().accounts[relationship!.following ? 'follow' : 'unfollow'](account.id)
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
// TODO error handling
|
// TODO error handling
|
||||||
|
|
|
@ -13,24 +13,24 @@ const toggleMute = async () => {
|
||||||
|
|
||||||
relationship!.muting = !relationship!.muting
|
relationship!.muting = !relationship!.muting
|
||||||
relationship = relationship!.muting
|
relationship = relationship!.muting
|
||||||
? await masto.accounts.mute(account.id, {
|
? await useMasto().accounts.mute(account.id, {
|
||||||
// TODO support more options
|
// TODO support more options
|
||||||
})
|
})
|
||||||
: await masto.accounts.unmute(account.id)
|
: await useMasto().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 masto.accounts[relationship!.blocking ? 'block' : 'unblock'](account.id)
|
relationship = await useMasto().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 masto.domainBlocks[relationship!.domainBlocking ? 'block' : 'unblock'](getServerName(account))
|
await useMasto().domainBlocks[relationship!.domainBlocking ? 'block' : 'unblock'](getServerName(account))
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
|
@ -76,7 +76,7 @@ async function toggleSensitive() {
|
||||||
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 masto.mediaAttachments.create({
|
const attachment = await useMasto().mediaAttachments.create({
|
||||||
file,
|
file,
|
||||||
})
|
})
|
||||||
draft.attachments.push(attachment)
|
draft.attachments.push(attachment)
|
||||||
|
@ -114,9 +114,9 @@ async function publish() {
|
||||||
isSending = true
|
isSending = true
|
||||||
|
|
||||||
if (!draft.editingStatus)
|
if (!draft.editingStatus)
|
||||||
await masto.statuses.create(payload)
|
await useMasto().statuses.create(payload)
|
||||||
else
|
else
|
||||||
await masto.statuses.update(draft.editingStatus.id, payload)
|
await useMasto().statuses.update(draft.editingStatus.id, payload)
|
||||||
|
|
||||||
draft = getDefaultDraft({ inReplyToId })
|
draft = getDefaultDraft({ inReplyToId })
|
||||||
isPublishDialogOpen.value = false
|
isPublishDialogOpen.value = false
|
||||||
|
|
|
@ -43,7 +43,7 @@ async function toggleStatusAction(action: Action, newStatus: Promise<Status>, co
|
||||||
}
|
}
|
||||||
const toggleReblog = () => toggleStatusAction(
|
const toggleReblog = () => toggleStatusAction(
|
||||||
'reblogged',
|
'reblogged',
|
||||||
masto.statuses[status.reblogged ? 'unreblog' : 'reblog'](status.id).then((res) => {
|
useMasto().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!
|
||||||
|
@ -54,17 +54,17 @@ const toggleReblog = () => toggleStatusAction(
|
||||||
|
|
||||||
const toggleFavourite = () => toggleStatusAction(
|
const toggleFavourite = () => toggleStatusAction(
|
||||||
'favourited',
|
'favourited',
|
||||||
masto.statuses[status.favourited ? 'unfavourite' : 'favourite'](status.id),
|
useMasto().statuses[status.favourited ? 'unfavourite' : 'favourite'](status.id),
|
||||||
'favouritesCount',
|
'favouritesCount',
|
||||||
)
|
)
|
||||||
|
|
||||||
const toggleBookmark = () => toggleStatusAction(
|
const toggleBookmark = () => toggleStatusAction(
|
||||||
'bookmarked',
|
'bookmarked',
|
||||||
masto.statuses[status.bookmarked ? 'unbookmark' : 'bookmark'](status.id),
|
useMasto().statuses[status.bookmarked ? 'unbookmark' : 'bookmark'](status.id),
|
||||||
)
|
)
|
||||||
const togglePin = async () => toggleStatusAction(
|
const togglePin = async () => toggleStatusAction(
|
||||||
'pinned',
|
'pinned',
|
||||||
masto.statuses[status.pinned ? 'unpin' : 'pin'](status.id),
|
useMasto().statuses[status.pinned ? 'unpin' : 'pin'](status.id),
|
||||||
)
|
)
|
||||||
|
|
||||||
const { toggle: _toggleTranslation, translation, enabled: isTranslationEnabled } = useTranslation(_status)
|
const { toggle: _toggleTranslation, translation, enabled: isTranslationEnabled } = useTranslation(_status)
|
||||||
|
@ -80,7 +80,7 @@ const copyLink = async () => {
|
||||||
const deleteStatus = async () => {
|
const deleteStatus = async () => {
|
||||||
// TODO confirm to delete
|
// TODO confirm to delete
|
||||||
|
|
||||||
await masto.statuses.remove(status.id)
|
await useMasto().statuses.remove(status.id)
|
||||||
if (route.name === '@user-post')
|
if (route.name === '@user-post')
|
||||||
router.back()
|
router.back()
|
||||||
|
|
||||||
|
@ -90,7 +90,7 @@ const deleteStatus = async () => {
|
||||||
const deleteAndRedraft = async () => {
|
const deleteAndRedraft = async () => {
|
||||||
// TODO confirm to delete
|
// TODO confirm to delete
|
||||||
|
|
||||||
const { text } = await masto.statuses.remove(status.id)
|
const { text } = await useMasto().statuses.remove(status.id)
|
||||||
|
|
||||||
if (!dialogDraft.isEmpty) {
|
if (!dialogDraft.isEmpty) {
|
||||||
// TODO confirm to overwrite
|
// TODO confirm to overwrite
|
||||||
|
|
|
@ -5,7 +5,7 @@ const { status } = defineProps<{
|
||||||
status: Status
|
status: Status
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
const { data: statusEdits } = useAsyncData(`status:history:${status.id}`, () => masto.statuses.fetchHistory(status.id).then(res => res.reverse()))
|
const { data: statusEdits } = useAsyncData(`status:history:${status.id}`, () => useMasto().statuses.fetchHistory(status.id).then(res => res.reverse()))
|
||||||
|
|
||||||
const showHistory = (edit: StatusEdit) => {
|
const showHistory = (edit: StatusEdit) => {
|
||||||
openEditHistoryDialog(edit)
|
openEditHistoryDialog(edit)
|
||||||
|
|
|
@ -19,7 +19,7 @@ export function fetchStatus(id: string): Promise<Status> {
|
||||||
const cached = cache.get(key)
|
const cached = cache.get(key)
|
||||||
if (cached)
|
if (cached)
|
||||||
return cached
|
return cached
|
||||||
const promise = masto.statuses.fetch(id)
|
const promise = useMasto().statuses.fetch(id)
|
||||||
.then((status) => {
|
.then((status) => {
|
||||||
cacheStatus(status)
|
cacheStatus(status)
|
||||||
return status
|
return status
|
||||||
|
@ -33,7 +33,7 @@ export function fetchAccount(id: string): Promise<Account> {
|
||||||
const cached = cache.get(key)
|
const cached = cache.get(key)
|
||||||
if (cached)
|
if (cached)
|
||||||
return cached
|
return cached
|
||||||
const promise = masto.accounts.fetch(id)
|
const promise = useMasto().accounts.fetch(id)
|
||||||
.then((account) => {
|
.then((account) => {
|
||||||
cacheAccount(account, true)
|
cacheAccount(account, true)
|
||||||
return account
|
return account
|
||||||
|
@ -47,7 +47,7 @@ export async function fetchAccountByName(acct: string): Promise<Account> {
|
||||||
const cached = cache.get(key)
|
const cached = cache.get(key)
|
||||||
if (cached)
|
if (cached)
|
||||||
return cached
|
return cached
|
||||||
const account = masto.accounts.lookup({ acct })
|
const account = useMasto().accounts.lookup({ acct })
|
||||||
.then((r) => {
|
.then((r) => {
|
||||||
cacheAccount(r, true)
|
cacheAccount(r, true)
|
||||||
return r
|
return r
|
||||||
|
|
|
@ -1,9 +1 @@
|
||||||
import { login } from 'masto'
|
export const useMasto = () => useNuxtApp().$masto
|
||||||
import { currentUser } from './users'
|
|
||||||
import { DEFAULT_SERVER } from '~/constants'
|
|
||||||
|
|
||||||
// TODO: improve upsteam to make this synchronous (delayed auth)
|
|
||||||
export const masto = await login({
|
|
||||||
url: `https://${currentUser.value?.server || DEFAULT_SERVER}`,
|
|
||||||
accessToken: currentUser.value?.token || undefined,
|
|
||||||
})
|
|
||||||
|
|
|
@ -112,7 +112,7 @@ async function fetchRelationships() {
|
||||||
const requested = Array.from(requestedRelationships.entries())
|
const requested = Array.from(requestedRelationships.entries())
|
||||||
requestedRelationships.clear()
|
requestedRelationships.clear()
|
||||||
|
|
||||||
const relationships = await masto.accounts.fetchRelationships(requested.map(([id]) => id))
|
const relationships = await useMasto().accounts.fetchRelationships(requested.map(([id]) => id))
|
||||||
for (let i = 0; i < requested.length; i++)
|
for (let i = 0; i < requested.length; i++)
|
||||||
requested[i][1].value = relationships[i]
|
requested[i][1].value = relationships[i]
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ export const MentionSuggestion: Partial<SuggestionOptions> = {
|
||||||
if (query.length === 0)
|
if (query.length === 0)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
const mentionPaginator = masto.search({ q: query, type: 'accounts', limit: 25, resolve: true })
|
const mentionPaginator = useMasto().search({ q: query, type: 'accounts', limit: 25, resolve: true })
|
||||||
const results = await mentionPaginator.next()
|
const results = await mentionPaginator.next()
|
||||||
|
|
||||||
return results.value.accounts
|
return results.value.accounts
|
||||||
|
|
|
@ -40,12 +40,12 @@ export async function loginTo(user: UserLogin & { account?: AccountCredentials }
|
||||||
url: `https://${user.server}`,
|
url: `https://${user.server}`,
|
||||||
accessToken: user.token,
|
accessToken: user.token,
|
||||||
})
|
})
|
||||||
const me = await masto.accounts.verifyCredentials()
|
const me = await useMasto().accounts.verifyCredentials()
|
||||||
user.account = me
|
user.account = me
|
||||||
|
|
||||||
users.value.push(user)
|
users.value.push(user)
|
||||||
currentUserId.value = me.id
|
currentUserId.value = me.id
|
||||||
servers.value[me.id] = await masto.instances.fetch()
|
servers.value[me.id] = await useMasto().instances.fetch()
|
||||||
await reloadPage()
|
await reloadPage()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ const id = $computed(() => route.params.status as string)
|
||||||
const main = ref<Component | null>(null)
|
const main = ref<Component | null>(null)
|
||||||
|
|
||||||
const status = window.history.state?.status ?? await fetchStatus(id)
|
const status = window.history.state?.status ?? await fetchStatus(id)
|
||||||
const { data: context } = useAsyncData(`context:${id}`, () => masto.statuses.fetchContext(id))
|
const { data: context } = useAsyncData(`context:${id}`, () => useMasto().statuses.fetchContext(id))
|
||||||
const unsubscribe = watch(context, async (context) => {
|
const unsubscribe = watch(context, async (context) => {
|
||||||
if (context) {
|
if (context) {
|
||||||
const statusElement = document.querySelector(`#status-${id}`)
|
const statusElement = document.querySelector(`#status-${id}`)
|
||||||
|
|
|
@ -3,7 +3,7 @@ const params = useRoute().params
|
||||||
const accountName = $computed(() => params.account as string)
|
const accountName = $computed(() => params.account as string)
|
||||||
|
|
||||||
const account = await fetchAccountByName(accountName)
|
const account = await fetchAccountByName(accountName)
|
||||||
const paginator = account ? masto.accounts.getFollowersIterable(account.id, {}) : null
|
const paginator = account ? useMasto().accounts.getFollowersIterable(account.id, {}) : null
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|
|
@ -3,7 +3,7 @@ const params = useRoute().params
|
||||||
const accountName = $computed(() => params.account as string)
|
const accountName = $computed(() => params.account as string)
|
||||||
|
|
||||||
const account = await fetchAccountByName(accountName)
|
const account = await fetchAccountByName(accountName)
|
||||||
const paginator = account ? masto.accounts.getFollowingIterable(account.id, {}) : null
|
const paginator = account ? useMasto().accounts.getFollowingIterable(account.id, {}) : null
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|
|
@ -8,8 +8,8 @@ const tabNames = ['Posts', 'Posts and replies'] as const
|
||||||
// Don't use local storage because it is better to default to Posts every time you visit a user's profile.
|
// Don't use local storage because it is better to default to Posts every time you visit a user's profile.
|
||||||
const tab = $ref('Posts')
|
const tab = $ref('Posts')
|
||||||
|
|
||||||
const paginatorPosts = masto.accounts.getStatusesIterable(account.id, { excludeReplies: true })
|
const paginatorPosts = useMasto().accounts.getStatusesIterable(account.id, { excludeReplies: true })
|
||||||
const paginatorPostsWithReply = masto.accounts.getStatusesIterable(account.id, { excludeReplies: false })
|
const paginatorPostsWithReply = useMasto().accounts.getStatusesIterable(account.id, { excludeReplies: false })
|
||||||
|
|
||||||
const paginator = $computed(() => {
|
const paginator = $computed(() => {
|
||||||
return tab === 'Posts' ? paginatorPosts : paginatorPostsWithReply
|
return tab === 'Posts' ? paginatorPosts : paginatorPostsWithReply
|
||||||
|
|
|
@ -3,7 +3,7 @@ definePageMeta({
|
||||||
middleware: 'auth',
|
middleware: 'auth',
|
||||||
})
|
})
|
||||||
|
|
||||||
const paginator = masto.blocks.getIterator()
|
const paginator = useMasto().blocks.getIterator()
|
||||||
|
|
||||||
useHead({
|
useHead({
|
||||||
title: 'Blocked users',
|
title: 'Blocked users',
|
||||||
|
|
|
@ -3,7 +3,7 @@ definePageMeta({
|
||||||
middleware: 'auth',
|
middleware: 'auth',
|
||||||
})
|
})
|
||||||
|
|
||||||
const paginator = masto.bookmarks.getIterator()
|
const paginator = useMasto().bookmarks.getIterator()
|
||||||
|
|
||||||
useHead({
|
useHead({
|
||||||
title: 'Bookmarks',
|
title: 'Bookmarks',
|
||||||
|
|
|
@ -3,7 +3,7 @@ definePageMeta({
|
||||||
middleware: 'auth',
|
middleware: 'auth',
|
||||||
})
|
})
|
||||||
|
|
||||||
const paginator = masto.conversations.getIterator()
|
const paginator = useMasto().conversations.getIterator()
|
||||||
|
|
||||||
useHead({
|
useHead({
|
||||||
title: 'Conversations',
|
title: 'Conversations',
|
||||||
|
|
|
@ -3,14 +3,14 @@ definePageMeta({
|
||||||
middleware: 'auth',
|
middleware: 'auth',
|
||||||
})
|
})
|
||||||
|
|
||||||
const paginator = masto.domainBlocks.getIterator()
|
const paginator = useMasto().domainBlocks.getIterator()
|
||||||
|
|
||||||
useHead({
|
useHead({
|
||||||
title: 'Blocked domains',
|
title: 'Blocked domains',
|
||||||
})
|
})
|
||||||
|
|
||||||
const unblock = async (domain: string) => {
|
const unblock = async (domain: string) => {
|
||||||
await masto.domainBlocks.unblock(domain)
|
await useMasto().domainBlocks.unblock(domain)
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
const paginator = masto.trends.getStatuses()
|
const paginator = useMasto().trends.getStatuses()
|
||||||
|
|
||||||
useHead({
|
useHead({
|
||||||
title: 'Explore',
|
title: 'Explore',
|
||||||
|
|
|
@ -3,7 +3,7 @@ definePageMeta({
|
||||||
middleware: 'auth',
|
middleware: 'auth',
|
||||||
})
|
})
|
||||||
|
|
||||||
const paginator = masto.favourites.getIterator()
|
const paginator = useMasto().favourites.getIterator()
|
||||||
|
|
||||||
useHead({
|
useHead({
|
||||||
title: 'Favourites',
|
title: 'Favourites',
|
||||||
|
|
|
@ -3,7 +3,7 @@ definePageMeta({
|
||||||
middleware: 'auth',
|
middleware: 'auth',
|
||||||
})
|
})
|
||||||
|
|
||||||
const paginator = masto.timelines.getHomeIterable()
|
const paginator = useMasto().timelines.getHomeIterable()
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|
|
@ -3,7 +3,7 @@ definePageMeta({
|
||||||
middleware: 'auth',
|
middleware: 'auth',
|
||||||
})
|
})
|
||||||
|
|
||||||
const paginator = masto.mutes.getIterator()
|
const paginator = useMasto().mutes.getIterator()
|
||||||
|
|
||||||
useHead({
|
useHead({
|
||||||
title: 'Muted users',
|
title: 'Muted users',
|
||||||
|
|
|
@ -9,7 +9,7 @@ const tabNames = ['All', 'Mentions'] as const
|
||||||
const tab = $(useLocalStorage<typeof tabNames[number]>(STORAGE_KEY_NOTIFY_TAB, 'All'))
|
const tab = $(useLocalStorage<typeof tabNames[number]>(STORAGE_KEY_NOTIFY_TAB, 'All'))
|
||||||
|
|
||||||
const paginator = $computed(() => {
|
const paginator = $computed(() => {
|
||||||
return masto.notifications.getIterator(tab === 'All' ? undefined : { types: ['mention'] })
|
return useMasto().notifications.getIterator(tab === 'All' ? undefined : { types: ['mention'] })
|
||||||
})
|
})
|
||||||
|
|
||||||
useHead({
|
useHead({
|
||||||
|
|
|
@ -3,7 +3,7 @@ definePageMeta({
|
||||||
middleware: 'auth',
|
middleware: 'auth',
|
||||||
})
|
})
|
||||||
|
|
||||||
const paginator = masto.accounts.getStatusesIterable(currentUser.value!.account.id, { pinned: true })
|
const paginator = useMasto().accounts.getStatusesIterable(currentUser.value!.account.id, { pinned: true })
|
||||||
|
|
||||||
useHead({
|
useHead({
|
||||||
title: 'Pinned',
|
title: 'Pinned',
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
const params = useRoute().params
|
const params = useRoute().params
|
||||||
const tag = $computed(() => params.tag as string)
|
const tag = $computed(() => params.tag as string)
|
||||||
|
|
||||||
const paginator = masto.timelines.getHashtagIterable(tag)
|
const paginator = useMasto().timelines.getHashtagIterable(tag)
|
||||||
|
|
||||||
useHead({
|
useHead({
|
||||||
title: `#${tag}`,
|
title: `#${tag}`,
|
||||||
|
|
27
plugins/masto.ts
Normal file
27
plugins/masto.ts
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
import { login } from 'masto'
|
||||||
|
import { currentUser } from '../composables/users'
|
||||||
|
import { DEFAULT_SERVER } from '~/constants'
|
||||||
|
|
||||||
|
export default defineNuxtPlugin(async () => {
|
||||||
|
try {
|
||||||
|
// TODO: improve upstream to make this synchronous (delayed auth)
|
||||||
|
const masto = await login({
|
||||||
|
url: `https://${currentUser.value?.server || DEFAULT_SERVER}`,
|
||||||
|
accessToken: currentUser.value?.token || undefined,
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
provide: {
|
||||||
|
masto,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
// TODO: handle error
|
||||||
|
// Show error page when Mastodon server is down
|
||||||
|
throw createError({
|
||||||
|
fatal: true,
|
||||||
|
statusMessage: 'Could not log into account.',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
Loading…
Add table
Reference in a new issue