|
| 1 | +// Copyright (c) 2022-2023 Yuki Kishimoto |
| 2 | +// Copyright (c) 2023-2025 Rust Nostr Developers |
| 3 | +// Distributed under the MIT software license |
| 4 | + |
| 5 | +//! NIP25: Reactions |
| 6 | +//! |
| 7 | +//! <https://github.com/nostr-protocol/nips/blob/master/25.md> |
| 8 | +
|
| 9 | +use super::nip01::Coordinate; |
| 10 | +use crate::{Event, EventId, Kind, PublicKey, RelayUrl, Tag, TagStandard, Tags}; |
| 11 | + |
| 12 | +/// Reaction target |
| 13 | +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 14 | +pub struct ReactionTarget { |
| 15 | + /// Event ID |
| 16 | + pub event_id: EventId, |
| 17 | + /// Public Key |
| 18 | + pub public_key: PublicKey, |
| 19 | + /// Coordinate |
| 20 | + pub coordinate: Option<Coordinate>, |
| 21 | + /// Kind |
| 22 | + pub kind: Option<Kind>, |
| 23 | + /// Relay hint |
| 24 | + pub relay_hint: Option<RelayUrl>, |
| 25 | +} |
| 26 | + |
| 27 | +impl ReactionTarget { |
| 28 | + /// Construct a new reaction target |
| 29 | + pub fn new(event: &Event, relay_hint: Option<RelayUrl>) -> Self { |
| 30 | + Self { |
| 31 | + event_id: event.id, |
| 32 | + public_key: event.pubkey, |
| 33 | + coordinate: event.coordinate().map(|c| c.into_owned()), |
| 34 | + kind: Some(event.kind), |
| 35 | + relay_hint, |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + pub(crate) fn into_tags(self) -> Tags { |
| 40 | + let mut tags: Tags = Tags::with_capacity( |
| 41 | + 2 + usize::from(self.coordinate.is_some()) + usize::from(self.kind.is_some()), |
| 42 | + ); |
| 43 | + |
| 44 | + // Serialization order: keep the `e` and `a` tags together, followed by the `p` and other tags. |
| 45 | + |
| 46 | + tags.push(Tag::from_standardized_without_cell(TagStandard::Event { |
| 47 | + event_id: self.event_id, |
| 48 | + relay_url: self.relay_hint.clone(), |
| 49 | + public_key: Some(self.public_key), |
| 50 | + marker: None, |
| 51 | + uppercase: false, |
| 52 | + })); |
| 53 | + |
| 54 | + if let Some(coordinate) = self.coordinate { |
| 55 | + tags.push(Tag::coordinate(coordinate, self.relay_hint.clone())); |
| 56 | + } |
| 57 | + |
| 58 | + tags.push(Tag::from_standardized_without_cell( |
| 59 | + TagStandard::PublicKey { |
| 60 | + public_key: self.public_key, |
| 61 | + relay_url: self.relay_hint, |
| 62 | + alias: None, |
| 63 | + uppercase: false, |
| 64 | + }, |
| 65 | + )); |
| 66 | + |
| 67 | + if let Some(kind) = self.kind { |
| 68 | + tags.push(Tag::from_standardized_without_cell(TagStandard::Kind { |
| 69 | + kind, |
| 70 | + uppercase: false, |
| 71 | + })); |
| 72 | + } |
| 73 | + |
| 74 | + tags |
| 75 | + } |
| 76 | +} |
0 commit comments