backend_sgx.rb

  1# frozen_string_literal: true
  2
  3require "blather"
  4require "blather/stanza/iq/command"
  5require "value_semantics/monkey_patched"
  6
  7require_relative "customer_fwd"
  8require_relative "not_loaded"
  9require_relative "blather_notify"
 10require_relative "form_to_h"
 11
 12class BackendSgx
 13	using FormToH
 14
 15	value_semantics do
 16		jid Blather::JID
 17		creds HashOf(Symbol => String)
 18		from_jid Blather::JID
 19		ogm_url Either(String, nil, NotLoaded)
 20		fwd Either(CustomerFwd, nil, NotLoaded)
 21		transcription_enabled Either(Bool(), NotLoaded)
 22		registered? Either(Blather::Stanza::Iq::IBR, FalseClass, NotLoaded)
 23	end
 24
 25	def register!(tel)
 26		iq = ibr
 27		iq.phone = tel
 28		IQ_MANAGER.write(iq).then do
 29			REDIS.set("catapult_jid-#{tel}", from_jid.to_s)
 30		end
 31	end
 32
 33	def deregister!
 34		ibr = Blather::Stanza::Iq::IBR.new(:set, @jid)
 35		ibr.from = from_jid
 36		ibr.remove!
 37		IQ_MANAGER.write(ibr)
 38	end
 39
 40	def stanza(s)
 41		s.dup.tap do |stanza|
 42			stanza.to = stanza.to.with(
 43				domain: jid.domain,
 44				node: jid.node || stanza.to.node
 45			)
 46			stanza.from = from_jid.with(resource: stanza.from&.resource)
 47		end
 48	end
 49
 50	def set_ogm_url(url)
 51		REDIS.set("catapult_ogm_url-#{from_jid}", url)
 52	end
 53
 54	def set_port_out_pin(pin)
 55		cmd = build_port_out_command(:execute)
 56
 57		IQ_MANAGER.write(cmd).then { |reply|
 58			session_id = reply.command[:sessionid]
 59			submit_cmd = build_submit_form(pin, session_id)
 60
 61			IQ_MANAGER.write(submit_cmd).then { |submit_reply|
 62				validate_submit_reply!(submit_reply)
 63			}.catch { |e|
 64				handle_pin_submission_error(e)
 65			}
 66		}
 67	end
 68
 69protected
 70
 71	def ibr
 72		s = Blather::Stanza::Iq::IBR.new(:set, jid)
 73		s.from = from_jid
 74		s.nick = creds[:account]
 75		s.username = creds[:username]
 76		s.password = creds[:password]
 77		s
 78	end
 79
 80	def build_submit_form(pin, session_id)
 81		build_port_out_command(:complete, session_id: session_id).tap { |iq|
 82			iq.form.type = :submit
 83			iq.form.fields = [
 84				{ var: "pin", value: pin, type: "text-private" },
 85				{ var: "confirm_pin", value: pin, type: "text-private" }
 86			]
 87		}
 88	end
 89
 90	def build_port_out_command(action, session_id: nil)
 91		Blather::Stanza::Iq::Command.new.tap { |iq|
 92			iq.to = jid
 93			iq.from = from_jid
 94			iq.node = "set-port-out-pin"
 95			iq.action = action
 96			iq.sessionid = session_id if session_id
 97		}
 98	end
 99
100	def validate_submit_reply!(submit_reply)
101		sub_text = submit_reply.note&.text
102		case submit_reply.status
103		when :completed
104			raise sub_text if submit_reply.note&.[]("type") == "error"
105		when :canceled
106			raise CanceledError, reply.note&.text
107		else
108			raise sub_text
109		end
110	end
111
112	def handle_pin_submission_error(e)
113		if e.is_a?(Blather::StanzaError) || e.is_a?(RuntimeError)
114			EMPromise.reject(e)
115		else
116			Sentry.capture_exception(e)
117			EMPromise.reject(
118				RuntimeError.new(
119					"Unable to communicate with service. Please try again later."
120				)
121			)
122		end
123	end
124end