use super::nip01::Coordinate;
use crate::{Event, EventId, Kind, PublicKey, RelayUrl, Tag, TagStandard, Tags};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ReactionTarget {
pub event_id: EventId,
pub public_key: PublicKey,
pub coordinate: Option<Coordinate>,
pub kind: Option<Kind>,
pub relay_hint: Option<RelayUrl>,
}
impl ReactionTarget {
pub fn new(event: &Event, relay_hint: Option<RelayUrl>) -> Self {
Self {
event_id: event.id,
public_key: event.pubkey,
coordinate: event.coordinate().map(|c| c.into_owned()),
kind: Some(event.kind),
relay_hint,
}
}
pub(crate) fn into_tags(self) -> Tags {
let mut tags: Tags = Tags::with_capacity(
2 + usize::from(self.coordinate.is_some()) + usize::from(self.kind.is_some()),
);
tags.push(Tag::from_standardized_without_cell(TagStandard::Event {
event_id: self.event_id,
relay_url: self.relay_hint.clone(),
public_key: Some(self.public_key),
marker: None,
uppercase: false,
}));
if let Some(coordinate) = self.coordinate {
tags.push(Tag::coordinate(coordinate, self.relay_hint.clone()));
}
tags.push(Tag::from_standardized_without_cell(
TagStandard::PublicKey {
public_key: self.public_key,
relay_url: self.relay_hint,
alias: None,
uppercase: false,
},
));
if let Some(kind) = self.kind {
tags.push(Tag::from_standardized_without_cell(TagStandard::Kind {
kind,
uppercase: false,
}));
}
tags
}
}
impl From<&Event> for ReactionTarget {
fn from(event: &Event) -> Self {
Self {
event_id: event.id,
public_key: event.pubkey,
coordinate: event.coordinate().map(|c| c.into_owned()),
kind: Some(event.kind),
relay_hint: None,
}
}
}