suggest and verify by account IDs, simplify hooks
This commit is contained in:
parent
d0a97c8c52
commit
40af1d91a4
5 changed files with 34 additions and 60 deletions
|
@ -2,7 +2,7 @@ import { defineMessages } from 'react-intl';
|
|||
|
||||
import { fetchRelationships } from 'soapbox/actions/accounts';
|
||||
import { importFetchedAccount, importFetchedAccounts, importFetchedStatuses } from 'soapbox/actions/importer';
|
||||
import { selectAccount } from 'soapbox/selectors';
|
||||
import { accountIdsToAccts } from 'soapbox/selectors';
|
||||
import toast from 'soapbox/toast';
|
||||
import { filterBadges, getTagDiff } from 'soapbox/utils/badges';
|
||||
import { getFeatures } from 'soapbox/utils/features';
|
||||
|
@ -114,8 +114,6 @@ const messages = defineMessages({
|
|||
announcementUpdateSuccess: { id: 'admin.edit_announcement.updated', defaultMessage: 'Announcement edited' },
|
||||
});
|
||||
|
||||
const nicknamesFromIds = (getState: () => RootState, ids: string[]) => ids.map((id) => selectAccount(getState(), id)!.acct);
|
||||
|
||||
const fetchConfig = () =>
|
||||
(dispatch: AppDispatch, getState: () => RootState) => {
|
||||
dispatch({ type: ADMIN_CONFIG_FETCH_REQUEST });
|
||||
|
@ -322,7 +320,7 @@ const deactivateMastodonUsers = (accountIds: string[], reportId?: string) =>
|
|||
|
||||
const deactivatePleromaUsers = (accountIds: string[]) =>
|
||||
(dispatch: AppDispatch, getState: () => RootState) => {
|
||||
const nicknames = nicknamesFromIds(getState, accountIds);
|
||||
const nicknames = accountIdsToAccts(getState(), accountIds);
|
||||
return api(getState)
|
||||
.patch('/api/v1/pleroma/admin/users/deactivate', { nicknames })
|
||||
.then(({ data: { users } }) => {
|
||||
|
@ -350,7 +348,7 @@ const deactivateUsers = (accountIds: string[], reportId?: string) =>
|
|||
|
||||
const deleteUsers = (accountIds: string[]) =>
|
||||
(dispatch: AppDispatch, getState: () => RootState) => {
|
||||
const nicknames = nicknamesFromIds(getState, accountIds);
|
||||
const nicknames = accountIdsToAccts(getState(), accountIds);
|
||||
dispatch({ type: ADMIN_USERS_DELETE_REQUEST, accountIds });
|
||||
return api(getState)
|
||||
.delete('/api/v1/pleroma/admin/users', { data: { nicknames } })
|
||||
|
@ -375,7 +373,7 @@ const approveMastodonUsers = (accountIds: string[]) =>
|
|||
|
||||
const approvePleromaUsers = (accountIds: string[]) =>
|
||||
(dispatch: AppDispatch, getState: () => RootState) => {
|
||||
const nicknames = nicknamesFromIds(getState, accountIds);
|
||||
const nicknames = accountIdsToAccts(getState(), accountIds);
|
||||
return api(getState)
|
||||
.patch('/api/v1/pleroma/admin/users/approve', { nicknames })
|
||||
.then(({ data: { users } }) => {
|
||||
|
@ -440,7 +438,7 @@ const fetchModerationLog = (params?: Record<string, any>) =>
|
|||
|
||||
const tagUsers = (accountIds: string[], tags: string[]) =>
|
||||
(dispatch: AppDispatch, getState: () => RootState) => {
|
||||
const nicknames = nicknamesFromIds(getState, accountIds);
|
||||
const nicknames = accountIdsToAccts(getState(), accountIds);
|
||||
dispatch({ type: ADMIN_USERS_TAG_REQUEST, accountIds, tags });
|
||||
return api(getState)
|
||||
.put('/api/v1/pleroma/admin/users/tag', { nicknames, tags })
|
||||
|
@ -453,7 +451,7 @@ const tagUsers = (accountIds: string[], tags: string[]) =>
|
|||
|
||||
const untagUsers = (accountIds: string[], tags: string[]) =>
|
||||
(dispatch: AppDispatch, getState: () => RootState) => {
|
||||
const nicknames = nicknamesFromIds(getState, accountIds);
|
||||
const nicknames = accountIdsToAccts(getState(), accountIds);
|
||||
|
||||
// Legacy: allow removing legacy 'donor' tags.
|
||||
if (tags.includes('badge:donor')) {
|
||||
|
@ -490,7 +488,7 @@ const setBadges = (accountId: string, oldTags: string[], newTags: string[]) =>
|
|||
|
||||
const addPermission = (accountIds: string[], permissionGroup: string) =>
|
||||
(dispatch: AppDispatch, getState: () => RootState) => {
|
||||
const nicknames = nicknamesFromIds(getState, accountIds);
|
||||
const nicknames = accountIdsToAccts(getState(), accountIds);
|
||||
dispatch({ type: ADMIN_ADD_PERMISSION_GROUP_REQUEST, accountIds, permissionGroup });
|
||||
return api(getState)
|
||||
.post(`/api/v1/pleroma/admin/users/permission_group/${permissionGroup}`, { nicknames })
|
||||
|
@ -503,7 +501,7 @@ const addPermission = (accountIds: string[], permissionGroup: string) =>
|
|||
|
||||
const removePermission = (accountIds: string[], permissionGroup: string) =>
|
||||
(dispatch: AppDispatch, getState: () => RootState) => {
|
||||
const nicknames = nicknamesFromIds(getState, accountIds);
|
||||
const nicknames = accountIdsToAccts(getState(), accountIds);
|
||||
dispatch({ type: ADMIN_REMOVE_PERMISSION_GROUP_REQUEST, accountIds, permissionGroup });
|
||||
return api(getState)
|
||||
.delete(`/api/v1/pleroma/admin/users/permission_group/${permissionGroup}`, { data: { nicknames } })
|
||||
|
|
|
@ -1,20 +1,16 @@
|
|||
import { Entities } from 'soapbox/entity-store/entities';
|
||||
import { useTransaction } from 'soapbox/entity-store/hooks';
|
||||
import { EntityCallbacks } from 'soapbox/entity-store/hooks/types';
|
||||
import { findEntity } from 'soapbox/entity-store/selectors';
|
||||
import { useApi, useGetState } from 'soapbox/hooks';
|
||||
import { accountIdsToAccts } from 'soapbox/selectors';
|
||||
|
||||
import type { Account } from 'soapbox/schemas';
|
||||
import type { RootState } from 'soapbox/store';
|
||||
|
||||
function useSuggest() {
|
||||
const api = useApi();
|
||||
const getState = useGetState();
|
||||
const { transaction } = useTransaction();
|
||||
|
||||
function suggestEffect(accts: string[], suggested: boolean) {
|
||||
const ids = selectIdsForAccts(getState(), accts);
|
||||
|
||||
function suggestEffect(accountIds: string[], suggested: boolean) {
|
||||
const updater = (account: Account): Account => {
|
||||
if (account.pleroma) {
|
||||
account.pleroma.is_suggested = suggested;
|
||||
|
@ -23,31 +19,33 @@ function useSuggest() {
|
|||
};
|
||||
|
||||
transaction({
|
||||
Accounts: ids.reduce<Record<string, (account: Account) => Account>>(
|
||||
Accounts: accountIds.reduce<Record<string, (account: Account) => Account>>(
|
||||
(result, id) => ({ ...result, [id]: updater }),
|
||||
{}),
|
||||
});
|
||||
}
|
||||
|
||||
async function suggest(accts: string[], callbacks?: EntityCallbacks<void, unknown>) {
|
||||
suggestEffect(accts, true);
|
||||
async function suggest(accountIds: string[], callbacks?: EntityCallbacks<void, unknown>) {
|
||||
const accts = accountIdsToAccts(getState(), accountIds);
|
||||
suggestEffect(accountIds, true);
|
||||
try {
|
||||
await api.patch('/api/v1/pleroma/admin/users/suggest', { nicknames: accts });
|
||||
callbacks?.onSuccess?.();
|
||||
} catch (e) {
|
||||
callbacks?.onError?.(e);
|
||||
suggestEffect(accts, false);
|
||||
suggestEffect(accountIds, false);
|
||||
}
|
||||
}
|
||||
|
||||
async function unsuggest(accts: string[], callbacks?: EntityCallbacks<void, unknown>) {
|
||||
suggestEffect(accts, false);
|
||||
async function unsuggest(accountIds: string[], callbacks?: EntityCallbacks<void, unknown>) {
|
||||
const accts = accountIdsToAccts(getState(), accountIds);
|
||||
suggestEffect(accountIds, false);
|
||||
try {
|
||||
await api.patch('/api/v1/pleroma/admin/users/unsuggest', { nicknames: accts });
|
||||
callbacks?.onSuccess?.();
|
||||
} catch (e) {
|
||||
callbacks?.onError?.(e);
|
||||
suggestEffect(accts, true);
|
||||
suggestEffect(accountIds, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -57,15 +55,4 @@ function useSuggest() {
|
|||
};
|
||||
}
|
||||
|
||||
function selectIdsForAccts(state: RootState, accts: string[]): string[] {
|
||||
return accts.map((acct) => {
|
||||
const account = findEntity<Account>(
|
||||
state,
|
||||
Entities.ACCOUNTS,
|
||||
(account) => account.acct === acct,
|
||||
);
|
||||
return account!.id;
|
||||
});
|
||||
}
|
||||
|
||||
export { useSuggest };
|
|
@ -1,20 +1,16 @@
|
|||
import { Entities } from 'soapbox/entity-store/entities';
|
||||
import { useTransaction } from 'soapbox/entity-store/hooks';
|
||||
import { EntityCallbacks } from 'soapbox/entity-store/hooks/types';
|
||||
import { findEntity } from 'soapbox/entity-store/selectors';
|
||||
import { useApi, useGetState } from 'soapbox/hooks';
|
||||
import { accountIdsToAccts } from 'soapbox/selectors';
|
||||
|
||||
import type { Account } from 'soapbox/schemas';
|
||||
import type { RootState } from 'soapbox/store';
|
||||
|
||||
function useVerify() {
|
||||
const api = useApi();
|
||||
const getState = useGetState();
|
||||
const { transaction } = useTransaction();
|
||||
|
||||
function verifyEffect(accts: string[], verified: boolean) {
|
||||
const ids = selectIdsForAccts(getState(), accts);
|
||||
|
||||
function verifyEffect(accountIds: string[], verified: boolean) {
|
||||
const updater = (account: Account): Account => {
|
||||
if (account.pleroma) {
|
||||
const tags = account.pleroma.tags.filter((tag) => tag !== 'verified');
|
||||
|
@ -28,31 +24,33 @@ function useVerify() {
|
|||
};
|
||||
|
||||
transaction({
|
||||
Accounts: ids.reduce<Record<string, (account: Account) => Account>>(
|
||||
Accounts: accountIds.reduce<Record<string, (account: Account) => Account>>(
|
||||
(result, id) => ({ ...result, [id]: updater }),
|
||||
{}),
|
||||
});
|
||||
}
|
||||
|
||||
async function verify(accts: string[], callbacks?: EntityCallbacks<void, unknown>) {
|
||||
verifyEffect(accts, true);
|
||||
async function verify(accountIds: string[], callbacks?: EntityCallbacks<void, unknown>) {
|
||||
const accts = accountIdsToAccts(getState(), accountIds);
|
||||
verifyEffect(accountIds, true);
|
||||
try {
|
||||
await api.put('/api/v1/pleroma/admin/users/tag', { nicknames: accts, tags: ['verified'] });
|
||||
callbacks?.onSuccess?.();
|
||||
} catch (e) {
|
||||
callbacks?.onError?.(e);
|
||||
verifyEffect(accts, false);
|
||||
verifyEffect(accountIds, false);
|
||||
}
|
||||
}
|
||||
|
||||
async function unverify(accts: string[], callbacks?: EntityCallbacks<void, unknown>) {
|
||||
verifyEffect(accts, false);
|
||||
async function unverify(accountIds: string[], callbacks?: EntityCallbacks<void, unknown>) {
|
||||
const accts = accountIdsToAccts(getState(), accountIds);
|
||||
verifyEffect(accountIds, false);
|
||||
try {
|
||||
await api.delete('/api/v1/pleroma/admin/users/tag', { data: { nicknames: accts, tags: ['verified'] } });
|
||||
callbacks?.onSuccess?.();
|
||||
} catch (e) {
|
||||
callbacks?.onError?.(e);
|
||||
verifyEffect(accts, true);
|
||||
verifyEffect(accountIds, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -62,15 +60,4 @@ function useVerify() {
|
|||
};
|
||||
}
|
||||
|
||||
function selectIdsForAccts(state: RootState, accts: string[]): string[] {
|
||||
return accts.map((acct) => {
|
||||
const account = findEntity<Account>(
|
||||
state,
|
||||
Entities.ACCOUNTS,
|
||||
(account) => account.acct === acct,
|
||||
);
|
||||
return account!.id;
|
||||
});
|
||||
}
|
||||
|
||||
export { useVerify };
|
|
@ -69,7 +69,7 @@ const AccountModerationModal: React.FC<IAccountModerationModal> = ({ onClose, ac
|
|||
const message = checked ? messages.userVerified : messages.userUnverified;
|
||||
const action = checked ? verify : unverify;
|
||||
|
||||
action([account.acct], {
|
||||
action([account.id], {
|
||||
onSuccess: () => toast.success(intl.formatMessage(message, { acct: account.acct })),
|
||||
});
|
||||
};
|
||||
|
@ -80,7 +80,7 @@ const AccountModerationModal: React.FC<IAccountModerationModal> = ({ onClose, ac
|
|||
const message = checked ? messages.userSuggested : messages.userUnsuggested;
|
||||
const action = checked ? suggest : unsuggest;
|
||||
|
||||
action([account.acct], {
|
||||
action([account.id], {
|
||||
onSuccess: () => toast.success(intl.formatMessage(message, { acct: account.acct })),
|
||||
});
|
||||
};
|
||||
|
|
|
@ -33,6 +33,8 @@ export function selectOwnAccount(state: RootState) {
|
|||
}
|
||||
}
|
||||
|
||||
export const accountIdsToAccts = (state: RootState, ids: string[]) => ids.map((id) => selectAccount(state, id)!.acct);
|
||||
|
||||
const getAccountBase = (state: RootState, id: string) => state.entities[Entities.ACCOUNTS]?.store[id] as Account | undefined;
|
||||
const getAccountRelationship = (state: RootState, id: string) => state.relationships.get(id);
|
||||
|
||||
|
|
Loading…
Reference in a new issue