Merge branch 'api-refactor' into shigusegubu-themes3

This commit is contained in:
Henry Jameson 2026-06-17 19:43:13 +03:00
commit 31a832fbe5
7 changed files with 52 additions and 51 deletions

View file

@ -5,8 +5,9 @@ import { paramsString, promisedRequest } from './helpers.js'
import { StatusCodeError } from 'src/services/errors/errors.js' import { StatusCodeError } from 'src/services/errors/errors.js'
const REDIRECT_URI = `${window.location.origin}/oauth-callback` const REDIRECT_URI = `${window.location.origin}/oauth-callback`
const MASTODON_APP_VERIFY_URL = '/api/v1/apps/verify_credentials'
export const createApp = ({ instance }) => { export const createApp = () => {
const formData = new window.FormData() const formData = new window.FormData()
formData.append('client_name', 'PleromaFE') formData.append('client_name', 'PleromaFE')
@ -15,7 +16,7 @@ export const createApp = ({ instance }) => {
formData.append('scopes', 'read write follow push admin') formData.append('scopes', 'read write follow push admin')
return promisedRequest({ return promisedRequest({
url: `${instance}/api/v1/apps`, url: '/api/v1/apps',
method: 'POST', method: 'POST',
formData, formData,
}).then(({ data, ...rest }) => ({ }).then(({ data, ...rest }) => ({
@ -28,6 +29,12 @@ export const createApp = ({ instance }) => {
})) }))
} }
export const verifyAppToken = ({ credentials }) =>
promisedRequest({
url: MASTODON_APP_VERIFY_URL,
credentials,
})
export const getLoginUrl = ({ instance, clientId }) => { export const getLoginUrl = ({ instance, clientId }) => {
const data = { const data = {
responseType: 'code', responseType: 'code',
@ -42,7 +49,6 @@ export const getLoginUrl = ({ instance, clientId }) => {
export const getTokenWithCredentials = ({ export const getTokenWithCredentials = ({
clientId, clientId,
clientSecret, clientSecret,
instance,
username, username,
password, password,
}) => { }) => {
@ -55,13 +61,13 @@ export const getTokenWithCredentials = ({
formData.append('password', password) formData.append('password', password)
return promisedRequest({ return promisedRequest({
url: `${instance}/oauth/token`, url: '/oauth/token',
method: 'POST', method: 'POST',
formData, formData,
}) })
} }
export const getToken = ({ clientId, clientSecret, instance, code }) => { export const getToken = ({ clientId, clientSecret, code }) => {
const formData = new window.FormData() const formData = new window.FormData()
formData.append('client_id', clientId) formData.append('client_id', clientId)
@ -71,13 +77,13 @@ export const getToken = ({ clientId, clientSecret, instance, code }) => {
formData.append('redirect_uri', `${window.location.origin}/oauth-callback`) formData.append('redirect_uri', `${window.location.origin}/oauth-callback`)
return promisedRequest({ return promisedRequest({
url: `${instance}/oauth/token`, url: '/oauth/token',
method: 'POST', method: 'POST',
formData, formData,
}) })
} }
export const getClientToken = ({ clientId, clientSecret, instance }) => { export const getClientToken = ({ clientId, clientSecret }) => {
const formData = new window.FormData() const formData = new window.FormData()
formData.append('client_id', clientId) formData.append('client_id', clientId)
@ -86,12 +92,13 @@ export const getClientToken = ({ clientId, clientSecret, instance }) => {
formData.append('redirect_uri', `${window.location.origin}/oauth-callback`) formData.append('redirect_uri', `${window.location.origin}/oauth-callback`)
return promisedRequest({ return promisedRequest({
url: `${instance}/oauth/token`, url: '/oauth/token',
method: 'POST', method: 'POST',
formData, formData,
}) })
} }
export const verifyOTPCode = ({ app, instance, mfaToken, code }) => {
export const verifyOTPCode = ({ app, mfaToken, code }) => {
const formData = new window.FormData() const formData = new window.FormData()
formData.append('client_id', app.client_id) formData.append('client_id', app.client_id)
@ -101,13 +108,13 @@ export const verifyOTPCode = ({ app, instance, mfaToken, code }) => {
formData.append('challenge_type', 'totp') formData.append('challenge_type', 'totp')
return promisedRequest({ return promisedRequest({
url: `${instance}/oauth/mfa/challenge`, url: '/oauth/mfa/challenge',
method: 'POST', method: 'POST',
formData, formData,
}) })
} }
export const verifyRecoveryCode = ({ app, instance, mfaToken, code }) => { export const verifyRecoveryCode = ({ app, mfaToken, code }) => {
const formData = new window.FormData() const formData = new window.FormData()
formData.append('client_id', app.client_id) formData.append('client_id', app.client_id)
@ -117,13 +124,13 @@ export const verifyRecoveryCode = ({ app, instance, mfaToken, code }) => {
formData.append('challenge_type', 'recovery') formData.append('challenge_type', 'recovery')
return promisedRequest({ return promisedRequest({
url: `${instance}/oauth/mfa/challenge`, url: '/oauth/mfa/challenge',
method: 'POST', method: 'POST',
formData, formData,
}) })
} }
export const revokeToken = ({ app, instance, token }) => { export const revokeToken = ({ app, token }) => {
const formData = new window.FormData() const formData = new window.FormData()
formData.append('client_id', app.clientId) formData.append('client_id', app.clientId)
@ -131,7 +138,7 @@ export const revokeToken = ({ app, instance, token }) => {
formData.append('token', token) formData.append('token', token)
return promisedRequest({ return promisedRequest({
url: `${instance}/oauth/revoke`, url: '/oauth/revoke',
method: 'POST', method: 'POST',
formData, formData,
}) })

View file

@ -54,7 +54,6 @@ const MASTODON_SEARCH_2 = '/api/v2/search'
const MASTODON_USER_SEARCH_URL = '/api/v1/accounts/search' const MASTODON_USER_SEARCH_URL = '/api/v1/accounts/search'
const MASTODON_STREAMING = '/api/v1/streaming' const MASTODON_STREAMING = '/api/v1/streaming'
const MASTODON_KNOWN_DOMAIN_LIST_URL = '/api/v1/instance/peers' const MASTODON_KNOWN_DOMAIN_LIST_URL = '/api/v1/instance/peers'
const MASTODON_ANNOUNCEMENTS_URL = '/api/v1/announcements'
const PLEROMA_EMOJI_REACTIONS_URL = (id) => const PLEROMA_EMOJI_REACTIONS_URL = (id) =>
`/api/v1/pleroma/statuses/${id}/reactions` `/api/v1/pleroma/statuses/${id}/reactions`
const PLEROMA_SCROBBLES_URL = (id, { maxId, sinceId, minId, limit, offset }) => const PLEROMA_SCROBBLES_URL = (id, { maxId, sinceId, minId, limit, offset }) =>
@ -431,9 +430,6 @@ export const search2 = ({
export const fetchKnownDomains = ({ credentials }) => export const fetchKnownDomains = ({ credentials }) =>
promisedRequest({ url: MASTODON_KNOWN_DOMAIN_LIST_URL, credentials }) promisedRequest({ url: MASTODON_KNOWN_DOMAIN_LIST_URL, credentials })
export const getAnnouncements = ({ credentials }) =>
promisedRequest({ url: MASTODON_ANNOUNCEMENTS_URL, credentials })
export const getMastodonSocketURI = ( export const getMastodonSocketURI = (
{ credentials, stream, args = {} }, { credentials, stream, args = {} },
base, base,

View file

@ -81,6 +81,7 @@ const MASTODON_MUTE_CONVERSATION = (id) => `/api/v1/statuses/${id}/mute`
const MASTODON_UNMUTE_CONVERSATION = (id) => `/api/v1/statuses/${id}/unmute` const MASTODON_UNMUTE_CONVERSATION = (id) => `/api/v1/statuses/${id}/unmute`
const MASTODON_DOMAIN_BLOCKS_URL = '/api/v1/domain_blocks' const MASTODON_DOMAIN_BLOCKS_URL = '/api/v1/domain_blocks'
const MASTODON_LISTS_URL = '/api/v1/lists' const MASTODON_LISTS_URL = '/api/v1/lists'
const MASTODON_ANNOUNCEMENTS_URL = '/api/v1/announcements'
const MASTODON_ANNOUNCEMENTS_DISMISS_URL = (id) => const MASTODON_ANNOUNCEMENTS_DISMISS_URL = (id) =>
`/api/v1/announcements/${id}/dismiss` `/api/v1/announcements/${id}/dismiss`
const PLEROMA_EMOJI_REACT_URL = (id, emoji) => const PLEROMA_EMOJI_REACT_URL = (id, emoji) =>
@ -335,13 +336,6 @@ export const dismissNotification = ({ credentials, id }) =>
credentials, credentials,
}) })
export const dismissAnnouncement = ({ id, credentials }) =>
promisedRequest({
url: MASTODON_ANNOUNCEMENTS_DISMISS_URL(id),
credentials,
method: 'POST',
})
export const markNotificationsAsSeen = ({ export const markNotificationsAsSeen = ({
id, id,
credentials, credentials,
@ -363,6 +357,17 @@ export const markNotificationsAsSeen = ({
}) })
} }
// #Announcements
export const getAnnouncements = ({ credentials }) =>
promisedRequest({ url: MASTODON_ANNOUNCEMENTS_URL, credentials })
export const dismissAnnouncement = ({ id, credentials }) =>
promisedRequest({
url: MASTODON_ANNOUNCEMENTS_DISMISS_URL(id),
credentials,
method: 'POST',
})
// #Imports // #Imports
export const importMutes = ({ file, credentials }) => { export const importMutes = ({ file, credentials }) => {
const formData = new FormData() const formData = new FormData()

View file

@ -566,10 +566,6 @@ const afterStoreSetup = async ({ pinia, store, storageError, i18n }) => {
getInstanceConfig({ store }), getInstanceConfig({ store }),
]).catch((e) => Promise.reject(e)) ]).catch((e) => Promise.reject(e))
// Start fetching things that don't need to block the UI
store.dispatch('fetchMutes')
store.dispatch('loadDrafts')
useAnnouncementsStore().startFetchingAnnouncements()
getTOS({ store }) getTOS({ store })
getStickers({ store }) getStickers({ store })

View file

@ -18,6 +18,7 @@ import {
windowWidth, windowWidth,
} from '../services/window_utils/window_utils' } from '../services/window_utils/window_utils'
import { useAnnouncementsStore } from 'src/stores/announcements.js'
import { useBookmarkFoldersStore } from 'src/stores/bookmark_folders.js' import { useBookmarkFoldersStore } from 'src/stores/bookmark_folders.js'
import { useEmojiStore } from 'src/stores/emoji.js' import { useEmojiStore } from 'src/stores/emoji.js'
import { useInstanceStore } from 'src/stores/instance.js' import { useInstanceStore } from 'src/stores/instance.js'
@ -832,8 +833,11 @@ const users = {
startPolling() startPolling()
} }
// Get user mutes // Start fetching things that don't need to block the UI
useAnnouncementsStore().startFetchingAnnouncements()
dispatch('fetchMutes') dispatch('fetchMutes')
dispatch('loadDrafts')
useInterfaceStore().setLayoutWidth(windowWidth()) useInterfaceStore().setLayoutWidth(windowWidth())
useInterfaceStore().setLayoutHeight(windowHeight()) useInterfaceStore().setLayoutHeight(windowHeight())

View file

@ -2,7 +2,7 @@ import { defineStore } from 'pinia'
import { useOAuthStore } from 'src/stores/oauth.js' import { useOAuthStore } from 'src/stores/oauth.js'
import { getAnnouncements } from 'src/api/public.js' import { dismissAnnouncement, getAnnouncements } from 'src/api/user.js'
const FETCH_ANNOUNCEMENT_INTERVAL_MS = 1000 * 60 * 5 const FETCH_ANNOUNCEMENT_INTERVAL_MS = 1000 * 60 * 5
@ -36,10 +36,6 @@ export const useAnnouncementsStore = defineStore('announcements', {
currentUser.privileges.has('announcements_manage_announcements') currentUser.privileges.has('announcements_manage_announcements')
try { try {
if (currentUser) {
this.userActions = await import('src/api/user.js')
}
if (isAdmin) { if (isAdmin) {
this.adminActions = await import('src/api/admin.js') this.adminActions = await import('src/api/admin.js')
} else { } else {
@ -86,20 +82,18 @@ export const useAnnouncementsStore = defineStore('announcements', {
} }
}, },
markAnnouncementAsRead(id) { markAnnouncementAsRead(id) {
return this.userActions return dismissAnnouncement({
.dismissAnnouncement({ id,
id, credentials: useOAuthStore().token,
credentials: useOAuthStore().token, }).then(() => {
}) const index = this.announcements.findIndex((a) => a.id === id)
.then(() => {
const index = this.announcements.findIndex((a) => a.id === id)
if (index < 0) { if (index < 0) {
return return
} }
this.announcements[index].read = true this.announcements[index].read = true
}) })
}, },
startFetchingAnnouncements() { startFetchingAnnouncements() {
if (this.fetchAnnouncementsTimer) { if (this.fetchAnnouncementsTimer) {

View file

@ -2,8 +2,7 @@ import { defineStore } from 'pinia'
import { useInstanceStore } from 'src/stores/instance.js' import { useInstanceStore } from 'src/stores/instance.js'
import { createApp, getClientToken } from 'src/api/oauth.js' import { createApp, getClientToken, verifyAppToken } from 'src/api/oauth.js'
import { verifyCredentials } from 'src/api/public.js'
// status codes about verifyAppToken (GET /api/v1/apps/verify_credentials) // status codes about verifyAppToken (GET /api/v1/apps/verify_credentials)
const isAppTokenRejected = (error) => const isAppTokenRejected = (error) =>
@ -39,7 +38,7 @@ export const useOAuthStore = defineStore('oauth', {
}), }),
getters: { getters: {
token() { token() {
return this.userToken || this.appToken return this.userToken
}, },
}, },
actions: { actions: {
@ -90,7 +89,7 @@ export const useOAuthStore = defineStore('oauth', {
async ensureAppToken() { async ensureAppToken() {
if (this.appToken) { if (this.appToken) {
try { try {
await verifyCredentials({ await verifyAppToken({
credentials: this.appToken, credentials: this.appToken,
}) })
return this.appToken return this.appToken