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) }