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