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 "form_to_h"
10
11class BackendSgx
12	COMMANDS_DISCO_NODE = "http://jabber.org/protocol/commands"
13
14	class CanceledError < StandardError; end
15
16	using FormToH
17
18	value_semantics do
19		jid Blather::JID
20		creds HashOf(Symbol => String)
21		from_jid Blather::JID
22		ogm_url Either(String, nil, NotLoaded)
23		fwd Either(CustomerFwd, nil, NotLoaded)
24		transcription_enabled Either(Bool(), NotLoaded)
25		registered? Either(Blather::Stanza::Iq::IBR, FalseClass, NotLoaded)
26	end
27
28	def register!(tel)
29		iq = ibr
30		iq.phone = tel
31		IQ_MANAGER.write(iq).then do
32			REDIS.set("catapult_jid-#{tel}", from_jid.to_s)
33		end
34	end
35
36	def deregister!
37		ibr = Blather::Stanza::Iq::IBR.new(:set, @jid)
38		ibr.from = from_jid
39		ibr.remove!
40		IQ_MANAGER.write(ibr)
41	end
42
43	def stanza(s)
44		s.dup.tap do |stanza|
45			stanza.to = stanza.to.with(
46				domain: jid.domain,
47				node: jid.node || stanza.to.node
48			)
49			stanza.from = from_jid.with(resource: stanza.from&.resource)
50		end
51	end
52
53	def set_ogm_url(url)
54		REDIS.set("catapult_ogm_url-#{from_jid}", url)
55	end
56
57	def commands
58		iq = Blather::Stanza::DiscoItems.new(:get, COMMANDS_DISCO_NODE)
59		iq.to = ""
60		IQ_MANAGER.write(stanza(iq)).then(&:items).catch {
61			[]
62		}
63	end
64
65protected
66
67	def ibr
68		s = Blather::Stanza::Iq::IBR.new(:set, jid)
69		s.from = from_jid
70		s.nick = creds[:account]
71		s.username = creds[:username]
72		s.password = creds[:password]
73		s
74	end
75end