Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/PolicyController.php
Show First 20 Lines • Show All 136 Lines • ▼ Show 20 Lines | public function ratelimit() | ||||
'recipient_count' => $recipientCount | 'recipient_count' => $recipientCount | ||||
]); | ]); | ||||
} else { | } else { | ||||
// ensure the request has an up to date timestamp | // ensure the request has an up to date timestamp | ||||
$request->updated_at = \Carbon\Carbon::now(); | $request->updated_at = \Carbon\Carbon::now(); | ||||
$request->save(); | $request->save(); | ||||
} | } | ||||
// exempt owners that have 100% discount. | |||||
if ($wallet->discount && $wallet->discount->discount == 100) { | |||||
return response()->json(['response' => 'DUNNO'], 200); | |||||
} | |||||
// exempt owners that have made at least two payments and currently maintain a positive balance. | // Paying users have a 15 messages per minute limit | ||||
if ($wallet->balance > 0) { | if ($wallet->hasMinimumBalanceAndPayments()) { | ||||
$payments = $wallet->payments()->where('amount', '>', 0)->where('status', 'paid'); | $ownerRates = RateLimit::where('owner_id', $owner->id) | ||||
machniak: This will make the 100%-discounted users also being limited. I'm not sure it's intentional. | |||||
->where('updated_at', '>=', \Carbon\Carbon::now()->subMinute()); | |||||
machniakUnsubmitted Not Done Inline ActionsThis way the limit is shared by all users in an account. Might be too small for bigger accounts. How about where('user_id', $user->id) instead? machniak: This way the limit is shared by all users in an account. Might be too small for bigger accounts. | |||||
if ($payments->count() >= 2) { | if (($count = $ownerRates->count()) >= 15) { | ||||
machniakUnsubmitted Not Done Inline ActionsNo need for $count variable. machniak: No need for `$count` variable. | |||||
return response()->json(['response' => 'DUNNO'], 200); | $result = [ | ||||
'response' => 'DEFER_IF_PERMIT', | |||||
'reason' => 'The account is at 15 messages per minute, cool down.' | |||||
]; | |||||
return response()->json($result, 403); | |||||
} | } | ||||
return response()->json(['response' => 'DUNNO'], 200); | |||||
} | } | ||||
// | // | ||||
// Examine the rates at which the owner (or its users) is sending | // Examine the rates at which the owner (or its users) is sending | ||||
// | // | ||||
$ownerRates = RateLimit::where('owner_id', $owner->id) | $ownerRates = RateLimit::where('owner_id', $owner->id) | ||||
->where('updated_at', '>=', \Carbon\Carbon::now()->subHour()); | ->where('updated_at', '>=', \Carbon\Carbon::now()->subHour()); | ||||
▲ Show 20 Lines • Show All 229 Lines • Show Last 20 Lines |
This will make the 100%-discounted users also being limited. I'm not sure it's intentional.