2023-07-21 10:48:47 -07:00
|
|
|
import { useTransaction } from 'soapbox/entity-store/hooks';
|
|
|
|
import { EntityCallbacks } from 'soapbox/entity-store/hooks/types';
|
|
|
|
import { useApi, useGetState } from 'soapbox/hooks';
|
2023-07-21 10:59:31 -07:00
|
|
|
import { accountIdsToAccts } from 'soapbox/selectors';
|
2023-07-21 10:48:47 -07:00
|
|
|
|
|
|
|
import type { Account } from 'soapbox/schemas';
|
|
|
|
|
|
|
|
function useVerify() {
|
|
|
|
const api = useApi();
|
|
|
|
const getState = useGetState();
|
|
|
|
const { transaction } = useTransaction();
|
|
|
|
|
2023-07-21 10:59:31 -07:00
|
|
|
function verifyEffect(accountIds: string[], verified: boolean) {
|
2023-07-21 10:48:47 -07:00
|
|
|
const updater = (account: Account): Account => {
|
|
|
|
if (account.pleroma) {
|
|
|
|
const tags = account.pleroma.tags.filter((tag) => tag !== 'verified');
|
|
|
|
if (verified) {
|
|
|
|
tags.push('verified');
|
|
|
|
}
|
|
|
|
account.pleroma.tags = tags;
|
|
|
|
}
|
|
|
|
account.verified = verified;
|
|
|
|
return account;
|
|
|
|
};
|
|
|
|
|
|
|
|
transaction({
|
2023-07-21 10:59:31 -07:00
|
|
|
Accounts: accountIds.reduce<Record<string, (account: Account) => Account>>(
|
2023-07-21 10:48:47 -07:00
|
|
|
(result, id) => ({ ...result, [id]: updater }),
|
|
|
|
{}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-21 10:59:31 -07:00
|
|
|
async function verify(accountIds: string[], callbacks?: EntityCallbacks<void, unknown>) {
|
|
|
|
const accts = accountIdsToAccts(getState(), accountIds);
|
|
|
|
verifyEffect(accountIds, true);
|
2023-07-21 10:48:47 -07:00
|
|
|
try {
|
|
|
|
await api.put('/api/v1/pleroma/admin/users/tag', { nicknames: accts, tags: ['verified'] });
|
|
|
|
callbacks?.onSuccess?.();
|
|
|
|
} catch (e) {
|
|
|
|
callbacks?.onError?.(e);
|
2023-07-21 10:59:31 -07:00
|
|
|
verifyEffect(accountIds, false);
|
2023-07-21 10:48:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-21 10:59:31 -07:00
|
|
|
async function unverify(accountIds: string[], callbacks?: EntityCallbacks<void, unknown>) {
|
|
|
|
const accts = accountIdsToAccts(getState(), accountIds);
|
|
|
|
verifyEffect(accountIds, false);
|
2023-07-21 10:48:47 -07:00
|
|
|
try {
|
|
|
|
await api.delete('/api/v1/pleroma/admin/users/tag', { data: { nicknames: accts, tags: ['verified'] } });
|
|
|
|
callbacks?.onSuccess?.();
|
|
|
|
} catch (e) {
|
|
|
|
callbacks?.onError?.(e);
|
2023-07-21 10:59:31 -07:00
|
|
|
verifyEffect(accountIds, true);
|
2023-07-21 10:48:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
verify,
|
|
|
|
unverify,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export { useVerify };
|