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
46 changes: 34 additions & 12 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,27 @@ 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 new GenericEventFactory<>(resolveSender(null), 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 new GenericEventFactory<>(resolveSender(null), 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 +59,47 @@ 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);
}

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();
return new GenericEventFactory<>(resolveSender(null), kind, tags, content).create();
Comment on lines +70 to +76

Choose a reason for hiding this comment

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

P0 Badge Remove duplicate buildReplaceableEvent overload

buildReplaceableEvent(List<BaseTag> tags, Integer kind, String content) is declared twice in succession—first delegating to a nonexistent (Identity, List<BaseTag>, Integer, String) overload and then constructing the event directly. Java rejects duplicate methods with identical signatures, so this class will not compile and the module’s tests cannot run. The delegating overload probably intended to accept an Identity parameter.

Useful? React with 👍 / 👎.

}

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