Skip to content

Commit

Permalink
js(nostr): add EventBuilder::file_metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
rustedmoon committed Feb 1, 2024
1 parent 47028b8 commit 8d8dc0c
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
8 changes: 8 additions & 0 deletions bindings/nostr-js/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::key::{JsKeys, JsPublicKey};
use crate::nips::nip57::JsZapRequestData;
use crate::nips::nip65::JsRelayListItem;
use crate::nips::nip90::JsDataVendingMachineStatus;
use crate::nips::nip94::JsFileMetadata;
use crate::types::{JsContact, JsMetadata};

#[wasm_bindgen(js_name = EventBuilder)]
Expand Down Expand Up @@ -323,4 +324,11 @@ impl JsEventBuilder {
),
}
}

#[wasm_bindgen(js_name = fileMetadata)]
pub fn file_metadata(description: String, metadata: JsFileMetadata) -> Self {
Self {
builder: EventBuilder::file_metadata(description, metadata.deref().clone()),
}
}
}
1 change: 1 addition & 0 deletions bindings/nostr-js/src/nips/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ pub mod nip47;
pub mod nip57;
pub mod nip65;
pub mod nip90;
pub mod nip94;
108 changes: 108 additions & 0 deletions bindings/nostr-js/src/nips/nip94.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

use std::{ops::Deref, str::FromStr};

use nostr::hashes::sha256::Hash as Sha256Hash;
use nostr::Url;
use nostr::nips::nip94::FileMetadata;
use wasm_bindgen::prelude::*;

use crate::error::{into_err, Result};
use crate::event::tag::JsImageDimensions;

#[wasm_bindgen(js_name = Aes256Gcm)]
pub struct JsAes256Gcm {
key: String,
iv: String,
}

impl From<JsAes256Gcm> for (String, String) {
fn from(value: JsAes256Gcm) -> Self {
(value.key, value.iv)
}
}

impl From<(String, String)> for JsAes256Gcm {
fn from(value: (String, String)) -> Self {
Self { key: value.0, iv: value.1 }
}
}

impl JsAes256Gcm {
pub fn new(key: String, iv: String) -> Self {
Self { key, iv }
}
}

#[wasm_bindgen(js_name = FileMetadata)]
pub struct JsFileMetadata {
inner: FileMetadata,
}

impl From<FileMetadata> for JsFileMetadata {
fn from(inner: FileMetadata) -> Self {
Self { inner }
}
}

impl Deref for JsFileMetadata {
type Target = FileMetadata;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

#[wasm_bindgen(js_class = FileMetadata)]
impl JsFileMetadata {
pub fn new(url: &str, mime_type: String, hash: &str) -> Result<JsFileMetadata> {
Ok(Self{
inner: FileMetadata::new(
Url::from_str(&url).map_err(into_err)?,
mime_type,
Sha256Hash::from_str(&hash).map_err(into_err)?,
),
})
}

#[wasm_bindgen(getter)]
pub fn urls(&self) -> String {
self.inner.url.to_string()
}

#[wasm_bindgen(getter, js_name = mimeType)]
pub fn mime_type(&self) -> String {
self.inner.mime_type.clone()
}

#[wasm_bindgen(getter)]
pub fn hash(&self) -> String {
self.inner.hash.to_string()
}

#[wasm_bindgen(getter, js_name = aes256Gcm)]
pub fn aes_256_gcm(&self) -> Option<JsAes256Gcm> {
self.inner.aes_256_gcm.clone().map(|t| t.into())
}

#[wasm_bindgen(getter)]
pub fn size(&self) -> Option<f64> {
self.inner.size.map(|n| n as f64)
}

#[wasm_bindgen(getter)]
pub fn dim(&self) -> Option<JsImageDimensions> {
self.inner.dim.map(|i| i.into())
}

#[wasm_bindgen(getter)]
pub fn magnet(&self) -> Option<String> {
self.inner.magnet.clone()
}

#[wasm_bindgen(getter)]
pub fn blurhash(&self) -> Option<String> {
self.inner.blurhash.clone()
}
}

0 comments on commit 8d8dc0c

Please sign in to comment.