Skip to content

Commit

Permalink
nostr: fix EventBuilder::git_issue
Browse files Browse the repository at this point in the history
- Enforce repository address kind
- Tag public key of repo owner
- Remove git issue `alt` tag
- Remove `public_keys` field from `GitIssue` since any kind of tag can be added with `EventBuilder::tags`
- Change `EventBuilder::git_issue` signature

Fixes 631a966
Closes #758

Signed-off-by: Yuki Kishimoto <yukikishimoto@protonmail.com>
  • Loading branch information
yukibtc committed Feb 14, 2025
1 parent b7c1168 commit 73e1198
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 28 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* nostr: update `Nip19Event` relays field type from `Vec<String>` to `Vec<RelayUrl>` ([Yuki Kishimoto])
* nostr: update `Tags::new` signature ([Yuki Kishimoto])
* nostr: remove `WeakTag` ([Yuki Kishimoto])
* nostr: change `EventBuilder::git_issue` signature ([Yuki Kishimoto])

### Changed

Expand Down Expand Up @@ -65,6 +66,8 @@

### Fixed

* fix `EventBuilder::git_issue` according to last NIP34 rev ([Yuki Kishimoto])

### Removed

* database: remove deprecated ([Yuki Kishimoto])
Expand Down
9 changes: 3 additions & 6 deletions bindings/nostr-sdk-ffi/src/protocol/nips/nip34.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,10 @@ impl From<GitRepositoryAnnouncement> for nip34::GitRepositoryAnnouncement {
/// Git Issue
#[derive(Record)]
pub struct GitIssue {
/// The issue content (markdown)
pub content: String,
/// The repository address
pub repository: Arc<Coordinate>,
/// Public keys (owners or other users)
pub public_keys: Vec<Arc<PublicKey>>,
/// The issue content (markdown)
pub content: String,
/// Subject
pub subject: Option<String>,
/// Labels
Expand All @@ -91,9 +89,8 @@ pub struct GitIssue {
impl From<GitIssue> for nip34::GitIssue {
fn from(value: GitIssue) -> Self {
Self {
content: value.content,
repository: value.repository.as_ref().deref().clone(),
public_keys: value.public_keys.into_iter().map(|p| **p).collect(),
content: value.content,
subject: value.subject,
labels: value.labels,
}
Expand Down
10 changes: 3 additions & 7 deletions bindings/nostr-sdk-js/src/protocol/nips/nip34.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,12 @@ impl From<JsGitRepositoryAnnouncement> for GitRepositoryAnnouncement {
/// Git Issue
#[wasm_bindgen(js_name = GitIssue)]
pub struct JsGitIssue {
/// The issue content (markdown)
#[wasm_bindgen(getter_with_clone)]
pub content: String,
/// The repository address
#[wasm_bindgen(getter_with_clone)]
pub repository: JsCoordinate,
/// Public keys (owners or other users)
/// The issue content (markdown)
#[wasm_bindgen(getter_with_clone)]
pub public_keys: Vec<JsPublicKey>,
pub content: String,
/// Subject
#[wasm_bindgen(getter_with_clone)]
pub subject: Option<String>,
Expand All @@ -97,9 +94,8 @@ pub struct JsGitIssue {
impl From<JsGitIssue> for GitIssue {
fn from(value: JsGitIssue) -> Self {
Self {
content: value.content,
repository: value.repository.deref().clone(),
public_keys: value.public_keys.into_iter().map(|p| *p).collect(),
content: value.content,
subject: value.subject,
labels: value.labels,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/nostr/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1691,7 +1691,7 @@ impl EventBuilder {
///
/// <https://github.com/nostr-protocol/nips/blob/master/34.md>
#[inline]
pub fn git_issue(issue: GitIssue) -> Self {
pub fn git_issue(issue: GitIssue) -> Result<Self, Error> {
issue.to_event_builder()
}

Expand Down
34 changes: 20 additions & 14 deletions crates/nostr/src/nips/nip34.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ use core::fmt;

use hashes::sha1::Hash as Sha1Hash;

use crate::event::builder::{Error, EventBuilder, WrongKindError};
use crate::nips::nip01::Coordinate;
use crate::nips::nip10::Marker;
use crate::types::url::Url;
use crate::{EventBuilder, EventId, Kind, PublicKey, Tag, TagKind, TagStandard, Timestamp};
use crate::{EventId, Kind, PublicKey, Tag, TagKind, TagStandard, Timestamp};

/// Earlier unique commit ID
pub const EUC: &str = "euc";

const GIT_REPO_ANNOUNCEMENT_ALT: &str = "git repository";
const GIT_ISSUE_ALT: &str = "git issue";
const GIT_PATCH_ALT: &str = "git patch";
const GIT_PATCH_COVER_LETTER_ALT: &str = "git patch cover letter";

Expand Down Expand Up @@ -122,27 +122,36 @@ impl GitRepositoryAnnouncement {
/// Git Issue
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GitIssue {
/// The issue content (markdown)
pub content: String,
/// The repository address
pub repository: Coordinate,
/// Public keys (owners or other users)
pub public_keys: Vec<PublicKey>,
/// The issue content (markdown)
pub content: String,
/// Subject
pub subject: Option<String>,
/// Labels
pub labels: Vec<String>,
}

impl GitIssue {
pub(crate) fn to_event_builder(self) -> EventBuilder {
let mut tags: Vec<Tag> = Vec::with_capacity(1);
/// Based on <https://github.com/nostr-protocol/nips/blob/ea36ec9ed7596e49bf7f217b05954c1fecacad88/34.md> revision.
pub(crate) fn to_event_builder(self) -> Result<EventBuilder, Error> {
// Check if repository address kind is wrong
if self.repository.kind != Kind::GitRepoAnnouncement {
return Err(Error::WrongKind {
received: self.repository.kind,
expected: WrongKindError::Single(Kind::GitRepoAnnouncement),
});
}

let owner_public_key: PublicKey = self.repository.public_key;

let mut tags: Vec<Tag> = Vec::with_capacity(2);

// Add coordinate
tags.push(Tag::coordinate(self.repository));

// Add public keys
tags.extend(self.public_keys.into_iter().map(Tag::public_key));
// Add owner public key
tags.push(Tag::public_key(owner_public_key));

// Add subject
if let Some(subject) = self.subject {
Expand All @@ -154,11 +163,8 @@ impl GitIssue {
// Add labels
tags.extend(self.labels.into_iter().map(Tag::hashtag));

// Add alt tag
tags.push(Tag::alt(GIT_ISSUE_ALT));

// Build
EventBuilder::new(Kind::GitIssue, self.content).tags(tags)
Ok(EventBuilder::new(Kind::GitIssue, self.content).tags(tags))
}
}

Expand Down

0 comments on commit 73e1198

Please sign in to comment.