diff --git a/forms/web_register.rb b/forms/web_register.rb new file mode 100644 index 0000000000000000000000000000000000000000..503e0baf8ae905e59e499397125dbc48ef91559c --- /dev/null +++ b/forms/web_register.rb @@ -0,0 +1,13 @@ +form! + +field( + var: "jid", + type: "jid-single", + required: true +) + +field( + var: "tel", + type: "text-single", + required: true +) diff --git a/lib/command_list.rb b/lib/command_list.rb index 3307cf398b41779495c4db08539d0a16fb0b9f4a..fb87b7e831c761dcd5d8912173bd57885dc75de8 100644 --- a/lib/command_list.rb +++ b/lib/command_list.rb @@ -8,15 +8,15 @@ class CommandList @commands << command end - def self.for(customer) - args_for(customer).then do |kwargs| + def self.for(customer, from_jid) + args_for(customer, from_jid).then do |kwargs| new(@commands.select { |c| c.list_for?(**kwargs) }) end end - def self.args_for(customer) + def self.args_for(customer, from_jid) args = { - customer: customer, + from_jid: from_jid, customer: customer, tel: customer&.registered? ? customer&.registered?&.phone : nil, fwd: customer&.fwd, payment_methods: [] diff --git a/sgx_jmp.rb b/sgx_jmp.rb index da41e128c7f896638caa13afce5402e3524c18df..127ed311ec3b1e52a0e77f5fef7e4e8a8656bb41 100644 --- a/sgx_jmp.rb +++ b/sgx_jmp.rb @@ -377,7 +377,7 @@ disco_items node: "http://jabber.org/protocol/commands" do |iq| ).catch { nil }.then { |customer| - CommandList.for(customer) + CommandList.for(customer, iq.from) }.then { |list| reply.items = list.map { |item| Blather::Stanza::DiscoItems::Item.new( @@ -714,19 +714,28 @@ def reply_with_note(iq, text, type: :info) self << reply end -command :execute?, node: "web-register" do |iq| - StatsD.increment("command", tags: ["node:#{iq.node}"]) - - sentry_hub = new_sentry_hub(iq, name: iq.node) +Command.new( + "web-register", + "Initiate Register from Web", + list_for: lambda { |from_jid: nil, **| + from_jid&.stripped.to_s == CONFIG[:web_register][:from] + } +) { + if Command.execution.iq.from.stripped != CONFIG[:web_register][:from] + next EMPromise.reject( + Command::Execution::FinalStanza.new(iq.as_error("forbidden", :auth)) + ) + end - begin + Command.reply { |reply| + reply.command << FormTemplate.render("web_register") + }.then do |iq| jid = iq.form.field("jid")&.value.to_s.strip tel = iq.form.field("tel")&.value.to_s.strip - sentry_hub.current_scope.set_user(jid: jid, tel: tel) - if iq.from.stripped != CONFIG[:web_register][:from] - BLATHER << iq.as_error("forbidden", :auth) - elsif jid !~ /\./ || tel !~ /\A\+\d+\Z/ - reply_with_note(iq, "Invalid JID or telephone number.", type: :error) + if jid !~ /\./ + Command.finish("The Jabber ID you entered was not valid.", type: :error) + elsif tel !~ /\A\+\d+\Z/ + Command.finish("Invalid telephone number", type: :error) else IQ_MANAGER.write(Blather::Stanza::Iq::Command.new.tap { |cmd| cmd.to = CONFIG[:web_register][:to] @@ -735,14 +744,10 @@ command :execute?, node: "web-register" do |iq| cmd.form.type = "submit" }).then { |result| TEL_SELECTIONS.set(result.form.field("from")&.value.to_s.strip, tel) - }.then { - BLATHER << iq.reply.tap { |reply| reply.status = :completed } - }.catch { |e| panic(e, sentry_hub) } + }.then { Command.finish }.catch { |e| panic(e, sentry_hub) } end - rescue StandardError => e - sentry_hub.capture_exception(e) end -end +}.register(self).then(&CommandList.method(:register)) command sessionid: /./ do |iq| COMMAND_MANAGER.fulfill(iq) diff --git a/test/test_command_list.rb b/test/test_command_list.rb index 0fe845106ddde6ae378b57a294d585fa3be9876f..31c99a71076c33c26dcb2012bc8a649a02e43fb0 100644 --- a/test/test_command_list.rb +++ b/test/test_command_list.rb @@ -32,7 +32,7 @@ class CommandListTest < Minitest::Test def test_for_no_customer assert_equal( ["no_customer"], - CommandList.for(nil).sync.map { |c| c[:node] } + CommandList.for(nil, "bob@example.com").sync.map { |c| c[:node] } ) end em :test_for_no_customer @@ -41,7 +41,7 @@ class CommandListTest < Minitest::Test customer = OpenStruct.new(registered?: false) assert_equal( ["no_customer"], - CommandList.for(customer).sync.map { |c| c[:node] } + CommandList.for(customer, "bob@example.com").sync.map { |c| c[:node] } ) end em :test_for_unregistered @@ -53,7 +53,7 @@ class CommandListTest < Minitest::Test ) assert_equal( ["no_customer", "registered"], - CommandList.for(customer).sync.map { |c| c[:node] } + CommandList.for(customer, "bob@example.com").sync.map { |c| c[:node] } ) end em :test_for_registered @@ -66,7 +66,7 @@ class CommandListTest < Minitest::Test ) assert_equal( ["no_customer", "registered", "fwd"], - CommandList.for(customer).sync.map { |c| c[:node] } + CommandList.for(customer, "bob@example.com").sync.map { |c| c[:node] } ) end em :test_for_registered_with_fwd @@ -79,7 +79,7 @@ class CommandListTest < Minitest::Test ) assert_equal( ["no_customer", "registered", "cc"], - CommandList.for(customer).sync.map { |c| c[:node] } + CommandList.for(customer, "bob@example.com").sync.map { |c| c[:node] } ) end em :test_for_registered_with_credit_card @@ -91,7 +91,7 @@ class CommandListTest < Minitest::Test ) assert_equal( ["no_customer", "registered", "currency"], - CommandList.for(customer).sync.map { |c| c[:node] } + CommandList.for(customer, "bob@example.com").sync.map { |c| c[:node] } ) end em :test_for_registered_with_currency