2021-01-11 21:36:57 +01:00
|
|
|
import {
|
2023-01-02 02:08:12 +01:00
|
|
|
QueryFunctionContext,
|
|
|
|
useMutation,
|
|
|
|
UseMutationOptions,
|
|
|
|
useQuery,
|
|
|
|
UseQueryOptions
|
2022-12-10 14:47:19 +01:00
|
|
|
} from '@tanstack/react-query'
|
2022-12-28 23:41:36 +01:00
|
|
|
import apiInstance from '@utils/api/instance'
|
|
|
|
import { AxiosError } from 'axios'
|
2021-01-07 19:13:09 +01:00
|
|
|
|
2023-01-02 02:08:12 +01:00
|
|
|
export type QueryKeyRelationship = ['Relationship', { id?: Mastodon.Account['id'] }]
|
2021-01-07 19:13:09 +01:00
|
|
|
|
2022-12-18 01:12:58 +01:00
|
|
|
const queryFunction = async ({ queryKey }: QueryFunctionContext<QueryKeyRelationship>) => {
|
2021-01-07 19:13:09 +01:00
|
|
|
const { id } = queryKey[1]
|
2023-01-02 02:08:12 +01:00
|
|
|
if (!id) return Promise.reject()
|
2021-01-07 19:13:09 +01:00
|
|
|
|
2022-12-10 14:47:19 +01:00
|
|
|
const res = await apiInstance<Mastodon.Relationship[]>({
|
2021-01-07 19:13:09 +01:00
|
|
|
method: 'get',
|
|
|
|
url: `accounts/relationships`,
|
|
|
|
params: {
|
|
|
|
'id[]': id
|
|
|
|
}
|
2022-12-10 14:47:19 +01:00
|
|
|
})
|
|
|
|
return res.body
|
2021-01-07 19:13:09 +01:00
|
|
|
}
|
|
|
|
|
2021-01-11 21:36:57 +01:00
|
|
|
const useRelationshipQuery = ({
|
2021-01-07 19:13:09 +01:00
|
|
|
options,
|
|
|
|
...queryKeyParams
|
2021-01-10 02:12:14 +01:00
|
|
|
}: QueryKeyRelationship[1] & {
|
2022-12-18 01:12:58 +01:00
|
|
|
options?: UseQueryOptions<Mastodon.Relationship[], AxiosError, Mastodon.Relationship>
|
2021-01-07 19:13:09 +01:00
|
|
|
}) => {
|
2021-01-10 02:12:14 +01:00
|
|
|
const queryKey: QueryKeyRelationship = ['Relationship', { ...queryKeyParams }]
|
|
|
|
return useQuery(queryKey, queryFunction, {
|
|
|
|
...options,
|
2023-01-02 02:08:12 +01:00
|
|
|
enabled:
|
|
|
|
(typeof options?.enabled === 'boolean' ? options.enabled : true) && !!queryKeyParams.id,
|
2021-01-10 02:12:14 +01:00
|
|
|
select: data => data[0]
|
|
|
|
})
|
2021-01-07 19:13:09 +01:00
|
|
|
}
|
|
|
|
|
2021-01-11 21:36:57 +01:00
|
|
|
type MutationVarsRelationship =
|
|
|
|
| {
|
|
|
|
id: Mastodon.Account['id']
|
|
|
|
type: 'incoming'
|
|
|
|
payload: { action: 'authorize' | 'reject' }
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
id: Mastodon.Account['id']
|
|
|
|
type: 'outgoing'
|
2022-12-18 01:12:58 +01:00
|
|
|
payload: {
|
|
|
|
action: 'block'
|
|
|
|
state: boolean
|
2022-12-23 18:49:50 +01:00
|
|
|
reblogs?: undefined
|
2022-12-18 01:12:58 +01:00
|
|
|
notify?: undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
id: Mastodon.Account['id']
|
|
|
|
type: 'outgoing'
|
|
|
|
payload: {
|
|
|
|
action: 'follow'
|
|
|
|
state: boolean
|
2022-12-23 18:49:50 +01:00
|
|
|
reblogs?: boolean
|
2022-12-18 01:12:58 +01:00
|
|
|
notify?: boolean
|
|
|
|
}
|
2021-01-11 21:36:57 +01:00
|
|
|
}
|
2023-02-12 19:47:28 +01:00
|
|
|
| {
|
|
|
|
id: Mastodon.Account['id']
|
|
|
|
type: 'note'
|
|
|
|
payload: Mastodon.Relationship['note']
|
|
|
|
}
|
2021-01-11 21:36:57 +01:00
|
|
|
|
|
|
|
const mutationFunction = async (params: MutationVarsRelationship) => {
|
|
|
|
switch (params.type) {
|
|
|
|
case 'incoming':
|
2021-02-20 19:12:44 +01:00
|
|
|
return apiInstance<Mastodon.Relationship>({
|
2021-01-11 21:36:57 +01:00
|
|
|
method: 'post',
|
|
|
|
url: `follow_requests/${params.id}/${params.payload.action}`
|
2021-02-11 01:33:31 +01:00
|
|
|
}).then(res => res.body)
|
2021-01-11 21:36:57 +01:00
|
|
|
case 'outgoing':
|
2023-01-29 00:37:56 +01:00
|
|
|
const body: { reblogs?: boolean; notify?: boolean } = {}
|
|
|
|
typeof params.payload.reblogs === 'boolean' && (body.reblogs = params.payload.reblogs)
|
|
|
|
typeof params.payload.notify === 'boolean' && (body.notify = params.payload.notify)
|
2022-12-18 01:12:58 +01:00
|
|
|
|
2021-02-20 19:12:44 +01:00
|
|
|
return apiInstance<Mastodon.Relationship>({
|
2021-01-11 21:36:57 +01:00
|
|
|
method: 'post',
|
2022-12-18 01:12:58 +01:00
|
|
|
url: `accounts/${params.id}/${params.payload.state ? 'un' : ''}${params.payload.action}`,
|
2023-01-29 00:37:56 +01:00
|
|
|
body
|
2021-02-11 01:33:31 +01:00
|
|
|
}).then(res => res.body)
|
2023-02-12 19:47:28 +01:00
|
|
|
case 'note':
|
|
|
|
return apiInstance<Mastodon.Relationship>({
|
|
|
|
method: 'post',
|
|
|
|
url: `accounts/${params.id}/note`,
|
|
|
|
body: { comment: params.payload }
|
|
|
|
}).then(res => res.body)
|
2021-01-11 21:36:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const useRelationshipMutation = (
|
2023-02-12 19:47:28 +01:00
|
|
|
options?: UseMutationOptions<Mastodon.Relationship, AxiosError, MutationVarsRelationship>
|
2021-01-11 21:36:57 +01:00
|
|
|
) => {
|
|
|
|
return useMutation(mutationFunction, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
export { useRelationshipQuery, useRelationshipMutation }
|