support plugin nexusphp-hit-and-run

This commit is contained in:
xiaomlove
2022-10-01 00:11:22 +08:00
parent 65ba6442b1
commit 6de5cb2f21
20 changed files with 222 additions and 81 deletions

View File

@@ -2,6 +2,8 @@
namespace App\Models;
use Carbon\Carbon;
use Carbon\Exceptions\InvalidArgumentException;
use Illuminate\Database\Eloquent\Casts\Attribute;
class HitAndRun extends NexusModel
@@ -17,7 +19,7 @@ class HitAndRun extends NexusModel
const STATUS_UNREACHED = 3;
const STATUS_PARDONED = 4;
public static $status = [
public static array $status = [
self::STATUS_INSPECTING => ['text' => 'Inspecting'],
self::STATUS_REACHED => ['text' => 'Reached'],
self::STATUS_UNREACHED => ['text' => 'Unreached'],
@@ -39,17 +41,37 @@ class HitAndRun extends NexusModel
protected function seedTimeRequired(): Attribute
{
return new Attribute(
get: fn($value, $attributes) => $this->status == self::STATUS_INSPECTING ? mkprettytime(3600 * Setting::get('hr.seed_time_minimum') - $this->snatch->seedtime) : '---'
get: fn($value, $attributes) => $this->doGetSeedTimeRequired()
);
}
protected function inspectTimeLeft(): Attribute
{
return new Attribute(
get: fn($value, $attributes) => $this->status == self::STATUS_INSPECTING ? mkprettytime(\Carbon\Carbon::now()->diffInSeconds($this->snatch->completedat->addHours(Setting::get('hr.inspect_time')))) : '---'
get: fn($value, $attributes) => $this->doGetInspectTimeLeft()
);
}
private function doGetInspectTimeLeft(): string
{
if ($this->status != self::STATUS_INSPECTING) {
return '---';
}
$inspectTime = HitAndRun::getConfig('inspect_time', $this->torrent->basic_category->mode);
$diffInSeconds = Carbon::now()->diffInSeconds($this->snatch->completedat->addHours($inspectTime));
return mkprettytime($diffInSeconds);
}
private function doGetSeedTimeRequired(): string
{
if ($this->status != self::STATUS_INSPECTING) {
return '---';
}
$seedTimeMinimum = HitAndRun::getConfig('seed_time_minimum', $this->torrent->basic_category->mode);
$diffInSeconds = 3600 * $seedTimeMinimum - $this->snatch->seedtime;
return mkprettytime($diffInSeconds);
}
public function getStatusTextAttribute()
{
return nexus_trans('hr.status_' . $this->status);
@@ -87,8 +109,29 @@ class HitAndRun extends NexusModel
public static function getIsEnabled(): bool
{
$result = Setting::get('hr.mode');
return $result && in_array($result, [self::MODE_GLOBAL, self::MODE_MANUAL]);
$enableSpecialSection = Setting::get('main.spsct') == 'yes';
$browseMode = self::getConfig('mode', Setting::get('main.browsecat'));
$browseEnabled = $browseMode && in_array($browseMode, [self::MODE_GLOBAL, self::MODE_MANUAL]);
if (!$enableSpecialSection) {
do_log("Not enable special section, browseEnabled: $browseEnabled");
return $browseEnabled;
}
$specialMode = self::getConfig('mode', Setting::get('main.specialcat'));
$specialEnabled = $specialMode && in_array($specialMode, [self::MODE_GLOBAL, self::MODE_MANUAL]);
$result = $browseEnabled || $specialEnabled;
do_log("Enable special section, browseEnabled: $browseEnabled, specialEnabled: $specialEnabled, result: $result");
return $result;
}
public static function getConfig($name, $searchBoxId)
{
if ($name == '*') {
$key = "hr";
} else {
$key = "hr.$name";
}
$default = Setting::get($key);
return apply_filter("nexus_setting_get", $default, $name, ['mode' => $searchBoxId]);
}
public function torrent(): \Illuminate\Database\Eloquent\Relations\BelongsTo