Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -71,4 +72,6 @@ private boolean compare(@NonNull GenericEvent genericEvent) {
private T getAddressableTag() {
return super.getFilterable();
}

public static Function<JsonNode, Filterable> fxn = node -> new AddressableTagFilter<>(AddressableTagFilter.createAddressTag(node));
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package nostr.event.filter;

import com.fasterxml.jackson.databind.JsonNode;
import lombok.EqualsAndHashCode;
import nostr.base.PublicKey;
import nostr.event.impl.GenericEvent;

import java.util.function.Function;
import java.util.function.Predicate;

@EqualsAndHashCode(callSuper = true)
Expand All @@ -28,4 +30,6 @@ public String getFilterableValue() {
private T getAuthor() {
return super.getFilterable();
}

public static Function<JsonNode, Filterable> fxn = node -> new AuthorFilter<>(new PublicKey(node.asText()));
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package nostr.event.filter;

import com.fasterxml.jackson.databind.JsonNode;
import lombok.EqualsAndHashCode;
import nostr.event.impl.GenericEvent;

import java.util.function.Function;
import java.util.function.Predicate;

@EqualsAndHashCode(callSuper = true)
Expand All @@ -27,4 +29,6 @@ public String getFilterableValue() {
private T getEvent() {
return super.getFilterable();
}

public static Function<JsonNode, Filterable> fxn = node -> new EventFilter<>(new GenericEvent(node.asText()));
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package nostr.event.filter;

import com.fasterxml.jackson.databind.JsonNode;
import lombok.EqualsAndHashCode;
import nostr.base.ElementAttribute;
import nostr.base.GenericTagQuery;
import nostr.event.impl.GenericEvent;
import nostr.event.impl.GenericTag;

import java.util.function.Function;
import java.util.function.Predicate;

@EqualsAndHashCode(callSuper = true)
Expand Down Expand Up @@ -49,4 +51,8 @@ private String stripLeadingHashTag() {
getFilterKey().substring(1) :
getFilterKey();
}

public static Function<JsonNode, Filterable> fxn(String type) {
return node -> new GenericTagQueryFilter<>(new GenericTagQuery(type, node.asText()));
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package nostr.event.filter;

import com.fasterxml.jackson.databind.JsonNode;
import lombok.EqualsAndHashCode;
import nostr.event.impl.GenericEvent;
import nostr.event.tag.GeohashTag;

import java.util.function.Function;
import java.util.function.Predicate;

@EqualsAndHashCode(callSuper = true)
Expand All @@ -29,4 +31,6 @@ public String getFilterableValue() {
private T getGeoHashTag() {
return super.getFilterable();
}

public static Function<JsonNode, Filterable> fxn = node -> new GeohashTagFilter<>(new GeohashTag(node.asText()));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package nostr.event.filter;

import com.fasterxml.jackson.databind.JsonNode;
import lombok.EqualsAndHashCode;
import nostr.event.impl.GenericEvent;
import nostr.event.tag.HashtagTag;

import java.util.function.Function;
import java.util.function.Predicate;

@EqualsAndHashCode(callSuper = true)
public class HashtagTagFilter<T extends HashtagTag> extends AbstractFilterable<T> {
public final static String FILTER_KEY = "#t";

public HashtagTagFilter(T hashtagTag) {
super(hashtagTag, FILTER_KEY);
}

@Override
public Predicate<GenericEvent> getPredicate() {
return (genericEvent) ->
getTypeSpecificTags(HashtagTag.class, genericEvent).stream().anyMatch(hashtagTag ->
hashtagTag.getHashTag().equals(getFilterableValue()));
}

@Override
public String getFilterableValue() {
return getHashtagTag().getHashTag();
}

private T getHashtagTag() {
return super.getFilterable();
}

public static Function<JsonNode, Filterable> fxn = node -> new HashtagTagFilter<>(new HashtagTag(node.asText()));
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package nostr.event.filter;

import com.fasterxml.jackson.databind.JsonNode;
import lombok.EqualsAndHashCode;
import nostr.event.impl.GenericEvent;
import nostr.event.tag.IdentifierTag;

import java.util.function.Function;
import java.util.function.Predicate;

@EqualsAndHashCode(callSuper = true)
Expand All @@ -29,4 +31,6 @@ public String getFilterableValue() {
private T getIdentifierTag() {
return super.getFilterable();
}

public static Function<JsonNode, Filterable> fxn = node -> new IdentifierTagFilter<>(new IdentifierTag(node.asText()));
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package nostr.event.filter;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import lombok.EqualsAndHashCode;
import nostr.event.Kind;
import nostr.event.impl.GenericEvent;

import java.util.function.Function;
import java.util.function.Predicate;

@EqualsAndHashCode(callSuper = true)
Expand Down Expand Up @@ -36,4 +38,6 @@ public Integer getFilterableValue() {
private T getKind() {
return super.getFilterable();
}

public static Function<JsonNode, Filterable> fxn = node -> new KindFilter<>(Kind.valueOf(node.asInt()));
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package nostr.event.filter;

import com.fasterxml.jackson.databind.JsonNode;
import lombok.EqualsAndHashCode;
import nostr.event.impl.GenericEvent;
import nostr.event.tag.EventTag;

import java.util.function.Function;
import java.util.function.Predicate;

@EqualsAndHashCode(callSuper = true)
Expand All @@ -30,4 +32,6 @@ public String getFilterableValue() {
private T getReferencedEventTag() {
return super.getFilterable();
}

public static Function<JsonNode, Filterable> fxn = node -> new ReferencedEventFilter<>(new EventTag(node.asText()));
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package nostr.event.filter;

import com.fasterxml.jackson.databind.JsonNode;
import lombok.EqualsAndHashCode;
import nostr.base.PublicKey;
import nostr.event.impl.GenericEvent;
import nostr.event.tag.PubKeyTag;

import java.util.function.Function;
import java.util.function.Predicate;

@EqualsAndHashCode(callSuper = true)
Expand All @@ -30,4 +33,6 @@ public String getFilterableValue() {
private T getReferencedPublicKey() {
return super.getFilterable();
}

public static Function<JsonNode, Filterable> fxn = node -> new ReferencedPublicKeyFilter<>(new PubKeyTag(new PublicKey(node.asText())));
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package nostr.event.filter;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.EqualsAndHashCode;
import nostr.event.impl.GenericEvent;

import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

@EqualsAndHashCode(callSuper = true)
Expand Down Expand Up @@ -33,4 +36,6 @@ public String getFilterableValue() {
private Long getSince() {
return super.getFilterable();
}

public static Function<JsonNode, List<Filterable>> fxn = node -> List.of(new SinceFilter(node.asLong()));
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package nostr.event.filter;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.EqualsAndHashCode;
import nostr.event.impl.GenericEvent;

import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

@EqualsAndHashCode(callSuper = true)
Expand Down Expand Up @@ -33,4 +36,6 @@ public String getFilterableValue() {
private Long getUntil() {
return super.getFilterable();
}

public static Function<JsonNode, List<Filterable>> fxn = node -> List.of(new UntilFilter(node.asLong()));
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,40 @@
package nostr.event.json.codec;

import com.fasterxml.jackson.databind.JsonNode;
import nostr.base.GenericTagQuery;
import nostr.base.PublicKey;
import nostr.event.Kind;
import lombok.NonNull;
import nostr.event.filter.AddressableTagFilter;
import nostr.event.filter.AuthorFilter;
import nostr.event.filter.EventFilter;
import nostr.event.filter.Filterable;
import nostr.event.filter.GenericTagQueryFilter;
import nostr.event.filter.GeohashTagFilter;
import nostr.event.filter.HashtagTagFilter;
import nostr.event.filter.IdentifierTagFilter;
import nostr.event.filter.KindFilter;
import nostr.event.filter.ReferencedEventFilter;
import nostr.event.filter.ReferencedPublicKeyFilter;
import nostr.event.filter.SinceFilter;
import nostr.event.filter.UntilFilter;
import nostr.event.impl.GenericEvent;
import nostr.event.tag.EventTag;
import nostr.event.tag.GeohashTag;
import nostr.event.tag.IdentifierTag;
import nostr.event.tag.PubKeyTag;

import java.util.List;
import java.util.function.Function;
import java.util.stream.StreamSupport;

class FilterableProvider {
protected static List<Filterable> getFilterable(String type, JsonNode node) {
public class FilterableProvider {
protected static List<Filterable> getFilterFunction(@NonNull JsonNode node, @NonNull String type) {
return switch (type) {
case ReferencedPublicKeyFilter.FILTER_KEY -> getFilterable(node, referencedPubKey -> new ReferencedPublicKeyFilter<>(new PubKeyTag(new PublicKey(referencedPubKey.asText()))));
case ReferencedEventFilter.FILTER_KEY -> getFilterable(node, referencedEvent -> new ReferencedEventFilter<>(new EventTag(referencedEvent.asText())));
case AddressableTagFilter.FILTER_KEY -> getFilterable(node, addressableTag -> new AddressableTagFilter<>(AddressableTagFilter.createAddressTag(addressableTag)));
case IdentifierTagFilter.FILTER_KEY -> getFilterable(node, identifierTag -> new IdentifierTagFilter<>(new IdentifierTag(identifierTag.asText())));
case GeohashTagFilter.FILTER_KEY -> getFilterable(node, geohashTag -> new GeohashTagFilter<>(new GeohashTag(geohashTag.asText())));
case AuthorFilter.FILTER_KEY -> getFilterable(node, author -> new AuthorFilter<>(new PublicKey(author.asText())));
case EventFilter.FILTER_KEY -> getFilterable(node, event -> new EventFilter<>(new GenericEvent(event.asText())));
case KindFilter.FILTER_KEY -> getFilterable(node, kindNode -> new KindFilter<>(Kind.valueOf(kindNode.asInt())));
case SinceFilter.FILTER_KEY -> List.of(new SinceFilter(node.asLong()));
case UntilFilter.FILTER_KEY -> List.of(new UntilFilter(node.asLong()));
default -> getFilterable(node, genericNode -> new GenericTagQueryFilter<>(new GenericTagQuery(type, genericNode.asText())));
case ReferencedPublicKeyFilter.FILTER_KEY -> getFilterable(node, ReferencedPublicKeyFilter.fxn);
case ReferencedEventFilter.FILTER_KEY -> getFilterable(node, ReferencedEventFilter.fxn);
case AddressableTagFilter.FILTER_KEY -> getFilterable(node, AddressableTagFilter.fxn);
case IdentifierTagFilter.FILTER_KEY -> getFilterable(node, IdentifierTagFilter.fxn);
case GeohashTagFilter.FILTER_KEY -> getFilterable(node, GeohashTagFilter.fxn);
case HashtagTagFilter.FILTER_KEY -> getFilterable(node, HashtagTagFilter.fxn);
case AuthorFilter.FILTER_KEY -> getFilterable(node, AuthorFilter.fxn);
case EventFilter.FILTER_KEY -> getFilterable(node, EventFilter.fxn);
case KindFilter.FILTER_KEY -> getFilterable(node, KindFilter.fxn);
case SinceFilter.FILTER_KEY -> SinceFilter.fxn.apply(node);
case UntilFilter.FILTER_KEY -> UntilFilter.fxn.apply(node);
default -> getFilterable(node, GenericTagQueryFilter.fxn(type));
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
package nostr.event.json.codec;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.NonNull;
import lombok.SneakyThrows;
import nostr.event.filter.AddressableTagFilter;
import nostr.event.filter.AuthorFilter;
import nostr.event.filter.EventFilter;
import nostr.event.filter.Filterable;
import nostr.event.filter.Filters;
import nostr.event.filter.GenericTagQueryFilter;
import nostr.event.filter.GeohashTagFilter;
import nostr.event.filter.HashtagTagFilter;
import nostr.event.filter.IdentifierTagFilter;
import nostr.event.filter.KindFilter;
import nostr.event.filter.ReferencedEventFilter;
import nostr.event.filter.ReferencedPublicKeyFilter;
import nostr.event.filter.SinceFilter;
import nostr.event.filter.UntilFilter;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.StreamSupport;

/**
* @author eric
*/
@Data
public class FiltersDecoder<T extends Filters> implements FDecoder<T> {
public class FiltersDecoder implements FDecoder<Filters> {
private final static ObjectMapper mapper = new ObjectMapper();

@SneakyThrows
public T decode(@NonNull String jsonFiltersList) {
public Filters decode(@NonNull String jsonFiltersList) {
final List<Filterable> filterables = new ArrayList<>();

mapper.readTree(jsonFiltersList).fields().forEachRemaining(field ->
mapper.readTree(jsonFiltersList).fields().forEachRemaining(node ->
filterables.addAll(
FilterableProvider.getFilterable(
field.getKey(),
field.getValue())));
FilterableProvider.getFilterFunction(
node.getValue(),
node.getKey())));

return (T) new Filters(filterables);
return new Filters(filterables);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static <T extends BaseMessage> T decode(@NonNull Object subscriptionId, @
ReqMessage reqMessage = new ReqMessage(
subscriptionId.toString(),
jsonFiltersList.stream().map(filtersList ->
new FiltersDecoder<>().decode(filtersList)).toList());
new FiltersDecoder().decode(filtersList)).toList());
return (T) reqMessage;
}

Expand Down
Loading