1# frozen_string_literal: true
2
3class BackendSgx
4 VOICEMAIL_TRANSCRIPTION_DISABLED = 0
5
6 def initialize(customer_id, jid=CONFIG[:sgx], creds=CONFIG[:creds])
7 @customer_id = customer_id
8 @jid = jid
9 @creds = creds
10 end
11
12 def register!(tel)
13 ibr = mkibr(:set)
14 ibr.nick = @creds[:account]
15 ibr.username = @creds[:username]
16 ibr.password = @creds[:password]
17 ibr.phone = tel
18 IQ_MANAGER.write(ibr)
19 end
20
21 def registered?
22 IQ_MANAGER.write(mkibr(:get)).catch { nil }.then do |result|
23 if result&.respond_to?(:registered?) && result&.registered?
24 result
25 else
26 false
27 end
28 end
29 end
30
31 def stanza(s)
32 s.dup.tap do |stanza|
33 stanza.to = stanza.to.with(domain: @jid)
34 stanza.from = from_jid.with(resource: stanza.from.resource)
35 end
36 end
37
38 def ogm_url
39 REDIS.get("catapult_ogm_url-#{from_jid}")
40 end
41
42 def catapult_flag(flagbit)
43 REDIS.getbit(
44 "catapult_setting_flags-#{from_jid}",
45 flagbit
46 ).then { |x| x == 1 }
47 end
48
49 def fwd_timeout
50 REDIS.get("catapult_fwd_timeout-#{from_jid}")
51 end
52
53 def set_fwd_timeout(timeout)
54 REDIS.set("catapult_fwd_timeout-#{from_jid}", timeout)
55 end
56
57protected
58
59 def from_jid
60 Blather::JID.new(
61 "customer_#{@customer_id}",
62 CONFIG[:component][:jid]
63 )
64 end
65
66 def mkibr(type)
67 ibr = IBR.new(type, @jid)
68 ibr.from = from_jid
69 ibr
70 end
71end