Add tag helper methods and refactor events#232
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR introduces tag helper methods to improve validation and retrieval of required tags in Nostr events. The changes simplify the codebase by replacing repetitive validation logic with reusable helper methods.
- Added
requireTagandrequireTagInstancehelper methods toGenericEventclass - Refactored validation logic in
ReactionEvent,ZapReceiptEvent, andMuteUserEventto use the new helpers - Added unit tests for
ReactionEventto verify tag validation behavior
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| nostr-java-event/src/main/java/nostr/event/impl/GenericEvent.java | Added requireTag and requireTagInstance helper methods with proper documentation |
| nostr-java-event/src/main/java/nostr/event/impl/ReactionEvent.java | Refactored to use requireTagInstance for EventTag validation and retrieval |
| nostr-java-event/src/main/java/nostr/event/impl/ZapReceiptEvent.java | Simplified validation logic using requireTag for multiple required tags |
| nostr-java-event/src/main/java/nostr/event/impl/MuteUserEvent.java | Replaced manual validation with requireTagInstance for PubKeyTag |
| nostr-java-id/src/test/java/nostr/id/ReactionEventTest.java | Added unit tests covering both successful tag retrieval and missing tag scenarios |
| README.md | Updated documentation to mention the new tag helper methods |
Comment on lines
+25
to
+32
| return requireTagInstance(PubKeyTag.class).getPublicKey(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void validateTags() { | ||
| super.validateTags(); | ||
|
|
||
| // Validate `tags` field for at least one PubKeyTag | ||
| boolean hasValidPubKeyTag = this.getTags().stream() | ||
| .anyMatch(tag -> tag instanceof PubKeyTag); | ||
| if (!hasValidPubKeyTag) { | ||
| throw new AssertionError("Invalid `tags`: Must include at least one valid PubKeyTag."); | ||
| } | ||
| requireTagInstance(PubKeyTag.class); |
There was a problem hiding this comment.
The original implementation used getTags().get(0) which suggests it expected the first tag to be a PubKeyTag. The new implementation returns the first PubKeyTag found anywhere in the tags list, which may have different behavior if there are non-PubKeyTag tags before the actual PubKeyTag.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
requireTagandrequireTagInstancehelpersReactionEvent,ZapReceiptEvent, andMuteUserEventReactionEventTesting
mvn -q verifyhttps://chatgpt.com/codex/tasks/task_b_688a8500df4c833192e7879aea6d2535