Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Add) Request #3650 #3744

Draft
wants to merge 5 commits into
base: 8.x.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
231 changes: 225 additions & 6 deletions app/Http/Livewire/TorrentSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
namespace App\Http\Livewire;

use App\Models\Category;
use App\Models\History;
use App\Models\Movie;
use App\Models\PrivateMessage;
use App\Models\Torrent;
use App\Models\Tv;
use App\Models\User;
use App\Services\Unit3dAnnounce;
use App\Traits\CastLivewireProperties;
use App\Traits\LivewireSort;
use App\Traits\TorrentMeta;
Expand All @@ -27,6 +30,7 @@
use Livewire\Component;
use Livewire\WithPagination;
use Closure;
use MarcReichel\IGDBLaravel\Models\Game;

class TorrentSearch extends Component
{
Expand All @@ -35,6 +39,7 @@
use TorrentMeta;
use WithPagination;

// Search Filters
#TODO: Update URL attributes once Livewire 3 fixes upstream bug. See: https://github.com/livewire/livewire/discussions/7746

#[Url(history: true)]
Expand Down Expand Up @@ -214,6 +219,38 @@
#[Url(history: true)]
public string $view = 'list';

// Bulk Actions

/**
* @var array<int, bool>
*/
public array $checked = [];

public bool $selectPage = false;

public int $category;

public int $freeleech;

public int $doubleupload;
HDVinnie marked this conversation as resolved.
Show resolved Hide resolved

protected $listeners = [

Check failure on line 237 in app/Http/Livewire/TorrentSearch.php

View workflow job for this annotation

GitHub Actions / php 8.3 on ubuntu-22.04

Property App\Http\Livewire\TorrentSearch::$listeners has no type specified.

Check failure on line 237 in app/Http/Livewire/TorrentSearch.php

View workflow job for this annotation

GitHub Actions / php 8.3 on ubuntu-22.04

Property App\Http\Livewire\TorrentSearch::$listeners has no type specified.
'destroy' => 'delete',
'updateFreeleech' => 'freeleech',
'updateDoubleUpload' => 'doubleupload',
'updateCategory' => 'category',
];

final public function updatedSelectPage(bool $value): void
{
$this->checked = $value ? $this->torrents->pluck('id')->toArray() : [];

Check failure on line 246 in app/Http/Livewire/TorrentSearch.php

View workflow job for this annotation

GitHub Actions / php 8.3 on ubuntu-22.04

Call to an undefined method Illuminate\Contracts\Pagination\LengthAwarePaginator<App\Models\Torrent>::pluck().

Check failure on line 246 in app/Http/Livewire/TorrentSearch.php

View workflow job for this annotation

GitHub Actions / php 8.3 on ubuntu-22.04

Call to an undefined method Illuminate\Contracts\Pagination\LengthAwarePaginator<App\Models\Torrent>::pluck().
}

final public function updatedChecked(): void
{
$this->selectPage = false;
}

final public function updating(string $field, mixed &$value): void
{
$this->castLivewireProperties($field, $value);
Expand Down Expand Up @@ -313,7 +350,7 @@
'leechers',
'times_completed',
'created_at',
'bumped_at'
'bumped_at',
])) {
$this->reset('sortField');
}
Expand Down Expand Up @@ -488,7 +525,7 @@
->sortBy([
['resolution.position', 'asc'],
['internal', 'desc'],
['size', 'desc']
['size', 'desc'],
])
->values()
);
Expand Down Expand Up @@ -516,7 +553,7 @@
->sortBy([
['resolution.position', 'asc'],
['internal', 'desc'],
['size', 'desc']
['size', 'desc'],
])
->values()
),
Expand All @@ -533,7 +570,7 @@
->sortBy([
['resolution.position', 'asc'],
['internal', 'desc'],
['size', 'desc']
['size', 'desc'],
])
->values()
)
Expand All @@ -554,7 +591,7 @@
->sortBy([
['resolution.position', 'asc'],
['internal', 'desc'],
['size', 'desc']
['size', 'desc'],
])
->values()
),
Expand All @@ -571,7 +608,7 @@
->sortBy([
['resolution.position', 'asc'],
['internal', 'desc'],
['size', 'desc']
['size', 'desc'],
])
->values()
)
Expand Down Expand Up @@ -668,6 +705,188 @@
return $groups;
}

final public function freeleech(): void
{
if (!auth()->user()->group->is_owner) {
HDVinnie marked this conversation as resolved.
Show resolved Hide resolved
$this->dispatch('error', type: 'error', message: 'Permission Denied!');

return;
}

$torrents = Torrent::whereKey($this->checked)->get();

$torrents->each(fn ($torrent) => $torrent->update([
'free' => $this->freeleech,
HDVinnie marked this conversation as resolved.
Show resolved Hide resolved
]));

$this->checked = [];
$this->selectPage = false;
Comment on lines +727 to +728
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does livewire have a function to reset the component to its initial state


$this->dispatch(
'swal:modal',
type: 'success',
message: 'Success!',
text: 'The torrents you selected now have '.$this->freeleech.'% Freeleech.',
);
}

final public function doubleupload(): void
{
if (!auth()->user()->group->is_owner) {
$this->dispatch('error', type: 'error', message: 'Permission Denied!');

return;
}

$torrents = Torrent::whereKey($this->checked)->get();

$torrents->each(fn ($torrent) => $torrent->update([
'doubleup' => $this->doubleupload,
]));

$this->checked = [];
$this->selectPage = false;

$this->dispatch(
'swal:modal',
type: 'success',
message: 'Success!',
text: $this->doubleupload ? 'The torrents you selected are now Double Upload.' : 'The torrents you selected are no longer Double Upload.',
);
}

final public function category(): void
{
if (!auth()->user()->group->is_owner) {
$this->dispatch('error', type: 'error', message: 'Permission Denied!');

return;
}

$torrents = Torrent::whereKey($this->checked)->get();

$torrents->each(fn ($torrent) => $torrent->update([
'category_id' => $this->category,
]));

$this->checked = [];
$this->selectPage = false;

$category = Category::find($this->category);

$this->dispatch(
'swal:modal',
type: 'success',
message: 'Success!',
text: 'The torrents you selected are now in the '.$category->name.' category.',
);
}

final public function delete(): void
{
if (!auth()->user()->group->is_modo) {
$this->dispatch('error', type: 'error', message: 'Permission Denied!');

return;
}

$torrents = Torrent::whereKey($this->checked)->get();
$names = [];
$users = [];
$title = match (true) {
$this->category->movie_meta => ($movie = Movie::find($this->tmdbId))->title.' ('.$movie->release_date.')',

Check failure on line 797 in app/Http/Livewire/TorrentSearch.php

View workflow job for this annotation

GitHub Actions / php 8.3 on ubuntu-22.04

Cannot access property $movie_meta on int.

Check failure on line 797 in app/Http/Livewire/TorrentSearch.php

View workflow job for this annotation

GitHub Actions / php 8.3 on ubuntu-22.04

Cannot access property $movie_meta on int.
$this->category->tv_meta => ($tv = Tv::find($this->tmdbId))->name.' ('.$tv->first_air_date.')',

Check failure on line 798 in app/Http/Livewire/TorrentSearch.php

View workflow job for this annotation

GitHub Actions / php 8.3 on ubuntu-22.04

Cannot access property $tv_meta on int.

Check failure on line 798 in app/Http/Livewire/TorrentSearch.php

View workflow job for this annotation

GitHub Actions / php 8.3 on ubuntu-22.04

Cannot access property $tv_meta on int.
$this->category->game_meta => ($game = Game::find($this->igdbId))->name.' ('.$game->first_release_date.')',

Check failure on line 799 in app/Http/Livewire/TorrentSearch.php

View workflow job for this annotation

GitHub Actions / php 8.3 on ubuntu-22.04

Access to an undefined property App\Http\Livewire\TorrentSearch::$igdbId.

Check failure on line 799 in app/Http/Livewire/TorrentSearch.php

View workflow job for this annotation

GitHub Actions / php 8.3 on ubuntu-22.04

Cannot access property $game_meta on int.

Check failure on line 799 in app/Http/Livewire/TorrentSearch.php

View workflow job for this annotation

GitHub Actions / php 8.3 on ubuntu-22.04

Access to an undefined property App\Http\Livewire\TorrentSearch::$igdbId.

Check failure on line 799 in app/Http/Livewire/TorrentSearch.php

View workflow job for this annotation

GitHub Actions / php 8.3 on ubuntu-22.04

Cannot access property $game_meta on int.
default => $torrents->pluck('name')->join(', '),
};

foreach ($torrents as $torrent) {
$names[] = $torrent->name;

foreach (History::where('torrent_id', '=', $torrent->id)->get() as $pm) {
if (!\in_array($pm->user_id, $users)) {
Comment on lines +802 to +806
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this to a trait to share with similar torrents

$users[] = $pm->user_id;
}
}

// Reset Requests
$torrent->requests()->update([
'torrent_id' => null,
]);

//Remove Torrent related info
cache()->forget(sprintf('torrent:%s', $torrent->info_hash));

$torrent->comments()->delete();
$torrent->peers()->delete();
$torrent->history()->delete();
$torrent->hitrun()->delete();
$torrent->files()->delete();
$torrent->playlists()->detach();
$torrent->subtitles()->delete();
$torrent->resurrections()->delete();
$torrent->featured()->delete();
HDVinnie marked this conversation as resolved.
Show resolved Hide resolved

$freeleechTokens = $torrent->freeleechTokens();

foreach ($freeleechTokens->get() as $freeleechToken) {
cache()->forget('freeleech_token:'.$freeleechToken->user_id.':'.$torrent->id);
}

$freeleechTokens->delete();

cache()->forget('announce-torrents:by-infohash:'.$torrent->info_hash);

Unit3dAnnounce::removeTorrent($torrent);

$torrent->delete();
}

foreach ($users as $user) {
$pmuser = new PrivateMessage();
$pmuser->sender_id = User::SYSTEM_USER_ID;
$pmuser->receiver_id = $user;
$pmuser->subject = 'Bulk Torrents Deleted - '.$title.'! ';
$pmuser->message = '[b]Attention: [/b] The following torrents have been removed from our site.
[list]
[*]'.implode(' [*]', $names).'
[/list]
Our system shows that you were either the uploader, a seeder or a leecher on said torrent. We just wanted to let you know you can safely remove it from your client.
[b]Removal Reason: [/b] '.$this->reason.'

Check failure on line 855 in app/Http/Livewire/TorrentSearch.php

View workflow job for this annotation

GitHub Actions / php 8.3 on ubuntu-22.04

Access to an undefined property App\Http\Livewire\TorrentSearch::$reason.

Check failure on line 855 in app/Http/Livewire/TorrentSearch.php

View workflow job for this annotation

GitHub Actions / php 8.3 on ubuntu-22.04

Access to an undefined property App\Http\Livewire\TorrentSearch::$reason.
[color=red][b]THIS IS AN AUTOMATED SYSTEM MESSAGE, PLEASE DO NOT REPLY![/b][/color]';
$pmuser->save();
}

$this->checked = [];
$this->selectPage = false;

$this->dispatch(
'swal:modal',
type: 'success',
message: 'Torrents Deleted Successfully!',
text: 'A personal message has been sent to all users that have downloaded these torrents.',
);
}

final public function alertConfirm(): void
{
if (!auth()->user()->group->is_modo) {
$this->dispatch('error', type: 'error', message: 'Permission Denied!');

return;
}

$torrents = Torrent::whereKey($this->checked)->pluck('name')->toArray();
$names = $torrents;
$this->dispatch(
'swal:confirm',
type: 'warning',
message: 'Are you sure?',
body: 'You are about to apply changes to following torrents!'.nl2br("\n")
.nl2br(implode("\n", $names)),
);
}

final public function render(): \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Contracts\Foundation\Application
{
return view('livewire.torrent-search', [
Expand Down
Loading
Loading