@@ -7,12 +7,12 @@ defmodule Nostr.Client do
77
88 require Logger
99
10- alias Nostr.Event
11- alias Nostr.Keys . { PublicKey , PrivateKey }
12- alias Nostr.Event . { Signer , Validator }
13- alias Nostr.Event.Types . { EncryptedDirectMessageEvent , TextEvent }
10+ alias NostrBasics . { Event }
11+ alias NostrBasics.Keys . { PublicKey , PrivateKey }
12+
1413 alias Nostr.Models . { Profile , Note }
1514 alias Nostr.Client.Relays.RelayManager
15+ alias Nostr.Client.Tasks
1616
1717 alias Nostr.Client.Subscriptions . {
1818 ProfileSubscription ,
@@ -36,9 +36,6 @@ defmodule Nostr.Client do
3636 UpdateProfile
3737 }
3838
39- alias Nostr.Crypto.AES256CBC
40- alias Nostr.Client.Relays.RelaySocket
41-
4239 @ default_config { }
4340
4441 @ doc """
@@ -184,29 +181,11 @@ defmodule Nostr.Client do
184181 Sends an encrypted direct message
185182 """
186183 @ spec send_encrypted_direct_messages ( PublicKey . id ( ) , String . t ( ) , PrivateKey . id ( ) ) ::
187- :ok | { :error , binary ( ) }
184+ :ok | { :error , String . t ( ) }
188185 def send_encrypted_direct_messages ( remote_pubkey , message , private_key ) do
189- with { :ok , binary_remote_pubkey } <- PublicKey . to_binary ( remote_pubkey ) ,
190- { :ok , binary_private_key } <- PrivateKey . to_binary ( private_key ) ,
191- { :ok , binary_local_pubkey } <- PublicKey . from_private_key ( binary_private_key ) ,
192- encrypted_message = AES256CBC . encrypt ( message , binary_private_key , binary_remote_pubkey ) ,
193- dm_event =
194- EncryptedDirectMessageEvent . create (
195- encrypted_message ,
196- binary_local_pubkey ,
197- binary_remote_pubkey
198- ) ,
199- { :ok , signed_event } <- Signer . sign_event ( dm_event . event , private_key ) ,
200- :ok <- Validator . validate_event ( signed_event ) do
201- for relay_pid <- RelayManager . active_pids ( ) do
202- RelaySocket . send_event ( relay_pid , signed_event )
203- end
204-
205- :ok
206- else
207- { :error , message } when is_atom ( message ) -> Atom . to_string ( message )
208- { :error , message } -> { :error , message }
209- end
186+ relay_pids = RelayManager . active_pids ( )
187+
188+ Tasks.SendEncryptedDirectMessage . execute ( message , remote_pubkey , private_key , relay_pids )
210189 end
211190
212191 @ doc """
@@ -215,7 +194,7 @@ defmodule Nostr.Client do
215194 @ spec subscribe_note ( Note . id ( ) ) :: DynamicSupervisor . on_start_child ( )
216195 def subscribe_note ( note_id ) do
217196 case Event.Id . to_binary ( note_id ) do
218- { :ok , "note" , binary_note_id } ->
197+ { :ok , binary_note_id } ->
219198 DynamicSupervisor . start_child (
220199 Nostr.Subscriptions ,
221200 { NoteSubscription , [ RelayManager . active_pids ( ) , binary_note_id , self ( ) ] }
@@ -227,9 +206,9 @@ defmodule Nostr.Client do
227206 end
228207
229208 @ doc """
230- Get an author's notes
209+ Get a list of author's notes
231210 """
232- @ spec subscribe_notes ( list ( Note . id ( ) ) ) ::
211+ @ spec subscribe_notes ( list ( Note . id ( ) ) | Note . id ( ) ) ::
233212 { :ok , DynamicSupervisor . on_start_child ( ) } | { :error , String . t ( ) }
234213 def subscribe_notes ( pubkeys ) when is_list ( pubkeys ) do
235214 case PublicKey . to_binary ( pubkeys ) do
@@ -247,14 +226,18 @@ defmodule Nostr.Client do
247226 end
248227 end
249228
229+ def subscribe_notes ( pubkey ) do
230+ subscribe_notes ( [ pubkey ] )
231+ end
232+
250233 @ doc """
251234 Deletes events
252235 """
253236 @ spec delete_events ( list ( Note . id ( ) ) , String . t ( ) , PrivateKey . id ( ) ) ::
254237 { :ok , GenServer . on_start ( ) } | { :error , String . t ( ) }
255238 def delete_events ( note_ids , note , privkey ) do
256239 with { :ok , binary_privkey } <- PrivateKey . to_binary ( privkey ) ,
257- { :ok , "note" , binary_note_ids } <- Event.Id . to_binary ( note_ids ) do
240+ { :ok , binary_note_ids } <- Event.Id . to_binary ( note_ids ) do
258241 { :ok ,
259242 DeleteEvents . start_link ( RelayManager . active_pids ( ) , binary_note_ids , note , binary_privkey ) }
260243 else
@@ -285,7 +268,7 @@ defmodule Nostr.Client do
285268 @ spec repost ( Note . id ( ) , PrivateKey . id ( ) ) :: { :ok , GenServer . on_start ( ) } | { :error , String . t ( ) }
286269 def repost ( note_id , privkey ) do
287270 with { :ok , binary_privkey } <- PrivateKey . to_binary ( privkey ) ,
288- { :ok , "note" , binary_note_id } <- Event.Id . to_binary ( note_id ) do
271+ { :ok , binary_note_id } <- Event.Id . to_binary ( note_id ) do
289272 { :ok , SendRepost . start_link ( RelayManager . active_pids ( ) , binary_note_id , binary_privkey ) }
290273 else
291274 { :error , message } -> { :error , message }
@@ -352,27 +335,16 @@ defmodule Nostr.Client do
352335 """
353336 @ spec send_note ( String . t ( ) , PrivateKey . id ( ) ) :: :ok | { :error , String . t ( ) }
354337 def send_note ( note , privkey ) do
355- with { :ok , binary_privkey } <- PrivateKey . to_binary ( privkey ) ,
356- { :ok , pubkey } <- PublicKey . from_private_key ( privkey ) ,
357- text_event = TextEvent . create ( note , pubkey ) ,
358- { :ok , signed_event } <- Signer . sign_event ( text_event . event , binary_privkey ) ,
359- :ok <- Validator . validate_event ( signed_event ) do
360- for relay_pid <- RelayManager . active_pids ( ) do
361- RelaySocket . send_event ( relay_pid , signed_event )
362- end
363-
364- :ok
365- else
366- { :error , message } when is_atom ( message ) -> { :error , Atom . to_string ( message ) }
367- { :error , message } -> { :error , message }
368- end
338+ relay_pids = RelayManager . active_pids ( )
339+
340+ Tasks.SendNote . execute ( note , privkey , relay_pids )
369341 end
370342
371343 @ spec react ( Note . id ( ) , PrivateKey . id ( ) , String . t ( ) ) ::
372344 { :ok , GenServer . on_start ( ) } | { :error , String . t ( ) }
373345 def react ( note_id , privkey , content \\ "+" ) do
374346 with { :ok , binary_privkey } <- PrivateKey . to_binary ( privkey ) ,
375- { :ok , "note" , binary_note_id } <- Event.Id . to_binary ( note_id ) do
347+ { :ok , binary_note_id } <- Event.Id . to_binary ( note_id ) do
376348 {
377349 :ok ,
378350 SendReaction . start_link (
0 commit comments