2022-11-15 12:42:22 -08:00
|
|
|
import KVStore from 'soapbox/storage/kv-store';
|
2022-06-19 11:38:51 -07:00
|
|
|
import { getAuthUserId, getAuthUserUrl } from 'soapbox/utils/auth';
|
|
|
|
|
|
|
|
import api from '../api';
|
|
|
|
|
|
|
|
import { loadCredentials } from './auth';
|
|
|
|
import { importFetchedAccount } from './importer';
|
|
|
|
|
2023-01-05 12:24:23 -08:00
|
|
|
import type { AxiosError, RawAxiosRequestHeaders } from 'axios';
|
2022-06-19 11:38:51 -07:00
|
|
|
import type { AppDispatch, RootState } from 'soapbox/store';
|
|
|
|
import type { APIEntity } from 'soapbox/types/entities';
|
|
|
|
|
|
|
|
const ME_FETCH_REQUEST = 'ME_FETCH_REQUEST';
|
|
|
|
const ME_FETCH_SUCCESS = 'ME_FETCH_SUCCESS';
|
|
|
|
const ME_FETCH_FAIL = 'ME_FETCH_FAIL';
|
|
|
|
const ME_FETCH_SKIP = 'ME_FETCH_SKIP';
|
|
|
|
|
|
|
|
const ME_PATCH_REQUEST = 'ME_PATCH_REQUEST';
|
|
|
|
const ME_PATCH_SUCCESS = 'ME_PATCH_SUCCESS';
|
|
|
|
const ME_PATCH_FAIL = 'ME_PATCH_FAIL';
|
|
|
|
|
|
|
|
const noOp = () => new Promise(f => f(undefined));
|
|
|
|
|
|
|
|
const getMeId = (state: RootState) => state.me || getAuthUserId(state);
|
|
|
|
|
|
|
|
const getMeUrl = (state: RootState) => {
|
|
|
|
const accountId = getMeId(state);
|
2022-06-20 10:59:51 -07:00
|
|
|
return state.accounts.get(accountId)?.url || getAuthUserUrl(state);
|
2022-06-19 11:38:51 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const getMeToken = (state: RootState) => {
|
|
|
|
// Fallback for upgrading IDs to URLs
|
2022-12-25 15:31:07 -08:00
|
|
|
const accountUrl = getMeUrl(state) || state.auth.me;
|
|
|
|
return state.auth.users.get(accountUrl!)?.access_token;
|
2022-06-19 11:38:51 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const fetchMe = () =>
|
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
const state = getState();
|
|
|
|
const token = getMeToken(state);
|
|
|
|
const accountUrl = getMeUrl(state);
|
|
|
|
|
|
|
|
if (!token) {
|
2022-07-18 09:50:18 -07:00
|
|
|
dispatch({ type: ME_FETCH_SKIP });
|
|
|
|
return noOp();
|
2022-06-19 11:38:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
dispatch(fetchMeRequest());
|
2022-12-25 15:31:07 -08:00
|
|
|
return dispatch(loadCredentials(token, accountUrl!))
|
2022-07-18 09:50:18 -07:00
|
|
|
.catch(error => dispatch(fetchMeFail(error)));
|
2022-06-19 11:38:51 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Update the auth account in IndexedDB for Mastodon, etc. */
|
|
|
|
const persistAuthAccount = (account: APIEntity, params: Record<string, any>) => {
|
|
|
|
if (account && account.url) {
|
|
|
|
if (!account.pleroma) account.pleroma = {};
|
|
|
|
|
|
|
|
if (!account.pleroma.settings_store) {
|
|
|
|
account.pleroma.settings_store = params.pleroma_settings_store || {};
|
|
|
|
}
|
|
|
|
KVStore.setItem(`authAccount:${account.url}`, account).catch(console.error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-05 19:04:42 -07:00
|
|
|
const patchMe = (params: Record<string, any>, isFormData = false) =>
|
2022-06-19 11:38:51 -07:00
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
dispatch(patchMeRequest());
|
|
|
|
|
2023-01-05 12:24:23 -08:00
|
|
|
const headers: RawAxiosRequestHeaders = isFormData ? {
|
2022-07-05 19:04:42 -07:00
|
|
|
'Content-Type': 'multipart/form-data',
|
|
|
|
} : {};
|
|
|
|
|
2022-06-19 11:38:51 -07:00
|
|
|
return api(getState)
|
2022-07-05 19:04:42 -07:00
|
|
|
.patch('/api/v1/accounts/update_credentials', params, { headers })
|
2022-06-19 11:38:51 -07:00
|
|
|
.then(response => {
|
|
|
|
persistAuthAccount(response.data, params);
|
|
|
|
dispatch(patchMeSuccess(response.data));
|
|
|
|
}).catch(error => {
|
|
|
|
dispatch(patchMeFail(error));
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const fetchMeRequest = () => ({
|
|
|
|
type: ME_FETCH_REQUEST,
|
|
|
|
});
|
|
|
|
|
|
|
|
const fetchMeSuccess = (me: APIEntity) =>
|
|
|
|
(dispatch: AppDispatch) => {
|
|
|
|
dispatch({
|
|
|
|
type: ME_FETCH_SUCCESS,
|
|
|
|
me,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const fetchMeFail = (error: APIEntity) => ({
|
|
|
|
type: ME_FETCH_FAIL,
|
|
|
|
error,
|
|
|
|
skipAlert: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const patchMeRequest = () => ({
|
|
|
|
type: ME_PATCH_REQUEST,
|
|
|
|
});
|
|
|
|
|
|
|
|
const patchMeSuccess = (me: APIEntity) =>
|
|
|
|
(dispatch: AppDispatch) => {
|
|
|
|
dispatch(importFetchedAccount(me));
|
|
|
|
dispatch({
|
|
|
|
type: ME_PATCH_SUCCESS,
|
|
|
|
me,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const patchMeFail = (error: AxiosError) => ({
|
|
|
|
type: ME_PATCH_FAIL,
|
|
|
|
error,
|
|
|
|
skipAlert: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
export {
|
|
|
|
ME_FETCH_REQUEST,
|
|
|
|
ME_FETCH_SUCCESS,
|
|
|
|
ME_FETCH_FAIL,
|
|
|
|
ME_FETCH_SKIP,
|
|
|
|
ME_PATCH_REQUEST,
|
|
|
|
ME_PATCH_SUCCESS,
|
|
|
|
ME_PATCH_FAIL,
|
|
|
|
fetchMe,
|
|
|
|
patchMe,
|
|
|
|
fetchMeRequest,
|
|
|
|
fetchMeSuccess,
|
|
|
|
fetchMeFail,
|
|
|
|
patchMeRequest,
|
|
|
|
patchMeSuccess,
|
|
|
|
patchMeFail,
|
|
|
|
};
|