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
+2 -2
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;
-15
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);
}
}
+6 -7
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;
}
}
@@ -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');
+31 -7
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)
));
}
/**
+19
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;
}
}