mirror of
https://github.com/tooot-app/app
synced 2025-06-05 22:19:13 +02:00
Fix searching for remote accounts
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import apiInstance, { InstanceResponse } from '@api/instance'
|
||||
import apiInstance from '@api/instance'
|
||||
import { AxiosError } from 'axios'
|
||||
import {
|
||||
QueryFunctionContext,
|
||||
@ -9,6 +9,7 @@ import {
|
||||
useQuery,
|
||||
UseQueryOptions
|
||||
} from '@tanstack/react-query'
|
||||
import { PagedResponse } from '@api/helpers'
|
||||
|
||||
export type QueryKeyLists = ['Lists']
|
||||
|
||||
@ -97,7 +98,7 @@ const useListAccountsQuery = ({
|
||||
options,
|
||||
...queryKeyParams
|
||||
}: QueryKeyListAccounts[1] & {
|
||||
options?: UseInfiniteQueryOptions<InstanceResponse<Mastodon.Account[]>, AxiosError>
|
||||
options?: UseInfiniteQueryOptions<PagedResponse<Mastodon.Account[]>, AxiosError>
|
||||
}) => {
|
||||
const queryKey: QueryKeyListAccounts = ['ListAccounts', queryKeyParams]
|
||||
return useInfiniteQuery(queryKey, accountsQueryFunction, options)
|
||||
|
@ -17,17 +17,18 @@ export type SearchResult = {
|
||||
statuses: Mastodon.Status[]
|
||||
}
|
||||
|
||||
const queryFunction = async ({
|
||||
queryKey
|
||||
}: QueryFunctionContext<QueryKeySearch>) => {
|
||||
const queryFunction = async ({ queryKey }: QueryFunctionContext<QueryKeySearch>) => {
|
||||
const { type, term, limit = 20 } = queryKey[1]
|
||||
if (!term?.length) {
|
||||
return Promise.reject()
|
||||
}
|
||||
const res = await apiInstance<SearchResult>({
|
||||
version: 'v2',
|
||||
method: 'get',
|
||||
url: 'search',
|
||||
params: {
|
||||
q: term,
|
||||
...(type && { type }),
|
||||
...(term && { q: term }),
|
||||
limit,
|
||||
resolve: true
|
||||
}
|
||||
@ -35,7 +36,7 @@ const queryFunction = async ({
|
||||
return res.body
|
||||
}
|
||||
|
||||
const useSearchQuery = <T = unknown>({
|
||||
const useSearchQuery = <T = SearchResult>({
|
||||
options,
|
||||
...queryKeyParams
|
||||
}: QueryKeySearch[1] & {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import apiInstance, { InstanceResponse } from '@api/instance'
|
||||
import apiInstance from '@api/instance'
|
||||
import { AxiosError } from 'axios'
|
||||
import {
|
||||
QueryFunctionContext,
|
||||
@ -10,12 +10,13 @@ import {
|
||||
UseQueryOptions
|
||||
} from '@tanstack/react-query'
|
||||
import { infinitePageParams } from './utils'
|
||||
import { PagedResponse } from '@api/helpers'
|
||||
|
||||
export type QueryKeyFollowedTags = ['FollowedTags']
|
||||
const useFollowedTagsQuery = (
|
||||
params: {
|
||||
options?: Omit<
|
||||
UseInfiniteQueryOptions<InstanceResponse<Mastodon.Tag[]>, AxiosError>,
|
||||
UseInfiniteQueryOptions<PagedResponse<Mastodon.Tag[]>, AxiosError>,
|
||||
'getPreviousPageParam' | 'getNextPageParam'
|
||||
>
|
||||
} | void
|
||||
|
@ -1,4 +1,4 @@
|
||||
import apiInstance, { InstanceResponse } from '@api/instance'
|
||||
import apiInstance from '@api/instance'
|
||||
import haptics from '@components/haptics'
|
||||
import queryClient from '@helpers/queryClient'
|
||||
import { store } from '@root/store'
|
||||
@ -15,6 +15,7 @@ import {
|
||||
import deleteItem from './timeline/deleteItem'
|
||||
import editItem from './timeline/editItem'
|
||||
import updateStatusProperty from './timeline/updateStatusProperty'
|
||||
import { PagedResponse } from '@api/helpers'
|
||||
|
||||
export type QueryKeyTimeline = [
|
||||
'Timeline',
|
||||
@ -240,7 +241,7 @@ const useTimelineQuery = ({
|
||||
options,
|
||||
...queryKeyParams
|
||||
}: QueryKeyTimeline[1] & {
|
||||
options?: UseInfiniteQueryOptions<InstanceResponse<Mastodon.Status[]>, AxiosError>
|
||||
options?: UseInfiniteQueryOptions<PagedResponse<Mastodon.Status[]>, AxiosError>
|
||||
}) => {
|
||||
const queryKey: QueryKeyTimeline = ['Timeline', { ...queryKeyParams }]
|
||||
return useInfiniteQuery(queryKey, queryFunction, {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import apiInstance, { InstanceResponse } from '@api/instance'
|
||||
import apiInstance from '@api/instance'
|
||||
import { TabSharedStackParamList } from '@utils/navigation/navigators'
|
||||
import { AxiosError } from 'axios'
|
||||
import {
|
||||
@ -7,10 +7,15 @@ import {
|
||||
UseInfiniteQueryOptions
|
||||
} from '@tanstack/react-query'
|
||||
import apiGeneral from '@api/general'
|
||||
import { PagedResponse } from '@api/helpers'
|
||||
|
||||
export type QueryKeyUsers = ['Users', TabSharedStackParamList['Tab-Shared-Users']]
|
||||
|
||||
const queryFunction = ({ queryKey, pageParam }: QueryFunctionContext<QueryKeyUsers>) => {
|
||||
const queryFunction = async ({
|
||||
queryKey,
|
||||
pageParam,
|
||||
meta
|
||||
}: QueryFunctionContext<QueryKeyUsers>) => {
|
||||
const page = queryKey[1]
|
||||
let params: { [key: string]: string } = { ...pageParam }
|
||||
|
||||
@ -20,7 +25,7 @@ const queryFunction = ({ queryKey, pageParam }: QueryFunctionContext<QueryKeyUse
|
||||
method: 'get',
|
||||
url: `${page.reference}/${page.status.id}/${page.type}`,
|
||||
params
|
||||
}).then(res => ({ ...res, warnIncomplete: false }))
|
||||
})
|
||||
case 'accounts':
|
||||
const localInstance = page.account.username === page.account.acct
|
||||
if (localInstance) {
|
||||
@ -28,59 +33,47 @@ const queryFunction = ({ queryKey, pageParam }: QueryFunctionContext<QueryKeyUse
|
||||
method: 'get',
|
||||
url: `${page.reference}/${page.account.id}/${page.type}`,
|
||||
params
|
||||
}).then(res => ({ ...res, warnIncomplete: false }))
|
||||
})
|
||||
} else {
|
||||
const domain = page.account.url.match(
|
||||
/^(?:https?:\/\/)?(?:[^@\/\n]+@)?(?:www\.)?([^:\/\n]+)/i
|
||||
)?.[1]
|
||||
if (!domain) {
|
||||
return apiInstance<Mastodon.Account[]>({
|
||||
let res: PagedResponse<Mastodon.Account[]>
|
||||
|
||||
try {
|
||||
const domain = page.account.url.match(
|
||||
/^(?:https?:\/\/)?(?:[^@\/\n]+@)?(?:www\.)?([^:\/\n]+)/i
|
||||
)?.[1]
|
||||
if (!domain?.length) {
|
||||
throw new Error()
|
||||
}
|
||||
|
||||
const resSearch = await apiGeneral<{ accounts: Mastodon.Account[] }>({
|
||||
method: 'get',
|
||||
domain,
|
||||
url: 'api/v2/search',
|
||||
params: {
|
||||
q: `@${page.account.acct}`,
|
||||
type: 'accounts',
|
||||
limit: '1'
|
||||
}
|
||||
})
|
||||
if (resSearch?.body?.accounts?.length === 1) {
|
||||
res = await apiGeneral<Mastodon.Account[]>({
|
||||
method: 'get',
|
||||
domain,
|
||||
url: `api/v1/${page.reference}/${resSearch.body.accounts[0].id}/${page.type}`,
|
||||
params
|
||||
})
|
||||
return { ...res, remoteData: true }
|
||||
} else {
|
||||
throw new Error()
|
||||
}
|
||||
} catch {
|
||||
res = await apiInstance<Mastodon.Account[]>({
|
||||
method: 'get',
|
||||
url: `${page.reference}/${page.account.id}/${page.type}`,
|
||||
params
|
||||
}).then(res => ({ ...res, warnIncomplete: true }))
|
||||
})
|
||||
return { ...res, warnIncomplete: true }
|
||||
}
|
||||
return apiGeneral<{ accounts: Mastodon.Account[] }>({
|
||||
method: 'get',
|
||||
domain,
|
||||
url: 'api/v2/search',
|
||||
params: {
|
||||
q: `@${page.account.acct}`,
|
||||
type: 'accounts',
|
||||
limit: '1'
|
||||
}
|
||||
})
|
||||
.then(res => {
|
||||
if (res?.body?.accounts?.length === 1) {
|
||||
return apiGeneral<Mastodon.Account[]>({
|
||||
method: 'get',
|
||||
domain,
|
||||
url: `api/v1/${page.reference}/${res.body.accounts[0].id}/${page.type}`,
|
||||
params
|
||||
})
|
||||
.catch(() => {
|
||||
return apiInstance<Mastodon.Account[]>({
|
||||
method: 'get',
|
||||
url: `${page.reference}/${page.account.id}/${page.type}`,
|
||||
params
|
||||
}).then(res => ({ ...res, warnIncomplete: true }))
|
||||
})
|
||||
.then(res => ({ ...res, warnIncomplete: false }))
|
||||
} else {
|
||||
return apiInstance<Mastodon.Account[]>({
|
||||
method: 'get',
|
||||
url: `${page.reference}/${page.account.id}/${page.type}`,
|
||||
params
|
||||
}).then(res => ({ ...res, warnIncomplete: true }))
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
return apiInstance<Mastodon.Account[]>({
|
||||
method: 'get',
|
||||
url: `${page.reference}/${page.account.id}/${page.type}`,
|
||||
params
|
||||
}).then(res => ({ ...res, warnIncomplete: true }))
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -90,7 +83,7 @@ const useUsersQuery = ({
|
||||
...queryKeyParams
|
||||
}: QueryKeyUsers[1] & {
|
||||
options?: UseInfiniteQueryOptions<
|
||||
InstanceResponse<Mastodon.Account[]> & { warnIncomplete: boolean },
|
||||
PagedResponse<Mastodon.Account[]> & { warnIncomplete: boolean; remoteData: boolean },
|
||||
AxiosError
|
||||
>
|
||||
}) => {
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { InstanceResponse } from '@api/instance'
|
||||
import { PagedResponse } from '@api/helpers'
|
||||
|
||||
export const infinitePageParams = {
|
||||
getPreviousPageParam: (firstPage: InstanceResponse<any>) =>
|
||||
getPreviousPageParam: (firstPage: PagedResponse<any>) =>
|
||||
firstPage.links?.prev && { min_id: firstPage.links.next },
|
||||
getNextPageParam: (lastPage: InstanceResponse<any>) =>
|
||||
getNextPageParam: (lastPage: PagedResponse<any>) =>
|
||||
lastPage.links?.next && { max_id: lastPage.links.next }
|
||||
}
|
||||
|
Reference in New Issue
Block a user