@@ -42,14 +42,11 @@ class CustomerUsage
end
def incr_body(amount, body)
- # Short message like "hello" we can't block dupes
- return 0 if body.to_s.length < 30
-
hash = Digest::SHA2.hexdigest(body[0..100])
EMPromise.all([
REDIS.incrby("jmp_outbound_body-#{hash}", amount),
- REDIS.expire("jmp_outbound_body-#{hash}", 120)
+ REDIS.expire("jmp_outbound_body-#{hash}", 180)
]).then(&:first)
end
@@ -400,6 +400,18 @@ def find_from_and_to_customer(from, to)
}
end
+def usage_guard(m, customer, trust_level, usage)
+ next if trust_level.send_message?(usage[:today]) && usage[:body] < 5
+
+ if m.body.to_s.length < 30 || (usage[:body] >= 5 && usage[:body] < 10)
+ OverLimit.new(customer, usage).notify_admin
+ return
+ end
+
+ log.warn "OverLimit", m
+ raise OverLimit.new(customer, usage)
+end
+
message do |m|
StatsD.increment("message")
@@ -417,10 +429,7 @@ message do |m|
TrustLevelRepo.new.find(customer),
customer.incr_message_usage(1, m.body)
]).then { |(tl, usage)|
- next if tl.send_message?(usage[:today]) && usage[:body] < 10
-
- log.warn "OverLimit", m
- raise OverLimit.new(customer, usage)
+ usage_guard(m, customer, tl, usage)
}.then do
customer.stanza_from(m)
end