Only support Pleroma for now

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2022-11-04 22:36:39 +01:00
parent 3448022965
commit 1ea4ae3a57
5 changed files with 6 additions and 41 deletions

View file

@ -1,6 +1,5 @@
import { createAsyncThunk } from '@reduxjs/toolkit'; import { createAsyncThunk } from '@reduxjs/toolkit';
import get from 'lodash/get'; import get from 'lodash/get';
import { gte } from 'semver';
import KVStore from 'soapbox/storage/kv_store'; import KVStore from 'soapbox/storage/kv_store';
import { RootState } from 'soapbox/store'; import { RootState } from 'soapbox/store';
@ -38,12 +37,6 @@ const needsNodeinfo = (instance: Record<string, any>): boolean => {
return v.software === 'Pleroma' && !get(instance, ['pleroma', 'metadata']); return v.software === 'Pleroma' && !get(instance, ['pleroma', 'metadata']);
}; };
/** Mastodon exposes features availabiliy under /api/v2/instance since 4.0.0 */
const supportsInstanceV2 = (instance: Record<string, any>): boolean => {
const v = parseVersion(get(instance, 'version'));
return v.software === 'Mastodon' && gte(v.compatVersion, '4.0.0');
};
export const fetchInstance = createAsyncThunk<void, void, { state: RootState }>( export const fetchInstance = createAsyncThunk<void, void, { state: RootState }>(
'instance/fetch', 'instance/fetch',
async(_arg, { dispatch, getState, rejectWithValue }) => { async(_arg, { dispatch, getState, rejectWithValue }) => {
@ -52,9 +45,6 @@ export const fetchInstance = createAsyncThunk<void, void, { state: RootState }>(
if (needsNodeinfo(instance)) { if (needsNodeinfo(instance)) {
dispatch(fetchNodeinfo()); dispatch(fetchNodeinfo());
} }
if (supportsInstanceV2(instance)) {
dispatch(fetchInstanceV2());
}
return instance; return instance;
} catch (e) { } catch (e) {
return rejectWithValue(e); return rejectWithValue(e);
@ -74,14 +64,6 @@ export const loadInstance = createAsyncThunk<void, void, { state: RootState }>(
}, },
); );
export const fetchInstanceV2 = createAsyncThunk<void, void, { state: RootState }>(
'nodeinfo/fetch',
async(_arg, { getState }) => {
const { data: instance } = await api(getState).get('/api/v2/instance');
return instance;
},
);
export const fetchNodeinfo = createAsyncThunk<void, void, { state: RootState }>( export const fetchNodeinfo = createAsyncThunk<void, void, { state: RootState }>(
'nodeinfo/fetch', 'nodeinfo/fetch',
async(_arg, { getState }) => await api(getState).get('/nodeinfo/2.1.json'), async(_arg, { getState }) => await api(getState).get('/nodeinfo/2.1.json'),

View file

@ -310,10 +310,12 @@ const toggleStatusHidden = (status: Status) => {
} }
}; };
const translateStatus = (id: string) => (dispatch: AppDispatch, getState: () => RootState) => { const translateStatus = (id: string, targetLanguage?: string) => (dispatch: AppDispatch, getState: () => RootState) => {
dispatch({ type: STATUS_TRANSLATE_REQUEST, id }); dispatch({ type: STATUS_TRANSLATE_REQUEST, id });
api(getState).post(`/api/v1/statuses/${id}/translate`).then(response => { api(getState).post(`/api/v1/statuses/${id}/translate`, {
target_language: targetLanguage,
}).then(response => {
dispatch({ dispatch({
type: STATUS_TRANSLATE_SUCCESS, type: STATUS_TRANSLATE_SUCCESS,
id, id,

View file

@ -27,7 +27,7 @@ const TranslateButton: React.FC<ITranslateButton> = ({ status }) => {
if (status.translation) { if (status.translation) {
dispatch(undoStatusTranslation(status.id)); dispatch(undoStatusTranslation(status.id));
} else { } else {
dispatch(translateStatus(status.id)); dispatch(translateStatus(status.id, intl.locale));
} }
}; };

View file

@ -10,7 +10,6 @@ import {
rememberInstance, rememberInstance,
fetchInstance, fetchInstance,
fetchNodeinfo, fetchNodeinfo,
fetchInstanceV2,
} from '../actions/instance'; } from '../actions/instance';
import type { AnyAction } from 'redux'; import type { AnyAction } from 'redux';
@ -33,20 +32,10 @@ const nodeinfoToInstance = (nodeinfo: ImmutableMap<string, any>) => {
})); }));
}; };
const instanceV2ToInstance = (instanceV2: ImmutableMap<string, any>) =>
normalizeInstance(ImmutableMap({
configuration: instanceV2.get('configuration'),
}));
const importInstance = (_state: typeof initialState, instance: ImmutableMap<string, any>) => { const importInstance = (_state: typeof initialState, instance: ImmutableMap<string, any>) => {
return normalizeInstance(instance); return normalizeInstance(instance);
}; };
const importInstanceV2 = (state: typeof initialState, instanceV2: ImmutableMap<string, any>) => {
console.log(instanceV2.toJS());
return state.mergeDeep(instanceV2ToInstance(instanceV2));
};
const importNodeinfo = (state: typeof initialState, nodeinfo: ImmutableMap<string, any>) => { const importNodeinfo = (state: typeof initialState, nodeinfo: ImmutableMap<string, any>) => {
return nodeinfoToInstance(nodeinfo).mergeDeep(state); return nodeinfoToInstance(nodeinfo).mergeDeep(state);
}; };
@ -131,8 +120,6 @@ export default function instance(state = initialState, action: AnyAction) {
case fetchInstance.fulfilled.type: case fetchInstance.fulfilled.type:
persistInstance(action.payload); persistInstance(action.payload);
return importInstance(state, ImmutableMap(fromJS(action.payload))); return importInstance(state, ImmutableMap(fromJS(action.payload)));
case fetchInstanceV2.fulfilled.type:
return importInstanceV2(state, ImmutableMap(fromJS(action.payload)));
case fetchInstance.rejected.type: case fetchInstance.rejected.type:
return handleInstanceFetchFail(state, action.error); return handleInstanceFetchFail(state, action.error);
case fetchNodeinfo.fulfilled.type: case fetchNodeinfo.fulfilled.type:

View file

@ -353,12 +353,6 @@ const getInstanceFeatures = (instance: Instance) => {
*/ */
importData: v.software === PLEROMA && gte(v.version, '2.2.0'), importData: v.software === PLEROMA && gte(v.version, '2.2.0'),
/**
* Supports V2 instance endpoint.
* @see GET /api/v2/instance
*/
instanceV2: v.software === MASTODON && gte(v.compatVersion, '4.0.0'),
/** /**
* Can create, view, and manage lists. * Can create, view, and manage lists.
* @see {@link https://docs.joinmastodon.org/methods/timelines/lists/} * @see {@link https://docs.joinmastodon.org/methods/timelines/lists/}
@ -618,7 +612,7 @@ const getInstanceFeatures = (instance: Instance) => {
* Can translate statuses. * Can translate statuses.
* @see POST /api/v1/statuses/:id/translate * @see POST /api/v1/statuses/:id/translate
*/ */
translations: v.software === MASTODON && instance.configuration.getIn(['translation', 'enabled'], false), translations: features.includes('translation'),
/** /**
* Trending statuses. * Trending statuses.