~singpolyma/sgx-jmp

ref: d8cb729a32b91afb11284d141a5beaf76a279de9 sgx-jmp/lib/bwmsgsv2_repo.rb -rw-r--r-- 1.4 KiB
d8cb729aStephen Paul Weber CustomerFwd uses ValueSemantics, translates old XMPP-SIP URI 1 year, 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: true

require "lazy_object"

require_relative "customer_fwd"
require_relative "ibr"
require_relative "trivial_backend_sgx_repo"

class Bwmsgsv2Repo
	VOICEMAIL_TRANSCRIPTION_DISABLED = 0

	def initialize(jid: CONFIG[:sgx], redis: LazyObject.new { REDIS }, **kwargs)
		@jid = jid
		@redis = redis
		@trivial_repo = TrivialBackendSgxRepo.new(jid: jid, **kwargs)
	end

	def get(customer_id)
		sgx = @trivial_repo.get(customer_id)
		fetch_raw(sgx.from_jid).then do |(((ogm_url, fwd_time, fwd), trans_d), reg)|
			sgx.with({
				ogm_url: ogm_url,
				fwd: CustomerFwd.for(uri: fwd, timeout: fwd_time),
				transcription_enabled: !trans_d,
				registered?: reg
			}.compact)
		end
	end

protected

	def fetch_raw(from_jid)
		registration(from_jid).then do |r|
			EMPromise.all([from_redis(from_jid, r ? r.phone : nil), r])
		end
	end

	def registration(from_jid)
		ibr = IBR.new(:get, @jid)
		ibr.from = from_jid

		IQ_MANAGER.write(ibr).catch { nil }.then do |result|
			if result&.respond_to?(:registered?) && result&.registered?
				result
			else
				false
			end
		end
	end

	def from_redis(from_jid, tel)
		EMPromise.all([
			@redis.mget(*[
				"catapult_ogm_url-#{from_jid}",
				"catapult_fwd_timeout-#{from_jid}",
				("catapult_fwd-#{tel}" if tel)
			].compact),
			@redis.getbit(
				"catapult_setting_flags-#{from_jid}", VOICEMAIL_TRANSCRIPTION_DISABLED
			).then { |x| x == 1 }
		])
	end
end