1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* App\Models\UserDownload. |
10
|
|
|
* |
11
|
|
|
* @property int $id |
12
|
|
|
* @property int $users_id |
13
|
|
|
* @property string $hosthash |
14
|
|
|
* @property string $timestamp |
15
|
|
|
* @property int $releases_id FK to releases.id |
16
|
|
|
* @property-read Release $release |
17
|
|
|
* @property-read User $user |
18
|
|
|
* |
19
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDownload whereHosthash($value) |
20
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDownload whereId($value) |
21
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDownload whereReleasesId($value) |
22
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDownload whereTimestamp($value) |
23
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDownload whereUsersId($value) |
24
|
|
|
* |
25
|
|
|
* @mixin \Eloquent |
26
|
|
|
* |
27
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDownload newModelQuery() |
28
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDownload newQuery() |
29
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDownload query() |
30
|
|
|
*/ |
31
|
|
|
class UserDownload extends Model |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
protected $dateFormat = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var bool |
40
|
|
|
*/ |
41
|
|
|
public $timestamps = false; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $guarded = []; |
47
|
|
|
|
48
|
|
|
public function user(): BelongsTo |
49
|
|
|
{ |
50
|
|
|
return $this->belongsTo(User::class, 'users_id'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function release(): BelongsTo |
54
|
|
|
{ |
55
|
|
|
return $this->belongsTo(Release::class, 'releases_id'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the COUNT of how many NZB's the user has downloaded in the past day. |
60
|
|
|
* |
61
|
|
|
* |
62
|
|
|
* @throws \Exception |
63
|
|
|
*/ |
64
|
|
|
public static function getDownloadRequests(int $userID): int |
65
|
|
|
{ |
66
|
|
|
// Clear old requests. |
67
|
|
|
self::whereUsersId($userID)->where('timestamp', '<', now()->subDay())->delete(); |
68
|
|
|
$value = self::whereUsersId($userID)->where('timestamp', '>', now()->subDay())->count('id'); |
69
|
|
|
|
70
|
|
|
return $value === false ? 0 : $value; |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|static[] |
75
|
|
|
*/ |
76
|
|
|
public static function getDownloadRequestsForUser($userID) |
77
|
|
|
{ |
78
|
|
|
return self::whereUsersId($userID)->with('release')->orderByDesc('timestamp')->get(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* If a user downloads a NZB, log it. |
83
|
|
|
* |
84
|
|
|
* |
85
|
|
|
* @return int|\Illuminate\Database\Eloquent\Builder |
86
|
|
|
*/ |
87
|
|
|
public static function addDownloadRequest($userID, $releaseID) |
88
|
|
|
{ |
89
|
|
|
return self::query() |
90
|
|
|
->insertGetId( |
91
|
|
|
[ |
92
|
|
|
'users_id' => $userID, |
93
|
|
|
'releases_id' => $releaseID, |
94
|
|
|
'timestamp' => now(), |
95
|
|
|
] |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return mixed |
101
|
|
|
* |
102
|
|
|
* @throws \Exception |
103
|
|
|
*/ |
104
|
|
|
public static function delDownloadRequestsForRelease(int $releaseID) |
105
|
|
|
{ |
106
|
|
|
return self::whereReleasesId($releaseID)->delete(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @throws \Exception |
111
|
|
|
*/ |
112
|
|
|
public static function delDownloadRequests($userID): void |
113
|
|
|
{ |
114
|
|
|
self::whereUsersId($userID)->delete(); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|