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) Setting to automatically unbookmark torrents upon completion #4576

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
52 changes: 52 additions & 0 deletions app/Console/Commands/AutoUnbookmarkCompletedTorrents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

/**
* 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 Roardom <roardom@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\Bookmark;
use Illuminate\Console\Command;

class AutoUnbookmarkCompletedTorrents extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'auto:unbookmark_completed_torrents';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Unbookmark user torrents automatically upon completion';

/**
* Execute the console command.
*/
final public function handle(): void
{
$start = now();

$affected = Bookmark::query()
->whereRelation('userSetting', 'unbookmark_torrents_on_completion', '=', true)
->whereRelation('history', 'completed_at', '>', now()->subDay())
->delete();

$this->comment($affected.' bookmarks unbookmarked on torrent completion in '.now()->floatDiffInSeconds($start).' seconds.');
}
}
2 changes: 2 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use App\Console\Commands\AutoSyncPeopleToMeilisearch;
use App\Console\Commands\AutoSyncTorrentsToMeilisearch;
use App\Console\Commands\AutoTorrentBalance;
use App\Console\Commands\AutoUnbookmarkCompletedTorrents;
use App\Console\Commands\AutoUpdateUserLastActions;
use App\Console\Commands\AutoUpsertAnnounces;
use App\Console\Commands\AutoUpsertHistories;
Expand Down Expand Up @@ -75,6 +76,7 @@ protected function schedule(Schedule $schedule): void

$schedule->command(AutoUpdateUserLastActions::class)->everyFiveSeconds();
$schedule->command(AutoDeleteStoppedPeers::class)->everyTwoMinutes();
$schedule->command(AutoUnbookmarkCompletedTorrents::class)->everyFifteenMinutes();
$schedule->command(AutoGroup::class)->daily();
$schedule->command(AutoNerdStat::class)->hourly();
$schedule->command(AutoCacheRandomMediaIds::class)->hourly();
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Requests/UpdateGeneralSettingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public function rules(): array
'required',
'boolean',
],
'unbookmark_torrents_on_completion' => [
'required',
'boolean',
],
];
}
}
20 changes: 20 additions & 0 deletions app/Models/Bookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
]);
}

/**
* Belongs To A User Setting.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<UserSetting, $this>
*/
public function userSetting(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(UserSetting::class, 'user_id', 'user_id');
}

/**
* Belongs To A History.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<History, $this>
*/
public function history(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(History::class, 'user_id', 'user_id')->whereColumn('bookmarks.torrent_id', '=', 'history.torrent_id');
}

/**
* Belongs To A Torrent.
*
Expand Down
18 changes: 13 additions & 5 deletions app/Models/UserSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* @property ?string $custom_css
* @property ?string $standalone_css
* @property bool $show_poster
* @property bool $unbookmark_torrents_on_completion
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
*/
Expand All @@ -44,15 +45,22 @@ class UserSetting extends Model
/**
* Get the attributes that should be cast.
*
* @return array{censor: 'bool', chat_hidden: 'bool', torrent_filters: 'bool', show_poster: 'bool'}
* @return array{
* censor: 'bool',
* chat_hidden: 'bool',
* torrent_filters: 'bool',
* show_poster: 'bool',
* unbookmark_torrents_on_completion: 'bool',
* }
*/
protected function casts(): array
{
return [
'censor' => 'bool',
'chat_hidden' => 'bool',
'torrent_filters' => 'bool',
'show_poster' => 'bool',
'censor' => 'bool',
'chat_hidden' => 'bool',
'torrent_filters' => 'bool',
'show_poster' => 'bool',
'unbookmark_torrents_on_completion' => 'bool',
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/**
* 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 Roardom <roardom@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('user_settings', function (Blueprint $table): void {
$table->boolean('unbookmark_torrents_on_completion')->after('show_poster');
});
}
};
2 changes: 2 additions & 0 deletions database/schema/mysql-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2437,6 +2437,7 @@ CREATE TABLE `user_settings` (
`custom_css` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`standalone_css` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`show_poster` tinyint(1) NOT NULL DEFAULT '0',
`unbookmark_torrents_on_completion` tinyint(1) NOT NULL,
`torrent_sort_field` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`torrent_search_autofocus` tinyint(1) NOT NULL DEFAULT '1',
`created_at` timestamp NULL DEFAULT NULL,
Expand Down Expand Up @@ -2970,3 +2971,4 @@ INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (338,'2025_03_11_13
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (339,'2025_03_12_043518_split_torrents_tmdb_into_movie_id_and_tv_id',2);
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (340,'2025_03_16_185628_update_torrents_table_to_int_igdb',2);
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (341,'2025_03_17_122748_add_tmdb_prefix_to_metadata_tables',3);
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (342,'2025_03_23_203227_add_automatically_unbookmark_torrents_user_setting',4);
17 changes: 17 additions & 0 deletions resources/views/user/general_setting/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,23 @@ class="form__checkbox"
Autofocus torrent search on page load
</label>
</p>
<p class="form__group">
<label class="form__label">
<input
type="hidden"
name="unbookmark_torrents_on_completion"
value="0"
/>
<input
class="form__checkbox"
type="checkbox"
name="unbookmark_torrents_on_completion"
value="1"
@checked($user->settings?->unbookmark_torrents_on_completion)
/>
Automatically unbookmark torrents upon completion
</label>
</p>
</div>
</fieldset>
<p class="form__group">
Expand Down