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
73 changes: 60 additions & 13 deletions nostr-java-api/src/main/java/nostr/api/nip01/NIP01EventBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,39 @@ public void updateDefaultSender(Identity defaultSender) {
}

public GenericEvent buildTextNote(String content) {
return new GenericEventFactory(resolveSender(null), Kind.TEXT_NOTE.getValue(), content)
.create();
return buildTextNote(null, content);
}

// Removed deprecated Identity-accepting overloads; use instance-configured sender
public GenericEvent buildTextNote(Identity sender, String content) {
return new GenericEventFactory(resolveSender(sender), Kind.TEXT_NOTE.getValue(), content)
.create();
}

public GenericEvent buildRecipientTextNote(String content, List<PubKeyTag> tags) {
return new GenericEventFactory<PubKeyTag>(resolveSender(null), Kind.TEXT_NOTE.getValue(), tags, content)
return buildRecipientTextNote(null, content, tags);
}

public GenericEvent buildRecipientTextNote(
Identity sender, String content, List<PubKeyTag> tags) {
return new GenericEventFactory<PubKeyTag>(
resolveSender(sender), Kind.TEXT_NOTE.getValue(), tags, content)
.create();
}

public GenericEvent buildTaggedTextNote(@NonNull List<BaseTag> tags, @NonNull String content) {
return new GenericEventFactory<BaseTag>(resolveSender(null), Kind.TEXT_NOTE.getValue(), tags, content)
return buildTaggedTextNote(null, tags, content);
}

public GenericEvent buildTaggedTextNote(
Identity sender, @NonNull List<BaseTag> tags, @NonNull String content) {
return new GenericEventFactory<BaseTag>(
resolveSender(sender), Kind.TEXT_NOTE.getValue(), tags, content)
.create();
}

public GenericEvent buildMetadataEvent(@NonNull Identity sender, @NonNull String payload) {
return new GenericEventFactory(sender, Kind.SET_METADATA.getValue(), payload).create();
return new GenericEventFactory(resolveSender(sender), Kind.SET_METADATA.getValue(), payload)
.create();
}

public GenericEvent buildMetadataEvent(@NonNull String payload) {
Expand All @@ -56,28 +71,60 @@ public GenericEvent buildMetadataEvent(@NonNull String payload) {
}

public GenericEvent buildReplaceableEvent(Integer kind, String content) {
return new GenericEventFactory(resolveSender(null), kind, content).create();
return buildReplaceableEvent(null, kind, content);
}
Comment on lines 73 to +75

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P0 Badge Disambiguate overloads when delegating

The new helper buildReplaceableEvent(Integer kind, String content) delegates to buildReplaceableEvent(null, kind, content) while two overloads with the same arity now exist (Identity, Integer, String and List<BaseTag>, Integer, String). Because null matches both first-parameter types, this call will not compile (“reference to buildReplaceableEvent is ambiguous”). The same ambiguity exists for buildEphemeralEvent(Integer, String) and buildAddressableEvent(Integer, String) which delegate in the same way. These delegations should cast the null to the intended parameter type or call the implementation directly to keep the API compiling.

Useful? React with 👍 / 👎.


public GenericEvent buildReplaceableEvent(
Identity sender, Integer kind, String content) {
return new GenericEventFactory(resolveSender(sender), kind, content).create();
}

public GenericEvent buildReplaceableEvent(
List<BaseTag> tags, Integer kind, String content) {
return buildReplaceableEvent(null, tags, kind, content);
}

public GenericEvent buildReplaceableEvent(List<BaseTag> tags, Integer kind, String content) {
return new GenericEventFactory<BaseTag>(resolveSender(null), kind, tags, content).create();
public GenericEvent buildReplaceableEvent(
Identity sender, List<BaseTag> tags, Integer kind, String content) {
return new GenericEventFactory<BaseTag>(resolveSender(sender), kind, tags, content).create();
}

public GenericEvent buildEphemeralEvent(List<BaseTag> tags, Integer kind, String content) {
return new GenericEventFactory<BaseTag>(resolveSender(null), kind, tags, content).create();
return buildEphemeralEvent(null, tags, kind, content);
}

public GenericEvent buildEphemeralEvent(
Identity sender, List<BaseTag> tags, Integer kind, String content) {
return new GenericEventFactory<BaseTag>(resolveSender(sender), kind, tags, content).create();
}

public GenericEvent buildEphemeralEvent(Integer kind, String content) {
return new GenericEventFactory(resolveSender(null), kind, content).create();
return buildEphemeralEvent(null, kind, content);
}

public GenericEvent buildEphemeralEvent(Identity sender, Integer kind, String content) {
return new GenericEventFactory(resolveSender(sender), kind, content).create();
}

public GenericEvent buildAddressableEvent(Integer kind, String content) {
return new GenericEventFactory(resolveSender(null), kind, content).create();
return buildAddressableEvent(null, kind, content);
}

public GenericEvent buildAddressableEvent(
Identity sender, Integer kind, String content) {
return new GenericEventFactory(resolveSender(sender), kind, content).create();
}

public GenericEvent buildAddressableEvent(
@NonNull List<GenericTag> tags, @NonNull Integer kind, String content) {
return new GenericEventFactory<GenericTag>(resolveSender(null), kind, tags, content).create();
return buildAddressableEvent(null, tags, kind, content);
}

public GenericEvent buildAddressableEvent(
Identity sender, @NonNull List<GenericTag> tags, @NonNull Integer kind, String content) {
return new GenericEventFactory<GenericTag>(
resolveSender(sender), kind, tags, content)
.create();
}

private Identity resolveSender(Identity override) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package nostr.api.unit;

import nostr.api.nip01.NIP01EventBuilder;
import nostr.base.PrivateKey;
import nostr.event.impl.GenericEvent;
import nostr.id.Identity;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class NIP01EventBuilderTest {

// Ensures that an explicitly provided sender overrides the default identity.
@Test
void buildTextNoteUsesOverrideIdentity() {
Identity defaultSender = Identity.create(PrivateKey.generateRandomPrivKey());
Identity overrideSender = Identity.create(PrivateKey.generateRandomPrivKey());
NIP01EventBuilder builder = new NIP01EventBuilder(defaultSender);

GenericEvent event = builder.buildTextNote(overrideSender, "override");

assertEquals(overrideSender.getPublicKey(), event.getPubKey());
}

// Ensures that the builder falls back to the configured sender when no override is supplied.
@Test
void buildTextNoteUsesDefaultIdentityWhenOverrideMissing() {
Identity defaultSender = Identity.create(PrivateKey.generateRandomPrivKey());
NIP01EventBuilder builder = new NIP01EventBuilder(defaultSender);

GenericEvent event = builder.buildTextNote("fallback");

assertEquals(defaultSender.getPublicKey(), event.getPubKey());
}
}