Files
newznab-tmux/app/Models/UserDownload.php
T
2018-11-12 22:51:43 +01:00

123 lines
3.1 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* App\Models\UserDownload.
*
* @property int $id
* @property int $users_id
* @property string $hosthash
* @property string $timestamp
* @property int $releases_id FK to releases.id
* @property-read \App\Models\Release $release
* @property-read \App\Models\User $user
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDownload whereHosthash($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDownload whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDownload whereReleasesId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDownload whereTimestamp($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDownload whereUsersId($value)
* @mixin \Eloquent
*/
class UserDownload extends Model
{
/**
* @var bool
*/
protected $dateFormat = false;
/**
* @var bool
*/
public $timestamps = false;
/**
* @var array
*/
protected $guarded = [];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class, 'users_id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function release()
{
return $this->belongsTo(Release::class, 'releases_id');
}
/**
* Get the COUNT of how many NZB's the user has downloaded in the past day.
*
* @param int $userID
*
* @return int
* @throws \Exception
*/
public static function getDownloadRequests($userID): int
{
// Clear old requests.
self::whereUsersId($userID)->where('timestamp', '<', now()->subDay())->delete();
$value = self::whereUsersId($userID)->where('timestamp', '>', now()->subDay())->count('id');
return $value === false ? 0 : $value;
}
/**
* @param $userID
* @return \Illuminate\Database\Eloquent\Collection|static[]
*/
public static function getDownloadRequestsForUser($userID)
{
return self::whereUsersId($userID)->with('release')->orderBy('timestamp', 'DESC')->get();
}
/**
* If a user downloads a NZB, log it.
*
*
* @param $userID
* @param $releaseID
*
* @return int|\Illuminate\Database\Eloquent\Builder
*/
public static function addDownloadRequest($userID, $releaseID)
{
return self::query()
->insertGetId(
[
'users_id' => $userID,
'releases_id' => $releaseID,
'timestamp' => now(),
]
);
}
/**
* @param int $releaseID
* @return mixed
* @throws \Exception
*/
public static function delDownloadRequestsForRelease(int $releaseID)
{
return self::whereReleasesId($releaseID)->delete();
}
/**
* @param $userID
* @throws \Exception
*/
public static function delDownloadRequests($userID): void
{
self::whereUsersId($userID)->delete();
}
}