Skip to content

Commit e5998e9

Browse files
committed
nostr: add title, image and description constructors to Tag
Ref #466 Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 527d11e commit e5998e9

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
### Added
3838

3939
* nostr: add `EventBuilder::interest_set` ([Yuki Kishimoto])
40+
* nostr: add `title`, `image` and `description` constructors to `Tag` ([Yuki Kishimoto])
4041
* pool: add `SendOutput` and `SendEventOutput` structs ([Yuki Kishimoto])
4142
* js(sdk): partially expose `JsRelayPool` ([Yuki Kishimoto])
4243
* book: add some python examples ([RydalWater])

bindings/nostr-ffi/src/event/tag.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,33 @@ impl Tag {
397397
}
398398
}
399399

400+
/// Compose `["title", "<title>"]` tag
401+
#[inline]
402+
#[uniffi::constructor]
403+
pub fn title(title: &str) -> Self {
404+
Self {
405+
inner: tag::Tag::title(title),
406+
}
407+
}
408+
409+
/// Compose image tag
410+
#[inline]
411+
#[uniffi::constructor(default(dimensions = None))]
412+
pub fn image(url: &str, dimensions: Option<Arc<ImageDimensions>>) -> Self {
413+
Self {
414+
inner: tag::Tag::image(UncheckedUrl::from(url), dimensions.map(|d| **d)),
415+
}
416+
}
417+
418+
/// Compose `["description", "<description>"]` tag
419+
#[inline]
420+
#[uniffi::constructor]
421+
pub fn description(description: &str) -> Self {
422+
Self {
423+
inner: tag::Tag::description(description),
424+
}
425+
}
426+
400427
/// Compose custom tag
401428
///
402429
/// JSON: `["<kind>", "<value-1>", "<value-2>", ...]`

bindings/nostr-js/src/event/tag.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,30 @@ impl JsTag {
313313
}
314314
}
315315

316+
/// Compose `["title", "<title>"]` tag
317+
#[inline]
318+
pub fn title(title: &str) -> Self {
319+
Self {
320+
inner: Tag::title(title),
321+
}
322+
}
323+
324+
/// Compose image tag
325+
#[inline]
326+
pub fn image(url: &str, dimensions: Option<JsImageDimensions>) -> Self {
327+
Self {
328+
inner: Tag::image(UncheckedUrl::from(url), dimensions.map(|d| d.into())),
329+
}
330+
}
331+
332+
/// Compose `["description", "<description>"]` tag
333+
#[inline]
334+
pub fn description(description: &str) -> Self {
335+
Self {
336+
inner: Tag::description(description),
337+
}
338+
}
339+
316340
/// Check if is a standard event tag with `root` marker
317341
#[inline]
318342
#[wasm_bindgen(js_name = isRoot)]

crates/nostr/src/event/tag/mod.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use crate::nips::nip10::Marker;
3131
use crate::nips::nip56::Report;
3232
use crate::nips::nip65::RelayMetadata;
3333
use crate::types::url::Url;
34-
use crate::{PublicKey, SingleLetterTag, Timestamp};
34+
use crate::{ImageDimensions, PublicKey, SingleLetterTag, Timestamp, UncheckedUrl};
3535

3636
/// Tag
3737
#[derive(Debug, Clone)]
@@ -257,6 +257,30 @@ impl Tag {
257257
Self::from_standardized_without_cell(TagStandard::Hashtag(hashtag.into()))
258258
}
259259

260+
/// Compose `["title", "<title>"]` tag
261+
#[inline]
262+
pub fn title<T>(title: T) -> Self
263+
where
264+
T: Into<String>,
265+
{
266+
Self::from_standardized_without_cell(TagStandard::Title(title.into()))
267+
}
268+
269+
/// Compose image tag
270+
#[inline]
271+
pub fn image(url: UncheckedUrl, dimensions: Option<ImageDimensions>) -> Self {
272+
Self::from_standardized_without_cell(TagStandard::Image(url, dimensions))
273+
}
274+
275+
/// Compose `["description", "<description>"]` tag
276+
#[inline]
277+
pub fn description<T>(description: T) -> Self
278+
where
279+
T: Into<String>,
280+
{
281+
Self::from_standardized_without_cell(TagStandard::Description(description.into()))
282+
}
283+
260284
/// Compose custom tag
261285
///
262286
/// JSON: `["<kind>", "<value-1>", "<value-2>", ...]`

0 commit comments

Comments
 (0)