From f529c81dc15929e439b3da73204c802cbedd6d7e Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Mon, 31 Jan 2022 10:27:12 -0500 Subject: [PATCH] Block messages over 3500/month Generic error message to contact support. No current customer will hit this limit based on historical usage. --- sgx_jmp.rb | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/sgx_jmp.rb b/sgx_jmp.rb index 44b04ef24c64b2357032838f192249de49324493..a49e5a7a53f02bcbbbd531e50ac5bb754e541f95 100644 --- a/sgx_jmp.rb +++ b/sgx_jmp.rb @@ -285,6 +285,16 @@ def billable_message(m) (m.body && !m.body.empty?) || m.find("ns:x", ns: OOB.registered_ns).first end +def notify_admin_of_usage(customer, usage, today) + ExpiringLock.new("jmp_usage_notify-#{customer.customer_id}").with do + BLATHER.join(CONFIG[:notify_admin], "sgx-jmp") + BLATHER.say( + CONFIG[:notify_admin], "#{customer.customer_id} has used " \ + "#{usage} messages since #{today - 30}", :groupchat + ) + end +end + message do |m| StatsD.increment("message") @@ -296,20 +306,17 @@ message do |m| ) EMPromise.all([ (customer.incr_message_usage if billable_message(m)), - customer.stanza_from(m) - ]).then { customer } - }.then { |customer| - customer.message_usage((today..(today - 30))).then do |usage| - next unless usage > 900 - - ExpiringLock.new("jmp_usage_notify-#{customer.customer_id}").with do - BLATHER.join(CONFIG[:notify_admin], "sgx-jmp") - BLATHER.say( - CONFIG[:notify_admin], "#{customer.customer_id} has used #{usage} " \ - "messages since #{today - 30}", :groupchat - ) + customer.message_usage((today..(today - 30))).then do |usage| + if usage < 3500 + 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 - end + ]) }.catch_only(CustomerRepo::NotFound) { |e| BLATHER << m.as_error("forbidden", :auth, e.message) }.catch { |e| panic(e, sentry_hub) }