1
0
mirror of https://github.com/tooot-app/app synced 2025-06-05 22:19:13 +02:00

Fix toot action for #638

This commit is contained in:
xmflsct
2023-01-02 23:18:22 +01:00
parent 6dafbc96af
commit 4c6b8f0959
25 changed files with 298 additions and 71 deletions

View File

@ -3,21 +3,24 @@ import apiInstance from '@utils/api/instance'
import { AxiosError } from 'axios'
import { SearchResult } from './search'
export type QueryKeyAccount = ['Account', { id?: Mastodon.Account['id']; remoteUrl?: string }]
export type QueryKeyAccount = [
'Account',
Pick<Mastodon.Account, 'id' | 'url' | '_remote'> | undefined
]
const accountQueryFunction = async ({ queryKey }: QueryFunctionContext<QueryKeyAccount>) => {
const { id, remoteUrl } = queryKey[1]
if (!id && !remoteUrl) return Promise.reject()
const key = queryKey[1]
if (!key) return Promise.reject()
let matchedId = id
let matchedId = key.id
if (remoteUrl) {
if (key._remote) {
await apiInstance<SearchResult>({
version: 'v2',
method: 'get',
url: 'search',
params: {
q: remoteUrl,
q: key.url,
type: 'accounts',
limit: 1,
resolve: true
@ -25,7 +28,7 @@ const accountQueryFunction = async ({ queryKey }: QueryFunctionContext<QueryKeyA
})
.then(res => {
const account = res.body.accounts[0]
if (account.url !== remoteUrl) {
if (account.url !== key.url) {
return Promise.reject()
} else {
matchedId = account.id
@ -44,11 +47,23 @@ const accountQueryFunction = async ({ queryKey }: QueryFunctionContext<QueryKeyA
const useAccountQuery = ({
options,
...queryKeyParams
}: QueryKeyAccount[1] & {
}: { account?: QueryKeyAccount[1] } & {
options?: UseQueryOptions<Mastodon.Account, AxiosError>
}) => {
const queryKey: QueryKeyAccount = ['Account', { ...queryKeyParams }]
return useQuery(queryKey, accountQueryFunction, { ...options })
const queryKey: QueryKeyAccount = [
'Account',
queryKeyParams.account
? {
id: queryKeyParams.account.id,
url: queryKeyParams.account.url,
_remote: queryKeyParams.account._remote
}
: undefined
]
return useQuery(queryKey, accountQueryFunction, {
...options,
enabled: (queryKeyParams.account?._remote ? !!queryKeyParams.account : true) && options?.enabled
})
}
/* ----- */

View File

@ -18,4 +18,9 @@ const queryClient = new QueryClient({
}
})
// @ts-ignore
import('react-query-native-devtools').then(({ addPlugin }) => {
addPlugin({ queryClient })
})
export default queryClient

View File

@ -20,7 +20,7 @@ export type SearchResult = {
const queryFunction = async ({ queryKey }: QueryFunctionContext<QueryKeySearch>) => {
const { type, term, limit = 20 } = queryKey[1]
if (!term?.length) {
return Promise.reject()
return Promise.reject('Empty search term')
}
const res = await apiInstance<SearchResult>({
version: 'v2',
@ -59,6 +59,6 @@ export const searchFetchToot = (uri: Mastodon.Status['uri']): Promise<Mastodon.S
}
})
.then(res => res.body.statuses[0])
.catch(() => {})
.catch(err => console.warn(err))
export { useSearchQuery }

View File

@ -37,11 +37,11 @@ const useFollowedTagsQuery = (
})
},
{
enabled: canFollowTags,
staleTime: Infinity,
cacheTime: Infinity,
...params?.options,
...infinitePageParams,
enabled: canFollowTags && params?.options?.enabled,
onSuccess: data => {
setAccountStorage([
{

View File

@ -131,7 +131,7 @@ const queryFunction = async ({ queryKey, pageParam }: QueryFunctionContext<Query
})
case 'Account':
if (!page.id) return Promise.reject()
if (!page.id) return Promise.reject('Timeline query account id not provided')
if (page.exclude_reblogs) {
if (pageParam && pageParam.hasOwnProperty('max_id')) {
@ -214,7 +214,7 @@ const queryFunction = async ({ queryKey, pageParam }: QueryFunctionContext<Query
params
})
default:
return Promise.reject()
return Promise.reject('Timeline query no page matched')
}
}
@ -253,11 +253,17 @@ export type MutationVarsTimelineUpdateStatusProperty = {
status: Mastodon.Status
payload:
| {
type: 'bookmarked' | 'muted' | 'pinned' | 'favourited'
type: 'bookmarked' | 'muted' | 'pinned'
to: boolean
}
| {
type: 'favourited'
to: boolean
}
| {
type: 'reblogged'
visibility: 'public' | 'unlisted'
to: boolean
}
| {
type: 'poll'
@ -340,7 +346,7 @@ const mutationFunction = async (params: MutationVarsTimeline) => {
if (fetched) {
tootId = fetched.id
} else {
return Promise.reject()
return Promise.reject('Fetching for remote toot failed')
}
}
const body = new FormData()
@ -349,7 +355,7 @@ const mutationFunction = async (params: MutationVarsTimeline) => {
}
return apiInstance<Mastodon.Status>({
method: 'post',
url: `statuses/${tootId}/${params.status[params.payload.type] ? '' : 'un'}${
url: `statuses/${tootId}/${params.payload.to ? '' : 'un'}${
MapPropertyToUrl[params.payload.type]
}`,
...(params.payload.type === 'reblogged' && { body })

View File

@ -9,10 +9,11 @@ const updateStatusProperty = ({
payload,
poll
}: MutationVarsTimelineUpdateStatusProperty & { poll?: Mastodon.Poll }) => {
for (const key of [queryKey, rootQueryKey]) {
for (const key of [queryKey]) {
if (!key) continue
queryClient.setQueryData<InfiniteData<TimelineData> | undefined>(key, old => {
console.log('key', key)
if (old) {
let foundToot: Mastodon.Status | undefined = undefined
old.pages = old.pages.map(page => {
@ -25,7 +26,6 @@ const updateStatusProperty = ({
? last_status.reblog.id === status.id
: last_status?.id === status.id
)?.last_status
return page
} else if (typeof (page.body as Mastodon.Notification[])[0].type === 'string') {
foundToot = (page.body as Mastodon.Notification[]).find(no =>
no.status?.reblog ? no.status.reblog.id === status.id : no.status?.id === status.id
@ -41,6 +41,8 @@ const updateStatusProperty = ({
})
if (foundToot) {
const toot = foundToot as Mastodon.Status
console.log('updating', toot.id)
enum MapPropertyToCount {
favourited = 'favourites_count',
reblogged = 'reblogs_count'
@ -48,18 +50,19 @@ const updateStatusProperty = ({
switch (payload.type) {
case 'poll':
status.poll = poll
toot.poll = poll
break
default:
status[payload.type] =
typeof status[payload.type] === 'boolean' ? !status[payload.type] : true
console.log('11', toot[payload.type])
toot[payload.type] = payload.to
console.log('22', toot[payload.type])
switch (payload.type) {
case 'favourited':
case 'reblogged':
if (typeof status[payload.type] === 'boolean' && status[payload.type]) {
status[MapPropertyToCount[payload.type]]--
if (payload.to) {
toot[MapPropertyToCount[payload.type]]++
} else {
status[MapPropertyToCount[payload.type]]++
toot[MapPropertyToCount[payload.type]]--
}
break
}