diff --git a/.rubocop.yml b/.rubocop.yml index 736857e827cbbed5e992fadd8f818880a8faaecd..c76c911780686212200e06f1438ca587e7825db7 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -106,6 +106,7 @@ Style/BlockDelimiters: AllowBracesOnProceduralOneLiners: true ProceduralMethods: - execute_command + - new Style/MultilineBlockChain: Enabled: false diff --git a/lib/customer_repo.rb b/lib/customer_repo.rb index 168fd5dd2899a87cc3c044c4dea0e7ddd909bd74..b187f7958233a15b76933061ef44f2fcd87d42ab 100644 --- a/lib/customer_repo.rb +++ b/lib/customer_repo.rb @@ -18,32 +18,73 @@ class CustomerRepo bandwidth_tn_repo Anything(), default: BandwidthTnRepo.new end - def find(customer_id) - @redis.get("jmp_customer_jid-#{customer_id}").then do |jid| - raise NotFound, "No jid" unless jid + module QueryKey + def self.for(s) + case s + when Blather::JID + JID.for(s) + when /\Axmpp:(.*)/ + JID.for($1) + when /\A(?:tel:)?(\+\d+)\Z/ + Tel.new($1) + else + ID.new(s) + end + end - find_inner(customer_id, jid) + ID = Struct.new(:customer_id) do + def keys(redis) + redis.get("jmp_customer_jid-#{customer_id}").then do |jid| + raise NotFound, "No jid" unless jid + + [customer_id, jid] + end + end end - end - def find_by_jid(jid) - if jid.to_s =~ /\Acustomer_(.+)@#{CONFIG[:component][:jid]}\Z/ - find($1) - else - @redis.get("jmp_customer_id-#{jid}").then do |customer_id| - raise NotFound, "No customer" unless customer_id + JID = Struct.new(:jid) do + def self.for(jid) + if jid.to_s =~ /\Acustomer_(.+)@#{CONFIG[:component][:jid]}\Z/ + ID.new($1) + else + new(jid) + end + end + + def keys(redis) + redis.get("jmp_customer_id-#{jid}").then do |customer_id| + raise NotFound, "No customer" unless customer_id - find_inner(customer_id, jid) + [customer_id, @jid] + end end end + + Tel = Struct.new(:tel) do + def keys(redis) + redis.get("catapult_jid-#{tel}").then do |jid| + raise NotFound, "No jid" unless jid + + JID.for(jid).keys(redis) + end + end + end + end + + def find(customer_id) + QueryKey::ID.new(customer_id).keys(redis).then { |k| find_inner(*k) } + end + + def find_by_jid(jid) + QueryKey::JID.for(jid).keys(redis).then { |k| find_inner(*k) } end def find_by_tel(tel) - @redis.get("catapult_jid-#{tel}").then do |jid| - raise NotFound, "No jid" unless jid + QueryKey::Tel.new(tel).keys(redis).then { |k| find_inner(*k) } + end - find_by_jid(jid) - end + def find_by_format(s) + QueryKey.for(s).keys(redis).then { |k| find_inner(*k) } end def create(jid) diff --git a/web.rb b/web.rb index f57d553ef8b5d3760b25109d54888deff1dd61bd..6dcaaaa82d8a71e7579804f9ccca8b74a90c7902 100644 --- a/web.rb +++ b/web.rb @@ -275,10 +275,10 @@ class Web < Roda end r.post do - customer_id = params["from"].sub(/^\+1/, "") + from = params["from"].sub(/^\+1/, "") customer_repo( sgx_repo: Bwmsgsv2Repo.new - ).find(customer_id).then do |c| + ).find_by_format(from).then do |c| render :forward, locals: { from: c.registered?.phone, to: params["to"]