bigbuffet-rw/app/soapbox/actions/preload.js

65 lines
1.9 KiB
JavaScript
Raw Normal View History

import mapValues from 'lodash/mapValues';
import { verifyCredentials } from './auth';
import { importFetchedAccounts } from './importer';
2020-08-24 13:23:05 -07:00
export const PLEROMA_PRELOAD_IMPORT = 'PLEROMA_PRELOAD_IMPORT';
export 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
const decodeUTF8Base64 = data => {
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;
};
const decodePleromaData = data => {
return mapValues(data, base64string => JSON.parse(decodeUTF8Base64(base64string)));
};
const pleromaDecoder = json => decodePleromaData(JSON.parse(json));
// This will throw if it fails.
// Should be called inside a try-catch.
const decodeFromMarkup = (elementId, decoder) => {
const { textContent } = document.getElementById(elementId);
return decoder(textContent);
};
function preloadFromMarkup(elementId, decoder, action) {
return (dispatch, getState) => {
try {
const data = decodeFromMarkup(elementId, decoder);
dispatch(action(data));
} catch {
// Do nothing
}
};
}
2020-08-24 13:23:05 -07:00
export function preload() {
return (dispatch, getState) => {
dispatch(preloadFromMarkup('initial-results', pleromaDecoder, preloadPleroma));
dispatch(preloadFromMarkup('initial-state', JSON.parse, preloadMastodon));
};
}
2020-08-24 13:23:05 -07:00
export function preloadPleroma(data) {
2020-08-24 13:23:05 -07:00
return {
type: PLEROMA_PRELOAD_IMPORT,
data,
};
}
export function preloadMastodon(data) {
return (dispatch, getState) => {
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)));
dispatch(verifyCredentials(access_token, url));
dispatch({ type: MASTODON_PRELOAD_IMPORT, data });
2020-08-24 13:23:05 -07:00
};
}