Block messages over 3500/month

Stephen Paul Weber created

Generic error message to contact support.  No current customer will hit this
limit based on historical usage.

Change summary

sgx_jmp.rb | 33 ++++++++++++++++++++-------------
1 file changed, 20 insertions(+), 13 deletions(-)

Detailed changes

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