HDInnovations / UNIT3D-Community-Edition

Private Torrent Tracker Built With Laravel, Livewire and AlpineJS.
https://unit3d.dev
GNU Affero General Public License v3.0
1.93k stars 372 forks source link

[Request] Invite purchase time limit #3990

Open Audionut opened 1 month ago

Audionut commented 1 month ago

The ability to place a time limit between invite purchases.

ChatGTP reckons a migration to update the database.

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

class AddLastInviteExchangeToUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->date('last_invite_exchange')->nullable()->after('invites');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('last_invite_exchange');
        });
    }
}

Then it can be defined with the existing invite limitation:

case $bonExchange->invite:
    // Check if the user has reached the maximum number of unused invites
    if ($user->invites >= config('other.max_unused_user_invites', 1)) {
        return back()->withErrors('You already have the maximum amount of unused invites allowed per user.');
    }

    // Check if the user has already exchanged an invite this calendar month
    $lastExchange = $user->last_invite_exchange;
    $currentDate = now();
    $startOfMonth = $currentDate->copy()->startOfMonth();

    if ($lastExchange && $lastExchange->gte($startOfMonth)) {
        return back()->withErrors('You can only exchange an invite once per calendar month.');
    }

    // Update the last invite exchange date
    $user->last_invite_exchange = $currentDate;
    $user->save();

    // Proceed with the invite exchange logic
    // ...
    break;

Upvote & Fund

Fund with Polar

Roardom commented 1 month ago

Chatgpt says something different than what you say. You say "time limit between invites", while your code allows someone to invite one user on July 31st at 23:59 and another user on August 1st 00:00.

The user table also has too many columns, it would be better to just check the created_at timestamp of the last invite the user sent.

Hard-coding the interval to 1 month probably isn't best either. Either a config option or saving the interval in the groups table would be better, that way it can be configurable per group as well.