44import static app .revanced .extension .youtube .returnyoutubedislike .requests .ReturnYouTubeDislikeRoutes .getRYDConnectionFromRoute ;
55
66import android .util .Base64 ;
7+ import android .widget .Toast ;
78
8- import androidx .annotation .NonNull ;
99import androidx .annotation .Nullable ;
1010
1111import org .json .JSONException ;
3030
3131public class ReturnYouTubeDislikeApi {
3232 /**
33- * {@link #fetchVotes(String)} TCP connection timeout
33+ * {@link #fetchVotes(String)} TCP connection timeout.
3434 */
35- private static final int API_GET_VOTES_TCP_TIMEOUT_MILLISECONDS = 2 * 1000 ; // 2 Seconds.
35+ private static final int API_GET_VOTES_TCP_TIMEOUT_MILLISECONDS = 3 * 1000 ; // 3 Seconds.
3636
3737 /**
3838 * {@link #fetchVotes(String)} HTTP read timeout.
3939 * To locally debug and force timeouts, change this to a very small number (ie: 100)
4040 */
41- private static final int API_GET_VOTES_HTTP_TIMEOUT_MILLISECONDS = 4 * 1000 ; // 4 Seconds.
41+ private static final int API_GET_VOTES_HTTP_TIMEOUT_MILLISECONDS = 7 * 1000 ; // 7 Seconds.
4242
4343 /**
4444 * Default connection and response timeout for voting and registration.
@@ -53,6 +53,15 @@ public class ReturnYouTubeDislikeApi {
5353 */
5454 private static final int HTTP_STATUS_CODE_SUCCESS = 200 ;
5555
56+ /**
57+ * RYD API sometimes returns 401 (authorization error), even though the user id is valid.
58+ * There is no known fix for this (resetting to a different user id does not fix it),
59+ * so instead just quietly ignore the error.
60+ *
61+ * See <a href="https://github.com/Anarios/return-youtube-dislike/issues/1153">RYD bug report</a>.
62+ */
63+ private static final int HTTP_STATUS_CODE_UNAUTHORIZED = 401 ;
64+
5665 /**
5766 * Indicates a client rate limit has been reached and the client must back off.
5867 */
@@ -232,14 +241,20 @@ private static void updateRateLimitAndStats(long timeNetworkCallStarted, boolean
232241 }
233242 }
234243
235- private static void handleConnectionError (@ NonNull String toastMessage ,
236- @ Nullable Exception ex ,
237- boolean showLongToast ) {
244+ /**
245+ * @param toastDuration Either {@link Toast#LENGTH_SHORT} or {@link Toast#LENGTH_LONG}.
246+ */
247+ public static void handleConnectionError (String toastMessage ,
248+ @ Nullable Integer responseCode ,
249+ @ Nullable Exception ex ,
250+ @ Nullable Integer toastDuration ) {
238251 if (!lastApiCallFailed && Settings .RYD_TOAST_ON_CONNECTION_ERROR .get ()) {
239- if (showLongToast ) {
240- Utils .showToastLong (toastMessage );
241- } else {
242- Utils .showToastShort (toastMessage );
252+ if (responseCode != null && responseCode == HTTP_STATUS_CODE_UNAUTHORIZED ) {
253+ Logger .printInfo (() -> "Ignoring status code " + HTTP_STATUS_CODE_UNAUTHORIZED
254+ + " (API authorization erorr)" );
255+ return ; // Do not set api failure field.
256+ } else if (toastDuration != null ) {
257+ Utils .showToast (toastMessage , toastDuration );
243258 }
244259 }
245260 lastApiCallFailed = true ;
@@ -297,13 +312,13 @@ public static RYDVoteData fetchVotes(String videoId) {
297312 } else {
298313 // Unexpected response code. Most likely RYD is temporarily broken.
299314 handleConnectionError (str ("revanced_ryd_failure_connection_status_code" , responseCode ),
300- null , true );
315+ responseCode , null , Toast . LENGTH_LONG );
301316 }
302317 connection .disconnect (); // Something went wrong, might as well disconnect.
303318 } catch (SocketTimeoutException ex ) {
304- handleConnectionError ((str ("revanced_ryd_failure_connection_timeout" )), ex , false );
319+ handleConnectionError ((str ("revanced_ryd_failure_connection_timeout" )), null , ex , Toast . LENGTH_SHORT );
305320 } catch (IOException ex ) {
306- handleConnectionError ((str ("revanced_ryd_failure_generic" , ex .getMessage ())), ex , true );
321+ handleConnectionError ((str ("revanced_ryd_failure_generic" , ex .getMessage ())), null , ex , Toast . LENGTH_LONG );
307322 } catch (Exception ex ) {
308323 // should never happen
309324 Logger .printException (() -> "fetchVotes failure" , ex );
@@ -344,13 +359,14 @@ public static String registerAsNewUser() {
344359 String solution = solvePuzzle (challenge , difficulty );
345360 return confirmRegistration (userId , solution );
346361 }
362+
347363 handleConnectionError (str ("revanced_ryd_failure_connection_status_code" , responseCode ),
348- null , true );
364+ responseCode , null , Toast . LENGTH_LONG );
349365 connection .disconnect ();
350366 } catch (SocketTimeoutException ex ) {
351- handleConnectionError (str ("revanced_ryd_failure_connection_timeout" ), ex , false );
367+ handleConnectionError (str ("revanced_ryd_failure_connection_timeout" ), null , ex , Toast . LENGTH_SHORT );
352368 } catch (IOException ex ) {
353- handleConnectionError (str ("revanced_ryd_failure_generic" , "registration failed" ), ex , true );
369+ handleConnectionError (str ("revanced_ryd_failure_generic" , "registration failed" ), null , ex , Toast . LENGTH_LONG );
354370 } catch (Exception ex ) {
355371 Logger .printException (() -> "Failed to register user" , ex ); // should never happen
356372 }
@@ -393,12 +409,12 @@ private static String confirmRegistration(String userId, String solution) {
393409 Logger .printInfo (() -> "Failed to confirm registration for user: " + userId
394410 + " solution: " + solution + " responseCode: " + responseCode + " response: '" + response + "''" );
395411 handleConnectionError (str ("revanced_ryd_failure_connection_status_code" , responseCode ),
396- null , true );
412+ responseCode , null , Toast . LENGTH_LONG );
397413 } catch (SocketTimeoutException ex ) {
398- handleConnectionError (str ("revanced_ryd_failure_connection_timeout" ), ex , false );
414+ handleConnectionError (str ("revanced_ryd_failure_connection_timeout" ), null , ex , Toast . LENGTH_SHORT );
399415 } catch (IOException ex ) {
400416 handleConnectionError (str ("revanced_ryd_failure_generic" , "confirm registration failed" ),
401- ex , true );
417+ null , ex , Toast . LENGTH_LONG );
402418 } catch (Exception ex ) {
403419 Logger .printException (() -> "Failed to confirm registration for user: " + userId
404420 + "solution: " + solution , ex );
@@ -469,12 +485,12 @@ public static boolean sendVote(String videoId, ReturnYouTubeDislike.Vote vote) {
469485 Logger .printInfo (() -> "Failed to send vote for video: " + videoId + " vote: " + vote
470486 + " response code was: " + responseCode );
471487 handleConnectionError (str ("revanced_ryd_failure_connection_status_code" , responseCode ),
472- null , true );
488+ responseCode , null , Toast . LENGTH_LONG );
473489 connection .disconnect (); // something went wrong, might as well disconnect
474490 } catch (SocketTimeoutException ex ) {
475- handleConnectionError (str ("revanced_ryd_failure_connection_timeout" ), ex , false );
491+ handleConnectionError (str ("revanced_ryd_failure_connection_timeout" ), null , ex , Toast . LENGTH_SHORT );
476492 } catch (IOException ex ) {
477- handleConnectionError (str ("revanced_ryd_failure_generic" , "send vote failed" ), ex , true );
493+ handleConnectionError (str ("revanced_ryd_failure_generic" , "send vote failed" ), null , ex , Toast . LENGTH_LONG );
478494 } catch (Exception ex ) {
479495 // should never happen
480496 Logger .printException (() -> "Failed to send vote for video: " + videoId + " vote: " + vote , ex );
@@ -518,12 +534,12 @@ private static boolean confirmVote(String videoId, String userId, String solutio
518534 Logger .printInfo (() -> "Failed to confirm vote for video: " + videoId
519535 + " solution: " + solution + " responseCode: " + responseCode + " response: '" + response + "'" );
520536 handleConnectionError (str ("revanced_ryd_failure_connection_status_code" , responseCode ),
521- null , true );
537+ responseCode , null , Toast . LENGTH_LONG );
522538 } catch (SocketTimeoutException ex ) {
523- handleConnectionError (str ("revanced_ryd_failure_connection_timeout" ), ex , false );
539+ handleConnectionError (str ("revanced_ryd_failure_connection_timeout" ), null , ex , Toast . LENGTH_SHORT );
524540 } catch (IOException ex ) {
525541 handleConnectionError (str ("revanced_ryd_failure_generic" , "confirm vote failed" ),
526- ex , true );
542+ null , ex , Toast . LENGTH_LONG );
527543 } catch (Exception ex ) {
528544 Logger .printException (() -> "Failed to confirm vote for video: " + videoId
529545 + " solution: " + solution , ex ); // should never happen
0 commit comments