(search) New toggle for reducing tracking

This commit is contained in:
Viktor Lofgren 2023-11-27 18:06:19 +01:00
parent 902f235b5b
commit 2ff64c3c12
6 changed files with 80 additions and 6 deletions

View File

@ -16,6 +16,7 @@ public class SearchQueryParamFactory {
var profile = userParams.profile();
profile.addTacitTerms(prototype);
userParams.js().addTacitTerms(prototype);
userParams.adtech().addTacitTerms(prototype);
return new QueryParams(
userParams.query(),

View File

@ -0,0 +1,29 @@
package nu.marginalia.search.command;
import nu.marginalia.index.client.model.query.SearchSubquery;
import javax.annotation.Nullable;
import java.util.Arrays;
public enum SearchAdtechParameter {
DEFAULT("default"),
REDUCE("reduce", "special:ads", "special:affiliate");
public final String value;
public final String[] implictExcludeSearchTerms;
SearchAdtechParameter(String value, String... implictExcludeSearchTerms) {
this.value = value;
this.implictExcludeSearchTerms = implictExcludeSearchTerms;
}
public static SearchAdtechParameter parse(@Nullable String value) {
if (REDUCE.value.equals(value)) return REDUCE;
return DEFAULT;
}
public void addTacitTerms(SearchSubquery subquery) {
subquery.searchTermsExclude.addAll(Arrays.asList(implictExcludeSearchTerms));
}
}

View File

@ -6,24 +6,33 @@ import nu.marginalia.search.model.SearchProfile;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
public record SearchParameters(String query, SearchProfile profile, SearchJsParameter js) {
public record SearchParameters(String query,
SearchProfile profile,
SearchJsParameter js,
SearchAdtechParameter adtech
) {
public String profileStr() {
return profile.filterId;
}
public SearchParameters withProfile(SearchProfile profile) {
return new SearchParameters(query, profile, js);
return new SearchParameters(query, profile, js, adtech);
}
public SearchParameters withJs(SearchJsParameter js) {
return new SearchParameters(query, profile, js);
return new SearchParameters(query, profile, js, adtech);
}
public SearchParameters withAdtech(SearchAdtechParameter adtech) {
return new SearchParameters(query, profile, js, adtech);
}
public String renderUrl(WebsiteUrl baseUrl) {
String path = String.format("/search?query=%s&profile=%s&js=%s",
String path = String.format("/search?query=%s&profile=%s&js=%s&adtech=%s",
URLEncoder.encode(query, StandardCharsets.UTF_8),
URLEncoder.encode(profile.filterId, StandardCharsets.UTF_8),
URLEncoder.encode(js.value, StandardCharsets.UTF_8));
URLEncoder.encode(js.value, StandardCharsets.UTF_8),
URLEncoder.encode(adtech.value, StandardCharsets.UTF_8)
);
return baseUrl.withPath(path);
}

View File

@ -2,8 +2,10 @@ package nu.marginalia.search.model;
import lombok.Getter;
import nu.marginalia.WebsiteUrl;
import nu.marginalia.search.command.SearchAdtechParameter;
import nu.marginalia.search.command.SearchJsParameter;
import nu.marginalia.search.command.SearchParameters;
import org.apache.regexp.RE;
import java.util.List;
@ -18,6 +20,8 @@ public class SearchFilters {
@Getter
public final RemoveJsOption removeJsOption;
@Getter
public final ReduceAdtechOption reduceAdtechOption;
@Getter
public final List<List<Filter>> filterGroups;
@ -26,6 +30,7 @@ public class SearchFilters {
this.url = url;
removeJsOption = new RemoveJsOption(parameters);
reduceAdtechOption = new ReduceAdtechOption(parameters);
currentFilter = parameters.profile().filterId;
@ -78,6 +83,31 @@ public class SearchFilters {
this.url = parameters.withJs(toggledValue).renderUrl(SearchFilters.this.url);
}
}
public class ReduceAdtechOption {
private final SearchAdtechParameter value;
@Getter
public final String url;
public boolean isSet() {
return value.equals(SearchAdtechParameter.REDUCE);
}
public String name() {
return "Reduce Adtech";
}
public ReduceAdtechOption(SearchParameters parameters) {
this.value = parameters.adtech();
var toggledValue = switch (parameters.adtech()) {
case REDUCE -> SearchAdtechParameter.DEFAULT;
default -> SearchAdtechParameter.REDUCE;
};
this.url = parameters.withAdtech(toggledValue).renderUrl(SearchFilters.this.url);
}
}
public class Filter {
@Getter

View File

@ -3,6 +3,7 @@ package nu.marginalia.search.svc;
import com.google.inject.Inject;
import lombok.SneakyThrows;
import nu.marginalia.WebsiteUrl;
import nu.marginalia.search.command.SearchAdtechParameter;
import nu.marginalia.search.model.SearchProfile;
import nu.marginalia.client.Context;
import nu.marginalia.search.command.CommandEvaluator;
@ -59,6 +60,7 @@ public class SearchQueryService {
return new SearchParameters(queryParam.trim(),
SearchProfile.getSearchProfile(request.queryParams("profile")),
SearchJsParameter.parse(request.queryParams("js")));
SearchJsParameter.parse(request.queryParams("js")),
SearchAdtechParameter.parse(request.queryParams("adtech")));
}
}

View File

@ -3,6 +3,9 @@
{{#with removeJsOption}}
<li class="filter-toggle-{{#if set}}on{{/if}}{{#unless set}}off{{/unless}}"><a href="{{url}}">{{name}}</a></li>
{{/with}}
{{#with reduceAdtechOption}}
<li class="filter-toggle-{{#if set}}on{{/if}}{{#unless set}}off{{/unless}}"><a href="{{url}}">{{name}}</a></li>
{{/with}}
</ul>
<h3>Domains</h3>
<ul>