From 33b16ed7acceb10a32e1ad2f36c306b1b78d8580 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Sun, 9 Apr 2023 14:53:15 +0800 Subject: [PATCH] improve login notify + migrate torrent purchase to announce --- app/Console/Commands/MeiliSearchImport.php | 4 +- app/Console/Commands/Test.php | 15 -------- app/Console/Commands/UserLoginNotify.php | 13 +++---- .../Controllers/AuthenticateController.php | 4 ++ app/Jobs/SendLoginNotify.php | 38 +++++++++++++++---- app/Repositories/UserRepository.php | 19 ++++++++++ public/announce.php | 10 +++-- public/download.php | 23 ++++++----- public/takelogin.php | 22 ++--------- 9 files changed, 86 insertions(+), 62 deletions(-) diff --git a/app/Console/Commands/MeiliSearchImport.php b/app/Console/Commands/MeiliSearchImport.php index 754fdcd7..7412c6c0 100644 --- a/app/Console/Commands/MeiliSearchImport.php +++ b/app/Console/Commands/MeiliSearchImport.php @@ -19,7 +19,7 @@ class MeiliSearchImport extends Command * * @var string */ - protected $description = 'Command description'; + protected $description = 'Import torrents to meilisearch'; /** * Execute the console command. @@ -29,7 +29,7 @@ class MeiliSearchImport extends Command public function handle() { $rep = new MeiliSearchRepository(); - $this->info("going to import torrents"); + $this->info("going to import torrents..."); $total = $rep->import(); $this->info("import $total torrents."); return Command::SUCCESS; diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index b79ac22d..acfacf9f 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -97,21 +97,6 @@ class Test extends Command */ public function handle() { - $thisLoginLog = LoginLog::query()->findOrFail(10); - $lastLoginLog = LoginLog::query()->findOrFail(9); - $user = User::query()->findOrFail(1, User::$commonFields); - $locale = $user->locale; - $toolRep = new ToolRepository(); - $subject = nexus_trans('message.login_notify.subject', ['site_name' => Setting::get('basic.SITENAME')], $locale); - $body = nexus_trans('message.login_notify.body', [ - 'this_login_time' => $thisLoginLog->created_at, - 'this_ip' => $thisLoginLog->ip, - 'this_location' => sprintf('%s·%s', $thisLoginLog->city, $thisLoginLog->country), - 'last_login_time' => $lastLoginLog->created_at, - 'last_ip' => $lastLoginLog->ip, - 'last_location' => sprintf('%s·%s', $lastLoginLog->city, $lastLoginLog->country), - ], $locale); - dd($body); } } diff --git a/app/Console/Commands/UserLoginNotify.php b/app/Console/Commands/UserLoginNotify.php index a246e9a1..bdce5520 100644 --- a/app/Console/Commands/UserLoginNotify.php +++ b/app/Console/Commands/UserLoginNotify.php @@ -13,14 +13,14 @@ class UserLoginNotify extends Command * * @var string */ - protected $signature = 'user:login_notify {--this_id=} {--last_id=}'; + protected $signature = 'user:login_notify {--this_id=}'; /** * The console command description. * * @var string */ - protected $description = 'Send login notify, option: --this_id, --last_id'; + protected $description = 'Send login notify, option: --this_id'; /** * Execute the console command. @@ -30,13 +30,12 @@ class UserLoginNotify extends Command public function handle() { $thisId = $this->option('this_id'); - $lastId = $this->option('last_id'); - $this->info("thisId: $thisId, lastId: $lastId"); - if (!$thisId || !$lastId) { - $this->error("require option --this_id=? and --last_id=?"); + $this->info("thisId: $thisId"); + if (!$thisId) { + $this->error("require option --this_id=?"); return Command::FAILURE; } - SendLoginNotify::dispatch($thisId, $lastId); + SendLoginNotify::dispatch($thisId); return Command::SUCCESS; } } diff --git a/app/Http/Controllers/AuthenticateController.php b/app/Http/Controllers/AuthenticateController.php index 7141c31d..8598011a 100644 --- a/app/Http/Controllers/AuthenticateController.php +++ b/app/Http/Controllers/AuthenticateController.php @@ -4,9 +4,11 @@ namespace App\Http\Controllers; use App\Http\Resources\ExamResource; use App\Http\Resources\UserResource; +use App\Models\LoginLog; use App\Models\Setting; use App\Models\User; use App\Repositories\AuthenticateRepository; +use App\Repositories\UserRepository; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Support\Facades\Auth; @@ -61,6 +63,8 @@ class AuthenticateController extends Controller logincookie($user->id, $passhash,false, get_setting('system.cookie_valid_days', 365) * 86400, true, true, true); $user->last_login = now(); $user->save(); + $userRep = new UserRepository(); + $userRep->saveLoginLog($user->id, $ip, 'Passkey', false); } } return redirect('index.php'); diff --git a/app/Jobs/SendLoginNotify.php b/app/Jobs/SendLoginNotify.php index bf5d4eaa..6d53db87 100644 --- a/app/Jobs/SendLoginNotify.php +++ b/app/Jobs/SendLoginNotify.php @@ -3,6 +3,7 @@ namespace App\Jobs; use App\Models\LoginLog; +use App\Models\NexusModel; use App\Models\Setting; use App\Models\User; use App\Repositories\ToolRepository; @@ -19,18 +20,14 @@ class SendLoginNotify implements ShouldQueue private int $thisLoginLogId; - private int $lastLoginLogId; - /** * Create a new job instance. * * @return void */ - public function __construct(int $thisLoginLogId, int $lastLoginLogId) + public function __construct(int $thisLoginLogId) { $this->thisLoginLogId = $thisLoginLogId; - - $this->lastLoginLogId = $lastLoginLogId; } /** @@ -40,8 +37,31 @@ class SendLoginNotify implements ShouldQueue */ public function handle() { + /** @var NexusModel $thisLoginLog */ $thisLoginLog = LoginLog::query()->findOrFail($this->thisLoginLogId); - $lastLoginLog = LoginLog::query()->findOrFail($this->lastLoginLogId); + $log = "handling login log: " . $thisLoginLog->toJson(); + if (!$thisLoginLog->country || !$thisLoginLog->city) { + do_log("$log, this login log no country or city"); + return; + } + $lastLoginLog = LoginLog::query() + ->where('uid', $thisLoginLog->uid) + ->where("id", "<", $thisLoginLog->id) + ->orderBy('id', 'desc') + ->first(); + if (!$lastLoginLog) { + do_log("$log, no last login log"); + return; + } + $log .= sprintf(", last login: ", $lastLoginLog->toJson()); + if (!$lastLoginLog->country || !$lastLoginLog->city) { + do_log("$log, last login log no country or city"); + return; + } + if ($thisLoginLog->country == $lastLoginLog->country && $thisLoginLog->city == $lastLoginLog->city) { + do_log("$log, country and city are equals"); + return; + } $user = User::query()->findOrFail($thisLoginLog->uid, User::$commonFields); $locale = $user->locale; $toolRep = new ToolRepository(); @@ -55,7 +75,11 @@ class SendLoginNotify implements ShouldQueue 'last_location' => sprintf('%s·%s', $lastLoginLog->city, $lastLoginLog->country), ], $locale); $result = $toolRep->sendMail($user->email, $subject, $body); - do_log(sprintf('user: %s login notify result: %s', $user->username, var_export($result, true))); + do_log(sprintf( + '%s, user: %s login notify result: %s', + $log, $user->username, var_export($result, true) + )); + } /** diff --git a/app/Repositories/UserRepository.php b/app/Repositories/UserRepository.php index 9d2e6f6f..4517d687 100644 --- a/app/Repositories/UserRepository.php +++ b/app/Repositories/UserRepository.php @@ -7,6 +7,7 @@ use App\Http\Resources\ExamUserResource; use App\Http\Resources\UserResource; use App\Models\ExamUser; use App\Models\Invite; +use App\Models\LoginLog; use App\Models\Message; use App\Models\Setting; use App\Models\User; @@ -698,4 +699,22 @@ class UserRepository extends BaseRepository return nexus_trans('invite.send_allow_text'); } + public function saveLoginLog(int $uid, string $ip, string $client = '', bool $notify = false) + { + $locationInfo = get_ip_location_from_geoip($ip); + $loginLog = LoginLog::query()->create([ + 'ip' => $ip, + 'uid' => $uid, + 'country' => $locationInfo['country_en'] ?? '', + 'city' => $locationInfo['city_en'] ?? '', + 'client' => $client, + ]); + if ($notify) { + $command = sprintf("user:login_notify --this_id=%s", $loginLog->id); + do_log("[LOGIN_NOTIFY], user: $uid, $command"); + executeCommand($command, "string", true, false); + } + return $loginLog; + } + } diff --git a/public/announce.php b/public/announce.php index 7de2eff0..f8ba0453 100644 --- a/public/announce.php +++ b/public/announce.php @@ -82,7 +82,7 @@ $seeder = ($left == 0) ? "yes" : "no"; // check passkey if (!$az = $Cache->get_value('user_passkey_'.$passkey.'_content')){ - $res = sql_query("SELECT id, username, downloadpos, enabled, uploaded, downloaded, class, parked, clientselect, showclienterror, passkey, donor, donoruntil FROM users WHERE passkey=". sqlesc($passkey)." LIMIT 1"); + $res = sql_query("SELECT id, username, downloadpos, enabled, uploaded, downloaded, class, parked, clientselect, showclienterror, passkey, donor, donoruntil, seedbonus FROM users WHERE passkey=". sqlesc($passkey)." LIMIT 1"); $az = mysql_fetch_array($res); do_log("[check passkey], currentUser: " . nexus_json_encode($az)); $Cache->cache_value('user_passkey_'.$passkey.'_content', $az, 3600); @@ -153,8 +153,12 @@ if ($torrent['approval_status'] != \App\Models\Torrent::APPROVAL_STATUS_ALLOW && } if ($seeder == 'no' && isset($torrent['price']) && $torrent['price'] > 0 && $torrent['owner'] != $userid) { $hasBuy = \App\Models\TorrentBuyLog::query()->where('uid', $userid)->where('torrent_id', $torrent['id'])->exists(); - if (!$hasBuy) { - err("You have not purchased this torrent yet"); + if (!$hasBuy && isset($az['seedbonus'])) { + if ($az['seedbonus'] < $torrent['price']) { + err("Not enough bonus to buy this paid torrent"); + } + $bonusRep = new \App\Repositories\BonusRepository(); + $bonusRep->consumeToBuyTorrent($az['id'], $torrent['id'], 'Web'); } } diff --git a/public/download.php b/public/download.php index bdfaa49a..6690bdce 100644 --- a/public/download.php +++ b/public/download.php @@ -117,16 +117,19 @@ if ((($row['banned'] == 'yes' || ($approvalNotAllowed && !$allowOwnerDownload)) denyDownload(); } -if ($row['price'] > 0 && $CURUSER['id'] != $row['owner']) { - $hasBuy = \App\Models\TorrentBuyLog::query()->where('uid', $CURUSER['id'])->where('torrent_id', $id)->exists(); - if (!$hasBuy) { - if ($CURUSER['seedbonus'] < $row['price']) { - stderr('Error', nexus_trans('bonus.not_enough', ['require_bonus' => number_format($row['price']), 'now_bonus' => number_format($CURUSER['seedbonus'])])); - } - $bonusRep = new \App\Repositories\BonusRepository(); - $bonusRep->consumeToBuyTorrent($CURUSER['id'], $id, 'Web'); - } -} +/** + * Migrate to announce.php, due to IYUU will download torrent automatically + */ +//if ($row['price'] > 0 && $CURUSER['id'] != $row['owner']) { +// $hasBuy = \App\Models\TorrentBuyLog::query()->where('uid', $CURUSER['id'])->where('torrent_id', $id)->exists(); +// if (!$hasBuy) { +// if ($CURUSER['seedbonus'] < $row['price']) { +// stderr('Error', nexus_trans('bonus.not_enough', ['require_bonus' => number_format($row['price']), 'now_bonus' => number_format($CURUSER['seedbonus'])])); +// } +// $bonusRep = new \App\Repositories\BonusRepository(); +// $bonusRep->consumeToBuyTorrent($CURUSER['id'], $id, 'Web'); +// } +//} sql_query("UPDATE torrents SET hits = hits + 1 WHERE id = ".sqlesc($id)) or sqlerr(__FILE__, __LINE__); diff --git a/public/takelogin.php b/public/takelogin.php index 303f66b1..42e4312e 100644 --- a/public/takelogin.php +++ b/public/takelogin.php @@ -37,24 +37,10 @@ $log = "user: {$row['id']}, ip: $ip"; if ($row["passhash"] != md5($row["secret"] . $password . $row["secret"])) { login_failedlogins(); } -$locationInfo = get_ip_location_from_geoip($ip); -$thisLoginLog = \App\Models\LoginLog::query()->create([ - 'ip' => $ip, - 'uid' => $row['id'], - 'country' => $locationInfo['country_en'] ?? '', - 'city' => $locationInfo['city_en'] ?? '', - 'client' => 'Web', -]); -$lastLoginLog = \App\Models\LoginLog::query()->where('uid', $row['id'])->orderBy('id', 'desc')->first(); -if ( - $lastLoginLog && $lastLoginLog->country && $lastLoginLog->city - && $locationInfo['country_en'] && $locationInfo['city_en'] - && ($lastLoginLog->country != $locationInfo['country_en'] || $lastLoginLog->city != $locationInfo['city_en']) -) { - $command = sprintf("user:login_notify --this_id=%s --last_id=%s", $thisLoginLog->id, $lastLoginLog->id); - do_log("[LOGIN_NOTIFY], user: {$row['id']}, $command"); - executeCommand($command, "string", true, false); -} + +$userRep = new \App\Repositories\UserRepository(); +$userRep->saveLoginLog($row['id'], $ip, 'Web', true); + if ($row["enabled"] == "no") bark($lang_takelogin['std_account_disabled']);