2022-06-16 12:32:17 -07:00
|
|
|
import mapValues from 'lodash/mapValues';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2021-09-24 16:47:22 -07:00
|
|
|
import { verifyCredentials } from './auth';
|
2022-01-10 14:17:52 -08:00
|
|
|
import { importFetchedAccounts } from './importer';
|
2020-08-24 13:23:05 -07:00
|
|
|
|
2022-06-19 11:38:51 -07:00
|
|
|
import type { AppDispatch } from 'soapbox/store';
|
|
|
|
|
|
|
|
const PLEROMA_PRELOAD_IMPORT = 'PLEROMA_PRELOAD_IMPORT';
|
|
|
|
const MASTODON_PRELOAD_IMPORT = 'MASTODON_PRELOAD_IMPORT';
|
2020-08-24 13:23:05 -07:00
|
|
|
|
|
|
|
// https://git.pleroma.social/pleroma/pleroma-fe/-/merge_requests/1176/diffs
|
2022-06-19 11:38:51 -07:00
|
|
|
const decodeUTF8Base64 = (data: string) => {
|
2020-08-24 13:23:05 -07:00
|
|
|
const rawData = atob(data);
|
|
|
|
const array = Uint8Array.from(rawData.split('').map((char) => char.charCodeAt(0)));
|
|
|
|
const text = new TextDecoder().decode(array);
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
2022-06-19 11:38:51 -07:00
|
|
|
const decodePleromaData = (data: Record<string, any>) => {
|
2021-09-15 10:55:21 -07:00
|
|
|
return mapValues(data, base64string => JSON.parse(decodeUTF8Base64(base64string)));
|
|
|
|
};
|
|
|
|
|
2022-06-19 11:38:51 -07:00
|
|
|
const pleromaDecoder = (json: string) => decodePleromaData(JSON.parse(json));
|
2021-09-15 10:55:21 -07:00
|
|
|
|
|
|
|
// This will throw if it fails.
|
|
|
|
// Should be called inside a try-catch.
|
2022-06-19 11:38:51 -07:00
|
|
|
const decodeFromMarkup = (elementId: string, decoder: (json: string) => Record<string, any>) => {
|
|
|
|
const { textContent } = document.getElementById(elementId)!;
|
|
|
|
return decoder(textContent as string);
|
2021-09-15 10:55:21 -07:00
|
|
|
};
|
|
|
|
|
2022-06-19 11:38:51 -07:00
|
|
|
const preloadFromMarkup = (elementId: string, decoder: (json: string) => Record<string, any>, action: (data: Record<string, any>) => any) =>
|
|
|
|
(dispatch: AppDispatch) => {
|
2021-09-15 10:55:21 -07:00
|
|
|
try {
|
|
|
|
const data = decodeFromMarkup(elementId, decoder);
|
|
|
|
dispatch(action(data));
|
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
};
|
2020-08-24 13:23:05 -07:00
|
|
|
|
2022-06-19 11:38:51 -07:00
|
|
|
const preload = () =>
|
|
|
|
(dispatch: AppDispatch) => {
|
2021-09-15 10:55:21 -07:00
|
|
|
dispatch(preloadFromMarkup('initial-results', pleromaDecoder, preloadPleroma));
|
|
|
|
dispatch(preloadFromMarkup('initial-state', JSON.parse, preloadMastodon));
|
|
|
|
};
|
2020-08-24 13:23:05 -07:00
|
|
|
|
2022-06-19 11:38:51 -07:00
|
|
|
const preloadPleroma = (data: Record<string, any>) => ({
|
|
|
|
type: PLEROMA_PRELOAD_IMPORT,
|
|
|
|
data,
|
|
|
|
});
|
2021-09-15 10:55:21 -07:00
|
|
|
|
2022-06-19 11:38:51 -07:00
|
|
|
const preloadMastodon = (data: Record<string, any>) =>
|
|
|
|
(dispatch: AppDispatch) => {
|
2021-09-24 16:47:22 -07:00
|
|
|
const { me, access_token } = data.meta;
|
|
|
|
const { url } = data.accounts[me];
|
|
|
|
|
2021-09-22 07:23:37 -07:00
|
|
|
dispatch(importFetchedAccounts(Object.values(data.accounts)));
|
2021-09-24 16:47:22 -07:00
|
|
|
dispatch(verifyCredentials(access_token, url));
|
2021-09-21 18:26:47 -07:00
|
|
|
dispatch({ type: MASTODON_PRELOAD_IMPORT, data });
|
2020-08-24 13:23:05 -07:00
|
|
|
};
|
2022-06-19 11:38:51 -07:00
|
|
|
|
|
|
|
export {
|
|
|
|
PLEROMA_PRELOAD_IMPORT,
|
|
|
|
MASTODON_PRELOAD_IMPORT,
|
|
|
|
preload,
|
|
|
|
preloadPleroma,
|
|
|
|
preloadMastodon,
|
|
|
|
};
|