Fix storing settings store on non-pleromas

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2024-08-26 14:38:22 +02:00
parent 5279160df9
commit 40141191c6
9 changed files with 64 additions and 26 deletions

View file

@ -132,7 +132,7 @@
"multiselect-react-dropdown": "^2.0.25",
"object-to-formdata": "^4.5.1",
"path-browserify": "^1.0.1",
"pl-api": "^0.0.17",
"pl-api": "^0.0.18",
"postcss": "^8.4.29",
"process": "^0.11.10",
"punycode": "^2.1.1",

View file

@ -8,7 +8,7 @@ import { getClient } from '../api';
import { loadCredentials } from './auth';
import { importFetchedAccount } from './importer';
import type { CredentialAccount } from 'pl-api';
import type { CredentialAccount, UpdateCredentialsParams } from 'pl-api';
import type { AppDispatch, RootState } from 'soapbox/store';
const ME_FETCH_REQUEST = 'ME_FETCH_REQUEST' as const;
@ -55,6 +55,17 @@ const fetchMe = () =>
/** Update the auth account in IndexedDB for Mastodon, etc. */
const persistAuthAccount = (account: CredentialAccount, params: Record<string, any>) => {
if (account && account.url) {
const key = `authAccount:${account.url}`;
KVStore.getItem(key).then((oldAccount: any) => {
const settings = oldAccount?.settings_store || {};
if (!account.settings_store) {
account.settings_store = settings;
}
KVStore.setItem(key, account);
})
.catch(console.error);
}
if (account && account.url) {
if (!account.settings_store) {
account.settings_store = params.pleroma_settings_store || {};
@ -63,7 +74,7 @@ const persistAuthAccount = (account: CredentialAccount, params: Record<string, a
}
};
const patchMe = (params: Record<string, any>) =>
const patchMe = (params: UpdateCredentialsParams) =>
(dispatch: AppDispatch, getState: () => RootState) => {
dispatch(patchMeRequest());

View file

@ -3,7 +3,10 @@ import { defineMessage } from 'react-intl';
import { createSelector } from 'reselect';
import { patchMe } from 'soapbox/actions/me';
import { getClient } from 'soapbox/api';
import messages from 'soapbox/messages';
import { makeGetAccount } from 'soapbox/selectors';
import KVStore from 'soapbox/storage/kv-store';
import toast from 'soapbox/toast';
import { isLoggedIn } from 'soapbox/utils/auth';
@ -15,6 +18,8 @@ const SETTINGS_UPDATE = 'SETTINGS_UPDATE' as const;
const FE_NAME = 'pl_fe';
const getAccount = makeGetAccount();
/** Options when changing/saving settings. */
type SettingOpts = {
/** Whether to display an alert when settings are saved. */
@ -171,11 +176,7 @@ const saveSettings = (opts?: SettingOpts) =>
const data = state.settings.delete('saved').toJS();
dispatch(patchMe({
pleroma_settings_store: {
[FE_NAME]: data,
},
})).then(() => {
dispatch(updateSettingsStore(data)).then(() => {
dispatch({ type: SETTING_SAVE });
if (opts?.showAlert) {
@ -186,6 +187,39 @@ const saveSettings = (opts?: SettingOpts) =>
});
};
/** Update settings store for Mastodon, etc. */
const updateAuthAccount = (url: string, settings: any) => {
const key = `authAccount:${url}`;
return KVStore.getItem(key).then((oldAccount: any) => {
if (!oldAccount) return;
if (!oldAccount.__meta) oldAccount.__meta = { pleroma: { settings_store: {} } };
else if (!oldAccount.__meta.pleroma) oldAccount.__meta.pleroma = { settings_store: {} };
else if (!oldAccount.__meta.pleroma.settings_store) oldAccount.__meta.pleroma.settings_store = {};
oldAccount.__meta.pleroma.settings_store[FE_NAME] = settings;
// const settingsStore = oldAccount?.__meta || {};
// settingsStore[FE_NAME] = settings;
KVStore.setItem(key, oldAccount);
}).catch(console.error);
};
const updateSettingsStore = (settings: any) =>
(dispatch: AppDispatch, getState: () => RootState) => {
const state = getState();
const client = getClient(state);
if (client.features.settingsStore) {
return dispatch(patchMe({
settings_store: {
[FE_NAME]: settings,
},
}));
} else {
const accountUrl = getAccount(state, state.me as string)!.url;
return updateAuthAccount(accountUrl, settings);
}
};
const getLocale = (state: RootState, fallback = 'en') => {
const localeWithVariant = (getSettings(state).get('locale') as string).replace('_', '-');
const locale = localeWithVariant.split('-')[0];
@ -206,6 +240,7 @@ export {
changeSettingImmediate,
changeSetting,
saveSettings,
updateSettingsStore,
getLocale,
type SettingsAction,
};

View file

@ -1,8 +1,7 @@
import React, { useState, useEffect } from 'react';
import { useIntl, FormattedMessage, defineMessages } from 'react-intl';
import { patchMe } from 'soapbox/actions/me';
import { FE_NAME, SETTINGS_UPDATE, changeSetting } from 'soapbox/actions/settings';
import { SETTINGS_UPDATE, changeSetting, updateSettingsStore } from 'soapbox/actions/settings';
import List, { ListItem } from 'soapbox/components/list';
import {
CardHeader,
@ -57,11 +56,7 @@ const SettingsStore: React.FC = () => {
const settings = JSON.parse(rawJSON);
setLoading(true);
dispatch(patchMe({
pleroma_settings_store: {
[FE_NAME]: settings,
},
})).then(response => {
dispatch(updateSettingsStore(settings)).then(() => {
dispatch({ type: SETTINGS_UPDATE, settings });
setLoading(false);
}).catch(error => {

View file

@ -215,7 +215,7 @@ const EditProfile: React.FC = () => {
if (header.file !== undefined) params.header = header.file || '';
if (avatar.file !== undefined) params.avatar = avatar.file || '';
promises.push(dispatch(patchMe(params)));
promises.push(dispatch(patchMe(params as any)));
if (features.muteStrangers) {
promises.push(

View file

@ -42,9 +42,7 @@ const AvatarSelectionStep = ({ onNext }: { onNext: () => void }) => {
setSelectedFile(url);
setSubmitting(true);
const formData = new FormData();
formData.append('avatar', rawFile);
const credentials = dispatch(patchMe(formData));
const credentials = dispatch(patchMe({ avatar: rawFile }));
return Promise.all([credentials]).then(() => {
setDisabled(false);

View file

@ -45,9 +45,7 @@ const CoverPhotoSelectionStep = ({ onNext }: { onNext: () => void }) => {
setSelectedFile(url);
setSubmitting(true);
const formData = new FormData();
formData.append('header', file);
const credentials = dispatch(patchMe(formData));
const credentials = dispatch(patchMe({ header: file }));
Promise.all([credentials]).then(() => {
setDisabled(false);

View file

@ -41,6 +41,7 @@ const settings = (
): State => {
switch (action.type) {
case ME_FETCH_SUCCESS:
console.log('importing', action.me);
return importSettings(state, action.me);
case NOTIFICATIONS_FILTER_SET:
case SEARCH_FILTER_SET:

View file

@ -8385,10 +8385,10 @@ pkg-types@^1.0.3:
mlly "^1.2.0"
pathe "^1.1.0"
pl-api@^0.0.17:
version "0.0.17"
resolved "https://registry.yarnpkg.com/pl-api/-/pl-api-0.0.17.tgz#4659be98c65bcf1cebab43b38b28a1f306c1eef4"
integrity sha512-qD/Qn+FGM5LX8y7Ud9mfnNMj8ZbXPGWs3Y2J3vVBAOxhkpIdhHX2JwETx08kPu1Mq7Kt3yTWDxREHSQc3XR10A==
pl-api@^0.0.18:
version "0.0.18"
resolved "https://registry.yarnpkg.com/pl-api/-/pl-api-0.0.18.tgz#23542323cb9450e4c084a38e411a8dc5f3e806f5"
integrity sha512-JYNR3hKO8bHUOnMNERCXwYj6PMNToY9uqPC5lFbe0vQry77ioHzaqiGzA4F1cK1nSSYZhDR41psuBqisvIp9kg==
dependencies:
blurhash "^2.0.5"
http-link-header "^1.1.3"