1# frozen_string_literal: true
 2
 3require "blather"
 4require "value_semantics/monkey_patched"
 5
 6require_relative "customer_fwd"
 7require_relative "not_loaded"
 8
 9class BackendSgx
10	value_semantics do
11		jid Blather::JID
12		creds HashOf(Symbol => String)
13		from_jid Blather::JID
14		ogm_url Either(String, nil, NotLoaded)
15		fwd Either(CustomerFwd, nil, NotLoaded)
16		transcription_enabled Either(Bool(), NotLoaded)
17		registered? Either(Blather::Stanza::Iq::IBR, FalseClass, NotLoaded)
18	end
19
20	def register!(tel)
21		ibr = Blather::Stanza::Iq::IBR.new(:set, @jid)
22		ibr.from = from_jid
23		ibr.nick = creds[:account]
24		ibr.username = creds[:username]
25		ibr.password = creds[:password]
26		ibr.phone = tel
27		IQ_MANAGER.write(ibr)
28	end
29
30	def deregister!
31		ibr = Blather::Stanza::Iq::IBR.new(:set, @jid)
32		ibr.from = from_jid
33		ibr.remove!
34		IQ_MANAGER.write(ibr)
35	end
36
37	def stanza(s)
38		s.dup.tap do |stanza|
39			stanza.to = stanza.to.with(
40				domain: jid.domain,
41				node: jid.node || stanza.to.node
42			)
43			stanza.from = from_jid.with(resource: stanza.from&.resource)
44		end
45	end
46
47	def set_ogm_url(url)
48		REDIS.set("catapult_ogm_url-#{from_jid}", url)
49	end
50end