Use defined tag classes

This commit is contained in:
Willem Cazander 2023-09-25 23:33:31 +02:00
parent bfb7f2ffcd
commit 5c4a7afabf
2 changed files with 56 additions and 27 deletions

View file

@ -10,8 +10,6 @@ import jakarta.json.Json;
import jakarta.json.JsonArray; import jakarta.json.JsonArray;
import jakarta.json.JsonValue; import jakarta.json.JsonValue;
import love.distributedrebirth.no2all.nostr.NoStrGospel; import love.distributedrebirth.no2all.nostr.NoStrGospel;
import love.distributedrebirth.no2all.nostr.model.tag.NoStrTagE;
import love.distributedrebirth.no2all.nostr.model.tag.NoStrTagNonce;
import love.distributedrebirth.no2all.nostr.nip.NoStrImplEventTag; import love.distributedrebirth.no2all.nostr.nip.NoStrImplEventTag;
import love.distributedrebirth.no2all.nostr.nip.NoStrImplEventTagArgument; import love.distributedrebirth.no2all.nostr.nip.NoStrImplEventTagArgument;
@ -72,11 +70,11 @@ public interface NoStrEventTag extends NoStrGospel {
static NoStrEventTag valueOf(NoStrEventTagMeta meta, String[] args) { static NoStrEventTag valueOf(NoStrEventTagMeta meta, String[] args) {
Objects.requireNonNull(meta); Objects.requireNonNull(meta);
Objects.requireNonNull(args); Objects.requireNonNull(args);
if (NoStrImplEventTag.E.equals(meta)) { if (meta instanceof NoStrImplEventTag) {
return new NoStrTagE(args); NoStrImplEventTag metaTag = NoStrImplEventTag.class.cast(meta);
if (metaTag.getMapperToEventTag().isPresent()) {
return metaTag.getMapperToEventTag().get().apply(args);
} }
if (NoStrImplEventTag.NONCE.equals(meta)) {
return new NoStrTagNonce(args);
} }
return new NoStrEventTagCustom(meta, args); return new NoStrEventTagCustom(meta, args);
} }

View file

@ -4,52 +4,73 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import love.distributedrebirth.no2all.nostr.model.event.NoStrEventTag;
import love.distributedrebirth.no2all.nostr.model.event.NoStrEventTagMeta; import love.distributedrebirth.no2all.nostr.model.event.NoStrEventTagMeta;
import love.distributedrebirth.no2all.nostr.model.tag.NoStrTagA;
import love.distributedrebirth.no2all.nostr.model.tag.NoStrTagClient;
import love.distributedrebirth.no2all.nostr.model.tag.NoStrTagContentWarning;
import love.distributedrebirth.no2all.nostr.model.tag.NoStrTagD;
import love.distributedrebirth.no2all.nostr.model.tag.NoStrTagDescription;
import love.distributedrebirth.no2all.nostr.model.tag.NoStrTagE;
import love.distributedrebirth.no2all.nostr.model.tag.NoStrTagI;
import love.distributedrebirth.no2all.nostr.model.tag.NoStrTagImage;
import love.distributedrebirth.no2all.nostr.model.tag.NoStrTagName;
import love.distributedrebirth.no2all.nostr.model.tag.NoStrTagNonce;
import love.distributedrebirth.no2all.nostr.model.tag.NoStrTagP;
import love.distributedrebirth.no2all.nostr.model.tag.NoStrTagPublishedAt;
import love.distributedrebirth.no2all.nostr.model.tag.NoStrTagR;
import love.distributedrebirth.no2all.nostr.model.tag.NoStrTagSubject;
import love.distributedrebirth.no2all.nostr.model.tag.NoStrTagSummary;
import love.distributedrebirth.no2all.nostr.model.tag.NoStrTagT;
import love.distributedrebirth.no2all.nostr.model.tag.NoStrTagThumb;
import love.distributedrebirth.no2all.nostr.model.tag.NoStrTagTitle;
public enum NoStrImplEventTag implements NoStrEventTagMeta { public enum NoStrImplEventTag implements NoStrEventTagMeta {
// the "p" tag is used for referencing public keys // the "p" tag is used for referencing public keys
P (NoStrImpl.NIP_1_BASE_PROTOCOL), P (v -> new NoStrTagP(v), NoStrImpl.NIP_1_BASE_PROTOCOL),
// the "e" tag for referencing event ids // the "e" tag for referencing event ids
E (NoStrImpl.NIP_1_BASE_PROTOCOL, NoStrImpl.NIP_10_CORRECT_BASE_TAGS), E (v -> new NoStrTagE(v), NoStrImpl.NIP_1_BASE_PROTOCOL, NoStrImpl.NIP_10_CORRECT_BASE_TAGS),
// ["a", "<kind>:<pubkey>:<d-identifier>", "<relay url>"] // ["a", "<kind>:<pubkey>:<d-identifier>", "<relay url>"]
A (NoStrImpl.NIP_23_LONG_FORM_CONTENT, NoStrImpl.NIP_33_PARA_REPLACEABLE_EVENTS), A (v -> new NoStrTagA(v), NoStrImpl.NIP_23_LONG_FORM_CONTENT, NoStrImpl.NIP_33_PARA_REPLACEABLE_EVENTS),
I (NoStrImpl.NIP_39_EXT_INDENTITIES_IN_PROFILE), I (v -> new NoStrTagI(v), NoStrImpl.NIP_39_EXT_INDENTITIES_IN_PROFILE),
// the "d" tag is a delete slug key for "pubkey:kind:d" + time, empty is def key // the "d" tag is a delete slug key for "pubkey:kind:d" + time, empty is def key
D (NoStrImpl.NIP_33_PARA_REPLACEABLE_EVENTS), // TODO: add single tag info + check D (v -> new NoStrTagD(v), NoStrImpl.NIP_33_PARA_REPLACEABLE_EVENTS), // TODO: add single tag info + check
T (NoStrImpl.NIP_12_GENERIC_TAG_QUERY), T (v -> new NoStrTagT(v), NoStrImpl.NIP_12_GENERIC_TAG_QUERY),
R (NoStrImpl.NIP_12_GENERIC_TAG_QUERY), R (v -> new NoStrTagR(v), NoStrImpl.NIP_12_GENERIC_TAG_QUERY),
G (NoStrImpl.NIP_12_GENERIC_TAG_QUERY), G (NoStrImpl.NIP_12_GENERIC_TAG_QUERY),
CHALLENGE (NoStrImpl.NIP_42_AUTH_RELAY), CHALLENGE (NoStrImpl.NIP_42_AUTH_RELAY),
RELAY (NoStrImpl.NIP_42_AUTH_RELAY), RELAY (NoStrImpl.NIP_42_AUTH_RELAY),
TITLE (NoStrImpl.NIP_23_LONG_FORM_CONTENT), TITLE (v -> new NoStrTagTitle(v), NoStrImpl.NIP_23_LONG_FORM_CONTENT),
SUMMARY (NoStrImpl.NIP_23_LONG_FORM_CONTENT), SUMMARY (v -> new NoStrTagSummary(v), NoStrImpl.NIP_23_LONG_FORM_CONTENT),
PUBLISHED_AT (NoStrImpl.NIP_23_LONG_FORM_CONTENT), PUBLISHED_AT (v -> new NoStrTagPublishedAt(v), NoStrImpl.NIP_23_LONG_FORM_CONTENT),
IMAGE (NoStrImpl.NIP_23_LONG_FORM_CONTENT, NoStrImpl.NIP_58_BADGES), IMAGE (v -> new NoStrTagImage(v), NoStrImpl.NIP_23_LONG_FORM_CONTENT, NoStrImpl.NIP_58_BADGES),
ZAP (NoStrImpl.NIP_57_LIGHTING_ZAPS), ZAP (NoStrImpl.NIP_57_LIGHTING_ZAPS),
LNURL (NoStrImpl.NIP_57_LIGHTING_ZAPS), LNURL (NoStrImpl.NIP_57_LIGHTING_ZAPS),
AMOUNT (NoStrImpl.NIP_57_LIGHTING_ZAPS), AMOUNT (NoStrImpl.NIP_57_LIGHTING_ZAPS),
BOLT11 (NoStrImpl.NIP_57_LIGHTING_ZAPS), BOLT11 (NoStrImpl.NIP_57_LIGHTING_ZAPS),
PREIMAGE (NoStrImpl.NIP_57_LIGHTING_ZAPS), PREIMAGE (NoStrImpl.NIP_57_LIGHTING_ZAPS),
DESCRIPTION (NoStrImpl.NIP_57_LIGHTING_ZAPS, NoStrImpl.NIP_58_BADGES), DESCRIPTION (v -> new NoStrTagDescription(v), NoStrImpl.NIP_57_LIGHTING_ZAPS, NoStrImpl.NIP_58_BADGES),
RELAYS (NoStrImpl.NIP_57_LIGHTING_ZAPS), RELAYS (NoStrImpl.NIP_57_LIGHTING_ZAPS),
CLIENT (true, NoStrImpl.NIP_0_UNKNOWN), CLIENT (v -> new NoStrTagClient(v), true, NoStrImpl.NIP_0_UNKNOWN),
NONCE (true, NoStrImpl.NIP_13_PROOF_OF_WORK), NONCE (v -> new NoStrTagNonce(v), true, NoStrImpl.NIP_13_PROOF_OF_WORK),
SUBJECT (NoStrImpl.NIP_14_TEXT_SUBJECT), SUBJECT (v -> new NoStrTagSubject(v), NoStrImpl.NIP_14_TEXT_SUBJECT),
DELEGATION (NoStrImpl.NIP_26_DELEGATED_EVENT_SIGN), DELEGATION (NoStrImpl.NIP_26_DELEGATED_EVENT_SIGN),
EMOJI (NoStrImpl.NIP_30_CUSTOM_EMOJI), EMOJI (NoStrImpl.NIP_30_CUSTOM_EMOJI),
CONTENT_WARNING (NoStrImpl.NIP_36_SENSITIVE_CONTENT), CONTENT_WARNING (v -> new NoStrTagContentWarning(v), NoStrImpl.NIP_36_SENSITIVE_CONTENT),
EXPIRATION (NoStrImpl.NIP_40_EXPIRATION_TIMESTAMP), EXPIRATION (NoStrImpl.NIP_40_EXPIRATION_TIMESTAMP),
ALT (NoStrImpl.NIP_31_ALT_FOR_UNKNOWN), ALT (NoStrImpl.NIP_31_ALT_FOR_UNKNOWN),
NAME (NoStrImpl.NIP_58_BADGES), NAME (v -> new NoStrTagName(v), NoStrImpl.NIP_58_BADGES),
THUMB (NoStrImpl.NIP_58_BADGES), THUMB (v -> new NoStrTagThumb(v), NoStrImpl.NIP_58_BADGES),
// Dat Package extensions // Dat Package extensions
DPFH (NoStrImpl.NIP_0_UNKNOWN), // "DAT Package File Hash" with event content = maven://ndaturl+versionhash DPFH (NoStrImpl.NIP_0_UNKNOWN), // "DAT Package File Hash" with event content = maven://ndaturl+versionhash
@ -57,22 +78,32 @@ public enum NoStrImplEventTag implements NoStrEventTagMeta {
private static final Map<String, NoStrEventTagMeta> MAP_QNAME = Collections.unmodifiableMap( private static final Map<String, NoStrEventTagMeta> MAP_QNAME = Collections.unmodifiableMap(
Arrays.asList(values()).stream().collect(Collectors.toMap(v -> v.getQName(), v -> v))); Arrays.asList(values()).stream().collect(Collectors.toMap(v -> v.getQName(), v -> v)));
private final Optional<Function<String[], NoStrEventTag>> mapperToEventTag;
private final String jsonQName; private final String jsonQName;
private final NoStrImplPossibility[] nips; private final NoStrImplPossibility[] nips;
private final boolean quariable; private final boolean quariable;
private final boolean singleton; private final boolean singleton;
private NoStrImplEventTag(NoStrImpl... nips) { private NoStrImplEventTag(NoStrImpl... nips) {
this(false, nips); // TODO: add info on all needed tags this(null, nips);
} }
private NoStrImplEventTag(boolean singleton, NoStrImpl... nips) { private NoStrImplEventTag(Function<String[], NoStrEventTag> mapper, NoStrImpl... nips) {
this(mapper, false, nips); // TODO: add info on all needed tags
}
private NoStrImplEventTag(Function<String[], NoStrEventTag> mapper, boolean singleton, NoStrImpl... nips) {
this.mapperToEventTag = Optional.ofNullable(mapper);
this.jsonQName = name().toLowerCase().replaceAll("_", "-"); this.jsonQName = name().toLowerCase().replaceAll("_", "-");
this.nips = nips; this.nips = nips;
this.quariable = isQuariable(name()); this.quariable = isQuariable(name());
this.singleton = singleton; this.singleton = singleton;
} }
public Optional<Function<String[], NoStrEventTag>> getMapperToEventTag() {
return mapperToEventTag;
}
@Override @Override
public String getQName() { public String getQName() {
return jsonQName; return jsonQName;