~singpolyma/sgx-jmp

f529c81dc15929e439b3da73204c802cbedd6d7e — Stephen Paul Weber 2 years ago 7d817d2
Block messages over 3500/month

Generic error message to contact support.  No current customer will hit this
limit based on historical usage.
1 files changed, 20 insertions(+), 13 deletions(-)

M sgx_jmp.rb
M sgx_jmp.rb => sgx_jmp.rb +20 -13
@@ 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) }