From a2c9aeb8dd2f8e3a9c56590a10d44fe25e612cf5 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 31 May 2020 12:01:21 -0500 Subject: [PATCH] Conditionally fetch /nodeinfo/2.1.json for Pleroma < 2.1, #137 --- app/soapbox/actions/instance.js | 37 ++++++++++++++++++++++++++++++-- app/soapbox/reducers/instance.js | 20 ++++++++++++++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/app/soapbox/actions/instance.js b/app/soapbox/actions/instance.js index 2be8418fd..16294034a 100644 --- a/app/soapbox/actions/instance.js +++ b/app/soapbox/actions/instance.js @@ -1,18 +1,36 @@ import api from '../api'; +import { get } from 'lodash'; +import { parseVersion } from 'soapbox/utils/features'; -export const INSTANCE_IMPORT = 'INSTANCE_IMPORT'; -export const INSTANCE_FAIL = 'INSTANCE_FAIL'; +export const INSTANCE_IMPORT = 'INSTANCE_IMPORT'; +export const INSTANCE_FAIL = 'INSTANCE_FAIL'; +export const NODEINFO_IMPORT = 'NODEINFO_IMPORT'; +export const NODEINFO_FAIL = 'NODEINFO_FAIL'; export function fetchInstance() { return (dispatch, getState) => { 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(fetchNodeinfo()); // Pleroma < 2.1 backwards compatibility + } }).catch(error => { dispatch(instanceFail(error)); }); }; } +export function fetchNodeinfo() { + return (dispatch, getState) => { + api(getState).get('/nodeinfo/2.1.json').then(response => { + dispatch(importNodeinfo(response.data)); + }).catch(error => { + dispatch(nodeinfoFail(error)); + }); + }; +} + export function importInstance(instance) { return { type: INSTANCE_IMPORT, @@ -27,3 +45,18 @@ export function instanceFail(error) { skipAlert: true, }; }; + +export function importNodeinfo(nodeinfo) { + return { + type: NODEINFO_IMPORT, + nodeinfo, + }; +} + +export function nodeinfoFail(error) { + return { + type: NODEINFO_FAIL, + error, + skipAlert: true, + }; +}; diff --git a/app/soapbox/reducers/instance.js b/app/soapbox/reducers/instance.js index 716b7aaa6..45211f754 100644 --- a/app/soapbox/reducers/instance.js +++ b/app/soapbox/reducers/instance.js @@ -1,6 +1,22 @@ -import { INSTANCE_IMPORT } from '../actions/instance'; +import { + INSTANCE_IMPORT, + NODEINFO_IMPORT, +} from '../actions/instance'; import { Map as ImmutableMap, fromJS } from 'immutable'; +const nodeinfoToInstance = nodeinfo => { + // Match Pleroma's develop branch + return ImmutableMap({ + pleroma: ImmutableMap({ + metadata: ImmutableMap({ + account_activation_required: nodeinfo.getIn(['metadata', 'accountActivationRequired']), + features: nodeinfo.getIn(['metadata', 'features']), + federation: nodeinfo.getIn(['metadata', 'federation']), + }), + }), + }); +}; + // Set Mastodon defaults, overridden by Pleroma servers const initialState = ImmutableMap({ max_toot_chars: 500, @@ -16,6 +32,8 @@ export default function instance(state = initialState, action) { switch(action.type) { case INSTANCE_IMPORT: return initialState.merge(fromJS(action.instance)); + case NODEINFO_IMPORT: + return nodeinfoToInstance(fromJS(action.nodeinfo)).merge(state); default: return state; }