From 1ca0ffb1f0c84977a26afc81a2a666e6050b0d16 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Wed, 17 Jun 2026 17:58:14 +0300 Subject: [PATCH] refactor promisedRequest to always return whole response --- src/api/chats.js | 5 +- src/api/helpers.js | 37 ++- src/api/public.js | 57 ++-- src/api/user.js | 52 ++-- src/components/chat/chat.js | 2 +- src/components/conversation/conversation.js | 9 +- .../settings_modal/tabs/appearance_tab.js | 2 +- .../settings_modal/tabs/composing_tab.js | 2 +- .../settings_modal/tabs/general_tab.js | 2 +- src/modules/api.js | 2 +- src/modules/notifications.js | 3 +- src/modules/profileConfig.js | 5 +- src/modules/statuses.js | 44 +-- src/modules/users.js | 284 +++++++++--------- .../notifications_fetcher.service.js | 4 +- .../status_poster/status_poster.service.js | 44 +-- .../timeline_fetcher.service.js | 3 +- src/stores/admin_settings.js | 72 +++-- src/stores/announcements.js | 7 +- src/stores/bookmark_folders.js | 9 +- src/stores/emoji.js | 20 +- src/stores/instance.js | 3 +- src/stores/lists.js | 5 +- src/stores/sync_config.js | 4 +- src/stores/user_highlight.js | 2 +- 25 files changed, 352 insertions(+), 327 deletions(-) diff --git a/src/api/chats.js b/src/api/chats.js index 4a0c55752..51e83c74f 100644 --- a/src/api/chats.js +++ b/src/api/chats.js @@ -1,6 +1,7 @@ -import { parseChat } from 'src/services/entity_normalizer/entity_normalizer.service.js' import { paramsString, promisedRequest } from './helpers.js' +import { parseChat } from 'src/services/entity_normalizer/entity_normalizer.service.js' + const PLEROMA_CHATS_URL = '/api/v1/pleroma/chats' const PLEROMA_CHAT_URL = (id) => `/api/v1/pleroma/chats/by-account-id/${id}` const PLEROMA_CHAT_MESSAGES_URL = (id, { maxId, sinceId, limit }) => @@ -13,7 +14,7 @@ export const chats = ({ credentials }) => promisedRequest({ url: PLEROMA_CHATS_URL, credentials, - }).then((data) => ({ + }).then(({ data }) => ({ chatList: data.map(parseChat).filter((c) => c), })) diff --git a/src/api/helpers.js b/src/api/helpers.js index 1e8f257fb..842efe9aa 100644 --- a/src/api/helpers.js +++ b/src/api/helpers.js @@ -98,6 +98,7 @@ export const promisedRequest = async ({ ) .join('&') } + if (formData || payload) { options.body = formData || JSON.stringify(payload) } @@ -109,33 +110,37 @@ export const promisedRequest = async ({ } } - const response = await fetch(url, options) - - // 204 is "No content", which fails to parse json (as you'd might think) - if (response.ok && response.status === 204) return { _response: response } - + let response = null try { - const json = await response.json() + response = await fetch(url, options) + const data = await (async () => { + const [contentType] = response.headers + .get('content-type') + .split(';') + .map((x) => x.toLowerCase().trim()) - if (typeof json !== 'object') { - return { - _response: response, - _value: json, + switch (contentType) { + case 'text/plain': + return await response.text() + case 'application/json': + return await response.json() + default: + return await response.bytes() } - } + })() - json._response = response + const { ok, status } = response - if (!response.ok) { + if (ok) { + return { response, status, data } + } else { throw new StatusCodeError( response.status, - json, + data, { url, options }, response, ) } - - return json } catch (error) { throw new StatusCodeError( response.status, diff --git a/src/api/public.js b/src/api/public.js index b0aa68140..573f97c7c 100644 --- a/src/api/public.js +++ b/src/api/public.js @@ -1,5 +1,7 @@ import { concat, each, last, map } from 'lodash' +import { paramsString, promisedRequest } from './helpers.js' + import { parseAttachment, parseChat, @@ -9,8 +11,6 @@ import { parseStatus, parseUser, } from 'src/services/entity_normalizer/entity_normalizer.service.js' -import { paramsString, promisedRequest } from './helpers.js' - import { RegistrationError, StatusCodeError } from 'src/services/errors/errors' const SUGGESTIONS_URL = '/api/v1/suggestions' @@ -102,7 +102,7 @@ export const fetchUser = ({ id, credentials }) => promisedRequest({ url: `${MASTODON_USER_URL}/${id}`, credentials, - }).then((data) => parseUser(data)) + }).then(({ data, ...rest }) => ({ ...rest, data: parseUser(data) })) export const fetchUserByName = ({ name, credentials }) => promisedRequest({ @@ -126,7 +126,7 @@ export const fetchFriends = ({ id, maxId, sinceId, limit = 20, credentials }) => promisedRequest({ url: MASTODON_FOLLOWING_URL(id, { maxId, sinceId, limit }), credentials, - }).then((data) => data.map(parseUser)) + }).then(({ data, ...rest }) => ({ ...rest, data: data.map(parseUser) })) export const fetchFollowers = ({ id, @@ -143,16 +143,20 @@ export const fetchFollowers = ({ withRelationships: true, }), credentials, - }).then((data) => data.map(parseUser)) + }).then(({ data, ...rest }) => ({ ...rest, data: data.map(parseUser) })) export const fetchConversation = ({ id, credentials }) => promisedRequest({ url: MASTODON_STATUS_CONTEXT_URL(id), credentials, }) - .then(({ ancestors, descendants }) => ({ - ancestors: ancestors.map(parseStatus), - descendants: descendants.map(parseStatus), + .then((result) => ({ + ...result, + data: { + ...result.data, + ancestors: result.data.ancestors.map(parseStatus), + descendants: result.data.descendants.map(parseStatus), + }, })) .catch((error) => { throw new Error('Error fetching timeline', error) @@ -163,7 +167,7 @@ export const fetchStatus = ({ id, credentials }) => url: MASTODON_STATUS_URL(id), credentials, }) - .then((data) => parseStatus(data)) + .then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) })) .catch((error) => { throw new Error('Error fetching timeline', error) }) @@ -173,7 +177,7 @@ export const fetchStatusSource = ({ id, credentials }) => url: MASTODON_STATUS_SOURCE_URL(id), credentials, }) - .then((data) => parseSource(data)) + .then(({ data, ...rest }) => ({ ...rest, data: parseSource(data) })) .catch((error) => { throw new Error('Error fetching timeline', error) }) @@ -182,9 +186,8 @@ export const fetchStatusHistory = ({ status, credentials }) => promisedRequest({ url: MASTODON_STATUS_HISTORY_URL(status.id), credentials, - }).then((data) => { - data.reverse() - return data.map((item) => { + }).then(({ data }) => { + return [...data].reverse().map((item) => { item.originalStatus = status return parseStatus(item) }) @@ -277,16 +280,17 @@ export const fetchTimeline = ({ return promisedRequest({ url: url + paramsString(params), credentials, - }).then(async (data) => { + }).then(async (result) => { const pagination = parseLinkHeaderPagination( - data._response.headers.get('Link'), + result.response.headers.get('Link'), { flakeId: timeline !== 'bookmarks' && timeline !== 'notifications', }, ) return { - data: data.map(isNotifications ? parseNotification : parseStatus), + ...result, + data: result.data.map(isNotifications ? parseNotification : parseStatus), pagination, } }) @@ -301,13 +305,13 @@ export const fetchPinnedStatuses = ({ id, credentials }) => promisedRequest({ url: MASTODON_USER_TIMELINE_URL(id) + '?pinned=true', credentials, - }).then((data) => data.map(parseStatus)) + }).then(({ data, ...rest }) => ({ ...rest, data: data.map(parseStatus) })) export const verifyCredentials = ({ credentials }) => promisedRequest({ url: MASTODON_LOGIN_URL, credentials, - }).then((data) => (data.error ? data : parseUser(data))) + }).then(({ data, ...rest }) => ({ ...rest, data: parseUser(data) })) export const suggestions = ({ credentials }) => promisedRequest({ @@ -327,25 +331,26 @@ export const fetchFavoritedByUsers = ({ id, credentials }) => url: MASTODON_STATUS_FAVORITEDBY_URL(id), method: 'GET', credentials, - }).then((users) => users.map(parseUser)) + }).then(({ data, ...rest }) => ({ ...rest, data: parseUser(data) })) export const fetchRebloggedByUsers = ({ id, credentials }) => promisedRequest({ url: MASTODON_STATUS_REBLOGGEDBY_URL(id), method: 'GET', credentials, - }).then((users) => users.map(parseUser)) + }).then(({ data, ...rest }) => ({ ...rest, data: parseUser(data) })) export const fetchEmojiReactions = ({ id, credentials }) => promisedRequest({ url: PLEROMA_EMOJI_REACTIONS_URL(id), credentials, - }).then((reactions) => - reactions.map((r) => { + }).then(({ data, ...rest }) => ({ + ...rest, + data: data.map((r) => { r.accounts = r.accounts.map(parseUser) return r }), - ) + })) export const searchUsers = ({ credentials, query }) => promisedRequest({ @@ -355,7 +360,7 @@ export const searchUsers = ({ credentials, query }) => resolve: true, }, credentials, - }).then((data) => data.map(parseUser)) + }).then(({ data, ...rest }) => ({ ...rest, data: data.map(parseUser) })) export const search2 = ({ credentials, @@ -404,10 +409,10 @@ export const search2 = ({ url, credentials, }) - .then((data) => { + .then(({ data, ...rest }) => { data.accounts = data.accounts.slice(0, limit).map((u) => parseUser(u)) data.statuses = data.statuses.slice(0, limit).map((s) => parseStatus(s)) - return data + return { ...rest, data } }) .catch((error) => { throw new Error('Error fetching timeline', error) diff --git a/src/api/user.js b/src/api/user.js index b2bffe057..ebbd8b473 100644 --- a/src/api/user.js +++ b/src/api/user.js @@ -1,14 +1,13 @@ import { concat, each, last, map } from 'lodash' +import { paramsString, promisedRequest } from './helpers.js' +import { fetchFriends, MASTODON_STATUS_URL } from './public.js' + import { parseAttachment, - parseNotification, - parseSource, parseStatus, parseUser, } from 'src/services/entity_normalizer/entity_normalizer.service.js' -import { paramsString, promisedRequest } from './helpers.js' -import { fetchFriends, MASTODON_STATUS_URL } from './public.js' const MUTES_IMPORT_URL = '/api/pleroma/mutes_import' const BLOCKS_IMPORT_URL = '/api/pleroma/blocks_import' @@ -99,42 +98,42 @@ export const favorite = ({ id, credentials }) => url: MASTODON_FAVORITE_URL(id), method: 'POST', credentials, - }).then((data) => parseStatus(data)) + }).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) })) export const unfavorite = ({ id, credentials }) => promisedRequest({ url: MASTODON_UNFAVORITE_URL(id), method: 'POST', credentials, - }).then((data) => parseStatus(data)) + }).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) })) export const retweet = ({ id, credentials }) => promisedRequest({ url: MASTODON_RETWEET_URL(id), method: 'POST', credentials, - }).then((data) => parseStatus(data)) + }).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) })) export const unretweet = ({ id, credentials }) => promisedRequest({ url: MASTODON_UNRETWEET_URL(id), method: 'POST', credentials, - }).then((data) => parseStatus(data)) + }).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) })) export const reactWithEmoji = ({ id, emoji, credentials }) => promisedRequest({ url: PLEROMA_EMOJI_REACT_URL(id, emoji), method: 'PUT', credentials, - }).then(parseStatus) + }).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) })) export const unreactWithEmoji = ({ id, emoji, credentials }) => promisedRequest({ url: PLEROMA_EMOJI_UNREACT_URL(id, emoji), method: 'DELETE', credentials, - }).then(parseStatus) + }).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) })) export const bookmarkStatus = ({ id, credentials, ...options }) => promisedRequest({ @@ -158,28 +157,28 @@ export const pinOwnStatus = ({ id, credentials }) => url: MASTODON_PIN_OWN_STATUS(id), credentials, method: 'POST', - }).then((data) => parseStatus(data)) + }).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) })) export const unpinOwnStatus = ({ id, credentials }) => promisedRequest({ url: MASTODON_UNPIN_OWN_STATUS(id), credentials, method: 'POST', - }).then((data) => parseStatus(data)) + }).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) })) export const muteConversation = ({ id, credentials }) => promisedRequest({ url: MASTODON_MUTE_CONVERSATION(id), credentials, method: 'POST', - }).then((data) => parseStatus(data)) + }).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) })) export const unmuteConversation = ({ id, credentials }) => promisedRequest({ url: MASTODON_UNMUTE_CONVERSATION(id), credentials, method: 'POST', - }).then((data) => parseStatus(data)) + }).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) })) export const vote = ({ pollId, choices, credentials }) => { const form = new FormData() @@ -256,7 +255,7 @@ export const postStatus = ({ method: 'POST', credentials, headers, - }).then((data) => (data.error ? data : parseStatus(data))) + }).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) })) } export const editStatus = ({ @@ -299,7 +298,7 @@ export const editStatus = ({ formData: form, method: 'PUT', credentials, - }).then((data) => (data.error ? data : parseStatus(data))) + }).then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) })) } export const deleteStatus = ({ id, credentials }) => @@ -315,7 +314,7 @@ export const uploadMedia = ({ formData, credentials }) => formData, method: 'POST', credentials, - }).then((data) => parseAttachment(data)) + }).then(({ data, ...rest }) => ({ ...rest, data: parseAttachment(data) })) export const setMediaDescription = ({ id, description, credentials }) => promisedRequest({ @@ -325,7 +324,7 @@ export const setMediaDescription = ({ id, description, credentials }) => payload: { description, }, - }).then((data) => parseAttachment(data)) + }).then(({ data, ...rest }) => ({ ...rest, data: parseAttachment(data) })) // #Notifications export const dismissNotification = ({ credentials, id }) => @@ -456,12 +455,7 @@ export const updateProfileImages = ({ credentials, method: 'PATCH', formData: form, - }).then((data) => { - if (data.error) { - throw new Error(data.error) - } - return parseUser(data) - }) + }).then(({ data, ...rest }) => ({ ...rest, data: parseUser(data) })) } export const updateProfile = ({ credentials, params }) => { @@ -489,7 +483,7 @@ export const updateProfile = ({ credentials, params }) => { credentials, method: 'PATCH', formData, - }).then((data) => parseUser(data)) + }).then(({ data, ...rest }) => ({ ...rest, data: parseUser(data) })) } export const updateProfileJSON = ({ credentials, params }) => @@ -498,7 +492,7 @@ export const updateProfileJSON = ({ credentials, params }) => credentials, payload: params, method: 'PATCH', - }).then((data) => parseUser(data)) + }).then(({ data, ...rest }) => ({ ...rest, data: parseUser(data) })) export const changeEmail = ({ credentials, email, password }) => { const form = new FormData() @@ -671,7 +665,7 @@ export const fetchFollowRequests = ({ credentials }) => promisedRequest({ url: MASTODON_FOLLOW_REQUESTS_URL, credentials, - }).then((data) => data.map(parseUser)) + }).then(({ data, ...rest }) => ({ ...rest, data: data.map(parseUser) })) export const approveUser = ({ id, credentials }) => promisedRequest({ @@ -701,7 +695,7 @@ export const fetchMutes = ({ maxId, credentials }) => promisedRequest({ url: MASTODON_USER_MUTES_URL({ maxId, withRelationships: true }), credentials, - }).then((users) => users.map(parseUser)) + }).then(({ data, ...rest }) => ({ ...rest, data: data.map(parseUser) })) export const muteUser = ({ id, expiresIn, credentials }) => { const payload = {} @@ -728,7 +722,7 @@ export const fetchBlocks = ({ maxId, credentials }) => promisedRequest({ url: MASTODON_USER_BLOCKS_URL({ maxId, withRelationships: true }), credentials, - }).then((users) => users.map(parseUser)) + }).then(({ data, ...rest }) => ({ ...rest, data: data.map(parseUser) })) export const blockUser = ({ id, expiresIn, credentials }) => { const payload = {} diff --git a/src/components/chat/chat.js b/src/components/chat/chat.js index 1b80b6321..b03bc0256 100644 --- a/src/components/chat/chat.js +++ b/src/components/chat/chat.js @@ -5,7 +5,6 @@ import { mapGetters, mapState } from 'vuex' import ChatMessage from 'src/components/chat_message/chat_message.vue' import ChatTitle from 'src/components/chat_title/chat_title.vue' import PostStatusForm from 'src/components/post_status_form/post_status_form.vue' -import { WSConnectionStatus } from 'src/api/public.js' import chatService from '../../services/chat_service/chat_service.js' import { buildFakeMessage } from '../../services/chat_utils/chat_utils.js' import { promiseInterval } from '../../services/promise_interval/promise_interval.js' @@ -24,6 +23,7 @@ import { getOrCreateChat, sendChatMessage, } from 'src/api/chats.js' +import { WSConnectionStatus } from 'src/api/public.js' import { library } from '@fortawesome/fontawesome-svg-core' import { faChevronDown, faChevronLeft } from '@fortawesome/free-solid-svg-icons' diff --git a/src/components/conversation/conversation.js b/src/components/conversation/conversation.js index 30c437459..27584d945 100644 --- a/src/components/conversation/conversation.js +++ b/src/components/conversation/conversation.js @@ -5,13 +5,16 @@ import { mapState } from 'vuex' import QuickFilterSettings from 'src/components/quick_filter_settings/quick_filter_settings.vue' import QuickViewSettings from 'src/components/quick_view_settings/quick_view_settings.vue' import ThreadTree from 'src/components/thread_tree/thread_tree.vue' -import { WSConnectionStatus } from 'src/api/public.js' -import { useOAuthStore } from 'src/stores/oauth.js' import { useInterfaceStore } from 'src/stores/interface' import { useMergedConfigStore } from 'src/stores/merged_config.js' +import { useOAuthStore } from 'src/stores/oauth.js' -import { fetchConversation, fetchStatus } from 'src/api/public.js' +import { + fetchConversation, + fetchStatus, + WSConnectionStatus, +} from 'src/api/public.js' import { library } from '@fortawesome/fontawesome-svg-core' import { diff --git a/src/components/settings_modal/tabs/appearance_tab.js b/src/components/settings_modal/tabs/appearance_tab.js index f159c6090..5dbd79de4 100644 --- a/src/components/settings_modal/tabs/appearance_tab.js +++ b/src/components/settings_modal/tabs/appearance_tab.js @@ -10,9 +10,9 @@ import SharedComputedObject from '../helpers/shared_computed_object.js' import UnitSetting from '../helpers/unit_setting.vue' import Preview from './old_theme_tab/theme_preview.vue' -import { useOAuthStore } from 'src/stores/oauth.js' import { useInstanceStore } from 'src/stores/instance.js' import { normalizeThemeData, useInterfaceStore } from 'src/stores/interface.js' +import { useOAuthStore } from 'src/stores/oauth.js' import { updateProfileImages } from 'src/api/user.js' import { newImporter } from 'src/services/export_import/export_import.js' diff --git a/src/components/settings_modal/tabs/composing_tab.js b/src/components/settings_modal/tabs/composing_tab.js index 0ad4f4cf0..3a2384832 100644 --- a/src/components/settings_modal/tabs/composing_tab.js +++ b/src/components/settings_modal/tabs/composing_tab.js @@ -11,10 +11,10 @@ import IntegerSetting from '../helpers/integer_setting.vue' import SharedComputedObject from '../helpers/shared_computed_object.js' import UnitSetting from '../helpers/unit_setting.vue' -import { useOAuthStore } from 'src/stores/oauth.js' import { useInstanceStore } from 'src/stores/instance.js' import { useInstanceCapabilitiesStore } from 'src/stores/instance_capabilities.js' import { useMergedConfigStore } from 'src/stores/merged_config.js' +import { useOAuthStore } from 'src/stores/oauth.js' import { useSyncConfigStore } from 'src/stores/sync_config.js' import { updateProfile } from 'src/api/user.js' diff --git a/src/components/settings_modal/tabs/general_tab.js b/src/components/settings_modal/tabs/general_tab.js index e2302c774..e40bb993e 100644 --- a/src/components/settings_modal/tabs/general_tab.js +++ b/src/components/settings_modal/tabs/general_tab.js @@ -8,11 +8,11 @@ import FloatSetting from '../helpers/float_setting.vue' import SharedComputedObject from '../helpers/shared_computed_object.js' import UnitSetting from '../helpers/unit_setting.vue' -import { useOAuthStore } from 'src/stores/oauth.js' import { useInstanceStore } from 'src/stores/instance.js' import { useInstanceCapabilitiesStore } from 'src/stores/instance_capabilities.js' import { useLocalConfigStore } from 'src/stores/local_config.js' import { useMergedConfigStore } from 'src/stores/merged_config.js' +import { useOAuthStore } from 'src/stores/oauth.js' import { useSyncConfigStore } from 'src/stores/sync_config.js' import { updateProfile } from 'src/api/user.js' diff --git a/src/modules/api.js b/src/modules/api.js index 2dd5d23df..a36e21c05 100644 --- a/src/modules/api.js +++ b/src/modules/api.js @@ -1,6 +1,5 @@ import { Socket } from 'phoenix' -import { WSConnectionStatus } from 'src/api/public.js' import { maybeShowChatNotification } from '../services/chat_utils/chat_utils.js' import { useInstanceStore } from 'src/stores/instance.js' @@ -13,6 +12,7 @@ import { fetchTimeline, getMastodonSocketURI, ProcessedWS, + WSConnectionStatus, } from 'src/api/public.js' import followRequestFetcher from 'src/services/follow_request_fetcher/follow_request_fetcher.service' import notificationsFetcher from 'src/services/notifications_fetcher/notifications_fetcher.service.js' diff --git a/src/modules/notifications.js b/src/modules/notifications.js index d89cc79ba..b77683811 100644 --- a/src/modules/notifications.js +++ b/src/modules/notifications.js @@ -1,4 +1,3 @@ -import { markNotificationsAsSeen } from 'src/api/user.js' import { closeAllDesktopNotifications, closeDesktopNotification, @@ -15,7 +14,7 @@ import { useOAuthStore } from 'src/stores/oauth.js' import { useReportsStore } from 'src/stores/reports.js' import { useSyncConfigStore } from 'src/stores/sync_config.js' -import { dismissNotification } from 'src/api/user.js' +import { dismissNotification, markNotificationsAsSeen } from 'src/api/user.js' const emptyNotifications = () => ({ desktopNotificationSilence: true, diff --git a/src/modules/profileConfig.js b/src/modules/profileConfig.js index 28536a2f4..80f881a25 100644 --- a/src/modules/profileConfig.js +++ b/src/modules/profileConfig.js @@ -2,10 +2,7 @@ import { get, set } from 'lodash' import { useOAuthStore } from 'src/stores/oauth.js' -import { - updateNotificationSettings, - updateProfile, -} from 'src/api/user.js' +import { updateNotificationSettings, updateProfile } from 'src/api/user.js' const defaultApi = ({ rootState, commit }, { path, value }) => { const params = {} diff --git a/src/modules/statuses.js b/src/modules/statuses.js index a8cecbecc..66bcda13d 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -158,7 +158,7 @@ const getLatestScrobble = (state, user) => { state.scrobblesNextFetch[user.id] = Date.now() + 24 * 60 * 60 * 1000 if (!scrobblesSupport) return fetchScrobbles({ accountId: user.id }) - .then((scrobbles) => { + .then(({ data: scrobbles }) => { if (scrobbles?.error) { useInstanceCapabilitiesStore().set('pleromaScrobblesAvailable', false) return @@ -627,7 +627,7 @@ const statuses = { }) }, fetchStatus({ rootState, dispatch }, id) { - return fetchStatus({ id }).then((status) => + return fetchStatus({ id }).then(({ data: status }) => dispatch('addNewStatuses', { statuses: [status] }), ) }, @@ -635,10 +635,10 @@ const statuses = { return fetchStatusSource({ id: status.id, credentials: useOAuthStore().token, - }) + }).then(({ data }) => data) }, fetchStatusHistory(_, status) { - return fetchStatusHistory({ status }) + return fetchStatusHistory({ status }).then(({ data }) => data) }, deleteStatus({ rootState, commit }, status) { deleteStatus({ @@ -670,7 +670,7 @@ const statuses = { favorite({ id: status.id, credentials: useOAuthStore().token, - }).then((status) => + }).then(({ data: status }) => commit('setFavoritedConfirm', { status, user: rootState.users.currentUser, @@ -683,7 +683,7 @@ const statuses = { unfavorite({ id: status.id, credentials: useOAuthStore().token, - }).then((status) => + }).then(({ data: status }) => commit('setFavoritedConfirm', { status, user: rootState.users.currentUser, @@ -694,7 +694,7 @@ const statuses = { fetchPinnedStatuses({ id: userId, credentials: useOAuthStore().token, - }).then((statuses) => + }).then(({ data: statuses }) => dispatch('addNewStatuses', { statuses, timeline: 'user', @@ -708,25 +708,29 @@ const statuses = { return pinOwnStatus({ id: statusId, credentials: useOAuthStore().token, - }).then((status) => dispatch('addNewStatuses', { statuses: [status] })) + }).then(({ data: status }) => + dispatch('addNewStatuses', { statuses: [status] }), + ) }, unpinStatus({ rootState, dispatch }, statusId) { return unpinOwnStatus({ id: statusId, credentials: useOAuthStore().token, - }).then((status) => dispatch('addNewStatuses', { statuses: [status] })) + }).then(({ data: status }) => + dispatch('addNewStatuses', { statuses: [status] }), + ) }, muteConversation({ rootState, commit }, { id: statusId }) { return muteConversation({ id: statusId, credentials: useOAuthStore().token, - }).then((status) => commit('setMutedStatus', status)) + }).then(({ data: status }) => commit('setMutedStatus', status)) }, unmuteConversation({ rootState, commit }, { id: statusId }) { return unmuteConversation({ id: statusId, credentials: useOAuthStore().token, - }).then((status) => commit('setMutedStatus', status)) + }).then(({ data: status }) => commit('setMutedStatus', status)) }, retweet({ rootState, commit }, status) { // Optimistic retweeting... @@ -734,7 +738,7 @@ const statuses = { retweet({ id: status.id, credentials: useOAuthStore().token, - }).then((status) => + }).then(({ data: status }) => commit('setRetweetedConfirm', { status: status.retweeted_status, user: rootState.users.currentUser, @@ -747,7 +751,7 @@ const statuses = { unretweet({ id: status.id, credentials: useOAuthStore().token, - }).then((status) => + }).then(({ data: status }) => commit('setRetweetedConfirm', { status, user: rootState.users.currentUser, @@ -760,7 +764,7 @@ const statuses = { id: status.id, folder_id: status.bookmark_folder_id, credentials: useOAuthStore().token, - }).then((status) => { + }).then(({ data: status }) => { commit('setBookmarkedConfirm', { status }) }) }, @@ -769,7 +773,7 @@ const statuses = { unbookmarkStatus({ id: status.id, credentials: useOAuthStore().token, - }).then((status) => { + }).then(({ data: status }) => { commit('setBookmarkedConfirm', { status }) }) }, @@ -789,7 +793,7 @@ const statuses = { id, credentials: useOAuthStore().token, }), - ]).then(([favoritedByUsers, rebloggedByUsers]) => { + ]).then(([{ data: favoritedByUsers }, { data: rebloggedByUsers }]) => { commit('addFavs', { id, favoritedByUsers, @@ -832,7 +836,7 @@ const statuses = { return fetchEmojiReactions({ id, credentials: useOAuthStore().token, - }).then((emojiReactions) => { + }).then(({ data: emojiReactions }) => { commit('addEmojiReactionsBy', { id, emojiReactions, @@ -844,7 +848,7 @@ const statuses = { fetchFavoritedByUsers({ id, credentials: useOAuthStore().token, - }).then((favoritedByUsers) => + }).then(({ data: favoritedByUsers }) => commit('addFavs', { id, favoritedByUsers, @@ -856,7 +860,7 @@ const statuses = { fetchRebloggedByUsers({ id, credentials: useOAuthStore().token, - }).then((rebloggedByUsers) => + }).then(({ data: rebloggedByUsers }) => commit('addRepeats', { id, rebloggedByUsers, @@ -873,7 +877,7 @@ const statuses = { following, type, credentials: useOAuthStore().token, - }).then((data) => { + }).then(({ data }) => { store.commit('addNewUsers', data.accounts) store.commit( 'addNewUsers', diff --git a/src/modules/users.js b/src/modules/users.js index 0dc1f6aeb..2eee21333 100644 --- a/src/modules/users.js +++ b/src/modules/users.js @@ -9,7 +9,6 @@ import { uniq, } from 'lodash' -import { register } from 'src/api/public.js' import oauthApi from '../services/new_api/oauth.js' import { registerPushNotifications, @@ -37,17 +36,21 @@ import { fetchUser, fetchUserByName, getCaptcha, + register, searchUsers, verifyCredentials, } from 'src/api/public.js' import { + blockUser as apiBlockUser, + muteUser as apiMuteUser, + unblockUser as apiUnblockUser, + unmuteUser as apiUnmuteUser, fetchBlocks, fetchDomainMutes, fetchMutes, fetchUserInLists, fetchUserRelationship, followUser, - muteUser, } from 'src/api/user.js' // TODO: Unify with mergeOrAdd in statuses.js @@ -92,7 +95,7 @@ const blockUser = (store, args) => { store.commit('updateUserRelationship', [predictedRelationship]) store.commit('addBlockId', id) - return blockUser({ id, expiresIn }).then((relationship) => { + return apiBlockUser({ id, expiresIn }).then(({ data: relationship }) => { store.commit('updateUserRelationship', [relationship]) store.commit('addBlockId', id) @@ -106,7 +109,7 @@ const blockUser = (store, args) => { } const unblockUser = (store, id) => { - return unblockUser({ id }).then((relationship) => + return apiUnblockUser({ id }).then(({ data: relationship }) => store.commit('updateUserRelationship', [relationship]), ) } @@ -123,7 +126,7 @@ const editUserNote = (store, { id, comment }) => { ) } -const localMuteUser = (store, args) => { +const muteUser = (store, args) => { const id = typeof args === 'object' ? args.id : args const expiresIn = typeof args === 'object' ? args.expiresIn : 0 @@ -131,11 +134,11 @@ const localMuteUser = (store, args) => { store.commit('updateUserRelationship', [predictedRelationship]) store.commit('addMuteId', id) - return muteUser({ + return apiMuteUser({ id, expiresIn, credentials: useOAuthStore().token, - }).then((relationship) => { + }).then(({ data: relationship }) => { store.commit('updateUserRelationship', [relationship]) store.commit('addMuteId', id) }) @@ -146,7 +149,7 @@ const unmuteUser = (store, id) => { predictedRelationship.muting = false store.commit('updateUserRelationship', [predictedRelationship]) - return unmuteUser({ id }).then((relationship) => + return apiUnmuteUser({ id }).then(({ data: relationship }) => store.commit('updateUserRelationship', [relationship]), ) } @@ -156,9 +159,9 @@ const hideReblogs = (store, userId) => { id: userId, reblogs: false, credentials: useOAuthStore().token, - }).then((relationship) => { - store.commit('updateUserRelationship', [relationship]) - }) + }).then(({ data: relationship }) => + store.commit('updateUserRelationship', [relationship]), + ) } const showReblogs = (store, userId) => { @@ -166,7 +169,7 @@ const showReblogs = (store, userId) => { id: userId, reblogs: true, credentials: useOAuthStore().token, - }).then((relationship) => + }).then(({ data: relationship }) => store.commit('updateUserRelationship', [relationship]), ) } @@ -406,16 +409,24 @@ const users = { return fetchUser({ id, credentials: useOAuthStore().token, - }).then((user) => { - store.commit('addNewUsers', [user]) - return user }) + .then(({ data: user }) => { + store.commit('addNewUsers', [user]) + return user + }) + .catch((error) => { + if (error.statusCode === 404) { + console.warn(`User ${id} not found`) + } else { + throw error + } + }) }, fetchUserByName(store, name) { return fetchUserByName({ name, credentials: useOAuthStore().token, - }).then((user) => { + }).then(({ data: user }) => { store.commit('addNewUsers', [user]) return user }) @@ -425,7 +436,7 @@ const users = { fetchUserRelationship({ id, credentials: useOAuthStore().token, - }).then((relationships) => + }).then(({ data: relationships }) => store.commit('updateUserRelationship', relationships), ) } @@ -435,7 +446,9 @@ const users = { fetchUserInLists({ id, credentials: useOAuthStore().token, - }).then((inLists) => store.commit('updateUserInLists', { id, inLists })) + }).then(({ data: inLists }) => + store.commit('updateUserInLists', { id, inLists }), + ) } }, fetchBlocks(store, args) { @@ -445,7 +458,7 @@ const users = { return fetchBlocks({ maxId, credentials: useOAuthStore().token, - }).then((blocks) => { + }).then(({ data: blocks }) => { if (reset) { store.commit('saveBlockIds', map(blocks, 'id')) } else { @@ -483,7 +496,7 @@ const users = { return fetchMutes({ maxId, credentials: useOAuthStore().token, - }).then((mutes) => { + }).then(({ data: mutes }) => { if (reset) { store.commit('saveMuteIds', map(mutes, 'id')) } else { @@ -497,7 +510,7 @@ const users = { }) }, muteUser(store, data) { - return localMuteUser(store, data) + return muteUser(store, data) }, unmuteUser(store, id) { return unmuteUser(store, id) @@ -509,7 +522,7 @@ const users = { return showReblogs(store, id) }, muteUsers(store, data = []) { - return Promise.all(data.map((d) => localMuteUser(store, d))) + return Promise.all(data.map((d) => muteUser(store, d))) }, unmuteUsers(store, ids = []) { return Promise.all(ids.map((d) => unmuteUser(store, d))) @@ -517,7 +530,7 @@ const users = { fetchDomainMutes(store) { return fetchDomainMutes({ credentials: useOAuthStore().token, - }).then((domainMutes) => { + }).then(({ data: domainMutes }) => { store.commit('saveDomainMutes', domainMutes) return domainMutes }) @@ -541,7 +554,7 @@ const users = { id, maxId, credentials: useOAuthStore().token, - }).then((friends) => { + }).then(({ data: friends }) => { commit('addNewUsers', friends) commit('saveFriendIds', { id, friendIds: map(friends, 'id') }) return friends @@ -554,7 +567,7 @@ const users = { id, maxId, credentials: useOAuthStore().token, - }).then((followers) => { + }).then(({ data: followers }) => { commit('addNewUsers', followers) commit('saveFollowerIds', { id, followerIds: map(followers, 'id') }) return followers @@ -571,7 +584,7 @@ const users = { id, notify: true, credentials: useOAuthStore().token, - }).then((relationship) => + }).then(({ data: relationship }) => commit('updateUserRelationship', [relationship]), ) }, @@ -580,7 +593,7 @@ const users = { id, notify: false, credentials: useOAuthStore().token, - }).then((relationship) => + }).then(({ data: relationship }) => commit('updateUserRelationship', [relationship]), ) }, @@ -646,7 +659,7 @@ const users = { return searchUsers({ query, credentials: useOAuthStore().token, - }).then((users) => { + }).then(({ data: users }) => { commit('addNewUsers', users) return users }) @@ -657,7 +670,7 @@ const users = { try { const token = await oauthStore.ensureAppToken() - const data = await register({ + const { data } = await register({ credentials: token, params: { ...userInfo }, }) @@ -681,7 +694,7 @@ const users = { getCaptcha(store) { return getCaptcha({ credentials: useOAuthStore().token, - }) + }).then(({ data }) => data) }, logout(store) { @@ -727,135 +740,128 @@ const users = { verifyCredentials({ credentials: useOAuthStore().token, }) - .then((data) => { - if (!data.error) { - const user = data - // user.credentials = userCredentials - user.credentials = accessToken - user.blockIds = [] - user.muteIds = [] - user.domainMutes = [] - commit('setCurrentUser', user) + .then(({ data: user }) => { + // user.credentials = userCredentials + user.credentials = accessToken + user.blockIds = [] + user.muteIds = [] + user.domainMutes = [] + commit('setCurrentUser', user) - useSyncConfigStore() - .initSyncConfig(user) - .then(() => { - useInterfaceStore() - .applyTheme() - .catch((e) => { - console.error('Error setting theme', e) - }) - }) - useUserHighlightStore().initUserHighlight(user) - commit('addNewUsers', [user]) - - useEmojiStore().fetchEmoji() - - getNotificationPermission().then((permission) => - useInterfaceStore().setNotificationPermission(permission), - ) - - // Do server-side storage migrations - - // Debug snippet to clean up storage and reset migrations - /* - // Reset wordfilter - Object.keys( - useSyncConfigStore().prefsStorage.simple.muteFilters - ).forEach(key => { - useSyncConfigStore().unsetSimplePrefAndSave({ path: 'muteFilters.' + key, value: null }) + useSyncConfigStore() + .initSyncConfig(user) + .then(() => { + useInterfaceStore() + .applyTheme() + .catch((e) => { + console.error('Error setting theme', e) + }) }) + useUserHighlightStore().initUserHighlight(user) + commit('addNewUsers', [user]) - // Reset flag to 0 to re-run migrations - useSyncConfigStore().setFlag({ flag: 'configMigration', value: 0 }) - /**/ + useEmojiStore().fetchEmoji() - if (user.token) { - dispatch('setWsToken', user.token) + getNotificationPermission().then((permission) => + useInterfaceStore().setNotificationPermission(permission), + ) - // Initialize the shout socket. - dispatch('initializeSocket') - } + // Do server-side storage migrations - const startPolling = () => { - // Start getting fresh posts. - dispatch('startFetchingTimeline', { timeline: 'friends' }) + // Debug snippet to clean up storage and reset migrations + /* + // Reset wordfilter + Object.keys( + useSyncConfigStore().prefsStorage.simple.muteFilters + ).forEach(key => { + useSyncConfigStore().unsetSimplePrefAndSave({ path: 'muteFilters.' + key, value: null }) + }) - // Start fetching notifications - dispatch('startFetchingNotifications') + // Reset flag to 0 to re-run migrations + useSyncConfigStore().setFlag({ flag: 'configMigration', value: 0 }) + /**/ - if ( - useInstanceCapabilitiesStore().pleromaChatMessagesAvailable - ) { - // Start fetching chats - dispatch('startFetchingChats') - } - } + if (user.token) { + dispatch('setWsToken', user.token) - useListsStore().startFetching() - useBookmarkFoldersStore().startFetching() + // Initialize the shout socket. + dispatch('initializeSocket') + } - if (user.locked) { - dispatch('startFetchingFollowRequests') - } + const startPolling = () => { + // Start getting fresh posts. + dispatch('startFetchingTimeline', { timeline: 'friends' }) - if (useMergedConfigStore().mergedConfig.useStreamingApi) { - dispatch('fetchTimeline', { - timeline: 'friends', - sinceId: null, - }) - dispatch('fetchNotifications', { sinceId: null }) - dispatch('enableMastoSockets', true) - .catch((error) => { - console.error( - 'Failed initializing MastoAPI Streaming socket', - error, - ) - }) - .then(() => { - dispatch('fetchChats', { latest: true }) - setTimeout( - () => dispatch('setNotificationsSilence', false), - 10000, - ) - }) - } else { - startPolling() - } + // Start fetching notifications + dispatch('startFetchingNotifications') - // Get user mutes - dispatch('fetchMutes') - - useInterfaceStore().setLayoutWidth(windowWidth()) - useInterfaceStore().setLayoutHeight(windowHeight()) - - // Fetch our friends - fetchFriends({ id: user.id }).then((friends) => - commit('addNewUsers', friends), - ) - } else { - const response = data.error - // Authentication failed - commit('endLogin') - - // remove authentication token on client/authentication errors - if ([400, 401, 403, 422].includes(response.status)) { - useOAuthStore().clearToken() - } - - if (response.status === 401) { - reject(new Error('Wrong username or password')) - } else { - reject(new Error('An error occurred, please try again')) + if (useInstanceCapabilitiesStore().pleromaChatMessagesAvailable) { + // Start fetching chats + dispatch('startFetchingChats') } } + + useListsStore().startFetching() + useBookmarkFoldersStore().startFetching() + + if (user.locked) { + dispatch('startFetchingFollowRequests') + } + + if (useMergedConfigStore().mergedConfig.useStreamingApi) { + dispatch('fetchTimeline', { + timeline: 'friends', + sinceId: null, + }) + dispatch('fetchNotifications', { sinceId: null }) + dispatch('enableMastoSockets', true) + .catch((error) => { + console.error( + 'Failed initializing MastoAPI Streaming socket', + error, + ) + }) + .then(() => { + dispatch('fetchChats', { latest: true }) + setTimeout( + () => dispatch('setNotificationsSilence', false), + 10000, + ) + }) + } else { + startPolling() + } + + // Get user mutes + dispatch('fetchMutes') + + useInterfaceStore().setLayoutWidth(windowWidth()) + useInterfaceStore().setLayoutHeight(windowHeight()) + + // Fetch our friends + fetchFriends({ id: user.id }).then(({ data: friends }) => + commit('addNewUsers', friends), + ) commit('endLogin') resolve() }) .catch((error) => { console.error(error) + + // Authentication failed commit('endLogin') - reject(new Error('Failed to connect to server, try again')) + + // remove authentication token on client/authentication errors + if ([400, 401, 403, 422].includes(error.statusCode)) { + useOAuthStore().clearToken() + } + + commit('endLogin') + if (error.tatusCode === 401) { + throw new Error('Wrong username or password', error) + } else { + throw new Error('An error occurred, please try again', error) + } }) }) }, diff --git a/src/services/notifications_fetcher/notifications_fetcher.service.js b/src/services/notifications_fetcher/notifications_fetcher.service.js index 6258b0ebe..bb94599eb 100644 --- a/src/services/notifications_fetcher/notifications_fetcher.service.js +++ b/src/services/notifications_fetcher/notifications_fetcher.service.js @@ -1,4 +1,3 @@ -import { fetchTimeline } from 'src/api/public.js' import { promiseInterval } from '../promise_interval/promise_interval.js' import { useInstanceStore } from 'src/stores/instance.js' @@ -6,6 +5,8 @@ import { useInstanceCapabilitiesStore } from 'src/stores/instance_capabilities.j import { useInterfaceStore } from 'src/stores/interface.js' import { useMergedConfigStore } from 'src/stores/merged_config.js' +import { fetchTimeline } from 'src/api/public.js' + const update = ({ store, notifications, older }) => { store.dispatch('addNewNotifications', { notifications, older }) } @@ -97,6 +98,7 @@ const fetchNotifications = ({ store, args, older }) => { throw new Error(`${response.status} ${response.statusText}`) } } + const notifications = response.data update({ store, notifications, older }) return notifications diff --git a/src/services/status_poster/status_poster.service.js b/src/services/status_poster/status_poster.service.js index ddc4a2f9a..7fa67e338 100644 --- a/src/services/status_poster/status_poster.service.js +++ b/src/services/status_poster/status_poster.service.js @@ -37,15 +37,16 @@ const postStatus = ({ preview, idempotencyKey, }) - .then((data) => { - if (!data.error && !preview) { - store.dispatch('addNewStatuses', { - statuses: [data], - timeline: 'friends', - showImmediately: true, - noIdUpdate: true, // To prevent missing notices on next pull. - }) - } + .then(({ data }) => { + if (preview) return data + + store.dispatch('addNewStatuses', { + statuses: [data], + timeline: 'friends', + showImmediately: true, + noIdUpdate: true, // To prevent missing notices on next pull. + }) + return data }) .catch((err) => { @@ -67,7 +68,7 @@ const editStatus = ({ }) => { const mediaIds = map(media, 'id') - return editStatus({ + return apiEditStatus({ id: statusId, credentials: store.state.users.currentUser.credentials, status, @@ -77,15 +78,14 @@ const editStatus = ({ mediaIds, contentType, }) - .then((data) => { - if (!data.error) { - store.dispatch('addNewStatuses', { - statuses: [data], - timeline: 'friends', - showImmediately: true, - noIdUpdate: true, // To prevent missing notices on next pull. - }) - } + .then(({ data }) => { + store.dispatch('addNewStatuses', { + statuses: [data], + timeline: 'friends', + showImmediately: true, + noIdUpdate: true, // To prevent missing notices on next pull. + }) + return data }) .catch((err) => { @@ -98,12 +98,14 @@ const editStatus = ({ const uploadMedia = ({ store, formData }) => { const credentials = store.state.users.currentUser.credentials - return apiUploadMedia({ credentials, formData }) + return apiUploadMedia({ credentials, formData }).then(({ data }) => data) } const setMediaDescription = ({ store, id, description }) => { const credentials = store.state.users.currentUser.credentials - return apiSetMediaDescription({ credentials, id, description }) + return apiSetMediaDescription({ credentials, id, description }).then( + ({ data }) => data, + ) } const statusPosterService = { diff --git a/src/services/timeline_fetcher/timeline_fetcher.service.js b/src/services/timeline_fetcher/timeline_fetcher.service.js index b2ed40e47..efa06258d 100644 --- a/src/services/timeline_fetcher/timeline_fetcher.service.js +++ b/src/services/timeline_fetcher/timeline_fetcher.service.js @@ -1,6 +1,5 @@ import { camelCase } from 'lodash' -import { fetchTimeline } from 'src/api/public.js' import { promiseInterval } from '../promise_interval/promise_interval.js' import { useInstanceStore } from 'src/stores/instance.js' @@ -8,6 +7,8 @@ import { useInstanceCapabilitiesStore } from 'src/stores/instance_capabilities.j import { useInterfaceStore } from 'src/stores/interface.js' import { useMergedConfigStore } from 'src/stores/merged_config.js' +import { fetchTimeline } from 'src/api/public.js' + const update = ({ store, statuses, diff --git a/src/stores/admin_settings.js b/src/stores/admin_settings.js index a437d56ca..9c9e69d8b 100644 --- a/src/stores/admin_settings.js +++ b/src/stores/admin_settings.js @@ -87,7 +87,7 @@ export const useAdminSettingsStore = defineStore('adminSettings', { loadAdminStuff() { getInstanceDBConfig({ credentials: useOAuthStore().token, - }).then((backendDbConfig) => { + }).then(({ data: backendDbConfig }) => { if (backendDbConfig.error) { if (backendDbConfig.error.status === 400) { backendDbConfig.error.json().then((errorJson) => { @@ -106,7 +106,7 @@ export const useAdminSettingsStore = defineStore('adminSettings', { if (this.descriptions === null) { getInstanceConfigDescriptions({ credentials: useOAuthStore().token, - }).then((backendDescriptions) => + }).then(({ data: backendDescriptions }) => this.setInstanceAdminDescriptions({ credentials: useOAuthStore().token, backendDescriptions, @@ -251,7 +251,7 @@ export const useAdminSettingsStore = defineStore('adminSettings', { .then(() => getInstanceDBConfig({ credentials: useOAuthStore().token, - }), + }).then(({ data }) => data), ) .then((backendDbConfig) => this.setInstanceAdminSettings({ @@ -294,7 +294,7 @@ export const useAdminSettingsStore = defineStore('adminSettings', { .then(() => getInstanceDBConfig({ credentials: useOAuthStore().token, - }), + }).then(({ data }) => data), ) .then((backendDbConfig) => this.setInstanceAdminSettings({ @@ -326,7 +326,7 @@ export const useAdminSettingsStore = defineStore('adminSettings', { .then(() => getInstanceDBConfig({ credentials: useOAuthStore().token, - }), + }).then(({ data }) => data), ) .then((backendDbConfig) => this.setInstanceAdminSettings({ backendDbConfig }), @@ -337,7 +337,9 @@ export const useAdminSettingsStore = defineStore('adminSettings', { loadFrontendsStuff() { getAvailableFrontends({ credentials: useOAuthStore().token, - }).then((frontends) => this.setAvailableFrontends({ frontends })) + }).then(({ data: frontends }) => + this.setAvailableFrontends({ frontends }), + ) }, setAvailableFrontends({ frontends }) { @@ -355,12 +357,14 @@ export const useAdminSettingsStore = defineStore('adminSettings', { installFrontend() { return installFrontend({ credentials: useOAuthStore().token, - }) + }).then(({ data }) => data) }, // Statuses stuff async fetchStatuses(opts) { - const { total, activities } = await listStatuses({ + const { + data: { total, activities }, + } = await listStatuses({ credentials: useOAuthStore().token, opts, }) @@ -375,20 +379,21 @@ export const useAdminSettingsStore = defineStore('adminSettings', { } }, async changeStatusScope(opts) { - const raw = await changeStatusScope({ + const { data } = await changeStatusScope({ credentials: useOAuthStore().token, opts, }) - const status = parseStatus(raw) + const status = parseStatus(data) await window.vuex.dispatch('addNewStatuses', { statuses: [status] }) }, // Users stuff async fetchUsers(opts) { - const { users, count } = await listUsers({ + const { + data: { users, count }, + } = await listUsers({ credentials: useOAuthStore().token, - opts, }) @@ -412,7 +417,8 @@ export const useAdminSettingsStore = defineStore('adminSettings', { credentials: useOAuthStore().token, screen_name, }) - window.vuex.commit('updateUserAdminData', { user: result }) + + window.vuex.commit('updateUserAdminData', { user: result.data }) }, async deleteUsers({ users }) { const screen_names = users.map((u) => u.screen_name) @@ -423,7 +429,7 @@ export const useAdminSettingsStore = defineStore('adminSettings', { screen_names, }) - resultUserIds.forEach((userId) => { + resultUserIds.data.forEach((userId) => { window.vuex.dispatch( 'markStatusesAsDeleted', (status) => userId === status.user.id, @@ -439,7 +445,7 @@ export const useAdminSettingsStore = defineStore('adminSettings', { return resendConfirmationEmail({ credentials: useOAuthStore().token, screen_names, - }) + }).then(({ data }) => data) }, requirePasswordChange({ users }) { const screen_names = users.map((u) => u.screen_name) @@ -447,7 +453,7 @@ export const useAdminSettingsStore = defineStore('adminSettings', { return requirePasswordChange({ credentials: useOAuthStore().token, screen_names, - }) + }).then(({ data }) => data) }, // Singular only! disableMFA({ user }) { @@ -456,7 +462,7 @@ export const useAdminSettingsStore = defineStore('adminSettings', { return disableMFA({ credentials: useOAuthStore().token, screen_name, - }) + }).then(({ data }) => data) }, async setUsersTags({ users, tags, value }) { const screen_names = users.map((u) => u.screen_name) @@ -498,7 +504,7 @@ export const useAdminSettingsStore = defineStore('adminSettings', { value, }) - resultUsers.forEach((user) => { + resultUsers.data.forEach((user) => { window.vuex.commit('updateUserAdminData', { user }) }) }, @@ -512,7 +518,7 @@ export const useAdminSettingsStore = defineStore('adminSettings', { value, }) - resultUsers.forEach((user) => { + resultUsers.data.forEach((user) => { window.vuex.commit('updateUserAdminData', { user }) }) }, @@ -538,27 +544,31 @@ export const useAdminSettingsStore = defineStore('adminSettings', { screen_names, }) - resultUsers.forEach((user) => { + resultUsers.data.forEach((user) => { window.vuex.commit('updateUserAdminData', { user }) }) }, reloadEmoji() { - return reloadEmoji({ credentials: useOAuthStore().token }) + return reloadEmoji({ credentials: useOAuthStore().token }).then( + ({ data }) => data, + ) }, importEmojiFromFS() { - return importEmojiFromFS({ credentials: useOAuthStore().token }) + return importEmojiFromFS({ credentials: useOAuthStore().token }).then( + ({ data }) => data, + ) }, listEmojiPacks(params) { return listEmojiPacks({ ...params, - credentials: useOAuthStore().token - }) + credentials: useOAuthStore().token, + }).then(({ data }) => data) }, listRemoteEmojiPacks(params) { return listRemoteEmojiPacks({ ...params, credentials: useOAuthStore().token, - }) + }).then(({ data }) => data) }, addNewEmojiFile({ packName, file, shortcode, filename }) { return addNewEmojiFile({ @@ -567,7 +577,7 @@ export const useAdminSettingsStore = defineStore('adminSettings', { shortcode, filename, credentials: useOAuthStore().token, - }) + }).then(({ data }) => data) }, downloadRemoteEmojiPack({ instance, packName, as }) { return downloadRemoteEmojiPack({ @@ -575,33 +585,33 @@ export const useAdminSettingsStore = defineStore('adminSettings', { packName, as, credentials: useOAuthStore().token, - }) + }).then(({ data }) => data) }, downloadRemoteEmojiPackZIP({ url, packName }) { return downloadRemoteEmojiPackZIP({ url, packName, credentials: useOAuthStore().token, - }) + }).then(({ data }) => data) }, createEmojiPack({ name }) { return createEmojiPack({ name, credentials: useOAuthStore().token, - }) + }).then(({ data }) => data) }, deleteEmojiPack({ name }) { return createEmojiPack({ name, credentials: useOAuthStore().token, - }) + }).then(({ data }) => data) }, saveEmojiPackMetadata({ name, newData }) { return createEmojiPack({ name, newData, credentials: useOAuthStore().token, - }) + }).then(({ data }) => data) }, }, }) diff --git a/src/stores/announcements.js b/src/stores/announcements.js index 2b738b905..f739ae726 100644 --- a/src/stores/announcements.js +++ b/src/stores/announcements.js @@ -44,15 +44,16 @@ export const useAnnouncementsStore = defineStore('announcements', { const fetchAnnouncements = async () => { if (!isAdmin) { - return getAnnouncements({ + const result = await getAnnouncements({ credentials: useOAuthStore().token, }) + return result.data } - const all = await adminGetAnnouncements({ + const { data: all } = await adminGetAnnouncements({ credentials: useOAuthStore().token, }) - const visible = await getAnnouncements({ + const { data: visible } = await getAnnouncements({ credentials: useOAuthStore().token, }) const visibleObject = visible.reduce((a, c) => { diff --git a/src/stores/bookmark_folders.js b/src/stores/bookmark_folders.js index 928ad13a6..6ffec807b 100644 --- a/src/stores/bookmark_folders.js +++ b/src/stores/bookmark_folders.js @@ -31,10 +31,7 @@ export const useBookmarkFoldersStore = defineStore('bookmarkFolders', { this.fetcher = fetchBookmarkFolders({ credentials: useOAuthStore().token, }) - .then( - (folders) => this.setBookmarkFolders(folders), - (rej) => console.error(rej), - ) + .then(({ data: folders }) => this.setBookmarkFolders(folders)) .catch((e) => { console.error(e) }) @@ -61,7 +58,7 @@ export const useBookmarkFoldersStore = defineStore('bookmarkFolders', { name, emoji, credentials: useOAuthStore().token, - }).then((folder) => { + }).then(({ data: folder }) => { this.setBookmarkFolder(folder) return folder }) @@ -72,7 +69,7 @@ export const useBookmarkFoldersStore = defineStore('bookmarkFolders', { folderId, name, emoji, - }).then((folder) => { + }).then(({ data: folder }) => { this.setBookmarkFolder(folder) return folder }) diff --git a/src/stores/emoji.js b/src/stores/emoji.js index aefa79342..6ea8d1032 100644 --- a/src/stores/emoji.js +++ b/src/stores/emoji.js @@ -1,11 +1,11 @@ import { merge } from 'lodash' import { defineStore } from 'pinia' -import { useOAuthStore } from 'src/stores/oauth.js' import { useInstanceStore } from 'src/stores/instance.js' +import { useOAuthStore } from 'src/stores/oauth.js' -import { ensureFinalFallback } from 'src/i18n/languages.js' import { listEmojiPacks } from 'src/api/public.js' +import { ensureFinalFallback } from 'src/i18n/languages.js' import { annotationsLoader } from 'virtual:pleroma-fe/emoji-annotations' @@ -188,7 +188,8 @@ export const useEmojiStore = defineStore('emoji', { this.adminPacksLocalLoading = true this.adminPacksLocal = await this.getAdminPacks( useInstanceStore().server, - (params) => listEmojiPacks({ + (params) => + listEmojiPacks({ ...params, credentials: useOAuthStore().token, }), @@ -221,14 +222,13 @@ export const useEmojiStore = defineStore('emoji', { instance, page: i, pageSize, - }) - .then((pageData) => { - if (pageData.error !== undefined) { - return Promise.reject(pageData.error) - } + }).then((pageData) => { + if (pageData.error !== undefined) { + return Promise.reject(pageData.error) + } - return pageData.packs - }), + return pageData.packs + }), ) } diff --git a/src/stores/instance.js b/src/stores/instance.js index 8abb8078a..08bcde1ae 100644 --- a/src/stores/instance.js +++ b/src/stores/instance.js @@ -11,10 +11,11 @@ import { LOCAL_DEFAULT_CONFIG_DEFINITIONS, validateSetting, } from '../modules/default_config_state.js' -import { fetchKnownDomains } from 'src/api/public.js' import { useInterfaceStore } from 'src/stores/interface.js' +import { fetchKnownDomains } from 'src/api/public.js' + const REMOTE_INTERACTION_URL = '/main/ostatus' const ROOT_STATE_DEFINITIONS = { diff --git a/src/stores/lists.js b/src/stores/lists.js index 20529f06b..b612b403a 100644 --- a/src/stores/lists.js +++ b/src/stores/lists.js @@ -38,10 +38,7 @@ export const useListsStore = defineStore('lists', { this.fetcher = fetchLists({ credentials: useOAuthStore().token, }) - .then( - (lists) => this.setLists(lists), - (rej) => console.error(rej), - ) + .then(({ data: lists }) => this.setLists(lists)) .catch((e) => { console.error(e) }) diff --git a/src/stores/sync_config.js b/src/stores/sync_config.js index 5ff42adaf..f022ffe86 100644 --- a/src/stores/sync_config.js +++ b/src/stores/sync_config.js @@ -20,10 +20,11 @@ import { toRaw } from 'vue' import { CURRENT_UPDATE_COUNTER } from 'src/components/update_notification/update_notification.js' -import { useOAuthStore } from 'src/stores/oauth.js' import { useInstanceStore } from 'src/stores/instance.js' import { useLocalConfigStore } from 'src/stores/local_config.js' +import { useOAuthStore } from 'src/stores/oauth.js' +import { updateProfileJSON } from 'src/api/user.js' import { storage } from 'src/lib/storage.js' import { makeUndefined, @@ -32,7 +33,6 @@ import { validateSetting, } from 'src/modules/default_config_state.js' import { oldDefaultConfigSync } from 'src/modules/old_default_config_state.js' -import { updateProfileJSON } from 'src/api/user.js' export const VERSION = 2 export const NEW_USER_DATE = new Date('2026-03-16') // date of writing this, basically diff --git a/src/stores/user_highlight.js b/src/stores/user_highlight.js index 41570521f..ca334f579 100644 --- a/src/stores/user_highlight.js +++ b/src/stores/user_highlight.js @@ -16,8 +16,8 @@ import { toRaw } from 'vue' import { useOAuthStore } from 'src/stores/oauth.js' -import { storage } from 'src/lib/storage.js' import { updateProfileJSON } from 'src/api/user.js' +import { storage } from 'src/lib/storage.js' export const NEW_USER_DATE = new Date('2022-08-04') // date of writing this, basically