Files
UNIT3D-Community-Edition/app/Console/Commands/AutoFlushPeers.php
2023-02-03 22:23:00 -05:00

64 lines
1.6 KiB
PHP

<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <hdinnovations@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
namespace App\Console\Commands;
use App\Models\History;
use App\Models\Peer;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Exception;
/**
* @see \Tests\Unit\Console\Commands\AutoFlushPeersTest
*/
class AutoFlushPeers extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'auto:flush_peers';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Flushes Ghost Peers';
/**
* Execute the console command.
*
* @throws Exception
*/
public function handle(): void
{
$carbon = new Carbon();
$peers = Peer::select(['id', 'torrent_id', 'user_id', 'updated_at'])->where('updated_at', '<', $carbon->copy()->subHours(2)->toDateTimeString())->get();
foreach ($peers as $peer) {
$history = History::where('torrent_id', '=', $peer->torrent_id)->where('user_id', '=', $peer->user_id)->first();
if ($history) {
$history->active = false;
$history->save();
}
$peer->delete();
}
$this->comment('Automated Flush Ghost Peers Command Complete');
}
}