backend_sgx.rb

 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		alternate_transcription_enabled Either(Bool(), NotLoaded)
18		registered? Either(Blather::Stanza::Iq::IBR, FalseClass, NotLoaded)
19	end
20
21	def register!(tel)
22		ibr = Blather::Stanza::Iq::IBR.new(:set, @jid)
23		ibr.from = from_jid
24		ibr.nick = creds[:account]
25		ibr.username = creds[:username]
26		ibr.password = creds[:password]
27		ibr.phone = tel
28		IQ_MANAGER.write(ibr)
29	end
30
31	def deregister!
32		ibr = Blather::Stanza::Iq::IBR.new(:set, @jid)
33		ibr.from = from_jid
34		ibr.remove!
35		IQ_MANAGER.write(ibr)
36	end
37
38	def stanza(s)
39		s.dup.tap do |stanza|
40			stanza.to = stanza.to.with(domain: jid.domain)
41			stanza.from = from_jid.with(resource: stanza.from.resource)
42		end
43	end
44
45	def set_ogm_url(url)
46		REDIS.set("catapult_ogm_url-#{from_jid}", url)
47	end
48end