|
1 | 1 | //! NIP58
|
2 | 2 | //!
|
3 | 3 | //! <https://github.com/nostr-protocol/nips/blob/master/58.md>
|
| 4 | +
|
| 5 | +use crate::{event::builder::Error, Event, EventBuilder, Keys, Kind, Tag}; |
| 6 | + |
| 7 | +/// Simple struct to hold `width` x `height. |
| 8 | +pub struct ImageDimensions(u64, u64); |
| 9 | + |
| 10 | +/// [`BadgeDefinition`] event builder |
| 11 | +pub struct BadgeDefinitionBuilder { |
| 12 | + badge_id: String, |
| 13 | + name: Option<String>, |
| 14 | + image: Option<String>, |
| 15 | + image_dimensions: Option<ImageDimensions>, |
| 16 | + description: Option<String>, |
| 17 | + thumbs: Option<Vec<(String, Option<ImageDimensions>)>>, |
| 18 | +} |
| 19 | + |
| 20 | +impl BadgeDefinitionBuilder { |
| 21 | + /// New [`BadgeDefinitionBuilder`] |
| 22 | + pub fn new(badge_id: String) -> Self { |
| 23 | + Self { |
| 24 | + badge_id, |
| 25 | + name: None, |
| 26 | + image: None, |
| 27 | + image_dimensions: None, |
| 28 | + description: None, |
| 29 | + thumbs: None, |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + /// Set name |
| 34 | + pub fn name(mut self, name: String) -> Self { |
| 35 | + self.name = Some(name); |
| 36 | + self |
| 37 | + } |
| 38 | + |
| 39 | + /// Set image |
| 40 | + pub fn image(mut self, image: String) -> Self { |
| 41 | + self.image = Some(image); |
| 42 | + self |
| 43 | + } |
| 44 | + |
| 45 | + /// Set `[ImageDimensions]` |
| 46 | + pub fn image_dimensions(mut self, image_dimensions: ImageDimensions) -> Self { |
| 47 | + self.image_dimensions = Some(image_dimensions); |
| 48 | + self |
| 49 | + } |
| 50 | + |
| 51 | + /// Set description |
| 52 | + pub fn description(mut self, description: String) -> Self { |
| 53 | + self.description = Some(description); |
| 54 | + self |
| 55 | + } |
| 56 | + |
| 57 | + /// Set thumbnails with their optional `[ImageDimensions]` |
| 58 | + pub fn thumbs(mut self, thumbs: Vec<(String, Option<ImageDimensions>)>) -> Self { |
| 59 | + self.thumbs = Some(thumbs); |
| 60 | + self |
| 61 | + } |
| 62 | + |
| 63 | + /// Build [`Event`] |
| 64 | + pub fn build(self, keys: &Keys) -> Result<BadgeDefinition, BuilderError> { |
| 65 | + let mut tags: Vec<Tag> = Vec::new(); |
| 66 | + let badge_id = Tag::Identifier(self.badge_id); |
| 67 | + tags.push(badge_id); |
| 68 | + |
| 69 | + if let Some(name) = self.name { |
| 70 | + let name_tag = Tag::Name(name); |
| 71 | + tags.push(name_tag); |
| 72 | + }; |
| 73 | + |
| 74 | + if let Some(image) = self.image { |
| 75 | + let image_tag = if let Some(width_height) = self.image_dimensions { |
| 76 | + let ImageDimensions(width, height) = width_height; |
| 77 | + Tag::Image(image, Some((width, height))) |
| 78 | + } else { |
| 79 | + Tag::Image(image, None) |
| 80 | + }; |
| 81 | + tags.push(image_tag); |
| 82 | + } |
| 83 | + |
| 84 | + if let Some(description) = self.description { |
| 85 | + let description_tag = Tag::Description(description); |
| 86 | + tags.push(description_tag); |
| 87 | + } |
| 88 | + if let Some(thumbs) = self.thumbs { |
| 89 | + for thumb in thumbs { |
| 90 | + let thumb_url = thumb.0; |
| 91 | + let thumb_tag = if let Some(width_height) = thumb.1 { |
| 92 | + let ImageDimensions(width, height) = width_height; |
| 93 | + Tag::Thumb(thumb_url, Some((width, height))) |
| 94 | + } else { |
| 95 | + Tag::Thumb(thumb_url, None) |
| 96 | + }; |
| 97 | + tags.push(thumb_tag); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + let event_builder = EventBuilder::new(Kind::BadgeDefinition, String::new(), &tags); |
| 102 | + let event = event_builder.to_event(keys)?; |
| 103 | + Ok(BadgeDefinition(event)) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/// Badge definition event as specified in NIP-58 |
| 108 | +pub struct BadgeDefinition(Event); |
0 commit comments