From 619bd954b70fb8e75477688c31786a9782fc9fc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Sun, 21 Apr 2024 10:51:41 +0200 Subject: [PATCH] WIP --- app/services/nostr_manager/publish_event.rb | 48 ++++++++++++++++++ config/sidekiq.yml | 1 + lib/nostr/event_kind.rb | 55 +++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 app/services/nostr_manager/publish_event.rb create mode 100644 lib/nostr/event_kind.rb diff --git a/app/services/nostr_manager/publish_event.rb b/app/services/nostr_manager/publish_event.rb new file mode 100644 index 0000000..35ab5be --- /dev/null +++ b/app/services/nostr_manager/publish_event.rb @@ -0,0 +1,48 @@ +module NostrManager + class PublishEvent < NostrManagerService + def initialize(event: nil, relay: nil) + # @relay = relay + @relay = Nostr::Relay.new(url: 'ws://nostr-relay:7777', name: 'strfry') + keypair = Nostr::KeyPair.new( + private_key: Nostr::PrivateKey.new(Setting.nostr_private_key), + public_key: Nostr::PublicKey.new(Setting.nostr_public_key) + ) + @user = Nostr::User.new(keypair: keypair) + @event = @user.create_event( + kind: Nostr::EventKind::TEXT_NOTE, + content: "The time is #{Time.now.strftime('%H:%M:%S')}" + ) + @client = Nostr::Client.new + end + + def call + client, relay, event = @client, @relay, @event + + client.on :connect do + puts "Connected to #{relay.url}" + puts "Publishing #{event.id}..." + client.publish event + end + + client.on :error do |e| + puts "Error: #{e}" + puts "Closing thread..." + Thread.exit + end + + client.on :message do |m| + puts "Message: #{m}" + msg = JSON.parse(m) rescue [] + if msg[0] == "OK" && msg[1] == event.id + puts "Event published. Closing thread..." + else + puts "Unexpected message from relay. Closing thread..." + end + Thread.exit + end + + puts "Connecting to #{relay.url}..." + client.connect relay + end + end +end diff --git a/config/sidekiq.yml b/config/sidekiq.yml index 481ec8e..eb26f1e 100644 --- a/config/sidekiq.yml +++ b/config/sidekiq.yml @@ -3,3 +3,4 @@ - default - mailers - remotestorage + - nostr diff --git a/lib/nostr/event_kind.rb b/lib/nostr/event_kind.rb new file mode 100644 index 0000000..7445a1c --- /dev/null +++ b/lib/nostr/event_kind.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +module Nostr + # Defines the event kinds that can be emitted by clients. + module EventKind + # The content is set to a stringified JSON object +{name: , about: , + # picture: }+ describing the user who created the event. A relay may delete past set_metadata + # events once it gets a new one for the same pubkey. + # + # @return [Integer] + # + SET_METADATA = 0 + + # The content is set to the text content of a note (anything the user wants to say). + # Non-plaintext notes should instead use kind 1000-10000 as described in NIP-16. + # + # @return [Integer] + # + TEXT_NOTE = 1 + + # The content is set to the URL (e.g., wss://somerelay.com) of a relay the event creator wants to + # recommend to its followers. + # + # @return [Integer] + # + RECOMMEND_SERVER = 2 + + # A special event with kind 3, meaning "contact list" is defined as having a list of p tags, one for each of + # the followed/known profiles one is following. + # + # @return [Integer] + # + CONTACT_LIST = 3 + + # A special event with kind 4, meaning "encrypted direct message". An event of this kind has its +content+ + # equal to the base64-encoded, aes-256-cbc encrypted string of anything a user wants to write, encrypted using a + # shared cipher generated by combining the recipient's public-key with the sender's private-key. + # + # @return [Integer] + # + ENCRYPTED_DIRECT_MESSAGE = 4 + + # NIP-57 Zap request + # + # @return [Integer] + # + ZAP_REQUEST = 9734 + + # NIP-57 Zap receipt + # + # @return [Integer] + # + ZAP_RECEIPT = 9735 + end +end