pstream is dead; long live pstream taciturnaxolotl.github.io/pstream-ng/
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

prevent multiple requests

Pas f75db9cf ff7a5f49

+19 -9
+19 -9
src/backend/metadata/traktApi.ts
··· 19 19 20 20 /** 21 21 * Get turnstile token from cookie or fetch new one 22 + * Returns an object indicating if the token was cached or freshly fetched 22 23 */ 23 - const getFreshTurnstileToken = async (): Promise<string> => { 24 + const getFreshTurnstileToken = async (): Promise<{ 25 + token: string; 26 + isCached: boolean; 27 + }> => { 24 28 const now = Date.now(); 25 29 26 30 // Check if we have a valid cached token in cookie ··· 38 42 39 43 // Check if token is still valid (within 10 minutes) 40 44 if (token && timestamp && now - timestamp < TOKEN_CACHE_DURATION) { 41 - return token; 45 + return { token, isCached: true }; 42 46 } 43 47 } catch (error) { 44 48 // Invalid cookie format, continue to get new token ··· 63 67 document.cookie = `${TOKEN_COOKIE_NAME}=${cookieValue}; expires=${expiresAt.toUTCString()}; path=/; SameSite=Strict`; 64 68 } 65 69 66 - return token; 70 + return { token, isCached: false }; 67 71 } catch (error) { 68 72 throw new Error(`Failed to get turnstile token: ${error}`); 69 73 } ··· 143 147 for (let attempt = 0; attempt < 2; attempt += 1) { 144 148 try { 145 149 // 1. Get turnstile token (cached or fresh) 146 - const turnstileToken = await getFreshTurnstileToken(); 150 + const { token: turnstileToken, isCached } = 151 + await getFreshTurnstileToken(); 147 152 148 - // 2. Validate token with server and store for 10 minutes 149 - await validateAndStoreToken(turnstileToken); 153 + // 2. Only validate with server if token wasn't cached (newly fetched) 154 + if (!isCached) { 155 + await validateAndStoreToken(turnstileToken); 156 + } 150 157 151 158 // 3. Make the API request with validated token 152 159 const response = await fetch(`${TRAKT_BASE_URL}${endpoint}`, { ··· 219 226 for (let attempt = 0; attempt < 2; attempt += 1) { 220 227 try { 221 228 // 1. Get turnstile token (cached or fresh) 222 - const turnstileToken = await getFreshTurnstileToken(); 229 + const { token: turnstileToken, isCached } = 230 + await getFreshTurnstileToken(); 223 231 224 - // 2. Validate token with server and store for 10 minutes 225 - await validateAndStoreToken(turnstileToken); 232 + // 2. Only validate with server if token wasn't cached (newly fetched) 233 + if (!isCached) { 234 + await validateAndStoreToken(turnstileToken); 235 + } 226 236 227 237 // 3. Make the API request with validated token 228 238 const response = await fetch(`${TRAKT_BASE_URL}${url}`, {