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 iq = ibr
22 iq.phone = tel
23 IQ_MANAGER.write(iq).then do
24 REDIS.set("catapult_jid-#{tel}", from_jid.to_s)
25 end
26 end
27
28 def deregister!
29 ibr = Blather::Stanza::Iq::IBR.new(:set, @jid)
30 ibr.from = from_jid
31 ibr.remove!
32 IQ_MANAGER.write(ibr)
33 end
34
35 def stanza(s)
36 s.dup.tap do |stanza|
37 stanza.to = stanza.to.with(
38 domain: jid.domain,
39 node: jid.node || stanza.to.node
40 )
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
48
49protected
50
51 def ibr
52 s = Blather::Stanza::Iq::IBR.new(:set, jid)
53 s.from = from_jid
54 s.nick = creds[:account]
55 s.username = creds[:username]
56 s.password = creds[:password]
57 s
58 end
59end