~singpolyma/sgx-jmp

94cef4ee6c1dbc487600a2ccee57f54bd6733414 — Stephen Paul Weber 2 months ago 9daaf10
Use TrustLevel to determine daily quota
2 files changed, 21 insertions(+), 2 deletions(-)

M lib/trust_level.rb
M sgx_jmp.rb
M lib/trust_level.rb => lib/trust_level.rb +16 -0
@@ 27,6 27,10 @@ module TrustLevel
		def support_call?(*)
			false
		end

		def send_message?(*)
			false
		end
	end

	class Basement


@@ 37,6 41,10 @@ module TrustLevel
		def support_call?(rate, concurrency)
			rate <= 0.02 && concurrency < 1
		end

		def send_message?(messages_today)
			messages_today < 200
		end
	end

	class Paragon


@@ 47,6 55,10 @@ module TrustLevel
		def support_call?(_, concurrency)
			concurrency < 10
		end

		def send_message?(messages_today)
			messages_today < 700
		end
	end

	class Customer


@@ 70,5 82,9 @@ module TrustLevel
		def support_call?(rate, concurrency)
			rate <= @max_rate && concurrency < 4
		end

		def send_message?(messages_today)
			messages_today < 500
		end
	end
end

M sgx_jmp.rb => sgx_jmp.rb +5 -2
@@ 339,8 339,11 @@ message do |m|
		.find_by_jid(m.from.stripped).then { |customer|
			next customer.stanza_from(m) unless billable_message(m)

			customer.message_usage((today..today)).then { |usage|
				raise OverLimit.new(customer, usage) if usage > 500
			EMPromise.all([
				TrustLevelRepo.new.find(customer),
				customer.message_usage((today..today))
			]).then { |(tl, usage)|
				raise OverLimit.new(customer, usage) unless tl.send_message?(usage)
			}.then do
				EMPromise.all([
					customer.incr_message_usage,