Skip to content

Commit

Permalink
nostr: add some missing kind and tag of NIP51
Browse files Browse the repository at this point in the history
  • Loading branch information
rustedmoon committed Mar 7, 2024
1 parent 5cbfd8a commit b26ba6a
Show file tree
Hide file tree
Showing 5 changed files with 373 additions and 8 deletions.
151 changes: 151 additions & 0 deletions crates/nostr/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ use super::kind::{Kind, NIP90_JOB_REQUEST_RANGE, NIP90_JOB_RESULT_RANGE};
use super::tag::ImageDimensions;
use super::{Event, EventId, Marker, Tag, TagKind, UnsignedEvent};
use crate::key::{self, Keys, PublicKey};
use crate::nips::nip01::Coordinate;
#[cfg(feature = "nip04")]
use crate::nips::nip04;
use crate::nips::nip15::{ProductData, StallData};
#[cfg(all(feature = "std", feature = "nip44"))]
use crate::nips::nip44::{self, Version};
#[cfg(all(feature = "std", feature = "nip46"))]
use crate::nips::nip46::Message as NostrConnectMessage;
use crate::nips::nip51::{ArticlesCuration, Bookmarks, Emojis, Interests, MuteList};
use crate::nips::nip53::LiveEvent;
#[cfg(feature = "nip57")]
use crate::nips::nip57::ZapRequestData;
Expand Down Expand Up @@ -1134,6 +1136,155 @@ impl EventBuilder {
{
Self::new(Kind::SealedDirect, message, [Tag::public_key(receiver)])
}

/// Mute list
///
/// <https://github.com/nostr-protocol/nips/blob/master/51.md>
pub fn mute_list(list: MuteList) -> Self {
let tags: Vec<Tag> = list.into();
Self::new(Kind::MuteList, "", tags)
}

/// Pinned notes
///
/// <https://github.com/nostr-protocol/nips/blob/master/51.md>
pub fn pinned_notes<I>(ids: I) -> Self
where
I: IntoIterator<Item = EventId>,
{
let tags = ids.into_iter().map(Tag::event);
Self::new(Kind::PinList, "", tags)
}

/// Bookmarks
///
/// <https://github.com/nostr-protocol/nips/blob/master/51.md>
pub fn bookmarks(list: Bookmarks) -> Self {
let tags: Vec<Tag> = list.into();
Self::new(Kind::Bookmarks, "", tags)
}

/// Communities
///
/// <https://github.com/nostr-protocol/nips/blob/master/51.md>
pub fn communities<I>(communities: I) -> Self
where
I: IntoIterator<Item = Coordinate>,
{
let tags = communities.into_iter().map(Tag::from);
Self::new(Kind::Communities, "", tags)
}

/// Public chats
///
/// <https://github.com/nostr-protocol/nips/blob/master/51.md>
pub fn public_chats<I>(chat: I) -> Self
where
I: IntoIterator<Item = EventId>,
{
let tags = chat.into_iter().map(Tag::event);
Self::new(Kind::Communities, "", tags)
}

/// Blocked relays
///
/// <https://github.com/nostr-protocol/nips/blob/master/51.md>
pub fn blocked_relays<I>(relay: I) -> Self
where
I: IntoIterator<Item = UncheckedUrl>,
{
let tags = relay.into_iter().map(Tag::Relay);
Self::new(Kind::BlockedRelays, "", tags)
}

/// Search relays
///
/// <https://github.com/nostr-protocol/nips/blob/master/51.md>
pub fn search_relays<I>(relay: I) -> Self
where
I: IntoIterator<Item = UncheckedUrl>,
{
let tags = relay.into_iter().map(Tag::Relay);
Self::new(Kind::SearchRelays, "", tags)
}

/// Interests
///
/// <https://github.com/nostr-protocol/nips/blob/master/51.md>
pub fn interests(list: Interests) -> Self {
let tags: Vec<Tag> = list.into();
Self::new(Kind::Interests, "", tags)
}

/// Emojis
///
/// <https://github.com/nostr-protocol/nips/blob/master/51.md>
pub fn emojis(list: Emojis) -> Self {
let tags: Vec<Tag> = list.into();
Self::new(Kind::Emojis, "", tags)
}

/// Follow sets
///
/// <https://github.com/nostr-protocol/nips/blob/master/51.md>
pub fn follow_sets<I>(publick_key: I) -> Self
where
I: IntoIterator<Item = PublicKey>,
{
let tags = publick_key.into_iter().map(Tag::public_key);
Self::new(Kind::FollowSets, "", tags)
}

/// Relay sets
///
/// <https://github.com/nostr-protocol/nips/blob/master/51.md>
pub fn relay_sets<I>(relay: I) -> Self
where
I: IntoIterator<Item = UncheckedUrl>,
{
let tags = relay.into_iter().map(Tag::Relay);
Self::new(Kind::RelaySets, "", tags)
}

/// Bookmark sets
///
/// <https://github.com/nostr-protocol/nips/blob/master/51.md>
pub fn bookmarks_sets(list: Bookmarks) -> Self {
let tags: Vec<Tag> = list.into();
Self::new(Kind::BookmarkSets, "", tags)
}

/// Article Curation sets
///
/// <https://github.com/nostr-protocol/nips/blob/master/51.md>
pub fn articles_curation_sets(list: ArticlesCuration) -> Self {
let tags: Vec<Tag> = list.into();
Self::new(Kind::ArticlesCurationSets, "", tags)
}

/// Videos Curation sets
///
/// <https://github.com/nostr-protocol/nips/blob/master/51.md>
pub fn videos_curation_sets<I>(video: I) -> Self
where
I: IntoIterator<Item = Coordinate>,
{
let tags = video.into_iter().map(Tag::from);
Self::new(Kind::VideosCurationSets, "", tags)
}

/// Emoji sets
///
/// <https://github.com/nostr-protocol/nips/blob/master/51.md>
pub fn emoji_sets<I>(emoji: I) -> Self
where
I: IntoIterator<Item = (String, UncheckedUrl)>,
{
let tags = emoji
.into_iter()
.map(|(s, url)| Tag::Emoji { shortcode: s, url });
Self::new(Kind::VideosCurationSets, "", tags)
}
}

#[cfg(test)]
Expand Down
72 changes: 64 additions & 8 deletions crates/nostr/src/event/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,38 @@ pub enum Kind {
MuteList,
/// Pin List (NIP51)
PinList,
/// Bookmarks (NIP51)
Bookmarks,
/// Communities (NIP51)
Communities,
/// Public Chats (NIP51)
PublicChats,
/// Blocked Relays (NIP51)
BlockedRelays,
/// Search Relays (NIP51)
SearchRelays,
/// Simple Groups (NIP51)
SimpleGroups,
/// Interests (NIP51)
Interests,
/// Emojis (NIP51)
Emojis,
/// Follow Sets (NIP51)
FollowSets,
/// Relay Sets (NIP51)
RelaySets,
/// Bookmark Sets (NIP51)
BookmarkSets,
/// Articles Curation Sets (NIP51)
ArticlesCurationSets,
/// Videos Curation Sets (NIP51)
VideosCurationSets,
/// Interest Sets (NIP51)
InterestSets,
/// Emoji Sets (NIP51)
EmojiSets,
/// Release Artifact Sets (NIP51)
ReleaseArtifactSets,
/// Relay List Metadata (NIP65)
RelayList,
/// Client Authentication (NIP42)
Expand All @@ -94,10 +126,6 @@ pub enum Kind {
WalletConnectResponse,
/// Nostr Connect (NIP46)
NostrConnect,
/// Categorized People List (NIP51)
CategorizedPeopleList,
/// Categorized Bookmark List (NIP51)
CategorizedBookmarkList,
/// Live Event (NIP53)
LiveEvent,
/// Live Event Message (NIP53)
Expand Down Expand Up @@ -268,13 +296,27 @@ impl From<u64> for Kind {
9735 => Self::ZapReceipt,
10000 => Self::MuteList,
10001 => Self::PinList,
10003 => Self::Bookmarks,
10004 => Self::Communities,
10005 => Self::PublicChats,
10006 => Self::BlockedRelays,
10007 => Self::SearchRelays,
10009 => Self::SimpleGroups,
10015 => Self::Interests,
10030 => Self::Emojis,
10002 => Self::RelayList,
22242 => Self::Authentication,
23194 => Self::WalletConnectRequest,
23195 => Self::WalletConnectResponse,
24133 => Self::NostrConnect,
30000 => Self::CategorizedPeopleList,
30001 => Self::CategorizedBookmarkList,
30000 => Self::FollowSets,
30002 => Self::RelaySets,
30003 => Self::BookmarkSets,
30004 => Self::ArticlesCurationSets,
30005 => Self::VideosCurationSets,
30015 => Self::InterestSets,
30030 => Self::EmojiSets,
30063 => Self::ReleaseArtifactSets,
30311 => Self::LiveEvent,
1311 => Self::LiveEventMessage,
30008 => Self::ProfileBadges,
Expand Down Expand Up @@ -332,13 +374,27 @@ impl From<Kind> for u64 {
Kind::ZapReceipt => 9735,
Kind::MuteList => 10000,
Kind::PinList => 10001,
Kind::Bookmarks => 10003,
Kind::Communities => 10004,
Kind::PublicChats => 10005,
Kind::BlockedRelays => 10006,
Kind::SearchRelays => 10007,
Kind::SimpleGroups => 10009,
Kind::Interests => 10015,
Kind::Emojis => 10030,
Kind::RelayList => 10002,
Kind::Authentication => 22242,
Kind::WalletConnectRequest => 23194,
Kind::WalletConnectResponse => 23195,
Kind::NostrConnect => 24133,
Kind::CategorizedPeopleList => 30000,
Kind::CategorizedBookmarkList => 30001,
Kind::FollowSets => 30000,
Kind::RelaySets => 30002,
Kind::BookmarkSets => 30003,
Kind::ArticlesCurationSets => 30004,
Kind::VideosCurationSets => 30005,
Kind::InterestSets => 30015,
Kind::EmojiSets => 30030,
Kind::ReleaseArtifactSets => 30063,
Kind::LiveEvent => 30311,
Kind::LiveEventMessage => 1311,
Kind::ProfileBadges => 30008,
Expand Down
8 changes: 8 additions & 0 deletions crates/nostr/src/event/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ pub enum TagKind {
Encrypted,
/// Request (NIP90)
Request,
/// Word
Word,
/// Custom tag kind
Custom(String),
}
Expand Down Expand Up @@ -495,6 +497,7 @@ impl fmt::Display for TagKind {
Self::Emoji => write!(f, "emoji"),
Self::Encrypted => write!(f, "encrypted"),
Self::Request => write!(f, "request"),
Self::Word => write!(f, "word"),
Self::Custom(tag) => write!(f, "{tag}"),
}
}
Expand Down Expand Up @@ -557,6 +560,7 @@ where
"emoji" => Self::Emoji,
"encrypted" => Self::Encrypted,
"request" => Self::Request,
"word" => Self::Word,
t => Self::Custom(t.to_owned()),
}
}
Expand Down Expand Up @@ -669,6 +673,7 @@ pub enum Tag {
status: DataVendingMachineStatus,
extra_info: Option<String>,
},
Word(String),
}

impl Tag {
Expand Down Expand Up @@ -784,6 +789,7 @@ impl Tag {
msg: (!tag_1.is_empty()).then_some(tag_1.to_owned()),
}),
TagKind::Request => Ok(Self::Request(Event::from_json(tag_1)?)),
TagKind::Word => Ok(Self::Word(tag_1.to_string())),
_ => Ok(Self::Generic(tag_kind, vec![tag_1.to_owned()])),
}
} else if tag_len == 3 {
Expand Down Expand Up @@ -1062,6 +1068,7 @@ impl Tag {
Self::Emoji { .. } => TagKind::Emoji,
Self::Encrypted => TagKind::Encrypted,
Self::Request(..) => TagKind::Request,
Self::Word(..) => TagKind::Word,
}
}
}
Expand Down Expand Up @@ -1284,6 +1291,7 @@ impl From<Tag> for Vec<String> {
}
tag
}
Tag::Word(word) => vec![TagKind::Word.to_string(), word],
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/nostr/src/nips/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub mod nip47;
pub mod nip48;
#[cfg(feature = "nip49")]
pub mod nip49;
pub mod nip51;
pub mod nip53;
#[cfg(feature = "nip57")]
pub mod nip57;
Expand Down
Loading

0 comments on commit b26ba6a

Please sign in to comment.