Skip to content

Commit 58d088a

Browse files
author
LisoUseInAIKyrios
authored
fix(YouTube - Return YouTube Dislike): Do not show error toast if API returns 401 status (#5949)
1 parent ece8076 commit 58d088a

File tree

3 files changed

+55
-27
lines changed

3 files changed

+55
-27
lines changed

extensions/shared/library/src/main/java/app/revanced/extension/shared/Utils.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,13 @@ public static void showToastLong(String messageToToast) {
588588
showToast(messageToToast, Toast.LENGTH_LONG);
589589
}
590590

591-
private static void showToast(String messageToToast, int toastDuration) {
591+
/**
592+
* Safe to call from any thread.
593+
*
594+
* @param messageToToast Message to show.
595+
* @param toastDuration Either {@link Toast#LENGTH_SHORT} or {@link Toast#LENGTH_LONG}.
596+
*/
597+
public static void showToast(String messageToToast, int toastDuration) {
592598
Objects.requireNonNull(messageToToast);
593599
runOnMainThreadNowOrLater(() -> {
594600
Context currentContext = context;

extensions/youtube/src/main/java/app/revanced/extension/youtube/returnyoutubedislike/ReturnYouTubeDislike.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.text.style.ForegroundColorSpan;
2222
import android.text.style.ImageSpan;
2323
import android.text.style.ReplacementSpan;
24+
import android.widget.Toast;
2425

2526
import androidx.annotation.GuardedBy;
2627
import androidx.annotation.NonNull;
@@ -507,6 +508,11 @@ private Spanned waitForFetchAndUpdateReplacementSpan(@NonNull Spanned original,
507508
try {
508509
RYDVoteData votingData = getFetchData(MAX_MILLISECONDS_TO_BLOCK_UI_WAITING_FOR_FETCH);
509510
if (votingData == null) {
511+
// Method automatically prevents showing multiple toasts if the connection failed.
512+
// This call is needed here in case the api call did succeed but took too long.
513+
ReturnYouTubeDislikeApi.handleConnectionError(
514+
str("revanced_ryd_failure_connection_timeout"),
515+
null, null, Toast.LENGTH_SHORT);
510516
Logger.printDebug(() -> "Cannot add dislike to UI (RYD data not available)");
511517
return original;
512518
}

extensions/youtube/src/main/java/app/revanced/extension/youtube/returnyoutubedislike/requests/ReturnYouTubeDislikeApi.java

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import static app.revanced.extension.youtube.returnyoutubedislike.requests.ReturnYouTubeDislikeRoutes.getRYDConnectionFromRoute;
55

66
import android.util.Base64;
7+
import android.widget.Toast;
78

8-
import androidx.annotation.NonNull;
99
import androidx.annotation.Nullable;
1010

1111
import org.json.JSONException;
@@ -30,15 +30,15 @@
3030

3131
public 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

Comments
 (0)