Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nostr: add Tags struct #585

Merged
merged 5 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@

### Changed

* nostr: move `TagsIndexes` into `Tags` struct ([Yuki Kishimoto])
* relay-builder: refactor `Session::check_rate_limit` method ([Yuki Kishimoto])
* pool: changes in `RelayPool::remove_relay` behavior ([Yuki Kishimoto])
* pool: deprecate `RelayPool::remove_all_relays` ([Yuki Kishimoto])
* signer: bootstrap NIP-46 signer on demand ([Yuki Kishimoto])
* bindings(nostr): adj. `tag` module ([Yuki Kishimoto])

### Added

* nostr: add `TagKind::Client` variant ([Yuki Kishimoto])
* nostr: add some shorthand constructors for `TagKind::SingleLetter` ([Yuki Kishimoto])
* nostr: add `Tags` struct ([Yuki Kishimoto])
* database: add `Backend::is_persistent` method ([Yuki Kishimoto])
* relay-builder: add `LocalRelay` and `RelayBuilder` ([Yuki Kishimoto])
* relay-builder: allow to serve local relay as hidden service ([Yuki Kishimoto])
Expand Down
82 changes: 4 additions & 78 deletions bindings/nostr-ffi/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Distributed under the MIT software license

use std::ops::Deref;
use std::sync::Arc;

use nostr::JsonUtil;
use uniffi::Object;
Expand All @@ -18,10 +17,9 @@ pub mod unsigned;
pub use self::builder::EventBuilder;
pub use self::id::EventId;
pub use self::kind::{Kind, KindEnum};
pub use self::tag::{Tag, TagKind, TagStandard};
pub use self::tag::{Tag, TagKind, TagStandard, Tags};
pub use self::unsigned::UnsignedEvent;
use crate::error::Result;
use crate::nips::nip01::Coordinate;
use crate::{PublicKey, Timestamp};

#[derive(Debug, PartialEq, Eq, Hash, Object)]
Expand Down Expand Up @@ -67,29 +65,8 @@ impl Event {
self.inner.kind.into()
}

pub fn tags(&self) -> Vec<Arc<Tag>> {
self.inner
.tags
.iter()
.cloned()
.map(|t| Arc::new(t.into()))
.collect()
}

/// Get content of **first** tag that match `TagKind`.
pub fn get_tag_content(&self, kind: TagKind) -> Option<String> {
self.inner
.get_tag_content(kind.into())
.map(|c| c.to_string())
}

/// Get content of all tags that match `TagKind`.
pub fn get_tags_content(&self, kind: TagKind) -> Vec<String> {
self.inner
.get_tags_content(kind.into())
.into_iter()
.map(|c| c.to_string())
.collect()
pub fn tags(&self) -> Tags {
self.inner.tags.clone().into()
}

#[inline]
Expand Down Expand Up @@ -121,66 +98,15 @@ impl Event {
self.inner.verify_signature()
}

/// Get `Timestamp` expiration if set
pub fn expiration(&self) -> Option<Arc<Timestamp>> {
self.inner.expiration().map(|t| Arc::new((*t).into()))
}

/// Returns `true` if the event has an expiration tag that is expired.
/// If an event has no `Expiration` tag, then it will return `false`.
/// If an event has no expiration tag, then it will return `false`.
///
/// <https://github.com/nostr-protocol/nips/blob/master/40.md>
#[inline]
pub fn is_expired(&self) -> bool {
self.inner.is_expired()
}

/// Extract identifier (`d` tag), if exists.
#[inline]
pub fn identifier(&self) -> Option<String> {
self.inner.identifier().map(|i| i.to_string())
}

/// Extract public keys from tags (`p` tag)
///
/// **This method extract ONLY supported standard variants**
pub fn public_keys(&self) -> Vec<Arc<PublicKey>> {
self.inner
.public_keys()
.copied()
.map(|p| Arc::new(p.into()))
.collect()
}

/// Extract event IDs from tags (`e` tag)
///
/// **This method extract ONLY supported standard variants**
pub fn event_ids(&self) -> Vec<Arc<EventId>> {
self.inner
.event_ids()
.copied()
.map(|p| Arc::new(p.into()))
.collect()
}

/// Extract coordinates from tags (`a` tag)
///
/// **This method extract ONLY supported standard variants**
pub fn coordinates(&self) -> Vec<Arc<Coordinate>> {
self.inner
.coordinates()
.cloned()
.map(|p| Arc::new(p.into()))
.collect()
}

/// Extract hashtags from tags (`t` tag)
///
/// **This method extract ONLY supported standard variants**
pub fn hashtags(&self) -> Vec<String> {
self.inner.hashtags().map(|t| t.to_owned()).collect()
}

/// Check if it's a protected event
///
/// <https://github.com/nostr-protocol/nips/blob/master/70.md>
Expand Down
223 changes: 223 additions & 0 deletions bindings/nostr-ffi/src/event/tag/kind.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

use std::borrow::Cow;
use std::sync::Arc;

use nostr::event::tag;
use uniffi::Enum;

use crate::SingleLetterTag;

#[derive(Enum)]
pub enum TagKind {
SingleLetter {
single_letter: Arc<SingleLetterTag>,
},
/// Protected event
///
/// <https://github.com/nostr-protocol/nips/blob/master/70.md>
Protected,
/// Human-readable plaintext summary of what that event is about
///
/// <https://github.com/nostr-protocol/nips/blob/master/31.md>
Alt,
/// Relay
RelayUrl,
/// Nonce
Nonce,
/// Delegation
Delegation,
/// Content warning
ContentWarning,
/// Expiration
Expiration,
/// Subject
Subject,
/// Auth challenge
Challenge,
/// Title (NIP23)
Title,
/// Image (NIP23)
Image,
/// Thumbnail
Thumb,
/// Summary (NIP23)
Summary,
/// PublishedAt (NIP23)
PublishedAt,
/// Description (NIP57)
Description,
/// Bolt11 Invoice (NIP57)
Bolt11,
/// Preimage (NIP57)
Preimage,
/// Relays (NIP57)
Relays,
/// Amount (NIP57)
Amount,
/// Lnurl (NIP57)
Lnurl,
/// Name tag
Name,
/// Url
Url,
/// AES 256 GCM
Aes256Gcm,
/// Size of file in bytes
Size,
/// Size of file in pixels
Dim,
/// Magnet
Magnet,
/// Blurhash
Blurhash,
/// Streaming
Streaming,
/// Recording
Recording,
/// Starts
Starts,
/// Ends
Ends,
/// Status
Status,
/// Current participants
CurrentParticipants,
/// Total participants
TotalParticipants,
/// HTTP Method Request
Method,
/// Payload HASH
Payload,
Anon,
Proxy,
Emoji,
/// Encrypted
Encrypted,
Request,
Word,
/// Client
///
/// <https://github.com/nostr-protocol/nips/blob/master/89.md>
Client,
Unknown {
unknown: String,
},
}

impl<'a> From<tag::TagKind<'a>> for TagKind {
fn from(value: tag::TagKind) -> Self {
match value {
tag::TagKind::SingleLetter(single_letter) => Self::SingleLetter {
single_letter: Arc::new(single_letter.into()),
},
tag::TagKind::Protected => Self::Protected,
tag::TagKind::Alt => Self::Alt,
tag::TagKind::Relay => Self::RelayUrl,
tag::TagKind::Nonce => Self::Nonce,
tag::TagKind::Delegation => Self::Delegation,
tag::TagKind::ContentWarning => Self::ContentWarning,
tag::TagKind::Expiration => Self::Expiration,
tag::TagKind::Subject => Self::Subject,
tag::TagKind::Challenge => Self::Challenge,
tag::TagKind::Title => Self::Title,
tag::TagKind::Image => Self::Image,
tag::TagKind::Thumb => Self::Thumb,
tag::TagKind::Summary => Self::Summary,
tag::TagKind::PublishedAt => Self::PublishedAt,
tag::TagKind::Description => Self::Description,
tag::TagKind::Bolt11 => Self::Bolt11,
tag::TagKind::Preimage => Self::Preimage,
tag::TagKind::Relays => Self::Relays,
tag::TagKind::Amount => Self::Amount,
tag::TagKind::Lnurl => Self::Lnurl,
tag::TagKind::Name => Self::Name,
tag::TagKind::Url => Self::Url,
tag::TagKind::Aes256Gcm => Self::Aes256Gcm,
tag::TagKind::Size => Self::Size,
tag::TagKind::Dim => Self::Dim,
tag::TagKind::Magnet => Self::Magnet,
tag::TagKind::Blurhash => Self::Blurhash,
tag::TagKind::Streaming => Self::Streaming,
tag::TagKind::Recording => Self::Recording,
tag::TagKind::Starts => Self::Starts,
tag::TagKind::Ends => Self::Ends,
tag::TagKind::Status => Self::Status,
tag::TagKind::CurrentParticipants => Self::CurrentParticipants,
tag::TagKind::TotalParticipants => Self::TotalParticipants,
tag::TagKind::Method => Self::Method,
tag::TagKind::Payload => Self::Payload,
tag::TagKind::Anon => Self::Anon,
tag::TagKind::Proxy => Self::Proxy,
tag::TagKind::Emoji => Self::Emoji,
tag::TagKind::Encrypted => Self::Encrypted,
tag::TagKind::Request => Self::Request,
tag::TagKind::Word => Self::Word,
tag::TagKind::Client => Self::Client,
tag::TagKind::Custom(unknown) => Self::Unknown {
unknown: unknown.to_string(),
},
}
}
}

impl<'a> From<TagKind> for tag::TagKind<'a> {
fn from(value: TagKind) -> Self {
match value {
TagKind::SingleLetter { single_letter } => Self::SingleLetter(**single_letter),
TagKind::Protected => Self::Protected,
TagKind::Alt => Self::Alt,
TagKind::RelayUrl => Self::Relay,
TagKind::Nonce => Self::Nonce,
TagKind::Delegation => Self::Delegation,
TagKind::ContentWarning => Self::ContentWarning,
TagKind::Expiration => Self::Expiration,
TagKind::Subject => Self::Subject,
TagKind::Challenge => Self::Challenge,
TagKind::Title => Self::Title,
TagKind::Image => Self::Image,
TagKind::Thumb => Self::Thumb,
TagKind::Summary => Self::Summary,
TagKind::PublishedAt => Self::PublishedAt,
TagKind::Description => Self::Description,
TagKind::Bolt11 => Self::Bolt11,
TagKind::Preimage => Self::Preimage,
TagKind::Relays => Self::Relays,
TagKind::Amount => Self::Amount,
TagKind::Lnurl => Self::Lnurl,
TagKind::Name => Self::Name,
TagKind::Url => Self::Url,
TagKind::Aes256Gcm => Self::Aes256Gcm,
TagKind::Size => Self::Size,
TagKind::Dim => Self::Dim,
TagKind::Magnet => Self::Magnet,
TagKind::Blurhash => Self::Blurhash,
TagKind::Streaming => Self::Streaming,
TagKind::Recording => Self::Recording,
TagKind::Starts => Self::Starts,
TagKind::Ends => Self::Ends,
TagKind::Status => Self::Status,
TagKind::CurrentParticipants => Self::CurrentParticipants,
TagKind::TotalParticipants => Self::TotalParticipants,
TagKind::Method => Self::Method,
TagKind::Payload => Self::Payload,
TagKind::Anon => Self::Anon,
TagKind::Proxy => Self::Proxy,
TagKind::Emoji => Self::Emoji,
TagKind::Encrypted => Self::Encrypted,
TagKind::Request => Self::Request,
TagKind::Word => Self::Word,
TagKind::Client => Self::Client,
TagKind::Unknown { unknown } => Self::Custom(Cow::Owned(unknown)),
}
}
}

/// Convert tag kind to string
#[uniffi::export]
pub fn tag_kind_to_string(kind: TagKind) -> String {
let kind: tag::TagKind<'_> = kind.into();
kind.to_string()
}
Loading
Loading