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