diff --git a/lib/backend_sgx.rb b/lib/backend_sgx.rb index 8001e96674cfdb78cf786f74fa0ea5a3643ad49c..2bcb112479c0b8d72bf9245693219d24aa828425 100644 --- a/lib/backend_sgx.rb +++ b/lib/backend_sgx.rb @@ -33,6 +33,10 @@ class BackendSgx end end + def ogm_url + REDIS.get("catapult_ogm_url-#{from_jid}") + end + def set_fwd_timeout(timeout) REDIS.set("catapult_fwd_timeout-#{from_jid}", timeout) end diff --git a/lib/customer.rb b/lib/customer.rb index 4cd6619deb2fe947319c7ceda6c26fb4a212dd6e..b66858371fc897d0fbcd1d6d660a97fa12fd4562 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -5,6 +5,7 @@ require "forwardable" require_relative "./api" require_relative "./blather_ext" require_relative "./customer_info" +require_relative "./customer_ogm" require_relative "./customer_plan" require_relative "./customer_usage" require_relative "./backend_sgx" @@ -80,6 +81,12 @@ class Customer stanza_to(iq, &IQ_MANAGER.method(:write)).then(&:vcard) end + def ogm(from_tel=nil) + @sgx.ogm_url.then do |url| + CustomerOGM.for(url, -> { fetch_vcard_temp(from_tel) }) + end + end + def sip_account SipAccount.find(customer_id) end diff --git a/lib/customer_ogm.rb b/lib/customer_ogm.rb new file mode 100644 index 0000000000000000000000000000000000000000..d52164181ec3244b32edf5e513b88e250baa3859 --- /dev/null +++ b/lib/customer_ogm.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +module CustomerOGM + def self.for(url, fetch_vcard_temp) + return Media.new(url) if url + TTS.for(fetch_vcard_temp) + end + + class Media + def initialize(url) + @url = url + end + + def to_render + [:voicemail_ogm_media, locals: { url: @url }] + end + end + + class TTS + def self.for(fetch_vcard_temp) + fetch_vcard_temp.call.then { |vcard| + new(vcard) + }.catch { new(Blather::Stanza::Iq::Vcard::Vcard.new) } + end + + def initialize(vcard) + @vcard = vcard + end + + def [](k) + value = @vcard[k] + return if value.to_s.empty? + value + end + + def fn + self["FN"] || self["NICKNAME"] || "a user of JMP.chat" + end + + def to_render + [:voicemail_ogm_tts, locals: { fn: fn }] + end + end +end diff --git a/test/test_customer_ogm.rb b/test/test_customer_ogm.rb new file mode 100644 index 0000000000000000000000000000000000000000..d272cddb36a07742665076c9ad800dd4ec58c1f5 --- /dev/null +++ b/test/test_customer_ogm.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require "test_helper" +require "customer_ogm" + +class CustomerOGMTest < Minitest::Test + def test_for_url + assert_kind_of( + CustomerOGM::Media, + CustomerOGM.for("https://example.com/test.mp3", -> {}) + ) + end + + def test_for_no_url + assert_kind_of( + CustomerOGM::TTS, + CustomerOGM.for(nil, -> { EMPromise.resolve(nil) }).sync + ) + end + em :test_for_no_url + + class TTSTest < Minitest::Test + def test_to_render_empty_vcard + vcard = Blather::Stanza::Iq::Vcard::Vcard.new + assert_equal( + [:voicemail_ogm_tts, locals: { fn: "a user of JMP.chat" }], + CustomerOGM::TTS.new(vcard).to_render + ) + end + + def test_to_render_fn + vcard = Blather::Stanza::Iq::Vcard::Vcard.new + vcard["FN"] = "name" + assert_equal( + [:voicemail_ogm_tts, locals: { fn: "name" }], + CustomerOGM::TTS.new(vcard).to_render + ) + end + + def test_to_render_nickname + vcard = Blather::Stanza::Iq::Vcard::Vcard.new + vcard["NICKNAME"] = "name" + assert_equal( + [:voicemail_ogm_tts, locals: { fn: "name" }], + CustomerOGM::TTS.new(vcard).to_render + ) + end + end +end