~singpolyma/sgx-jmp

4b1183784defa863c2dcb7eb9a23a74d1aaccba3 — Stephen Paul Weber 1 year, 4 months ago 4251960
Refactor web-register to use lib/command
4 files changed, 45 insertions(+), 27 deletions(-)

A forms/web_register.rb
M lib/command_list.rb
M sgx_jmp.rb
M test/test_command_list.rb
A forms/web_register.rb => forms/web_register.rb +13 -0
@@ 0,0 1,13 @@
form!

field(
	var: "jid",
	type: "jid-single",
	required: true
)

field(
	var: "tel",
	type: "text-single",
	required: true
)

M lib/command_list.rb => lib/command_list.rb +4 -4
@@ 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: []

M sgx_jmp.rb => sgx_jmp.rb +22 -17
@@ 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)

M test/test_command_list.rb => test/test_command_list.rb +6 -6
@@ 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