NNTmux /
newznab-tmux
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Models; |
||
| 4 | |||
| 5 | use Illuminate\Database\Eloquent\Model; |
||
| 6 | use Illuminate\Database\Eloquent\Relations\BelongsTo; |
||
| 7 | use Illuminate\Support\Facades\DB; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * App\Models\UserRequest. |
||
| 11 | * |
||
| 12 | * @property int $id |
||
| 13 | * @property int $users_id |
||
| 14 | * @property string $hosthash |
||
| 15 | * @property string $request |
||
| 16 | * @property string $timestamp |
||
| 17 | * @property-read User $user |
||
| 18 | * |
||
| 19 | * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserRequest whereHosthash($value) |
||
| 20 | * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserRequest whereId($value) |
||
| 21 | * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserRequest whereRequest($value) |
||
| 22 | * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserRequest whereTimestamp($value) |
||
| 23 | * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserRequest whereUsersId($value) |
||
| 24 | * |
||
| 25 | * @mixin \Eloquent |
||
| 26 | * |
||
| 27 | * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserRequest newModelQuery() |
||
| 28 | * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserRequest newQuery() |
||
| 29 | * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserRequest query() |
||
| 30 | */ |
||
| 31 | class UserRequest extends Model |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var bool |
||
| 39 | */ |
||
| 40 | protected $dateFormat = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var bool |
||
| 44 | */ |
||
| 45 | public $timestamps = false; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $fillable = ['id', 'users_id', 'request', 'hosthash', 'timestamp']; |
||
| 51 | |||
| 52 | public function user(): BelongsTo |
||
| 53 | { |
||
| 54 | return $this->belongsTo(User::class, 'users_id'); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @throws \Throwable |
||
| 59 | */ |
||
| 60 | public static function delApiRequests($userID): void |
||
| 61 | { |
||
| 62 | DB::transaction(function () use ($userID) { |
||
| 63 | self::query()->where('users_id', $userID)->delete(); |
||
| 64 | }, 3); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Get the quantity of API requests in the last day for the users_id. |
||
| 69 | * |
||
| 70 | * |
||
| 71 | * @throws \Exception |
||
| 72 | * @throws \Throwable |
||
| 73 | */ |
||
| 74 | public static function getApiRequests(int $userID): int |
||
| 75 | { |
||
| 76 | // Clear old requests. |
||
| 77 | self::clearApiRequests($userID); |
||
| 78 | $requests = self::query()->where('users_id', $userID)->count('id'); |
||
| 79 | |||
| 80 | return ! $requests ? 0 : $requests; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * If a user accesses the API, log it. |
||
| 85 | * |
||
| 86 | * @param string $token API token of the user |
||
| 87 | * @param string $request The API request. |
||
| 88 | */ |
||
| 89 | public static function addApiRequest(string $token, string $request): void |
||
| 90 | { |
||
| 91 | $userID = User::query()->select(['id'])->where('api_token', $token)->value('id'); |
||
| 92 | self::query()->insert(['users_id' => $userID, 'request' => $request, 'timestamp' => now()]); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Delete api requests older than a day. |
||
| 97 | * |
||
| 98 | * @param int|bool $userID |
||
| 99 | * int The users ID. |
||
| 100 | * bool false do all user ID's.. |
||
| 101 | * |
||
| 102 | * @throws \Exception |
||
| 103 | * @throws \Throwable |
||
| 104 | */ |
||
| 105 | public static function clearApiRequests($userID): void |
||
| 106 | { |
||
| 107 | DB::transaction(function () use ($userID) { |
||
| 108 | if ($userID === false) { |
||
| 109 | self::query()->where('timestamp', '<', now()->subDay())->delete(); |
||
| 110 | } else { |
||
| 111 | self::query()->where('users_id', $userID)->where('timestamp', '<', now()->subDay())->delete(); |
||
| 112 | } |
||
| 113 | }, 3); |
||
| 114 | } |
||
| 115 | } |
||
| 116 |