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 set_fwd_timeout(timeout)
37 REDIS.set("catapult_fwd_timeout-#{from_jid}", timeout)
38 end
39
40protected
41
42 def from_jid
43 Blather::JID.new(
44 "customer_#{@customer_id}",
45 CONFIG[:component][:jid]
46 )
47 end
48
49 def mkibr(type)
50 ibr = IBR.new(type, @jid)
51 ibr.from = from_jid
52 ibr
53 end
54end