Skip to content

Commit 54d0474

Browse files
committed
nostr/event/tag: add image / thumbnail tags
1 parent f710fbc commit 54d0474

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

crates/nostr/src/event/tag.rs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ pub enum TagKind {
171171
Title,
172172
/// Image (NIP23)
173173
Image,
174+
/// Thumbnail
175+
Thumb,
174176
/// Summary (NIP23)
175177
Summary,
176178
/// PublishedAt (NIP23)
@@ -212,6 +214,7 @@ impl fmt::Display for TagKind {
212214
Self::Challenge => write!(f, "challenge"),
213215
Self::Title => write!(f, "title"),
214216
Self::Image => write!(f, "image"),
217+
Self::Thumb => write!(f, "thumb"),
215218
Self::Summary => write!(f, "summary"),
216219
Self::PublishedAt => write!(f, "published_at"),
217220
Self::Description => write!(f, "description"),
@@ -249,6 +252,7 @@ where
249252
"challenge" => Self::Challenge,
250253
"title" => Self::Title,
251254
"image" => Self::Image,
255+
"thumb" => Self::Thumb,
252256
"summary" => Self::Summary,
253257
"published_at" => Self::PublishedAt,
254258
"description" => Self::Description,
@@ -305,6 +309,7 @@ pub enum Tag {
305309
Challenge(String),
306310
Title(String),
307311
Image(String, Option<(u64, u64)>),
312+
Thumb(String, Option<(u64, u64)>),
308313
Summary(String),
309314
Description(String),
310315
Bolt11(String),
@@ -354,6 +359,7 @@ impl Tag {
354359
Tag::Challenge(..) => TagKind::Challenge,
355360
Tag::Title(..) => TagKind::Title,
356361
Tag::Image(..) => TagKind::Image,
362+
Tag::Thumb(..) => TagKind::Thumb,
357363
Tag::Summary(..) => TagKind::Summary,
358364
Tag::PublishedAt(..) => TagKind::PublishedAt,
359365
Tag::Description(..) => TagKind::Description,
@@ -411,7 +417,8 @@ where
411417
TagKind::Subject => Ok(Self::Subject(content.to_string())),
412418
TagKind::Challenge => Ok(Self::Challenge(content.to_string())),
413419
TagKind::Title => Ok(Self::Title(content.to_string())),
414-
TagKind::Image => Ok(Self::Image(content.to_string())),
420+
TagKind::Image => Ok(Self::Image(content.to_string(), None)),
421+
TagKind::Thumb => Ok(Self::Thumb(content.to_string(), None)),
415422
TagKind::Summary => Ok(Self::Summary(content.to_string())),
416423
TagKind::PublishedAt => Ok(Self::PublishedAt(Timestamp::from_str(content)?)),
417424
TagKind::Description => Ok(Self::Description(content.to_string())),
@@ -470,6 +477,26 @@ where
470477
Err(Error::InvalidLength)
471478
}
472479
}
480+
TagKind::Image => {
481+
let image = tag[1].clone();
482+
let dimensions: Vec<&str> = tag[2].split('x').collect();
483+
if dimensions.len() == 2 {
484+
let (width, height) = (dimensions[0], dimensions[1]);
485+
Ok(Self::Image(image, Some((width.parse()?, height.parse()?))))
486+
} else {
487+
Err(Error::InvalidLength)
488+
}
489+
}
490+
TagKind::Thumb => {
491+
let thumb = tag[1].clone();
492+
let dimensions: Vec<&str> = tag[2].split('x').collect();
493+
if dimensions.len() == 2 {
494+
let (width, height) = (dimensions[0], dimensions[1]);
495+
Ok(Self::Thumb(thumb, Some((width.parse()?, height.parse()?))))
496+
} else {
497+
Err(Error::InvalidLength)
498+
}
499+
}
473500
_ => Ok(Self::Generic(tag_kind, tag[1..].to_vec())),
474501
}
475502
} else if tag_len == 4 {
@@ -587,7 +614,22 @@ impl From<Tag> for Vec<String> {
587614
Tag::Subject(sub) => vec![TagKind::Subject.to_string(), sub],
588615
Tag::Challenge(challenge) => vec![TagKind::Challenge.to_string(), challenge],
589616
Tag::Title(title) => vec![TagKind::Title.to_string(), title],
590-
Tag::Image(image) => vec![TagKind::Image.to_string(), image],
617+
Tag::Image(image, dimensions) => match dimensions {
618+
None => vec![TagKind::Image.to_string(), image],
619+
Some((width, height)) => vec![
620+
TagKind::Image.to_string(),
621+
image,
622+
format!("{}x{}", height, width),
623+
],
624+
},
625+
Tag::Thumb(thumb, dimensions) => match dimensions {
626+
None => vec![TagKind::Thumb.to_string(), thumb],
627+
Some((width, height)) => vec![
628+
TagKind::Thumb.to_string(),
629+
thumb,
630+
format!("{}x{}", height, width),
631+
],
632+
},
591633
Tag::Summary(summary) => vec![TagKind::Summary.to_string(), summary],
592634
Tag::PublishedAt(timestamp) => {
593635
vec![TagKind::PublishedAt.to_string(), timestamp.to_string()]

0 commit comments

Comments
 (0)