feat: upgrade to masto.js v6 (#2530)
This commit is contained in:
parent
d8ea685803
commit
6c5bb83ac3
62 changed files with 262 additions and 263 deletions
|
@ -24,7 +24,7 @@ export function fetchStatus(id: string, force = false): Promise<mastodon.v1.Stat
|
|||
const cached = cache.get(key)
|
||||
if (cached && !force)
|
||||
return cached
|
||||
const promise = useMastoClient().v1.statuses.fetch(id)
|
||||
const promise = useMastoClient().v1.statuses.$select(id).fetch()
|
||||
.then((status) => {
|
||||
cacheStatus(status)
|
||||
return status
|
||||
|
@ -44,7 +44,7 @@ export function fetchAccountById(id?: string | null): Promise<mastodon.v1.Accoun
|
|||
if (cached)
|
||||
return cached
|
||||
const domain = getInstanceDomainFromServer(server)
|
||||
const promise = useMastoClient().v1.accounts.fetch(id)
|
||||
const promise = useMastoClient().v1.accounts.$select(id).fetch()
|
||||
.then((r) => {
|
||||
if (r.acct && !r.acct.includes('@') && domain)
|
||||
r.acct = `${r.acct}@${domain}`
|
||||
|
@ -74,7 +74,7 @@ export async function fetchAccountByHandle(acct: string): Promise<mastodon.v1.Ac
|
|||
}
|
||||
else {
|
||||
const userAcctDomain = userAcct.includes('@') ? userAcct : `${userAcct}@${domain}`
|
||||
account = (await client.v1.search({ q: `@${userAcctDomain}`, type: 'accounts' })).accounts[0]
|
||||
account = (await client.v1.search.fetch({ q: `@${userAcctDomain}`, type: 'accounts' })).accounts[0]
|
||||
}
|
||||
|
||||
if (account.acct && !account.acct.includes('@') && domain)
|
||||
|
|
|
@ -1,27 +1,14 @@
|
|||
import type { Pausable } from '@vueuse/core'
|
||||
import type { CreateClientParams, WsEvents, mastodon } from 'masto'
|
||||
import { createClient, fetchV1Instance } from 'masto'
|
||||
import type { mastodon } from 'masto'
|
||||
import { createRestAPIClient, createStreamingAPIClient } from 'masto'
|
||||
import type { Ref } from 'vue'
|
||||
import type { ElkInstance } from '../users'
|
||||
import type { Mutable } from '~/types/utils'
|
||||
import type { UserLogin } from '~/types'
|
||||
|
||||
export function createMasto() {
|
||||
let client = $shallowRef<mastodon.Client>(undefined as never)
|
||||
let params = $ref<Mutable<CreateClientParams>>()
|
||||
const canStreaming = $computed(() => !!params?.streamingApiUrl)
|
||||
|
||||
const setParams = (newParams: Partial<CreateClientParams>) => {
|
||||
const p = { ...params, ...newParams } as CreateClientParams
|
||||
client = createClient(p)
|
||||
params = p
|
||||
}
|
||||
|
||||
return {
|
||||
client: $$(client),
|
||||
params: readonly($$(params)),
|
||||
canStreaming: $$(canStreaming),
|
||||
setParams,
|
||||
client: shallowRef<mastodon.rest.Client>(undefined as never),
|
||||
streamingClient: shallowRef<mastodon.streaming.Client | undefined>(),
|
||||
}
|
||||
}
|
||||
export type ElkMasto = ReturnType<typeof createMasto>
|
||||
|
@ -34,23 +21,25 @@ export function useMastoClient() {
|
|||
}
|
||||
|
||||
export function mastoLogin(masto: ElkMasto, user: Pick<UserLogin, 'server' | 'token'>) {
|
||||
const { setParams } = $(masto)
|
||||
|
||||
const server = user.server
|
||||
const url = `https://${server}`
|
||||
const instance: ElkInstance = reactive(getInstanceCache(server) || { uri: server, accountDomain: server })
|
||||
setParams({
|
||||
url,
|
||||
accessToken: user?.token,
|
||||
disableVersionCheck: true,
|
||||
streamingApiUrl: instance?.urls?.streamingApi,
|
||||
})
|
||||
const accessToken = user.token
|
||||
|
||||
fetchV1Instance({ url }).then((newInstance) => {
|
||||
const createStreamingClient = (streamingApiUrl: string | undefined) => {
|
||||
return streamingApiUrl ? createStreamingAPIClient({ streamingApiUrl, accessToken, implementation: globalThis.WebSocket }) : undefined
|
||||
}
|
||||
|
||||
const streamingApiUrl = instance?.urls?.streamingApi
|
||||
masto.client.value = createRestAPIClient({ url, accessToken })
|
||||
masto.streamingClient.value = createStreamingClient(streamingApiUrl)
|
||||
|
||||
// Refetch instance info in the background on login
|
||||
masto.client.value.v1.instance.fetch().then((newInstance) => {
|
||||
Object.assign(instance, newInstance)
|
||||
setParams({
|
||||
streamingApiUrl: newInstance.urls.streamingApi,
|
||||
})
|
||||
if (newInstance.urls.streamingApi !== streamingApiUrl)
|
||||
masto.streamingClient.value = createStreamingClient(newInstance.urls.streamingApi)
|
||||
|
||||
instanceStorage.value[server] = newInstance
|
||||
})
|
||||
|
||||
|
@ -73,21 +62,21 @@ interface UseStreamingOptions<Controls extends boolean> {
|
|||
}
|
||||
|
||||
export function useStreaming(
|
||||
cb: (client: mastodon.Client) => Promise<WsEvents>,
|
||||
cb: (client: mastodon.streaming.Client) => mastodon.streaming.Subscription,
|
||||
options: UseStreamingOptions<true>,
|
||||
): { stream: Ref<Promise<WsEvents> | undefined> } & Pausable
|
||||
): { stream: Ref<mastodon.streaming.Subscription | undefined> } & Pausable
|
||||
export function useStreaming(
|
||||
cb: (client: mastodon.Client) => Promise<WsEvents>,
|
||||
cb: (client: mastodon.streaming.Client) => mastodon.streaming.Subscription,
|
||||
options?: UseStreamingOptions<false>,
|
||||
): Ref<Promise<WsEvents> | undefined>
|
||||
): Ref<mastodon.streaming.Subscription | undefined>
|
||||
export function useStreaming(
|
||||
cb: (client: mastodon.Client) => Promise<WsEvents>,
|
||||
cb: (client: mastodon.streaming.Client) => mastodon.streaming.Subscription,
|
||||
{ immediate = true, controls }: UseStreamingOptions<boolean> = {},
|
||||
): ({ stream: Ref<Promise<WsEvents> | undefined> } & Pausable) | Ref<Promise<WsEvents> | undefined> {
|
||||
const { canStreaming, client } = useMasto()
|
||||
): ({ stream: Ref<mastodon.streaming.Subscription | undefined> } & Pausable) | Ref<mastodon.streaming.Subscription | undefined> {
|
||||
const { streamingClient } = useMasto()
|
||||
|
||||
const isActive = ref(immediate)
|
||||
const stream = ref<Promise<WsEvents>>()
|
||||
const stream = ref<mastodon.streaming.Subscription>()
|
||||
|
||||
function pause() {
|
||||
isActive.value = false
|
||||
|
@ -99,15 +88,15 @@ export function useStreaming(
|
|||
|
||||
function cleanup() {
|
||||
if (stream.value) {
|
||||
stream.value.then(s => s.disconnect()).catch(() => Promise.resolve())
|
||||
stream.value.unsubscribe()
|
||||
stream.value = undefined
|
||||
}
|
||||
}
|
||||
|
||||
watchEffect(() => {
|
||||
cleanup()
|
||||
if (canStreaming.value && isActive.value)
|
||||
stream.value = cb(client.value)
|
||||
if (streamingClient.value && isActive.value)
|
||||
stream.value = cb(streamingClient.value)
|
||||
})
|
||||
|
||||
if (process.client && !process.test)
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
import type { WsEvents } from 'masto'
|
||||
import type { mastodon } from 'masto'
|
||||
|
||||
const notifications = reactive<Record<string, undefined | [Promise<WsEvents>, string[]]>>({})
|
||||
const notifications = reactive<Record<string, undefined | [Promise<mastodon.streaming.Subscription>, string[]]>>({})
|
||||
|
||||
export function useNotifications() {
|
||||
const id = currentUser.value?.account.id
|
||||
|
||||
const { client, canStreaming } = $(useMasto())
|
||||
const { client, streamingClient } = $(useMasto())
|
||||
|
||||
async function clearNotifications() {
|
||||
if (!id || !notifications[id])
|
||||
|
@ -19,21 +19,27 @@ export function useNotifications() {
|
|||
}
|
||||
}
|
||||
|
||||
async function processNotifications(stream: mastodon.streaming.Subscription, id: string) {
|
||||
for await (const entry of stream) {
|
||||
if (entry.event === 'notification' && notifications[id])
|
||||
notifications[id]![1].unshift(entry.payload.id)
|
||||
}
|
||||
}
|
||||
|
||||
async function connect(): Promise<void> {
|
||||
if (!isHydrated.value || !id || notifications[id] || !currentUser.value?.token)
|
||||
if (!isHydrated.value || !id || notifications[id] !== undefined || !currentUser.value?.token)
|
||||
return
|
||||
|
||||
let resolveStream
|
||||
const stream = new Promise<WsEvents>(resolve => resolveStream = resolve)
|
||||
notifications[id] = [stream, []]
|
||||
const streamPromise = new Promise<mastodon.streaming.Subscription>(resolve => resolveStream = resolve)
|
||||
notifications[id] = [streamPromise, []]
|
||||
|
||||
await until($$(canStreaming)).toBe(true)
|
||||
await until($$(streamingClient)).toBe(true)
|
||||
|
||||
client.v1.stream.streamUser().then(resolveStream)
|
||||
stream.then(s => s.on('notification', (n) => {
|
||||
if (notifications[id])
|
||||
notifications[id]![1].unshift(n.id)
|
||||
}))
|
||||
const stream = streamingClient!.user.subscribe()
|
||||
resolveStream!(stream)
|
||||
|
||||
processNotifications(stream, id)
|
||||
|
||||
const position = await client.v1.markers.fetch({ timeline: ['notifications'] })
|
||||
const paginator = client.v1.notifications.list({ limit: 30 })
|
||||
|
@ -55,7 +61,7 @@ export function useNotifications() {
|
|||
function disconnect(): void {
|
||||
if (!id || !notifications[id])
|
||||
return
|
||||
notifications[id]![0].then(stream => stream.disconnect())
|
||||
notifications[id]![0].then(stream => stream.unsubscribe())
|
||||
notifications[id] = undefined
|
||||
}
|
||||
|
||||
|
@ -67,7 +73,6 @@ export function useNotifications() {
|
|||
|
||||
return {
|
||||
notifications: computed(() => id ? notifications[id]?.[1].length ?? 0 : 0),
|
||||
disconnect,
|
||||
clearNotifications,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -93,7 +93,7 @@ export function usePublish(options: {
|
|||
language: draft.params.language || preferredLanguage,
|
||||
poll,
|
||||
...(isGlitchEdition.value ? { 'content-type': 'text/markdown' } : {}),
|
||||
} as mastodon.v1.CreateStatusParams
|
||||
} as mastodon.rest.v1.CreateStatusParams
|
||||
|
||||
if (process.dev) {
|
||||
// eslint-disable-next-line no-console
|
||||
|
@ -116,14 +116,13 @@ export function usePublish(options: {
|
|||
}
|
||||
|
||||
else {
|
||||
const updatePayload = {
|
||||
status = await client.v1.statuses.$select(draft.editingStatus.id).update({
|
||||
...payload,
|
||||
mediaAttributes: draft.attachments.map(media => ({
|
||||
id: media.id,
|
||||
description: media.description,
|
||||
})),
|
||||
} as mastodon.v1.UpdateStatusParams
|
||||
status = await client.v1.statuses.update(draft.editingStatus.id, updatePayload)
|
||||
})
|
||||
}
|
||||
if (draft.params.inReplyToId)
|
||||
navigateToStatus({ status })
|
||||
|
@ -232,7 +231,7 @@ export function useUploadMediaAttachment(draftRef: Ref<Draft>) {
|
|||
if (draft.attachments.length < limit) {
|
||||
isExceedingAttachmentLimit = false
|
||||
try {
|
||||
const attachment = await client.v1.mediaAttachments.create({
|
||||
const attachment = await client.v1.media.create({
|
||||
file: await processFile(file),
|
||||
})
|
||||
draft.attachments.push(attachment)
|
||||
|
@ -266,7 +265,7 @@ export function useUploadMediaAttachment(draftRef: Ref<Draft>) {
|
|||
async function setDescription(att: mastodon.v1.MediaAttachment, description: string) {
|
||||
att.description = description
|
||||
if (!draft.editingStatus)
|
||||
await client.v1.mediaAttachments.update(att.id, { description: att.description })
|
||||
await client.v1.media.$select(att.id).update({ description: att.description })
|
||||
}
|
||||
|
||||
function removeAttachment(index: number) {
|
||||
|
|
|
@ -27,7 +27,7 @@ export function useRelationship(account: mastodon.v1.Account): Ref<mastodon.v1.R
|
|||
|
||||
async function fetchRelationships() {
|
||||
const requested = Array.from(requestedRelationships.entries()).filter(([, r]) => !r.value)
|
||||
const relationships = await useMastoClient().v1.accounts.fetchRelationships(requested.map(([id]) => id))
|
||||
const relationships = await useMastoClient().v1.accounts.relationships.fetch({ id: requested.map(([id]) => id) })
|
||||
for (let i = 0; i < requested.length; i++)
|
||||
requested[i][1].value = relationships[i]
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ export async function toggleFollowAccount(relationship: mastodon.v1.Relationship
|
|||
relationship!.following = true
|
||||
}
|
||||
|
||||
relationship = await client.v1.accounts[unfollow ? 'unfollow' : 'follow'](account.id)
|
||||
relationship = await client.v1.accounts.$select(account.id)[unfollow ? 'unfollow' : 'follow']()
|
||||
}
|
||||
|
||||
export async function toggleMuteAccount(relationship: mastodon.v1.Relationship, account: mastodon.v1.Account) {
|
||||
|
@ -74,10 +74,10 @@ export async function toggleMuteAccount(relationship: mastodon.v1.Relationship,
|
|||
|
||||
relationship!.muting = !relationship!.muting
|
||||
relationship = relationship!.muting
|
||||
? await client.v1.accounts.mute(account.id, {
|
||||
? await client.v1.accounts.$select(account.id).mute({
|
||||
// TODO support more options
|
||||
})
|
||||
: await client.v1.accounts.unmute(account.id)
|
||||
: await client.v1.accounts.$select(account.id).unmute()
|
||||
}
|
||||
|
||||
export async function toggleBlockAccount(relationship: mastodon.v1.Relationship, account: mastodon.v1.Account) {
|
||||
|
@ -92,7 +92,7 @@ export async function toggleBlockAccount(relationship: mastodon.v1.Relationship,
|
|||
return
|
||||
|
||||
relationship!.blocking = !relationship!.blocking
|
||||
relationship = await client.v1.accounts[relationship!.blocking ? 'block' : 'unblock'](account.id)
|
||||
relationship = await client.v1.accounts.$select(account.id)[relationship!.blocking ? 'block' : 'unblock']()
|
||||
}
|
||||
|
||||
export async function toggleBlockDomain(relationship: mastodon.v1.Relationship, account: mastodon.v1.Account) {
|
||||
|
@ -107,5 +107,5 @@ export async function toggleBlockDomain(relationship: mastodon.v1.Relationship,
|
|||
return
|
||||
|
||||
relationship!.domainBlocking = !relationship!.domainBlocking
|
||||
await client.v1.domainBlocks[relationship!.domainBlocking ? 'block' : 'unblock'](getServerName(account))
|
||||
await client.v1.domainBlocks[relationship!.domainBlocking ? 'create' : 'remove']({ domain: getServerName(account) })
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import type { MaybeRefOrGetter } from '@vueuse/core'
|
||||
import type { Paginator, mastodon } from 'masto'
|
||||
import type { mastodon } from 'masto'
|
||||
import type { RouteLocation } from 'vue-router'
|
||||
|
||||
export type UseSearchOptions = MaybeRefOrGetter<
|
||||
Partial<Omit<mastodon.v1.SearchParams, keyof mastodon.DefaultPaginationParams | 'q'>>
|
||||
Partial<Omit<mastodon.rest.v2.SearchParams, keyof mastodon.DefaultPaginationParams | 'q'>>
|
||||
>
|
||||
|
||||
export interface BuildSearchResult<K extends keyof any, T> {
|
||||
|
@ -30,7 +30,7 @@ export function useSearch(query: MaybeRefOrGetter<string>, options: UseSearchOpt
|
|||
|
||||
const q = $computed(() => resolveUnref(query).trim())
|
||||
|
||||
let paginator: Paginator<mastodon.v2.Search, mastodon.v2.SearchParams> | undefined
|
||||
let paginator: mastodon.Paginator<mastodon.v2.Search, mastodon.rest.v2.SearchParams> | undefined
|
||||
|
||||
const appendResults = (results: mastodon.v2.Search, empty = false) => {
|
||||
if (empty) {
|
||||
|
@ -72,7 +72,7 @@ export function useSearch(query: MaybeRefOrGetter<string>, options: UseSearchOpt
|
|||
* Based on the source it seems like modifying the params when calling next would result in a new search,
|
||||
* but that doesn't seem to be the case. So instead we just create a new paginator with the new params.
|
||||
*/
|
||||
paginator = client.v2.search({
|
||||
paginator = client.v2.search.list({
|
||||
q,
|
||||
...resolveUnref(options),
|
||||
resolve: !!currentUser.value,
|
||||
|
|
|
@ -61,7 +61,7 @@ export function useStatusActions(props: StatusActionsProps) {
|
|||
|
||||
const toggleReblog = () => toggleStatusAction(
|
||||
'reblogged',
|
||||
() => client.v1.statuses[status.reblogged ? 'unreblog' : 'reblog'](status.id).then((res) => {
|
||||
() => client.v1.statuses.$select(status.id)[status.reblogged ? 'unreblog' : 'reblog']().then((res) => {
|
||||
if (status.reblogged)
|
||||
// returns the original status
|
||||
return res.reblog!
|
||||
|
@ -72,23 +72,23 @@ export function useStatusActions(props: StatusActionsProps) {
|
|||
|
||||
const toggleFavourite = () => toggleStatusAction(
|
||||
'favourited',
|
||||
() => client.v1.statuses[status.favourited ? 'unfavourite' : 'favourite'](status.id),
|
||||
() => client.v1.statuses.$select(status.id)[status.favourited ? 'unfavourite' : 'favourite'](),
|
||||
'favouritesCount',
|
||||
)
|
||||
|
||||
const toggleBookmark = () => toggleStatusAction(
|
||||
'bookmarked',
|
||||
() => client.v1.statuses[status.bookmarked ? 'unbookmark' : 'bookmark'](status.id),
|
||||
() => client.v1.statuses.$select(status.id)[status.bookmarked ? 'unbookmark' : 'bookmark'](),
|
||||
)
|
||||
|
||||
const togglePin = async () => toggleStatusAction(
|
||||
'pinned',
|
||||
() => client.v1.statuses[status.pinned ? 'unpin' : 'pin'](status.id),
|
||||
() => client.v1.statuses.$select(status.id)[status.pinned ? 'unpin' : 'pin'](),
|
||||
)
|
||||
|
||||
const toggleMute = async () => toggleStatusAction(
|
||||
'muted',
|
||||
() => client.v1.statuses[status.muted ? 'unmute' : 'mute'](status.id),
|
||||
() => client.v1.statuses.$select(status.id)[status.muted ? 'unmute' : 'mute'](),
|
||||
)
|
||||
|
||||
return {
|
||||
|
|
|
@ -25,7 +25,7 @@ function getDefaultVisibility(currentVisibility: mastodon.v1.StatusVisibility) {
|
|||
: preferredVisibility
|
||||
}
|
||||
|
||||
export function getDefaultDraft(options: Partial<Mutable<mastodon.v1.CreateStatusParams> & Omit<Draft, 'params'>> = {}): Draft {
|
||||
export function getDefaultDraft(options: Partial<Mutable<mastodon.rest.v1.CreateStatusParams> & Omit<Draft, 'params'>> = {}): Draft {
|
||||
const {
|
||||
attachments = [],
|
||||
initialText = '',
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
import type { Paginator, WsEvents, mastodon } from 'masto'
|
||||
import type { mastodon } from 'masto'
|
||||
import type { Ref } from 'vue'
|
||||
import type { PaginatorState } from '~/types'
|
||||
|
||||
export function usePaginator<T, P, U = T>(
|
||||
_paginator: Paginator<T[], P>,
|
||||
stream: Ref<Promise<WsEvents> | undefined>,
|
||||
_paginator: mastodon.Paginator<T[], P>,
|
||||
stream: Ref<mastodon.streaming.Subscription | undefined>,
|
||||
eventType: 'notification' | 'update' = 'update',
|
||||
preprocess: (items: (T | U)[]) => U[] = items => items as unknown as U[],
|
||||
buffer = 10,
|
||||
|
@ -30,10 +30,15 @@ export function usePaginator<T, P, U = T>(
|
|||
prevItems.value = []
|
||||
}
|
||||
|
||||
watch(stream, (stream) => {
|
||||
stream?.then((s) => {
|
||||
s.on(eventType, (status) => {
|
||||
if ('uri' in status)
|
||||
watch(stream, async (stream) => {
|
||||
if (!stream)
|
||||
return
|
||||
|
||||
for await (const entry of stream) {
|
||||
if (entry.event === 'update') {
|
||||
const status = entry.payload
|
||||
|
||||
if ('uri' in entry)
|
||||
cacheStatus(status, undefined, true)
|
||||
|
||||
const index = prevItems.value.findIndex((i: any) => i.id === status.id)
|
||||
|
@ -41,27 +46,27 @@ export function usePaginator<T, P, U = T>(
|
|||
prevItems.value.splice(index, 1)
|
||||
|
||||
prevItems.value.unshift(status as any)
|
||||
})
|
||||
|
||||
// TODO: update statuses
|
||||
s.on('status.update', (status) => {
|
||||
}
|
||||
else if (entry.event === 'status.update') {
|
||||
const status = entry.payload
|
||||
cacheStatus(status, undefined, true)
|
||||
|
||||
const data = items.value as mastodon.v1.Status[]
|
||||
const index = data.findIndex(s => s.id === status.id)
|
||||
if (index >= 0)
|
||||
data[index] = status
|
||||
})
|
||||
}
|
||||
|
||||
s.on('delete', (id) => {
|
||||
else if (entry.event === 'delete') {
|
||||
const id = entry.payload
|
||||
removeCachedStatus(id)
|
||||
|
||||
const data = items.value as mastodon.v1.Status[]
|
||||
const index = data.findIndex(s => s.id === id)
|
||||
if (index >= 0)
|
||||
data.splice(index, 1)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
}, { immediate: true })
|
||||
|
||||
async function loadNext() {
|
||||
|
|
|
@ -8,7 +8,7 @@ import { PushSubscriptionError } from '~/composables/push-notifications/types'
|
|||
|
||||
export async function createPushSubscription(user: RequiredUserLogin,
|
||||
notificationData: CreatePushNotification,
|
||||
policy: mastodon.v1.SubscriptionPolicy = 'all',
|
||||
policy: mastodon.v1.WebPushSubscriptionPolicy = 'all',
|
||||
force = false): Promise<mastodon.v1.WebPushSubscription | undefined> {
|
||||
const { server: serverEndpoint, vapidKey } = user
|
||||
|
||||
|
@ -115,10 +115,10 @@ async function removePushNotificationDataOnError(e: Error) {
|
|||
async function sendSubscriptionToBackend(
|
||||
subscription: PushSubscription,
|
||||
data: CreatePushNotification,
|
||||
policy: mastodon.v1.SubscriptionPolicy,
|
||||
policy: mastodon.v1.WebPushSubscriptionPolicy,
|
||||
): Promise<mastodon.v1.WebPushSubscription> {
|
||||
const { endpoint, keys } = subscription.toJSON()
|
||||
const params: mastodon.v1.CreateWebPushSubscriptionParams = {
|
||||
return await useMastoClient().v1.push.subscription.create({
|
||||
policy,
|
||||
subscription: {
|
||||
endpoint: endpoint!,
|
||||
|
@ -128,7 +128,5 @@ async function sendSubscriptionToBackend(
|
|||
},
|
||||
},
|
||||
data,
|
||||
}
|
||||
|
||||
return await useMastoClient().v1.webPushSubscriptions.create(params)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -14,11 +14,11 @@ export interface RequiredUserLogin extends Required<Omit<UserLogin, 'account' |
|
|||
|
||||
export interface CreatePushNotification {
|
||||
alerts?: Partial<mastodon.v1.WebPushSubscriptionAlerts> | null
|
||||
policy?: mastodon.v1.SubscriptionPolicy
|
||||
policy?: mastodon.v1.WebPushSubscriptionPolicy
|
||||
}
|
||||
|
||||
export type PushNotificationRequest = Record<string, boolean>
|
||||
export type PushNotificationPolicy = Record<string, mastodon.v1.SubscriptionPolicy>
|
||||
export type PushNotificationPolicy = Record<string, mastodon.v1.WebPushSubscriptionPolicy>
|
||||
|
||||
export interface CustomEmojisInfo {
|
||||
lastUpdate: number
|
||||
|
|
|
@ -61,7 +61,7 @@ export function usePushManager() {
|
|||
|
||||
const subscribe = async (
|
||||
notificationData?: CreatePushNotification,
|
||||
policy?: mastodon.v1.SubscriptionPolicy,
|
||||
policy?: mastodon.v1.WebPushSubscriptionPolicy,
|
||||
force?: boolean,
|
||||
): Promise<SubscriptionResult> => {
|
||||
if (!isSupported)
|
||||
|
@ -116,7 +116,7 @@ export function usePushManager() {
|
|||
await removePushNotificationData(currentUser.value)
|
||||
}
|
||||
|
||||
const saveSettings = async (policy?: mastodon.v1.SubscriptionPolicy) => {
|
||||
const saveSettings = async (policy?: mastodon.v1.WebPushSubscriptionPolicy) => {
|
||||
if (policy)
|
||||
pushNotificationData.value.policy = policy
|
||||
|
||||
|
@ -173,7 +173,7 @@ export function usePushManager() {
|
|||
if (policyChanged)
|
||||
await subscribe(data, policy, true)
|
||||
else
|
||||
currentUser.value.pushSubscription = await client.v1.webPushSubscriptions.update({ data })
|
||||
currentUser.value.pushSubscription = await client.v1.push.subscription.update({ data })
|
||||
|
||||
policyChanged && await nextTick()
|
||||
|
||||
|
@ -198,7 +198,7 @@ export function usePushManager() {
|
|||
|
||||
function createRawSettings(
|
||||
pushSubscription?: mastodon.v1.WebPushSubscription,
|
||||
subscriptionPolicy?: mastodon.v1.SubscriptionPolicy,
|
||||
subscriptionPolicy?: mastodon.v1.WebPushSubscriptionPolicy,
|
||||
) {
|
||||
return {
|
||||
follow: pushSubscription?.alerts.follow ?? true,
|
||||
|
|
|
@ -27,8 +27,8 @@ export const TiptapMentionSuggestion: Partial<SuggestionOptions> = process.serve
|
|||
if (query.length === 0)
|
||||
return []
|
||||
|
||||
const results = await useMastoClient().v2.search({ q: query, type: 'accounts', limit: 25, resolve: true })
|
||||
return results.accounts
|
||||
const paginator = useMastoClient().v2.search.list({ q: query, type: 'accounts', limit: 25, resolve: true })
|
||||
return (await paginator.next()).value?.accounts ?? []
|
||||
},
|
||||
render: createSuggestionRenderer(TiptapMentionList),
|
||||
}
|
||||
|
@ -40,14 +40,14 @@ export const TiptapHashtagSuggestion: Partial<SuggestionOptions> = {
|
|||
if (query.length === 0)
|
||||
return []
|
||||
|
||||
const results = await useMastoClient().v2.search({
|
||||
const paginator = useMastoClient().v2.search.list({
|
||||
q: query,
|
||||
type: 'hashtags',
|
||||
limit: 25,
|
||||
resolve: false,
|
||||
excludeUnreviewed: true,
|
||||
})
|
||||
return results.hashtags
|
||||
return (await paginator.next()).value?.hashtags ?? []
|
||||
},
|
||||
render: createSuggestionRenderer(TiptapHashtagList),
|
||||
}
|
||||
|
|
|
@ -149,7 +149,7 @@ export async function loginTo(masto: ElkMasto, user: Overwrite<UserLogin, { acco
|
|||
// if PWA is not enabled, don't get push subscription
|
||||
useAppConfig().pwaEnabled
|
||||
// we get 404 response instead empty data
|
||||
? client.v1.webPushSubscriptions.fetch().catch(() => Promise.resolve(undefined))
|
||||
? client.v1.push.subscription.fetch().catch(() => Promise.resolve(undefined))
|
||||
: Promise.resolve(undefined),
|
||||
])
|
||||
|
||||
|
@ -172,6 +172,7 @@ export async function loginTo(masto: ElkMasto, user: Overwrite<UserLogin, { acco
|
|||
const accountPreferencesMap = new Map<string, Partial<mastodon.v1.Preference>>()
|
||||
|
||||
/**
|
||||
* @param account
|
||||
* @returns `true` when user ticked the preference to always expand posts with content warnings
|
||||
*/
|
||||
export function getExpandSpoilersByDefault(account: mastodon.v1.AccountCredentials) {
|
||||
|
@ -179,6 +180,7 @@ export function getExpandSpoilersByDefault(account: mastodon.v1.AccountCredentia
|
|||
}
|
||||
|
||||
/**
|
||||
* @param account
|
||||
* @returns `true` when user selected "Always show media" as Media Display preference
|
||||
*/
|
||||
export function getExpandMediaByDefault(account: mastodon.v1.AccountCredentials) {
|
||||
|
@ -186,13 +188,14 @@ export function getExpandMediaByDefault(account: mastodon.v1.AccountCredentials)
|
|||
}
|
||||
|
||||
/**
|
||||
* @param account
|
||||
* @returns `true` when user selected "Always hide media" as Media Display preference
|
||||
*/
|
||||
export function getHideMediaByDefault(account: mastodon.v1.AccountCredentials) {
|
||||
return accountPreferencesMap.get(account.acct)?.['reading:expand:media'] === 'hide_all' ?? false
|
||||
}
|
||||
|
||||
export async function fetchAccountInfo(client: mastodon.Client, server: string) {
|
||||
export async function fetchAccountInfo(client: mastodon.rest.Client, server: string) {
|
||||
// Try to fetch user preferences if the backend supports it.
|
||||
const fetchPrefs = async (): Promise<Partial<mastodon.v1.Preference>> => {
|
||||
try {
|
||||
|
@ -267,7 +270,7 @@ export async function removePushNotifications(user: UserLogin) {
|
|||
return
|
||||
|
||||
// unsubscribe push notifications
|
||||
await useMastoClient().v1.webPushSubscriptions.remove().catch(() => Promise.resolve())
|
||||
await useMastoClient().v1.push.subscription.remove().catch(() => Promise.resolve())
|
||||
}
|
||||
|
||||
export async function switchUser(user: UserLogin) {
|
||||
|
@ -336,6 +339,8 @@ interface UseUserLocalStorageCache {
|
|||
|
||||
/**
|
||||
* Create reactive storage for the current user
|
||||
* @param key
|
||||
* @param initial
|
||||
*/
|
||||
export function useUserLocalStorage<T extends object>(key: string, initial: () => T): Ref<T> {
|
||||
if (process.server || process.test)
|
||||
|
@ -382,6 +387,7 @@ export function useUserLocalStorage<T extends object>(key: string, initial: () =
|
|||
|
||||
/**
|
||||
* Clear all storages for the given account
|
||||
* @param account
|
||||
*/
|
||||
export function clearUserLocalStorage(account?: mastodon.v1.Account) {
|
||||
if (!account)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue