Skip to content

Commit

Permalink
ffi(nostr): add Kind and KindEnum
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Mar 5, 2024
1 parent 7ceea7e commit f46c656
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 35 deletions.
10 changes: 5 additions & 5 deletions bindings/nostr-ffi/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::sync::Arc;
use nostr::{Contact as ContactSdk, UncheckedUrl, Url};
use uniffi::Object;

use super::{Event, EventId};
use super::{Event, EventId, Kind};
use crate::error::Result;
use crate::helper::unwrap_or_clone_arc;
use crate::key::Keys;
Expand Down Expand Up @@ -46,10 +46,10 @@ impl Deref for EventBuilder {
#[uniffi::export]
impl EventBuilder {
#[uniffi::constructor]
pub fn new(kind: u64, content: &str, tags: &[Arc<Tag>]) -> Result<Self> {
pub fn new(kind: &Kind, content: &str, tags: &[Arc<Tag>]) -> Result<Self> {
let tags = tags.iter().map(|t| t.as_ref().deref().clone());
Ok(Self {
inner: nostr::EventBuilder::new(kind.into(), content, tags),
inner: nostr::EventBuilder::new(**kind, content, tags),
})
}

Expand Down Expand Up @@ -354,10 +354,10 @@ impl EventBuilder {
///
/// <https://github.com/nostr-protocol/nips/blob/master/90.md>
#[uniffi::constructor]
pub fn job_request(kind: u64, tags: &[Arc<Tag>]) -> Result<Self> {
pub fn job_request(kind: &Kind, tags: &[Arc<Tag>]) -> Result<Self> {
Ok(Self {
inner: nostr::EventBuilder::job_request(
kind.into(),
**kind,
tags.iter().map(|t| t.as_ref().deref().clone()),
)?,
})
Expand Down
6 changes: 3 additions & 3 deletions bindings/nostr-ffi/src/event/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use std::sync::Arc;

use nostr::nips::nip21::NostrURI;
use nostr::prelude::{FromBech32, ToBech32};
use nostr::Kind;
use uniffi::Object;

use super::Kind;
use crate::error::Result;
use crate::{PublicKey, Tag, Timestamp};

Expand Down Expand Up @@ -38,7 +38,7 @@ impl EventId {
pub fn new(
public_key: &PublicKey,
created_at: &Timestamp,
kind: u64,
kind: &Kind,
tags: &[Arc<Tag>],
content: &str,
) -> Result<Self> {
Expand All @@ -47,7 +47,7 @@ impl EventId {
inner: nostr::EventId::new(
public_key.deref(),
**created_at,
&Kind::from(kind),
kind.deref(),
&tags,
content,
),
Expand Down
320 changes: 320 additions & 0 deletions bindings/nostr-ffi/src/event/kind.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,320 @@
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

use std::ops::Deref;

use uniffi::{Enum, Object};

/// Event Kind
///
/// **Note: currently is NOT possible to compare 2 `Kind` objects, like `kind1 == kind2`!**
///
/// To check if 2 kinds are equal, you can do:
/// * `kind1.match(kind2)`
/// * `kind1.match_u64(1)` or `kind1.as_u64() == 1`
/// * `kind1.match_enum(KindEnum.TEXT_NOTE())` or `kind1.as_enum() = KindEnum.TEXT_NOTE()`
#[derive(Object)]
pub struct Kind {
inner: nostr::Kind,
}

impl Deref for Kind {
type Target = nostr::Kind;
fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl From<nostr::Kind> for Kind {
fn from(inner: nostr::Kind) -> Self {
Self { inner }
}
}

#[uniffi::export]
impl Kind {
#[uniffi::constructor]
pub fn new(kind: u64) -> Self {
Self {
inner: nostr::Kind::from(kind),
}
}

#[uniffi::constructor]
pub fn from_enum(e: KindEnum) -> Self {
Self { inner: e.into() }
}

pub fn as_u64(&self) -> u64 {
self.inner.as_u64()
}

pub fn as_enum(&self) -> KindEnum {
self.inner.into()
}

/// Check if `Kind` match another `Kind`
pub fn r#match(&self, other: &Self) -> bool {
self.inner == other.inner
}

/// Check if `Kind` match `KindEnum`
pub fn match_enum(&self, e: KindEnum) -> bool {
self.inner == e.into()
}

/// Check if `Kind` match `u64`
pub fn match_u64(&self, kind: u64) -> bool {
self.inner.as_u64() == kind
}
}

#[derive(Enum)]
pub enum KindEnum {
/// Metadata (NIP01 and NIP05)
Metadata,
/// Short Text Note (NIP01)
TextNote,
/// Recommend Relay (NIP01 - deprecated)
RecommendRelay,
/// Contacts (NIP02)
ContactList,
/// OpenTimestamps Attestations (NIP03)
OpenTimestamps,
/// Encrypted Direct Messages (NIP04)
EncryptedDirectMessage,
/// Event Deletion (NIP09)
EventDeletion,
/// Repost (NIP18)
Repost,
/// Generic Repost (NIP18)
GenericRepost,
/// Reaction (NIP25)
Reaction,
/// Badge Award (NIP58)
BadgeAward,
/// Channel Creation (NIP28)
ChannelCreation,
/// Channel Metadata (NIP28)
ChannelMetadata,
/// Channel Message (NIP28)
ChannelMessage,
/// Channel Hide Message (NIP28)
ChannelHideMessage,
/// Channel Mute User (NIP28)
ChannelMuteUser,
/// Public Chat Reserved (NIP28)
PublicChatReserved45,
/// Public Chat Reserved (NIP28)
PublicChatReserved46,
/// Public Chat Reserved (NIP28)
PublicChatReserved47,
/// Public Chat Reserved (NIP28)
PublicChatReserved48,
/// Public Chat Reserved (NIP28)
PublicChatReserved49,
/// Wallet Service Info (NIP47)
WalletConnectInfo,
/// Reporting (NIP56)
Reporting,
/// Zap Private Message (NIP57)
ZapPrivateMessage,
/// Zap Request (NIP57)
ZapRequest,
/// Zap Receipt (NIP57)
ZapReceipt,
/// Mute List (NIP51)
MuteList,
/// Pin List (NIP51)
PinList,
/// Relay List Metadata (NIP65)
RelayList,
/// Client Authentication (NIP42)
Authentication,
/// Wallet Connect Request (NIP47)
WalletConnectRequest,
/// Wallet Connect Response (NIP47)
WalletConnectResponse,
/// Nostr Connect (NIP46)
NostrConnect,
/// Categorized People List (NIP51)
CategorizedPeopleList,
/// Categorized Bookmark List (NIP51)
CategorizedBookmarkList,
/// Live Event (NIP53)
LiveEvent,
/// Live Event Message (NIP53)
LiveEventMessage,
/// Profile Badges (NIP58)
ProfileBadges,
/// Badge Definition (NIP58)
BadgeDefinition,
/// Seal (NIP59)
Seal,
/// Gift Wrap (NIP59)
GiftWrap,
/// GiftWrapped Sealed Direct message
SealedDirect,
/// Long-form Text Note (NIP23)
LongFormTextNote,
/// Application-specific Data (NIP78)
ApplicationSpecificData,
/// File Metadata (NIP94)
FileMetadata,
/// HTTP Auth (NIP98)
HttpAuth,
/// Set stall (NIP15)
SetStall,
/// Set product (NIP15)
SetProduct,
/// Job Feedback (NIP90)
JobFeedback,
JobRequest {
kind: u16,
},
JobResult {
kind: u16,
},
Regular {
kind: u16,
},
Replaceable {
kind: u16,
},
Ephemeral {
kind: u16,
},
ParameterizedReplaceable {
kind: u16,
},
Custom {
kind: u64,
},
}

impl From<nostr::Kind> for KindEnum {
fn from(value: nostr::Kind) -> Self {
match value {
nostr::Kind::Metadata => Self::Metadata,
nostr::Kind::TextNote => Self::TextNote,
nostr::Kind::RecommendRelay => Self::RecommendRelay,
nostr::Kind::ContactList => Self::ContactList,
nostr::Kind::OpenTimestamps => Self::OpenTimestamps,
nostr::Kind::EncryptedDirectMessage => Self::EncryptedDirectMessage,
nostr::Kind::EventDeletion => Self::EventDeletion,
nostr::Kind::Repost => Self::Repost,
nostr::Kind::GenericRepost => Self::GenericRepost,
nostr::Kind::Reaction => Self::Reaction,
nostr::Kind::BadgeAward => Self::BadgeAward,
nostr::Kind::ChannelCreation => Self::ChannelCreation,
nostr::Kind::ChannelMetadata => Self::ChannelMetadata,
nostr::Kind::ChannelMessage => Self::ChannelMessage,
nostr::Kind::ChannelHideMessage => Self::ChannelHideMessage,
nostr::Kind::ChannelMuteUser => Self::ChannelMuteUser,
nostr::Kind::PublicChatReserved45 => Self::PublicChatReserved45,
nostr::Kind::PublicChatReserved46 => Self::PublicChatReserved46,
nostr::Kind::PublicChatReserved47 => Self::PublicChatReserved47,
nostr::Kind::PublicChatReserved48 => Self::PublicChatReserved48,
nostr::Kind::PublicChatReserved49 => Self::PublicChatReserved49,
nostr::Kind::WalletConnectInfo => Self::WalletConnectInfo,
nostr::Kind::Reporting => Self::Reporting,
nostr::Kind::ZapPrivateMessage => Self::ZapPrivateMessage,
nostr::Kind::ZapRequest => Self::ZapRequest,
nostr::Kind::ZapReceipt => Self::ZapReceipt,
nostr::Kind::MuteList => Self::MuteList,
nostr::Kind::PinList => Self::PinList,
nostr::Kind::RelayList => Self::RelayList,
nostr::Kind::Authentication => Self::Authentication,
nostr::Kind::WalletConnectRequest => Self::WalletConnectRequest,
nostr::Kind::WalletConnectResponse => Self::WalletConnectResponse,
nostr::Kind::NostrConnect => Self::NostrConnect,
nostr::Kind::CategorizedPeopleList => Self::CategorizedPeopleList,
nostr::Kind::CategorizedBookmarkList => Self::CategorizedBookmarkList,
nostr::Kind::LiveEvent => Self::LiveEvent,
nostr::Kind::LiveEventMessage => Self::LiveEventMessage,
nostr::Kind::ProfileBadges => Self::ProfileBadges,
nostr::Kind::BadgeDefinition => Self::BadgeDefinition,
nostr::Kind::Seal => Self::Seal,
nostr::Kind::GiftWrap => Self::GiftWrap,
nostr::Kind::SealedDirect => Self::SealedDirect,
nostr::Kind::LongFormTextNote => Self::LongFormTextNote,
nostr::Kind::ApplicationSpecificData => Self::ApplicationSpecificData,
nostr::Kind::FileMetadata => Self::FileMetadata,
nostr::Kind::HttpAuth => Self::HttpAuth,
nostr::Kind::SetStall => Self::SetStall,
nostr::Kind::SetProduct => Self::SetProduct,
nostr::Kind::JobFeedback => Self::JobFeedback,
nostr::Kind::JobRequest(kind) => Self::JobRequest { kind },
nostr::Kind::JobResult(kind) => Self::JobResult { kind },
nostr::Kind::Regular(u) => Self::Regular { kind: u },
nostr::Kind::Replaceable(u) => Self::Replaceable { kind: u },
nostr::Kind::Ephemeral(u) => Self::Ephemeral { kind: u },
nostr::Kind::ParameterizedReplaceable(u) => Self::ParameterizedReplaceable { kind: u },
nostr::Kind::Custom(u) => Self::Custom { kind: u },
}
}
}

impl From<KindEnum> for nostr::Kind {
fn from(value: KindEnum) -> Self {
match value {
KindEnum::Metadata => Self::Metadata,
KindEnum::TextNote => Self::TextNote,
KindEnum::RecommendRelay => Self::RecommendRelay,
KindEnum::ContactList => Self::ContactList,
KindEnum::OpenTimestamps => Self::OpenTimestamps,
KindEnum::EncryptedDirectMessage => Self::EncryptedDirectMessage,
KindEnum::EventDeletion => Self::EventDeletion,
KindEnum::Repost => Self::Repost,
KindEnum::GenericRepost => Self::GenericRepost,
KindEnum::Reaction => Self::Reaction,
KindEnum::BadgeAward => Self::BadgeAward,
KindEnum::ChannelCreation => Self::ChannelCreation,
KindEnum::ChannelMetadata => Self::ChannelMetadata,
KindEnum::ChannelMessage => Self::ChannelMessage,
KindEnum::ChannelHideMessage => Self::ChannelHideMessage,
KindEnum::ChannelMuteUser => Self::ChannelMuteUser,
KindEnum::PublicChatReserved45 => Self::PublicChatReserved45,
KindEnum::PublicChatReserved46 => Self::PublicChatReserved46,
KindEnum::PublicChatReserved47 => Self::PublicChatReserved47,
KindEnum::PublicChatReserved48 => Self::PublicChatReserved48,
KindEnum::PublicChatReserved49 => Self::PublicChatReserved49,
KindEnum::WalletConnectInfo => Self::WalletConnectInfo,
KindEnum::Reporting => Self::Reporting,
KindEnum::ZapPrivateMessage => Self::ZapPrivateMessage,
KindEnum::ZapRequest => Self::ZapRequest,
KindEnum::ZapReceipt => Self::ZapReceipt,
KindEnum::MuteList => Self::MuteList,
KindEnum::PinList => Self::PinList,
KindEnum::RelayList => Self::RelayList,
KindEnum::Authentication => Self::Authentication,
KindEnum::WalletConnectRequest => Self::WalletConnectRequest,
KindEnum::WalletConnectResponse => Self::WalletConnectResponse,
KindEnum::NostrConnect => Self::NostrConnect,
KindEnum::CategorizedPeopleList => Self::CategorizedPeopleList,
KindEnum::CategorizedBookmarkList => Self::CategorizedBookmarkList,
KindEnum::LiveEvent => Self::LiveEvent,
KindEnum::LiveEventMessage => Self::LiveEventMessage,
KindEnum::ProfileBadges => Self::ProfileBadges,
KindEnum::BadgeDefinition => Self::BadgeDefinition,
KindEnum::Seal => Self::Seal,
KindEnum::GiftWrap => Self::GiftWrap,
KindEnum::SealedDirect => Self::SealedDirect,
KindEnum::LongFormTextNote => Self::LongFormTextNote,
KindEnum::ApplicationSpecificData => Self::ApplicationSpecificData,
KindEnum::FileMetadata => Self::FileMetadata,
KindEnum::HttpAuth => Self::HttpAuth,
KindEnum::SetStall => Self::SetStall,
KindEnum::SetProduct => Self::SetProduct,
KindEnum::JobFeedback => Self::JobFeedback,
KindEnum::JobRequest { kind } => Self::JobRequest(kind),
KindEnum::JobResult { kind } => Self::JobResult(kind),
KindEnum::Regular { kind } => Self::Regular(kind),
KindEnum::Replaceable { kind } => Self::Replaceable(kind),
KindEnum::Ephemeral { kind } => Self::Ephemeral(kind),
KindEnum::ParameterizedReplaceable { kind } => Self::ParameterizedReplaceable(kind),
KindEnum::Custom { kind } => Self::Custom(kind),
}
}
}
Loading

0 comments on commit f46c656

Please sign in to comment.