1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use crate::bech32::auto_bech32_to_hex;
use crate::{
events::{Event, EventPrepare},
nostr_client::{Client, ClientError},
utils::get_timestamp,
Identity,
};
use thiserror::Error;
#[derive(Error, Debug)]
pub enum NIP25Error {
#[error("The client has an error")]
ClientError(ClientError),
#[error("Bech32 Error: {}", _0)]
Bech32Error(#[from] crate::bech32::Bech32Error),
}
impl From<ClientError> for NIP25Error {
fn from(err: ClientError) -> Self {
Self::ClientError(err)
}
}
impl Client {
#[cfg(not(feature = "async"))]
/// React to an event
///
/// '+' = Like\
/// '-' = Dislike\
/// Emoji = React with an emoji
///
/// # Example
/// ```rust
/// use nostr_rust::{nostr_client::Client, Identity};
/// use std::str::FromStr;
/// let mut client = Client::new(vec![env!("RELAY_URL")]).unwrap();
/// let identity = Identity::from_str(env!("SECRET_KEY")).unwrap();
///
/// // Here we react to an event
/// client.react_to(&identity, "342060554ca30a9792f6e6959675ae734aed02c23e35037d2a0f72ac6316e83d", "884704bd421721e292edbff42eb77547fe115c6ff9825b08fc366be4cd69e9f6", "+", 0).unwrap();
/// ```
pub fn react_to(
&mut self,
identity: &Identity,
event_id: &str,
event_pub_key: &str,
reaction: &str,
difficulty_target: u16,
) -> Result<Event, NIP25Error> {
let hex_id = auto_bech32_to_hex(event_id)?;
let hex_pk = auto_bech32_to_hex(event_pub_key)?;
let event = EventPrepare {
pub_key: identity.public_key_str.clone(),
created_at: get_timestamp(),
kind: 7,
tags: vec![vec!["e".to_string(), hex_id], vec!["p".to_string(), hex_pk]],
content: reaction.to_string(),
}
.to_event(identity, difficulty_target);
self.publish_event(&event)?;
Ok(event)
}
#[cfg(feature = "async")]
/// React to an event
///
/// '+' = Like\
/// '-' = Dislike\
/// Emoji = React with an emoji
///
/// # Example
/// ```rust
/// use nostr_rust::{nostr_client::Client, Identity};
/// use std::str::FromStr;
///
/// #[tokio::test]
/// async fn test_react_to() {
/// let mut client = Client::new(vec![env!("RELAY_URL")]).await.unwrap();
/// let identity = Identity::from_str(env!("SECRET_KEY")).unwrap();
///
/// // Here we react to an event
/// client.react_to(&identity, "342060554ca30a9792f6e6959675ae734aed02c23e35037d2a0f72ac6316e83d", "884704bd421721e292edbff42eb77547fe115c6ff9825b08fc366be4cd69e9f6", "+", 0).await.unwrap();
/// }
/// ```
pub async fn react_to(
&mut self,
identity: &Identity,
event_id: &str,
event_pub_key: &str,
reaction: &str,
difficulty_target: u16,
) -> Result<Event, NIP25Error> {
let hex_id = auto_bech32_to_hex(event_id)?;
let hex_pk = auto_bech32_to_hex(event_pub_key)?;
let event = EventPrepare {
pub_key: identity.public_key_str.clone(),
created_at: get_timestamp(),
kind: 7,
tags: vec![vec!["e".to_string(), hex_id], vec!["p".to_string(), hex_pk]],
content: reaction.to_string(),
}
.to_event(identity, difficulty_target);
self.publish_event(&event).await?;
Ok(event)
}
#[cfg(not(feature = "async"))]
/// Add a like to an event
///
/// # Example
/// ```rust
/// use nostr_rust::{nostr_client::Client, Identity};
/// use std::str::FromStr;
/// let mut client = Client::new(vec![env!("RELAY_URL")]).unwrap();
/// let identity = Identity::from_str(env!("SECRET_KEY")).unwrap();
/// client.like(&identity, "342060554ca30a9792f6e6959675ae734aed0223e35037d2a0f72ac6316e83d", "884704bd421721e292edbff42eb77547fe115c6ff9825b08fc366be4cd69e9f6", 0).unwrap();
/// ```
pub fn like(
&mut self,
identity: &Identity,
event_id: &str,
event_pub_key: &str,
difficulty_target: u16,
) -> Result<Event, NIP25Error> {
let hex_id = auto_bech32_to_hex(event_id)?;
let hex_pk = auto_bech32_to_hex(event_pub_key)?;
self.react_to(identity, &hex_id, &hex_pk, "+", difficulty_target)
}
#[cfg(feature = "async")]
/// Add a like to an event
///
/// # Example
/// ```rust
/// use nostr_rust::{nostr_client::Client, Identity};
/// use std::str::FromStr;
///
/// #[tokio::test]
/// async fn test_like() {
/// let mut client = Client::new(vec![env!("RELAY_URL")]).await.unwrap();
/// let identity = Identity::from_str(env!("SECRET_KEY")).unwrap();
/// client.like(&identity, "342060554ca30a9792f6e6959675ae734aed0223e35037d2a0f72ac6316e83d", "884704bd421721e292edbff42eb77547fe115c6ff9825b08fc366be4cd69e9f6", 0).await.unwrap();
/// }
/// ```
pub async fn like(
&mut self,
identity: &Identity,
event_id: &str,
event_pub_key: &str,
difficulty_target: u16,
) -> Result<Event, NIP25Error> {
let hex_id = auto_bech32_to_hex(event_id)?;
let hex_pk = auto_bech32_to_hex(event_pub_key)?;
self.react_to(identity, &hex_id, &hex_pk, "+", difficulty_target)
.await
}
#[cfg(not(feature = "async"))]
/// Add a dislike to an event
///
/// # Example
/// ```rust
/// use nostr_rust::{nostr_client::Client, Identity};
/// use std::str::FromStr;
/// let mut client = Client::new(vec![env!("RELAY_URL")]).unwrap();
/// let identity = Identity::from_str(env!("SECRET_KEY")).unwrap();
/// client.dislike(&identity, "342060554ca30a9792f6e6959675ae734aed02c23e35037d2a0f72ac6316e83d", "884704bd421721e292edbff42eb77547fe115c6ff9825b08fc366be4cd69e9f6", 0).unwrap();
/// ```
pub fn dislike(
&mut self,
identity: &Identity,
event_id: &str,
event_pub_key: &str,
difficulty_target: u16,
) -> Result<Event, NIP25Error> {
let hex_id = auto_bech32_to_hex(event_id)?;
let hex_pk = auto_bech32_to_hex(event_pub_key)?;
self.react_to(identity, &hex_id, &hex_pk, "-", difficulty_target)
}
#[cfg(feature = "async")]
/// Add a dislike to an event
///
/// # Example
/// ```rust
/// use nostr_rust::{nostr_client::Client, Identity};
/// use std::str::FromStr;
///
/// #[tokio::test]
/// async fn test_dislike() {
/// let mut client = Client::new(vec![env!("RELAY_URL")]).await.unwrap();
/// let identity = Identity::from_str(env!("SECRET_KEY")).unwrap();
/// client.dislike(&identity, "342060554ca30a9792f6e6959675ae734aed02c23e35037d2a0f72ac6316e83d", "884704bd421721e292edbff42eb77547fe115c6ff9825b08fc366be4cd69e9f6", 0).await.unwrap();
/// }
/// ```
pub async fn dislike(
&mut self,
identity: &Identity,
event_id: &str,
event_pub_key: &str,
difficulty_target: u16,
) -> Result<Event, NIP25Error> {
let hex_id = auto_bech32_to_hex(event_id)?;
let hex_pk = auto_bech32_to_hex(event_pub_key)?;
self.react_to(identity, &hex_id, &hex_pk, "-", difficulty_target)
.await
}
}