~singpolyma/sgx-jmp

5dfee341471ad8e315649bab239551478088a7b4 — Stephen Paul Weber 2 years ago 0925ae7
Block outgoing messages when expired
1 files changed, 9 insertions(+), 5 deletions(-)

M sgx_jmp.rb
M sgx_jmp.rb => sgx_jmp.rb +9 -5
@@ 332,16 332,21 @@ class OverLimit < StandardError
	end
end

class CustomerExpired < StandardError; end

message do |m|
	StatsD.increment("message")

	sentry_hub = new_sentry_hub(m, name: "message")
	today = Time.now.utc.to_date
	CustomerRepo
		.new(set_user: sentry_hub.current_scope.method(:set_user))
	CustomerRepo.new(set_user: sentry_hub.current_scope.method(:set_user))
		.find_by_jid(m.from.stripped).then { |customer|
			next customer.stanza_from(m) unless billable_message(m)

			if customer.plan_name && !customer.active?
				raise CustomerExpired, "Your account is expired, please top up"
			end

			EMPromise.all([
				TrustLevelRepo.new.find(customer),
				customer.message_usage((today..today))


@@ 349,14 354,13 @@ message do |m|
				raise OverLimit.new(customer, usage) unless tl.send_message?(usage)
			}.then do
				EMPromise.all([
					customer.incr_message_usage,
					customer.stanza_from(m)
					customer.incr_message_usage, customer.stanza_from(m)
				])
			end
		}.catch_only(OverLimit) { |e|
			e.notify_admin
			BLATHER << m.as_error("policy-violation", :wait, e.message)
		}.catch_only(CustomerRepo::NotFound) { |e|
		}.catch_only(CustomerRepo::NotFound, CustomerExpired) { |e|
			BLATHER << m.as_error("forbidden", :auth, e.message)
		}.catch { |e| panic(e, sentry_hub) }
end