improve login notify + migrate torrent purchase to announce

This commit is contained in:
xiaomlove
2023-04-09 14:53:15 +08:00
parent 4535ba94a4
commit 33b16ed7ac
9 changed files with 86 additions and 62 deletions

View File

@@ -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;

View File

@@ -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);
}
}

View File

@@ -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;
}
}

View File

@@ -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');

View File

@@ -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)
));
}
/**

View File

@@ -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;
}
}

View File

@@ -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');
}
}

View File

@@ -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__);

View File

@@ -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']);