feat(mastodon): say in Settings when the instance no longer accepts the token

When the instance refuses the token on a post (it was revoked or expired), the account now remembers it: GET /me/mastodon says problem: token_refused and nothing is posted until I connect again with a new token.

- migration 0027 adds mastodon_accounts.problem (SQLite and PostgreSQL).
- Only a refused token marks the account; an instance that is down does not.
- Saving the account again clears the problem.
- Settings shows the warning and the form with the instance already filled in, next to Disconnect.
- English and Italian.
This commit is contained in:
tgiachi 2026-11-14 10:04:07 +01:00
commit a7a258464b
5 changed files with 229 additions and 21 deletions

View file

@ -3749,6 +3749,12 @@ export interface components {
instance?: string;
/** @description Your handle, like `@name@mastodon.social`. Only when connected. */
handle?: string;
/**
* @description Only when posts are paused. `token_refused`: the instance refused the token on a post (it was revoked or
* expired), so nothing is posted until you connect again with a new token (`PUT /me/mastodon`).
* @enum {string}
*/
problem?: "token_refused";
};
ConnectMastodon: {
/** @description The address of your instance: `mastodon.social` or `https://mastodon.social`. */

View file

@ -15,6 +15,7 @@ interface Account {
connected: boolean
instance?: string
handle?: string
problem?: 'token_refused'
}
function setup(initial: Account, over: MockRoutes = {}) {
@ -94,3 +95,26 @@ test('a connected account says where the listens go, and can be disconnected', a
expect(await screen.findByLabelText('Instance')).toBeInTheDocument()
expect(calls.filter((c) => c.key === 'DELETE /api/v1/me/mastodon')).toHaveLength(1)
})
test('a token the instance no longer accepts is said at once, and a new one can be pasted', async () => {
const { calls } = setup({ connected: true, instance: 'https://mastodon.social', handle: '@squid@mastodon.social', problem: 'token_refused' })
expect(await screen.findByRole('alert')).toHaveTextContent('Mastodon no longer accepts the token for @squid@mastodon.social, so nothing is posted.')
expect(screen.queryByText(/Your listens are posted to/)).not.toBeInTheDocument()
// The instance is already there; only the new token is missing.
expect(screen.getByLabelText('Instance')).toHaveValue('mastodon.social')
expect(screen.getByLabelText('Access token')).toHaveValue('')
expect(screen.getByRole('button', { name: 'Disconnect' })).toBeInTheDocument()
await userEvent.type(screen.getByLabelText('Access token'), 'new-token')
await userEvent.click(screen.getByRole('button', { name: 'Connect again' }))
expect(JSON.parse(String(puts(calls)[0].init.body))).toEqual({ instance: 'mastodon.social', token: 'new-token' })
// Connected again: the warning is gone and the account is shown as before.
expect(await screen.findByText(/Your listens are posted to/)).toBeInTheDocument()
expect(screen.queryByRole('alert')).not.toBeInTheDocument()
})
test('with a refused token the account can still be disconnected', async () => {
const { calls } = setup({ connected: true, instance: 'https://mastodon.social', handle: '@squid@mastodon.social', problem: 'token_refused' })
await userEvent.click(await screen.findByRole('button', { name: 'Disconnect' }))
expect(await screen.findByRole('button', { name: 'Connect' })).toBeInTheDocument()
expect(calls.filter((c) => c.key === 'DELETE /api/v1/me/mastodon')).toHaveLength(1)
})

View file

@ -16,18 +16,21 @@ export default function MastodonBlock() {
const account = useMastodon()
const connect = useConnectMastodon()
const disconnect = useDisconnectMastodon()
const [instance, setInstance] = useState('')
// What was typed; until then, the instance of an account whose token needs replacing.
const [typed, setTyped] = useState<string | null>(null)
const [token, setToken] = useState('')
function submit(event: FormEvent) {
event.preventDefault()
connect.mutate({ instance: instance.trim(), token: token.trim() }, { onSuccess: () => setToken('') })
connect.mutate({ instance: instance.trim(), token: token.trim() }, { onSuccess: () => { setToken(''); setTyped(null) } })
}
if (account.data === undefined) return null
const error = connect.error ?? disconnect.error
const refused = account.data.connected && account.data.problem === 'token_refused'
const instance = typed ?? (refused ? (account.data.instance ?? '').replace(/^https:\/\//, '') : '')
if (account.data.connected) {
if (account.data.connected && !refused) {
return (
<div className="flex max-w-xl flex-col gap-3">
<p className="font-semibold">{t('mastodon.connected', { handle: account.data.handle })}</p>
@ -46,7 +49,13 @@ export default function MastodonBlock() {
return (
<form onSubmit={submit} className="flex max-w-xl flex-col gap-4">
<p className="text-sm text-muted-foreground">{t('mastodon.intro')}</p>
{refused ? (
<p role="alert" className="text-sm font-semibold text-destructive">
{t('mastodon.refused', { handle: account.data.handle })}
</p>
) : (
<p className="text-sm text-muted-foreground">{t('mastodon.intro')}</p>
)}
<div className="flex flex-col gap-2">
<Label htmlFor="mastodon-instance">{t('mastodon.instance')}</Label>
<Input
@ -57,7 +66,7 @@ export default function MastodonBlock() {
spellCheck={false}
placeholder={t('mastodon.instancePlaceholder')}
value={instance}
onChange={(e) => setInstance(e.target.value)}
onChange={(e) => setTyped(e.target.value)}
required
/>
</div>
@ -71,9 +80,16 @@ export default function MastodonBlock() {
{t(errorMessageKey(error instanceof ApiError ? error.code : 'unknown'))}
</p>
)}
<Button type="submit" disabled={connect.isPending} className="self-start">
{connect.isPending ? t('mastodon.connecting') : t('mastodon.connect')}
</Button>
<div className="flex flex-wrap gap-3">
<Button type="submit" disabled={connect.isPending}>
{connect.isPending ? t('mastodon.connecting') : t(refused ? 'mastodon.connectAgain' : 'mastodon.connect')}
</Button>
{refused && (
<Button type="button" variant="outline" disabled={disconnect.isPending} onClick={() => disconnect.mutate()}>
{t('mastodon.disconnect')}
</Button>
)}
</div>
</form>
)
}

View file

@ -71,6 +71,8 @@
"tokenHelp": "In your instance open Preferences, Development, New application, and give it only the write:statuses permission. Paste its access token here. The server keeps it and never shows it again.",
"connect": "Connect",
"connecting": "Connecting…",
"refused": "Mastodon no longer accepts the token for {{handle}}, so nothing is posted. Make a new token as described below and paste it here.",
"connectAgain": "Connect again",
"connected": "Your listens are posted to {{handle}}.",
"connectedHelp": "Each track that counts as a listen is posted as an unlisted status with #NowPlaying. Music of a friend is never posted.",
"disconnect": "Disconnect"

View file

@ -71,6 +71,8 @@
"tokenHelp": "Nella tua istanza apri Preferenze, Sviluppo, Nuova applicazione, e dalle solo il permesso write:statuses. Incolla qui il suo token di accesso. Il server lo tiene e non lo mostra più.",
"connect": "Collega",
"connecting": "Collego…",
"refused": "Mastodon non accetta più il token di {{handle}}, quindi non si pubblica niente. Crea un nuovo token come descritto sotto e incollalo qui.",
"connectAgain": "Collega di nuovo",
"connected": "I tuoi ascolti vengono pubblicati su {{handle}}.",
"connectedHelp": "Ogni brano che conta come ascolto viene pubblicato come post non in elenco con #NowPlaying. La musica di un amico non viene mai pubblicata.",
"disconnect": "Scollega"