M lib/customer_repo.rb => lib/customer_repo.rb +7 -2
@@ 11,6 11,7 @@ class CustomerRepo
class NotFound < RuntimeError; end
value_semantics do
+ set_user Proc, default: ->(**) {}
redis Anything(), default: LazyObject.new { REDIS }
db Anything(), default: LazyObject.new { DB }
braintree Anything(), default: LazyObject.new { BRAINTREE }
@@ 72,18 73,22 @@ class CustomerRepo
end
def find(customer_id)
+ set_user.call(customer_id: customer_id)
QueryKey::ID.new(customer_id).keys(redis).then { |k| find_inner(*k) }
end
def find_by_jid(jid)
+ set_user.call(jid: jid)
QueryKey::JID.for(jid).keys(redis).then { |k| find_inner(*k) }
end
def find_by_tel(tel)
+ set_user.call(tel: tel)
QueryKey::Tel.new(tel).keys(redis).then { |k| find_inner(*k) }
end
def find_by_format(s)
+ set_user.call(q: s)
QueryKey.for(s).keys(redis).then { |k| find_inner(*k) }
end
@@ 166,11 171,11 @@ protected
end
def find_inner(customer_id, jid)
+ set_user.call(customer_id: customer_id, jid: jid)
fetch_all(customer_id).then do |(sgx, data)|
Customer.new(
customer_id, Blather::JID.new(jid),
- sgx: sgx, tndetails: tndetails(sgx),
- plan: CustomerPlan.for(
+ sgx: sgx, tndetails: tndetails(sgx), plan: CustomerPlan.for(
customer_id,
**data.reject { |(k, _)| k == :balance }
), **data.slice(:balance)
M sgx_jmp.rb => sgx_jmp.rb +33 -33
@@ 254,13 254,9 @@ before nil, to: /\Acustomer_/, from: /(\A|@)#{CONFIG[:sgx]}(\/|\Z)/ do |s|
StatsD.increment("stanza_customer")
sentry_hub = new_sentry_hub(s, name: "stanza_customer")
- CustomerRepo.new.find(
+ CustomerRepo.new(set_user: sentry_hub.current_scope.method(:set_user)).find(
s.to.node.delete_prefix("customer_")
).then { |customer|
- sentry_hub.current_scope.set_user(
- id: customer.customer_id,
- jid: s.from.stripped.to_s
- )
customer.stanza_to(s)
}.catch { |e| panic(e, sentry_hub) }
halt
@@ 279,14 275,16 @@ message(
&.find { |el| el["jid"].to_s.start_with?("customer_") }
pass unless address
- CustomerRepo.new.find_by_jid(address["jid"]).then { |customer|
- m.from = m.from.with(domain: CONFIG[:component][:jid])
- m.to = m.to.with(domain: customer.jid.domain)
- address["jid"] = customer.jid.to_s
- BLATHER << m
- }.catch_only(CustomerRepo::NotFound) { |e|
- BLATHER << m.as_error("forbidden", :auth, e.message)
- }.catch { |e| panic(e, sentry_hub) }
+ CustomerRepo
+ .new(set_user: sentry_hub.current_scope.method(:set_user))
+ .find_by_jid(address["jid"]).then { |customer|
+ m.from = m.from.with(domain: CONFIG[:component][:jid])
+ m.to = m.to.with(domain: customer.jid.domain)
+ address["jid"] = customer.jid.to_s
+ BLATHER << m
+ }.catch_only(CustomerRepo::NotFound) { |e|
+ BLATHER << m.as_error("forbidden", :auth, e.message)
+ }.catch { |e| panic(e, sentry_hub) }
end
# Ignore groupchat messages
@@ 312,26 310,25 @@ message do |m|
sentry_hub = new_sentry_hub(m, name: "message")
today = Time.now.utc.to_date
- CustomerRepo.new.find_by_jid(m.from.stripped).then { |customer|
- sentry_hub.current_scope.set_user(
- id: customer.customer_id, jid: m.from.stripped.to_s
- )
- EMPromise.all([
- (customer.incr_message_usage if billable_message(m)),
- customer.message_usage((today..(today - 30))).then do |usage|
- if usage < 4500
- customer.stanza_from(m)
- else
- BLATHER << m.as_error(
- "policy-violation", :wait, "Please contact support"
- )
+ CustomerRepo
+ .new(set_user: sentry_hub.current_scope.method(:set_user))
+ .find_by_jid(m.from.stripped).then { |customer|
+ EMPromise.all([
+ (customer.incr_message_usage if billable_message(m)),
+ customer.message_usage((today..(today - 30))).then do |usage|
+ if usage < 4500
+ customer.stanza_from(m)
+ else
+ BLATHER << m.as_error(
+ "policy-violation", :wait, "Please contact support"
+ )
+ end
+ notify_admin_of_usage(customer, usage, today) if usage > 900
end
- notify_admin_of_usage(customer, usage, today) if usage > 900
- end
- ])
- }.catch_only(CustomerRepo::NotFound) { |e|
- BLATHER << m.as_error("forbidden", :auth, e.message)
- }.catch { |e| panic(e, sentry_hub) }
+ ])
+ }.catch_only(CustomerRepo::NotFound) { |e|
+ BLATHER << m.as_error("forbidden", :auth, e.message)
+ }.catch { |e| panic(e, sentry_hub) }
end
message :error? do |m|
@@ 391,7 388,10 @@ disco_items node: "http://jabber.org/protocol/commands" do |iq|
reply = iq.reply
reply.node = "http://jabber.org/protocol/commands"
- CustomerRepo.new(sgx_repo: Bwmsgsv2Repo.new).find_by_jid(
+ CustomerRepo.new(
+ sgx_repo: Bwmsgsv2Repo.new,
+ set_user: sentry_hub.current_scope.method(:set_user)
+ ).find_by_jid(
iq.from.stripped
).catch {
nil
M web.rb => web.rb +1 -8
@@ 101,6 101,7 @@ class Web < Roda
end
def customer_repo(**kwargs)
+ kwargs[:set_user] = Sentry.method(:set_user) unless kwargs[:set_user]
opts[:customer_repo] || CustomerRepo.new(**kwargs)
end
@@ 169,7 170,6 @@ class Web < Roda
end
customer_repo.find_by_tel(params["to"]).then do |customer|
- Sentry.set_user(id: customer.customer_id)
CDR.for_inbound(customer.customer_id, params).save
end
end
@@ 205,7 205,6 @@ class Web < Roda
)
customer_repo.find_by_tel(params["to"]).then do |customer|
- Sentry.set_user(id: customer.customer_id)
m = Blather::Stanza::Message.new
m.chat_state = nil
m.from = from_jid
@@ 224,7 223,6 @@ class Web < Roda
next "OK<5" unless duration > 5
customer_repo.find_by_tel(params["to"]).then do |customer|
- Sentry.set_user(id: customer.customer_id)
m = Blather::Stanza::Message.new
m.chat_state = nil
m.from = from_jid
@@ 242,7 240,6 @@ class Web < Roda
customer_repo(sgx_repo: Bwmsgsv2Repo.new)
.find_by_tel(params["to"])
.then { |c|
- Sentry.set_user(id: c.customer_id)
EMPromise.all([c, c.ogm(params["from"])])
}.then do |(customer, ogm)|
render :voicemail, locals: {
@@ 257,7 254,6 @@ class Web < Roda
customer_repo(
sgx_repo: Bwmsgsv2Repo.new
).find(params.fetch("customer_id")).then do |customer|
- Sentry.set_user(id: customer.customer_id)
call_attempt_repo.find_inbound(
customer,
params["from"],
@@ 269,11 265,9 @@ class Web < Roda
end
r.post do
- Sentry.set_user(tel: params["to"])
customer_repo(
sgx_repo: Bwmsgsv2Repo.new
).find_by_tel(params["to"]).then { |customer|
- Sentry.set_user(id: customer.customer_id)
EMPromise.all([
customer.customer_id, customer.fwd,
call_attempt_repo.find_inbound(
@@ 314,7 308,6 @@ class Web < Roda
customer_repo(
sgx_repo: Bwmsgsv2Repo.new
).find_by_format(from).then do |c|
- Sentry.set_user(id: c.customer_id)
call_attempt_repo.find_outbound(
c,
params["to"],