Skip to content

Commit

Permalink
Merge #266: js(nostr): complete EventBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Mar 13, 2024
2 parents 9d377ff + 3e15486 commit 62e6b0b
Show file tree
Hide file tree
Showing 15 changed files with 1,227 additions and 3 deletions.
197 changes: 197 additions & 0 deletions bindings/nostr-js/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
// Distributed under the MIT software license

use core::ops::Deref;
use std::str::FromStr;

use nostr::prelude::*;
use wasm_bindgen::prelude::*;

use super::tag::{JsImageDimensions, JsThumbnails};
use super::{JsEvent, JsEventId, JsTag, JsUnsignedEvent};
use crate::error::{into_err, Result};
use crate::key::{JsKeys, JsPublicKey};
use crate::nips::nip15::{JsProductData, JsStallData};
use crate::nips::nip53::JsLiveEvent;
use crate::nips::nip57::JsZapRequestData;
use crate::nips::nip65::JsRelayListItem;
use crate::nips::nip90::JsDataVendingMachineStatus;
use crate::nips::nip94::JsFileMetadata;
use crate::nips::nip98::JsHttpData;
use crate::types::{JsContact, JsMetadata, JsTimestamp};

#[wasm_bindgen(js_name = EventBuilder)]
Expand Down Expand Up @@ -89,13 +98,27 @@ impl JsEventBuilder {
}
}

#[wasm_bindgen(js_name = relayList)]
pub fn relay_list(relays: Vec<JsRelayListItem>) -> Self {
Self {
builder: EventBuilder::relay_list(relays.into_iter().map(|r| r.into())),
}
}

#[wasm_bindgen(js_name = textNote)]
pub fn text_note(content: &str, tags: Vec<JsTag>) -> Self {
Self {
builder: EventBuilder::text_note(content, tags.into_iter().map(|t| t.into())),
}
}

#[wasm_bindgen(js_name = longFormTextNote)]
pub fn long_form_text_note(content: &str, tags: Vec<JsTag>) -> Self {
Self {
builder: EventBuilder::long_form_text_note(content, tags.into_iter().map(|t| t.into())),
}
}

#[wasm_bindgen(js_name = contactList)]
pub fn contact_list(list: Vec<JsContact>) -> Self {
let list = list.into_iter().map(|c| c.inner());
Expand Down Expand Up @@ -201,6 +224,180 @@ impl JsEventBuilder {
})
}

#[wasm_bindgen(js_name = liveEvent)]
pub fn live_event(live_event: JsLiveEvent) -> Self {
Self {
builder: EventBuilder::live_event(live_event.into()),
}
}

#[wasm_bindgen(js_name = liveEventMsg)]
pub fn live_event_msg(
live_event_id: String,
live_event_host: JsPublicKey,
content: String,
relay_url: Option<String>,
tags: Vec<JsTag>,
) -> Result<JsEventBuilder> {
Ok(Self {
builder: EventBuilder::live_event_msg(
live_event_id,
live_event_host.deref().to_owned(),
content,
match relay_url {
Some(url) => Some(Url::from_str(&url).map_err(into_err)?),
None => None,
},
tags.into_iter().map(|t| t.into()).collect(),
),
})
}

#[wasm_bindgen]
pub fn report(tags: Vec<JsTag>, content: String) -> Self {
Self {
builder: EventBuilder::report(tags.into_iter().map(|t| t.into()), content),
}
}

#[wasm_bindgen(js_name = publicZapRequest)]
pub fn public_zap_request(data: JsZapRequestData) -> Self {
Self {
builder: EventBuilder::public_zap_request(data.deref().clone()),
}
}

#[wasm_bindgen(js_name = zapReceipt)]
pub fn zap_receipt(bolt11: String, preimage: Option<String>, zap_request: JsEvent) -> Self {
Self {
builder: EventBuilder::zap_receipt(bolt11, preimage, zap_request.deref().to_owned()),
}
}

#[wasm_bindgen(js_name = defineBadge)]
pub fn define_badge(
badge_id: String,
name: Option<String>,
description: Option<String>,
image: Option<String>,
image_dimensions: Option<JsImageDimensions>,
thumbnails: Vec<JsThumbnails>,
) -> Self {
Self {
builder: EventBuilder::define_badge(
badge_id,
name,
description,
image.map(UncheckedUrl::from),
image_dimensions.map(|i| i.into()),
thumbnails.into_iter().map(|t| t.into()).collect(),
),
}
}

#[wasm_bindgen(js_name = awardBadge)]
pub fn award_badge(
badge_definition: &JsEvent,
awarded_pubkeys: Vec<JsTag>,
) -> Result<JsEventBuilder> {
Ok(Self {
builder: EventBuilder::award_badge(
badge_definition.deref(),
awarded_pubkeys.into_iter().map(|t| t.into()),
)
.map_err(into_err)?,
})
}

#[wasm_bindgen(js_name = profileBadges)]
pub fn profile_badges(
badge_definitions: Vec<JsEvent>,
badge_awards: Vec<JsEvent>,
pubkey_awarded: &JsPublicKey,
) -> Result<JsEventBuilder> {
Ok(Self {
builder: EventBuilder::profile_badges(
badge_definitions.into_iter().map(|e| e.into()).collect(),
badge_awards.into_iter().map(|e| e.into()).collect(),
pubkey_awarded.deref(),
)
.map_err(into_err)?,
})
}

#[wasm_bindgen(js_name = jobRequest)]
pub fn job_request(kind: f64, tags: Vec<JsTag>) -> Result<JsEventBuilder> {
Ok(Self {
builder: EventBuilder::job_request(kind.into(), tags.into_iter().map(|t| t.into()))
.map_err(into_err)?,
})
}

#[wasm_bindgen(js_name = jobResult)]
pub fn job_result(
job_request: &JsEvent,
amount_millisats: f64,
bolt11: Option<String>,
) -> Result<JsEventBuilder> {
Ok(Self {
builder: EventBuilder::job_result(
job_request.deref().clone(),
amount_millisats as u64,
bolt11,
)
.map_err(into_err)?,
})
}

#[wasm_bindgen(js_name = jobFeedback)]
pub fn job_feedback(
job_request: &JsEvent,
status: JsDataVendingMachineStatus,
extra_info: Option<String>,
amount_millisats: u64,
bolt11: Option<String>,
payload: Option<String>,
) -> Self {
Self {
builder: EventBuilder::job_feedback(
job_request.deref(),
status.into(),
extra_info,
amount_millisats,
bolt11,
payload,
),
}
}

#[wasm_bindgen(js_name = fileMetadata)]
pub fn file_metadata(description: String, metadata: JsFileMetadata) -> Self {
Self {
builder: EventBuilder::file_metadata(description, metadata.deref().clone()),
}
}

#[wasm_bindgen(js_name = httpAuth)]
pub fn http_auth(data: JsHttpData) -> Self {
Self {
builder: EventBuilder::http_auth(data.into()),
}
}

#[wasm_bindgen(js_name = stallData)]
pub fn stall_data(data: &JsStallData) -> Self {
Self {
builder: EventBuilder::stall_data(data.deref().clone()),
}
}

#[wasm_bindgen(js_name = productData)]
pub fn product_data(data: JsProductData) -> Self {
Self {
builder: EventBuilder::product_data(data.into()),
}
}

/// Gift Wrap from seal
///
/// <https://github.com/nostr-protocol/nips/blob/master/59.md>
Expand Down
2 changes: 1 addition & 1 deletion bindings/nostr-js/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use wasm_bindgen::prelude::*;

mod builder;
mod id;
mod tag;
pub mod tag;
mod unsigned;

pub use self::builder::JsEventBuilder;
Expand Down
110 changes: 109 additions & 1 deletion bindings/nostr-js/src/event/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,115 @@
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

use nostr::Tag;
use nostr::prelude::*;
use wasm_bindgen::prelude::*;

use crate::error::{into_err, Result};

#[wasm_bindgen(js_name = HttpMethod)]
pub enum JsHttpMethod {
GET,
POST,
PUT,
PATCH,
}

impl From<HttpMethod> for JsHttpMethod {
fn from(value: HttpMethod) -> Self {
match value {
HttpMethod::GET => Self::GET,
HttpMethod::POST => Self::POST,
HttpMethod::PUT => Self::PUT,
HttpMethod::PATCH => Self::PATCH,
}
}
}

impl From<JsHttpMethod> for HttpMethod {
fn from(value: JsHttpMethod) -> Self {
match value {
JsHttpMethod::GET => Self::GET,
JsHttpMethod::POST => Self::POST,
JsHttpMethod::PUT => Self::PUT,
JsHttpMethod::PATCH => Self::PATCH,
}
}
}

#[wasm_bindgen(js_name = Thumbnails)]
pub struct JsThumbnails {
#[wasm_bindgen(getter_with_clone)]
pub url: String,
pub dimensions: Option<JsImageDimensions>,
}

impl From<JsThumbnails> for (UncheckedUrl, Option<ImageDimensions>) {
fn from(value: JsThumbnails) -> Self {
(
UncheckedUrl::from(value.url),
value.dimensions.map(|r| r.into()),
)
}
}

#[wasm_bindgen(js_class = Thumbnails)]
impl JsThumbnails {
#[wasm_bindgen(constructor)]
pub fn new(url: String, dimensions: Option<JsImageDimensions>) -> Self {
Self { url, dimensions }
}
}

#[derive(Clone, Copy)]
#[wasm_bindgen(js_name = ImageDimensions)]
pub struct JsImageDimensions {
pub width: u64,
pub height: u64,
}

impl From<ImageDimensions> for JsImageDimensions {
fn from(value: ImageDimensions) -> Self {
Self {
width: value.width,
height: value.height,
}
}
}

impl From<JsImageDimensions> for ImageDimensions {
fn from(value: JsImageDimensions) -> Self {
Self {
width: value.width,
height: value.height,
}
}
}

#[derive(Clone, Copy)]
#[wasm_bindgen(js_name = RelayMetadata)]
pub enum JsRelayMetadata {
Read,
Write,
}

impl From<RelayMetadata> for JsRelayMetadata {
fn from(value: RelayMetadata) -> Self {
match value {
RelayMetadata::Read => Self::Read,
RelayMetadata::Write => Self::Write,
}
}
}

impl From<JsRelayMetadata> for RelayMetadata {
fn from(value: JsRelayMetadata) -> Self {
match value {
JsRelayMetadata::Read => Self::Read,
JsRelayMetadata::Write => Self::Write,
}
}
}

#[wasm_bindgen(js_name = Tag)]
pub struct JsTag {
inner: Tag,
Expand All @@ -33,6 +137,10 @@ impl JsTag {
})
}

pub fn kind(&self) -> String {
self.inner.kind().to_string()
}

/// Get tag as vector of string
#[wasm_bindgen(js_name = asVec)]
pub fn as_vec(&self) -> Vec<String> {
Expand Down
1 change: 1 addition & 0 deletions bindings/nostr-js/src/key/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use wasm_bindgen::prelude::*;

use crate::error::{into_err, Result};

#[derive(Clone, Copy)]
#[wasm_bindgen(js_name = PublicKey)]
pub struct JsPublicKey {
pub(crate) inner: PublicKey,
Expand Down
7 changes: 7 additions & 0 deletions bindings/nostr-js/src/nips/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

pub mod nip01;
pub mod nip04;
pub mod nip05;
pub mod nip07;
pub mod nip11;
pub mod nip15;
pub mod nip19;
pub mod nip26;
pub mod nip44;
pub mod nip46;
pub mod nip47;
pub mod nip49;
pub mod nip53;
pub mod nip57;
pub mod nip65;
pub mod nip90;
pub mod nip94;
pub mod nip98;
Loading

0 comments on commit 62e6b0b

Please sign in to comment.