diff --git a/src/actions/fetch.js b/src/actions/fetch.js index f42ea3a..5499869 100644 --- a/src/actions/fetch.js +++ b/src/actions/fetch.js @@ -34,6 +34,17 @@ async function fetchWithTimeout(resource, options) { async function parseAPIresponse(response) { if (!response.ok) { const code = response.status; + if (code === 429) { + let error = t`You made too many requests`; + const retryAfter = response.headers.get('Retry-After'); + if (!Number.isNaN(Number(retryAfter))) { + const ti = Math.floor(retryAfter / 60); + error += `, ${t`try again after ${ti}min`}`; + } + return { + errors: [error], + }; + } return { errors: [t`Connection error ${code} :(`], }; @@ -169,11 +180,17 @@ export async function requestLeaveChan(channelId: boolean) { return t`Unknown Error`; } -export function requestSolveCaptcha(text) { - return makeAPIPOSTRequest( +export async function requestSolveCaptcha(text) { + const res = await makeAPIPOSTRequest( 'api/captcha', { text }, ); + if (!res.errors && !res.success) { + return { + errors: [t`Server answered with gibberish :(`], + }; + } + return res; } export function requestPasswordChange(newPassword, password) { diff --git a/src/actions/index.js b/src/actions/index.js index 42cf06a..062a27c 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -120,9 +120,9 @@ export function toggleOpenMenu(): Action { }; } -export function setPlaceAllowed(requestingPixel: boolean): Action { +export function setRequestingPixel(requestingPixel: boolean): Action { return { - type: 'SET_PLACE_ALLOWED', + type: 'SET_REQUESTING_PIXEL', requestingPixel, }; } diff --git a/src/actions/types.js b/src/actions/types.js index 5b75cd4..9dadb69 100644 --- a/src/actions/types.js +++ b/src/actions/types.js @@ -29,7 +29,7 @@ export type Action = | { type: 'SELECT_STYLE', style: string } | { type: 'SET_NOTIFICATION', notification: string } | { type: 'UNSET_NOTIFICATION' } - | { type: 'SET_PLACE_ALLOWED', requestingPixel: boolean } + | { type: 'SET_REQUESTING_PIXEL', requestingPixel: boolean } | { type: 'SET_HOVER', hover: Cell } | { type: 'UNSET_HOVER' } | { type: 'SET_WAIT', wait: ?number } diff --git a/src/components/Alert.jsx b/src/components/Alert.jsx index 242db59..c43c2e1 100644 --- a/src/components/Alert.jsx +++ b/src/components/Alert.jsx @@ -56,7 +56,7 @@ const Alert = () => {

{(alertType === 'captcha') - ? + ? : ( diff --git a/src/components/UserArea.jsx b/src/components/UserArea.jsx index 28c5ada..cffd819 100644 --- a/src/components/UserArea.jsx +++ b/src/components/UserArea.jsx @@ -183,25 +183,6 @@ class UserArea extends React.Component { done={() => { this.setState({ socialSettingsExtended: false }); }} /> )} - {(typeof window.hcaptcha !== 'undefined') - && ( - hCaptcha { - window.pixel = null; - window.hcaptcha.execute(); - }} - style={{ - width: '5%', - height: '5%', - paddingTop: 20, - cursor: 'pointer', - }} - /> - )}

); } diff --git a/src/reducers/user.js b/src/reducers/user.js index 37a5a08..2739ab7 100644 --- a/src/reducers/user.js +++ b/src/reducers/user.js @@ -68,7 +68,7 @@ export default function user( }; } - case 'SET_PLACE_ALLOWED': { + case 'SET_REQUESTING_PIXEL': { const { requestingPixel } = action; return { ...state, diff --git a/src/store/rendererHook.js b/src/store/rendererHook.js index c313349..5b6d9ac 100644 --- a/src/store/rendererHook.js +++ b/src/store/rendererHook.js @@ -47,7 +47,7 @@ export default (store) => (next) => (action) => { break; } - case 'SET_PLACE_ALLOWED': { + case 'SET_REQUESTING_PIXEL': { const renderer = getRenderer(); renderer.forceNextSubRender = true; break; diff --git a/src/ui/placePixel.js b/src/ui/placePixel.js index 61d5943..ac4c3b9 100644 --- a/src/ui/placePixel.js +++ b/src/ui/placePixel.js @@ -8,7 +8,7 @@ import { t } from 'ttag'; import { notify, - setPlaceAllowed, + setRequestingPixel, sweetAlert, gotCoolDownDelta, pixelFailure, @@ -48,7 +48,7 @@ function requestFromQueue(store) { pixelTimeout = setTimeout(() => { pixelQueue = []; pixelTimeout = null; - store.dispatch(setPlaceAllowed(true)); + store.dispatch(setRequestingPixel(true)); store.dispatch(sweetAlert( t`Error :(`, t`Didn't get an answer from pixelplanet. Maybe try to refresh?`, @@ -60,16 +60,7 @@ function requestFromQueue(store) { lastRequestValues = pixelQueue.shift(); const { i, j, pixels } = lastRequestValues; ProtocolClient.requestPlacePixels(i, j, pixels); - store.dispatch(setPlaceAllowed(false)); - - // TODO: - // this is for resending after captcha returned - // window is ugly, put it into redux or something - window.pixel = { - i, - j, - pixels, - }; + store.dispatch(setRequestingPixel(false)); } export function receivePixelUpdate( @@ -245,6 +236,7 @@ export function receivePixelReturn( 'captcha', t`OK`, )); + store.dispatch(setRequestingPixel(true)); return; case 11: errorTitle = t`No Proxies Allowed :(`; @@ -265,7 +257,7 @@ export function receivePixelReturn( )); } - store.dispatch(setPlaceAllowed(true)); + store.dispatch(setRequestingPixel(true)); /* start next request if queue isn't empty */ requestFromQueue(store); }