@@ -8,12 +8,12 @@ defmodule Nostr.Client.Workflows.UpdateProfile do
88
99 require Logger
1010
11+ alias NostrBasics.Event
1112 alias NostrBasics.Event . { Signer , Validator }
1213 alias NostrBasics.Keys.PublicKey
1314
1415 alias Nostr.Client.Relays.RelaySocket
1516 alias Nostr.Models.Profile
16- alias Nostr.Event.Types . { MetadataEvent }
1717
1818 def start_link ( relay_pids , % Profile { } = new_profile , privkey ) do
1919 GenServer . start ( __MODULE__ , % {
@@ -27,40 +27,44 @@ defmodule Nostr.Client.Workflows.UpdateProfile do
2727 def init ( % { new_profile: new_profile } = state ) do
2828 send ( self ( ) , { :update , new_profile } )
2929
30- {
31- :ok ,
32- state
33- |> Map . put ( :treated , false )
34- }
30+ { :ok , state }
3531 end
3632
3733 @ impl true
3834 def handle_info (
3935 { :update , new_profile } ,
40- % { treated: false , privkey: privkey , relay_pids: relay_pids } = state
36+ % { privkey: privkey , relay_pids: relay_pids } = state
4137 ) do
4238 update_profile ( new_profile , privkey , relay_pids )
4339
44- {
45- :noreply ,
46- state
47- |> Map . put ( :treated , true )
48- }
40+ { :noreply , state }
4941 end
5042
51- defp update_profile ( % Profile { } = new_profile , privkey , relay_pids ) do
52- pubkey = PublicKey . from_private_key! ( privkey )
43+ defp update_profile ( % Profile { } = new_profile , private_key , relay_pids ) do
44+ with { :ok , pubkey } <- PublicKey . from_private_key ( private_key ) ,
45+ { :ok , profile_event } <- create_profile_event ( new_profile , pubkey ) ,
46+ { :ok , signed_event } <- prepare_and_sign_event ( profile_event , private_key ) do
47+ :ok = Validator . validate_event ( signed_event )
5348
54- with { :ok , event } <- MetadataEvent . create_event ( new_profile , pubkey ) ,
55- { :ok , signed_event } <- Signer . sign_event ( event , privkey ) do
56- Validator . validate_event ( signed_event )
5749 send_event ( signed_event , relay_pids )
50+
51+ :ok
5852 else
59- { :error , message } ->
60- Logger . warning ( message )
53+ { :error , message } -> { :error , message }
6154 end
6255 end
6356
57+ defp create_profile_event ( % Profile { } = profile , pubkey ) do
58+ profile
59+ |> Profile . to_event ( pubkey )
60+ end
61+
62+ defp prepare_and_sign_event ( event , private_key ) do
63+ % Event { event | created_at: DateTime . utc_now ( ) }
64+ |> Event . add_id ( )
65+ |> Signer . sign_event ( private_key )
66+ end
67+
6468 defp send_event ( validated_event , relay_pids ) do
6569 for relay_pid <- relay_pids do
6670 RelaySocket . send_event ( relay_pid , validated_event )
0 commit comments