Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nostr: add EventBuilder::text_note_reply #363

Merged
merged 1 commit into from
Mar 14, 2024
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
22 changes: 22 additions & 0 deletions bindings/nostr-ffi/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,28 @@ impl EventBuilder {
})
}

/// Text note reply
///
/// If no `root` is passed, the `rely_to` will be used for root `e` tag.
///
/// <https://github.com/nostr-protocol/nips/blob/master/10.md>
#[uniffi::constructor]
pub fn text_note_reply(
content: String,
reply_to: &Event,
root: Option<Arc<Event>>,
relay_url: Option<String>,
) -> Self {
Self {
inner: nostr::EventBuilder::text_note_reply(
content,
reply_to.deref(),
root.as_ref().map(|e| e.as_ref().deref()),
relay_url.map(UncheckedUrl::from),
),
}
}

#[uniffi::constructor]
pub fn long_form_text_note(content: &str, tags: &[Arc<Tag>]) -> Result<Self> {
let tags = tags.iter().map(|t| t.as_ref().deref().clone());
Expand Down
22 changes: 22 additions & 0 deletions bindings/nostr-js/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,28 @@ impl JsEventBuilder {
}
}

/// Text note reply
///
/// If no `root` is passed, the `rely_to` will be used for root `e` tag.
///
/// <https://github.com/nostr-protocol/nips/blob/master/10.md>
#[wasm_bindgen(js_name = textNoteReply)]
pub fn text_note_reply(
content: &str,
reply_to: &JsEvent,
root: Option<JsEvent>,
relay_url: Option<String>,
) -> Self {
Self {
builder: EventBuilder::text_note_reply(
content,
reply_to.deref(),
root.as_deref(),
relay_url.map(UncheckedUrl::from),
),
}
}

#[wasm_bindgen(js_name = longFormTextNote)]
pub fn long_form_text_note(content: &str, tags: Vec<JsTag>) -> Self {
Self {
Expand Down
76 changes: 76 additions & 0 deletions crates/nostr/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,82 @@ impl EventBuilder {
Self::new(Kind::TextNote, content, tags)
}

/// Text note reply
///
/// If no `root` is passed, the `rely_to` will be used for root `e` tag.
///
/// <https://github.com/nostr-protocol/nips/blob/master/10.md>
pub fn text_note_reply<S>(
content: S,
reply_to: &Event,
root: Option<&Event>,
relay_url: Option<UncheckedUrl>,
) -> Self
where
S: Into<String>,
{
let mut tags: Vec<Tag> = Vec::new();

// Add `e` and `p` tag of **root** event
match root {
Some(root) => {
// ID and author
tags.push(Tag::Event {
event_id: root.id(),
relay_url: relay_url.clone(),
marker: Some(Marker::Root),
});
tags.push(Tag::public_key(root.author()));

// Add others `p` tags
tags.extend(
root.iter_tags()
.filter(|t| {
t.kind()
== TagKind::SingleLetter(SingleLetterTag {
character: Alphabet::P,
uppercase: false,
})
})
.cloned(),
);
}
None => {
// No root event is passed, use `reply_to` event ID for `root` marker
tags.push(Tag::Event {
event_id: reply_to.id(),
relay_url: relay_url.clone(),
marker: Some(Marker::Root),
});
}
}

// Add `e` and `p` tag of event author
tags.push(Tag::Event {
event_id: reply_to.id(),
relay_url,
marker: Some(Marker::Reply),
});
tags.push(Tag::public_key(reply_to.author()));

// Add others `p` tags of reply_to event
tags.extend(
reply_to
.iter_tags()
.filter(|t| {
t.kind()
== TagKind::SingleLetter(SingleLetterTag {
character: Alphabet::P,
uppercase: false,
})
})
.cloned(),
);

// Compose event
Self::new(Kind::TextNote, content, tags)
}

/// Long-form text note (generally referred to as "articles" or "blog posts").
///
/// <https://github.com/nostr-protocol/nips/blob/master/23.md>
Expand Down
Loading