Migrate to external library for interacting with API
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
@ -1,9 +1,10 @@
|
||||
import { defineMessages } from 'react-intl';
|
||||
|
||||
import api, { type PlfeResponse, getLinks } from 'soapbox/api';
|
||||
import { getClient } from 'soapbox/api';
|
||||
import { normalizeAccount } from 'soapbox/normalizers';
|
||||
import toast from 'soapbox/toast';
|
||||
|
||||
import type { Account, PaginatedResponse } from 'pl-api';
|
||||
import type { RootState } from 'soapbox/store';
|
||||
|
||||
const EXPORT_FOLLOWS_REQUEST = 'EXPORT_FOLLOWS_REQUEST';
|
||||
@ -48,25 +49,25 @@ const fileExport = (content: string, fileName: string) => {
|
||||
document.body.removeChild(fileToDownload);
|
||||
};
|
||||
|
||||
const listAccounts = (getState: () => RootState) => async(apiResponse: PlfeResponse) => {
|
||||
const followings = apiResponse.json;
|
||||
const listAccounts = async (response: PaginatedResponse<Account>) => {
|
||||
const followings = response.items;
|
||||
let accounts = [];
|
||||
let next = getLinks(apiResponse).refs.find(link => link.rel === 'next');
|
||||
while (next) {
|
||||
apiResponse = await api(getState)(next.uri);
|
||||
next = getLinks(apiResponse).refs.find(link => link.rel === 'next');
|
||||
Array.prototype.push.apply(followings, apiResponse.json);
|
||||
while (response.next) {
|
||||
response = await response.next();
|
||||
Array.prototype.push.apply(followings, response.items);
|
||||
}
|
||||
|
||||
accounts = followings.map((account: any) => normalizeAccount(account).fqn);
|
||||
return Array.from(new Set(accounts));
|
||||
};
|
||||
|
||||
const exportFollows = () => (dispatch: React.Dispatch<ExportDataActions>, getState: () => RootState) => {
|
||||
const exportFollows = () => async (dispatch: React.Dispatch<ExportDataActions>, getState: () => RootState) => {
|
||||
dispatch({ type: EXPORT_FOLLOWS_REQUEST });
|
||||
const me = getState().me;
|
||||
return api(getState)(`/api/v1/accounts/${me}/following?limit=40`)
|
||||
.then(listAccounts(getState))
|
||||
if (!me) return;
|
||||
|
||||
return getClient(getState()).accounts.getAccountFollowing(me, { limit: 40 })
|
||||
.then(listAccounts)
|
||||
.then((followings) => {
|
||||
followings = followings.map(fqn => fqn + ',true');
|
||||
followings.unshift('Account address,Show boosts');
|
||||
@ -81,8 +82,8 @@ const exportFollows = () => (dispatch: React.Dispatch<ExportDataActions>, getSta
|
||||
|
||||
const exportBlocks = () => (dispatch: React.Dispatch<ExportDataActions>, getState: () => RootState) => {
|
||||
dispatch({ type: EXPORT_BLOCKS_REQUEST });
|
||||
return api(getState)('/api/v1/blocks?limit=40')
|
||||
.then(listAccounts(getState))
|
||||
return getClient(getState()).filtering.getBlocks({ limit: 40 })
|
||||
.then(listAccounts)
|
||||
.then((blocks) => {
|
||||
fileExport(blocks.join('\n'), 'export_block.csv');
|
||||
|
||||
@ -95,8 +96,8 @@ const exportBlocks = () => (dispatch: React.Dispatch<ExportDataActions>, getStat
|
||||
|
||||
const exportMutes = () => (dispatch: React.Dispatch<ExportDataActions>, getState: () => RootState) => {
|
||||
dispatch({ type: EXPORT_MUTES_REQUEST });
|
||||
return api(getState)('/api/v1/mutes?limit=40')
|
||||
.then(listAccounts(getState))
|
||||
return getClient(getState()).filtering.getMutes({ limit: 40 })
|
||||
.then(listAccounts)
|
||||
.then((mutes) => {
|
||||
fileExport(mutes.join('\n'), 'export_mutes.csv');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user