diff --git a/src/app/Http/Controllers/API/V4/PolicyController.php b/src/app/Http/Controllers/API/V4/PolicyController.php --- a/src/app/Http/Controllers/API/V4/PolicyController.php +++ b/src/app/Http/Controllers/API/V4/PolicyController.php @@ -142,18 +142,21 @@ $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. - if ($wallet->balance > 0) { - $payments = $wallet->payments()->where('amount', '>', 0)->where('status', 'paid'); + // Paying users have a 15 messages per minute limit + if ($wallet->hasMinimumBalanceAndPayments()) { + $ownerRates = RateLimit::where('owner_id', $owner->id) + ->where('updated_at', '>=', \Carbon\Carbon::now()->subMinute()); - if ($payments->count() >= 2) { - return response()->json(['response' => 'DUNNO'], 200); + if (($count = $ownerRates->count()) >= 15) { + $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); } // diff --git a/src/app/Wallet.php b/src/app/Wallet.php --- a/src/app/Wallet.php +++ b/src/app/Wallet.php @@ -743,4 +743,28 @@ return $this; } + + + /** + * Ensure that this wallet has a positive balance and a minimum number of payments, + * or a 100% discount (in which case there are no payments). + * + * @return bool + */ + public function hasMinimumBalanceAndPayments($minimumPayments = 2): bool + { + if ($this->discount && $this->discount->discount == 100) { + return true; + } + + if ($this->balance > 0) { + $payments = $this->payments()->where('amount', '>', 0)->where('status', 'paid'); + + if ($payments->count() >= $minimumPayments) { + return true; + } + } + + return false; + } }