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

NIP38: User Statuses #771

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

NIP38: User Statuses #771

wants to merge 8 commits into from

Conversation

reyamir
Copy link
Contributor

@reyamir reyamir commented Feb 17, 2025

Description

Add support for create NIP38 event with EventBuilder. Close: #666

Usage:

// Send a General statuses event to relays
let general = LiveStatus {
    status_type: StatusType::General,
    expiration: None,
    reference: None,
};
let builder = EventBuilder::live_statuses(general, "Building rust-nostr", vec![]);
client.send_event_builder(builder).await?;

// Send a Music statuses event to relays
let music = LiveStatus {
    status_type: StatusType::Music,
    expiration: Some(Timestamp::now()),
    reference: Some("spotify:search:Intergalatic%20-%20Beastie%20Boys".into()),
};
let builder = EventBuilder::live_statuses(music, "Intergalatic - Beastie Boys", vec![]);
client.send_event_builder(builder).await?;

Notes to the reviewers

None

Checklist

@reyamir reyamir marked this pull request as ready for review February 19, 2025 00:21
Copy link
Member

@yukibtc yukibtc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I've marked some changes to do

///
/// <https://github.com/nostr-protocol/nips/blob/master/38.md>
#[inline]
pub fn live_statuses<S, I>(status: LiveStatus, content: S, extra_tags: I) -> Self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename it to live_status, what do you think?
And since any extra tag can be added with EventBuilder::tags I think is better to remove the extra_tags arg.

@@ -150,6 +150,7 @@ kind_variants! {
Torrent => 2003, "Torrent", "<https://github.com/nostr-protocol/nips/blob/master/35.md>",
TorrentComment => 2004, "Torrent Comment", "<https://github.com/nostr-protocol/nips/blob/master/35.md>",
PeerToPeerOrder => 38383, "Peer-to-peer Order events", "<https://github.com/nostr-protocol/nips/blob/master/69.md>",
UserStatuses => 30315, "User Statuses", "<https://github.com/nostr-protocol/nips/blob/master/38.md>",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here, I would call it UserStatus


/// NIP38 types
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum StatusType {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rewrite this to:

/// NIP38 types
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum StatusType {
    /// General status: "Working", "Hiking", etc.
    #[default]
    General,
    /// Music what you are currently listening to
    Music,
    /// Custom status: "Playing", "Reading", etc.
    Custom(String),
}

reference,
}: LiveStatus,
) -> Self {
let mut tags = Vec::with_capacity(3);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rewrite this to:

let mut tags = Vec::with_capacity(3);

tags.push(Tag::identifier(status_type.to_string()));

if let Some(expire_at) = expiration {
    tags.push(Tag::expiration(expire_at));
}

if let Some(content) = reference {
    tags.push(Tag::reference(content));
}

@@ -28,11 +28,5 @@ async fn main() -> Result<()> {
let builder = EventBuilder::text_note("POW text note from rust-nostr").pow(20);
client.send_event_builder(builder).await?;

// Send a text note POW event to specific relays
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was removed accidentaly during tests

// Copyright (c) 2023-2025 Rust Nostr Developers
// Distributed under the MIT software license

use nips::nip38::{LiveStatus, StatusType};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add this line to the prelude module, so all NIP38 module is re-exported:

pub use crate::nips::nip38::{self, *};

/// Reference to the external resource (Optional)
pub reference: Option<String>,
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you thin about adding a constructor to build a status without optional stuff? Like:

impl LiveStatus {
    pub fn new(status_type: StatusType) -> Self {
        Self {
            status_type,
            expiration: None,
            reference: None
        }
    }
}

@yukibtc
Copy link
Member

yukibtc commented Feb 19, 2025

Ah, and can you also mark NIP38 as supported in crates/nostr/README.md?

@reyamir
Copy link
Contributor Author

reyamir commented Feb 20, 2025

Ah, thank for your suggestions, I will update it later today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NIP-38 support
2 participants