mirror of
https://github.com/cydrobolt/polr.git
synced 2024-11-12 21:22:28 +01:00
Implement API rate limitations fix #155
This commit is contained in:
parent
8cac5b3ec1
commit
f5aeb2d8ab
@ -24,7 +24,7 @@ class LinkFactory {
|
||||
return $short_url;
|
||||
}
|
||||
|
||||
public static function createLink($long_url, $is_secret=false, $custom_ending=null, $link_ip='127.0.0.1', $creator=false, $return_object=false) {
|
||||
public static function createLink($long_url, $is_secret=false, $custom_ending=null, $link_ip='127.0.0.1', $creator=false, $return_object=false, $is_api=false) {
|
||||
/**
|
||||
* Given parameters needed to create a link, generate appropriate ending and
|
||||
* return formatted link.
|
||||
@ -34,6 +34,8 @@ class LinkFactory {
|
||||
* @param string (optional) $custom_ending
|
||||
* @param string $link_ip
|
||||
* @param string $creator
|
||||
* @param bool $return_object
|
||||
* @param bool $is_api
|
||||
* @return string $formatted_link
|
||||
*/
|
||||
|
||||
@ -76,6 +78,8 @@ class LinkFactory {
|
||||
$link->ip = $link_ip;
|
||||
$link->is_custom = $custom_ending != null;
|
||||
|
||||
$link->is_api = $is_api;
|
||||
|
||||
if ($creator) {
|
||||
// if user is logged in, save user as creator
|
||||
$link->creator = $creator;
|
||||
|
@ -1,8 +1,26 @@
|
||||
<?php
|
||||
namespace App\Helpers;
|
||||
use App\Models\Link;
|
||||
use App\Helpers\UserHelper;
|
||||
|
||||
class ApiHelper {
|
||||
public static function checkUserApiQuota($username) {
|
||||
return false;
|
||||
/**
|
||||
*
|
||||
* @return boolean; whether API quota is met
|
||||
*/
|
||||
|
||||
$last_minute_unix = time() - 60;
|
||||
$last_minute = new \DateTime();
|
||||
$last_minute->setTimestamp($last_minute_unix);
|
||||
|
||||
$user = UserHelper::getUserByUsername($username);
|
||||
|
||||
$links_last_minute = Link::where('is_api', 1)
|
||||
->where('creator', $username)
|
||||
->where('created_at', '>=', $last_minute)
|
||||
->count();
|
||||
|
||||
return ($links_last_minute >= $user->api_quota);
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ class ApiLinkController extends ApiController {
|
||||
$custom_ending = $request->input('custom_ending');
|
||||
|
||||
try {
|
||||
$formatted_link = LinkFactory::createLink($long_url, $is_secret, $custom_ending, $link_ip, $user->username);
|
||||
$formatted_link = LinkFactory::createLink($long_url, $is_secret, $custom_ending, $link_ip, $user->username, false, true);
|
||||
}
|
||||
catch (\Exception $e) {
|
||||
abort(400, $e->getMessage());
|
||||
|
@ -5,6 +5,4 @@ use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Link extends Model {
|
||||
protected $table = 'links';
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user