fix(YouTube - Litho filter): Correctly filter identifier of older YouTube targets

This commit is contained in:
LisoUseInAIKyrios
2025-07-31 06:33:12 -04:00
parent 87c39dd485
commit b1d164b446
2 changed files with 16 additions and 17 deletions

View File

@@ -41,8 +41,6 @@ abstract class Filter {
* Adds callbacks to {@link #isFiltered(String, String, byte[], StringFilterGroup, FilterContentType, int)}
* if any of the groups are found.
* <p>
* Note: This callback is done only during the initial component creation,
* and the path will always be an empty string.
*/
protected final void addIdentifierCallbacks(StringFilterGroup... groups) {
identifierCallbacks.addAll(Arrays.asList(groups));

View File

@@ -127,14 +127,14 @@ public final class LithoFilterPatch {
private static void filterUsingCallbacks(StringTrieSearch pathSearchTree,
Filter filter, List<StringFilterGroup> groups,
Filter.FilterContentType type) {
String filterSimpleName = filter.getClass().getSimpleName();
for (StringFilterGroup group : groups) {
if (!group.includeInSearch()) {
continue;
}
for (String pattern : group.filters) {
String filterSimpleName = filter.getClass().getSimpleName();
pathSearchTree.addPattern(pattern, (textSearched, matchedStartIndex,
matchedLength, callbackParameter) -> {
if (!group.isEnabled()) return false;
@@ -162,6 +162,7 @@ public final class LithoFilterPatch {
/**
* Injection point. Called off the main thread.
* Targets 20.22+
*/
public static void setProtoBuffer(byte[] buffer) {
// Set the buffer to a thread local. The buffer will remain in memory, even after the call to #filter completes.
@@ -194,6 +195,10 @@ public final class LithoFilterPatch {
*/
public static boolean isFiltered(String lithoIdentifier, StringBuilder pathBuilder) {
try {
if (lithoIdentifier.isEmpty() && pathBuilder.length() == 0) {
return false;
}
byte[] buffer = bufferThreadLocal.get();
// Potentially the buffer may have been null or never set up until now.
// Use an empty buffer so the litho id/path filters still work correctly.
@@ -201,23 +206,19 @@ public final class LithoFilterPatch {
buffer = EMPTY_BYTE_ARRAY;
}
String path = pathBuilder.toString();
LithoFilterParameters parameter = new LithoFilterParameters(lithoIdentifier, path, buffer);
LithoFilterParameters parameter = new LithoFilterParameters(
lithoIdentifier, pathBuilder.toString(), buffer);
Logger.printDebug(() -> "Searching " + parameter);
if (path.isEmpty()) {
// Identifier is filtered only if there is no path,
// meaning no component or sub components have been created yet.
if (identifierSearchTree.matches(parameter.identifier, parameter)) {
return true;
}
} else {
if (pathSearchTree.matches(parameter.path, parameter)) {
return true;
}
if (identifierSearchTree.matches(parameter.identifier, parameter)) {
return true;
}
if (pathSearchTree.matches(parameter.path, parameter)) {
return true;
}
} catch (Exception ex) {
Logger.printException(() -> "Litho filter failure", ex);
Logger.printException(() -> "isFiltered failure", ex);
}
return false;