localforage: create custom KVStore instance, refactor Instance actions
This commit is contained in:
@ -2,12 +2,17 @@ import api from '../api';
|
||||
import { get } from 'lodash';
|
||||
import { parseVersion } from 'soapbox/utils/features';
|
||||
import { getAuthUserUrl } from 'soapbox/utils/auth';
|
||||
import localforage from 'localforage';
|
||||
import KVStore from 'soapbox/storage/kv_store';
|
||||
|
||||
export const INSTANCE_FETCH_SUCCESS = 'INSTANCE_FETCH_SUCCESS';
|
||||
export const INSTANCE_FETCH_REQUEST = 'INSTANCE_FETCH_REQUEST';
|
||||
export const INSTANCE_FETCH_SUCCESS = 'INSTANCE_FETCH_SUCCESS';
|
||||
export const INSTANCE_FETCH_FAIL = 'INSTANCE_FETCH_FAIL';
|
||||
|
||||
export const INSTANCE_REMEMBER_REQUEST = 'INSTANCE_REMEMBER_REQUEST';
|
||||
export const INSTANCE_REMEMBER_SUCCESS = 'INSTANCE_REMEMBER_SUCCESS';
|
||||
export const INSTANCE_FETCH_FAIL = 'INSTANCE_FETCH_FAIL';
|
||||
export const INSTANCE_REMEMBER_FAIL = 'INSTANCE_REMEMBER_FAIL';
|
||||
|
||||
export const NODEINFO_FETCH_REQUEST = 'NODEINFO_FETCH_REQUEST';
|
||||
export const NODEINFO_FETCH_SUCCESS = 'NODEINFO_FETCH_SUCCESS';
|
||||
export const NODEINFO_FETCH_FAIL = 'NODEINFO_FETCH_FAIL';
|
||||
|
||||
@ -29,23 +34,32 @@ const getHost = state => {
|
||||
|
||||
export function rememberInstance(host) {
|
||||
return (dispatch, getState) => {
|
||||
return localforage.getItem(`instance:${host}`).then(instance => {
|
||||
dispatch({ type: INSTANCE_REMEMBER_SUCCESS, instance });
|
||||
dispatch({ type: INSTANCE_REMEMBER_REQUEST, host });
|
||||
return KVStore.getItemOrError(`instance:${host}`).then(instance => {
|
||||
dispatch({ type: INSTANCE_REMEMBER_SUCCESS, host, instance });
|
||||
return instance;
|
||||
}).catch(error => {
|
||||
dispatch({ type: INSTANCE_REMEMBER_FAIL, host, error, skipAlert: true });
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
// We may need to fetch nodeinfo on Pleroma < 2.1
|
||||
const needsNodeinfo = instance => {
|
||||
const v = parseVersion(get(instance, 'version'));
|
||||
return v.software === 'Pleroma' && !get(instance, ['pleroma', 'metadata']);
|
||||
};
|
||||
|
||||
export function fetchInstance() {
|
||||
return (dispatch, getState) => {
|
||||
return api(getState).get('/api/v1/instance').then(response => {
|
||||
dispatch(importInstance(response.data));
|
||||
const v = parseVersion(get(response.data, 'version'));
|
||||
if (v.software === 'Pleroma' && !get(response.data, ['pleroma', 'metadata'])) {
|
||||
dispatch({ type: INSTANCE_FETCH_REQUEST });
|
||||
return api(getState).get('/api/v1/instance').then(({ data: instance }) => {
|
||||
dispatch({ type: INSTANCE_FETCH_SUCCESS, instance });
|
||||
if (needsNodeinfo(instance)) {
|
||||
dispatch(fetchNodeinfo()); // Pleroma < 2.1 backwards compatibility
|
||||
}
|
||||
}).catch(error => {
|
||||
dispatch(instanceFail(error));
|
||||
dispatch({ type: INSTANCE_FETCH_FAIL, error, skipAlert: true });
|
||||
});
|
||||
};
|
||||
}
|
||||
@ -55,7 +69,7 @@ export function loadInstance() {
|
||||
return (dispatch, getState) => {
|
||||
const host = getHost(getState());
|
||||
|
||||
return dispatch(rememberInstance(host)).then(instance => {
|
||||
return dispatch(rememberInstance(host)).finally(instance => {
|
||||
return dispatch(fetchInstance());
|
||||
});
|
||||
};
|
||||
@ -63,40 +77,11 @@ export function loadInstance() {
|
||||
|
||||
export function fetchNodeinfo() {
|
||||
return (dispatch, getState) => {
|
||||
api(getState).get('/nodeinfo/2.1.json').then(response => {
|
||||
dispatch(importNodeinfo(response.data));
|
||||
dispatch({ type: NODEINFO_FETCH_REQUEST });
|
||||
api(getState).get('/nodeinfo/2.1.json').then(({ data: nodeinfo }) => {
|
||||
dispatch({ type: NODEINFO_FETCH_SUCCESS, nodeinfo });
|
||||
}).catch(error => {
|
||||
dispatch(nodeinfoFail(error));
|
||||
dispatch({ type: NODEINFO_FETCH_FAIL, error, skipAlert: true });
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export function importInstance(instance) {
|
||||
return {
|
||||
type: INSTANCE_FETCH_SUCCESS,
|
||||
instance,
|
||||
};
|
||||
}
|
||||
|
||||
export function instanceFail(error) {
|
||||
return {
|
||||
type: INSTANCE_FETCH_FAIL,
|
||||
error,
|
||||
skipAlert: true,
|
||||
};
|
||||
}
|
||||
|
||||
export function importNodeinfo(nodeinfo) {
|
||||
return {
|
||||
type: NODEINFO_FETCH_SUCCESS,
|
||||
nodeinfo,
|
||||
};
|
||||
}
|
||||
|
||||
export function nodeinfoFail(error) {
|
||||
return {
|
||||
type: NODEINFO_FETCH_FAIL,
|
||||
error,
|
||||
skipAlert: true,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user