refactor announce

This commit is contained in:
xiaomlove
2022-03-17 18:46:49 +08:00
parent 3ad8fabe7b
commit 6665c98169
16 changed files with 1486 additions and 27 deletions

12
app/Models/Cheater.php Normal file
View File

@@ -0,0 +1,12 @@
<?php
namespace App\Models;
class Cheater extends NexusModel
{
protected $fillable = [
'added', 'userid', 'torrentid', 'uploaded', 'downloaded', 'anctime', 'seeders', 'leechers', 'hit',
'dealtby', 'dealtwith', 'comment',
];
}

View File

@@ -21,4 +21,20 @@ class NexusModel extends Model
return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
}
/**
* Check is valid date string
*
* @see https://stackoverflow.com/questions/19271381/correctly-determine-if-date-string-is-a-valid-date-in-that-format
* @param $name
* @param string $format
* @return bool
*/
public function isValidDate($name, $format = 'Y-m-d H:i:s'): bool
{
$date = $this->getRawOriginal($name);
$d = \DateTime::createFromFormat($format, $date);
// The Y ( 4 digits year ) returns TRUE for any integer with any number of digits so changing the comparison from == to === fixes the issue.
return $d && $d->format($format) === $date;
}
}

View File

@@ -4,9 +4,15 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Cache;
class Peer extends NexusModel
{
protected $fillable = [
'torrent', 'peer_id', 'ip', 'port', 'uploaded', 'downloaded', 'to_go', 'seeder', 'started', 'last_action',
'prev_action', 'connectable', 'userid', 'agent', 'finishedat', 'downloadoffset', 'uploadedoffset', 'passkey',
];
const CONNECTABLE_YES = 'yes';
const CONNECTABLE_NO = 'no';
@@ -71,4 +77,27 @@ class Peer extends NexusModel
{
return $this->belongsTo(Torrent::class, 'torrent');
}
/**
*
*/
public function updateConnectableStateIfNeeded()
{
$tmp_ip = $this->ip;
// IPv6 Check
if (filter_var($tmp_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
$tmp_ip = '['.$tmp_ip.']';
}
$cacheKey = 'peers:connectable:'.$tmp_ip.'-'.$this->port.'-'.$this->agent;
if (!Cache::has($cacheKey)) {
$con = @fsockopen($tmp_ip, $this->port, $error_code, $error_message, 1);
if (is_resource($con)) {
$this->connectable = self::CONNECTABLE_YES;
fclose($con);
} else {
$this->connectable = self::CONNECTABLE_NO;
}
Cache::put($cacheKey, $this->connectable, 600);
}
}
}

View File

@@ -61,7 +61,7 @@ class Torrent extends NexusModel
const PROMOTION_HALF_DOWN_TWO_TIMES_UP = 6;
const PROMOTION_ONE_THIRD_DOWN = 7;
public static $promotionTypes = [
public static array $promotionTypes = [
self::PROMOTION_NORMAL => ['text' => 'Normal', 'up_multiplier' => 1, 'down_multiplier' => 1],
self::PROMOTION_FREE => ['text' => 'Free', 'up_multiplier' => 1, 'down_multiplier' => 0],
self::PROMOTION_TWO_TIMES_UP => ['text' => '2X', 'up_multiplier' => 2, 'down_multiplier' => 1],
@@ -79,6 +79,9 @@ class Torrent extends NexusModel
public function getSpStateRealAttribute()
{
if ($this->getRawOriginal('sp_state') === null) {
throw new \RuntimeException('no select sp_state field');
}
$spState = $this->sp_state;
$global = self::getGlobalPromotionState();
$log = sprintf('torrent: %s sp_state: %s, global sp state: %s', $this->id, $spState, $global);

View File

@@ -348,7 +348,7 @@ class User extends Authenticatable
public function updateWithModComment(array $update, $modComment)
{
if (!$this->exists) {
throw new \RuntimeException('This mehtod only works when user exists!');
throw new \RuntimeException('This method only works when user exists!');
}
//@todo how to do prepare bindings here ?
$modComment = addslashes($modComment);