Merge "Fixed spam user process emails"

This commit is contained in:
Zuul 2020-04-01 16:15:02 +00:00 committed by Gerrit Code Review
commit 559fdfcbb9
10 changed files with 116 additions and 78 deletions

View File

@ -13,6 +13,7 @@
**/
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Process\Process;
use Exception;
/**
@ -50,35 +51,44 @@ final class RebuildUserSpammerEstimator extends Command
*/
public function handle()
{
$connections = Config::get('database.connections', []);
$db = $connections['openstackid'] ?? [];
$host = $db['host'] ?? '';
$database = $db['database'] ?? '';
$username = $db['username'] ?? '';
$password = $db['password'] ?? '';
try {
$connections = Config::get('database.connections', []);
$db = $connections['openstackid'] ?? [];
$host = $db['host'] ?? '';
$database = $db['database'] ?? '';
$username = $db['username'] ?? '';
$password = $db['password'] ?? '';
$command = sprintf(
'%s/app/Console/Commands/SpammerProcess/estimator_build.sh "%s" "%s" "%s" "%s" "%s"',
base_path(),
base_path().'/app/Console/Commands/SpammerProcess',
$host,
$username,
$password,
$database
);
$command = sprintf(
'%s/app/Console/Commands/SpammerProcess/estimator_build.sh "%s" "%s" "%s" "%s" "%s"',
base_path(),
base_path() . '/app/Console/Commands/SpammerProcess',
$host,
$username,
$password,
$database
);
$process = new Process($command);
$process->setTimeout(PHP_INT_MAX);
$process->setIdleTimeout(PHP_INT_MAX);
$process->run();
Log::debug(sprintf("RebuildUserSpammerEstimator::handle running command %s", $command));
while ($process->isRunning()) {
$process = new Process($command);
$process->setTimeout(PHP_INT_MAX);
$process->setIdleTimeout(PHP_INT_MAX);
$process->run();
while ($process->isRunning()) {
}
$output = $process->getOutput();
Log::debug(sprintf("RebuildUserSpammerEstimator::handle output %s", $output));
if (!$process->isSuccessful()) {
throw new Exception("Process Error!");
}
}
$output = $process->getOutput();
if (!$process->isSuccessful()) {
throw new Exception("Process Error!");
catch (Exception $ex){
Log::error($ex);
}
}
}

View File

@ -17,6 +17,7 @@ use Auth\Repositories\IUserRepository;
use Auth\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\URL;
use Symfony\Component\Process\Process;
@ -68,60 +69,70 @@ final class UserSpammerProcessor extends Command
*/
public function handle()
{
$connections = Config::get('database.connections', []);
$db = $connections['openstackid'] ?? [];
$host = $db['host'] ?? '';
$database = $db['database'] ?? '';
$username = $db['username'] ?? '';
$password = $db['password'] ?? '';
try {
$connections = Config::get('database.connections', []);
$db = $connections['openstackid'] ?? [];
$host = $db['host'] ?? '';
$database = $db['database'] ?? '';
$username = $db['username'] ?? '';
$password = $db['password'] ?? '';
$command = sprintf(
'%s/app/Console/Commands/SpammerProcess/estimator_process.sh "%s" "%s" "%s" "%s" "%s"',
base_path(),
base_path().'/app/Console/Commands/SpammerProcess',
$host,
$username,
$password,
$database
);
$default = Config::get("database.default");
$process = new Process($command);
$process->setTimeout(PHP_INT_MAX);
$process->setIdleTimeout(PHP_INT_MAX);
$process->run();
$command = sprintf(
'%s/app/Console/Commands/SpammerProcess/estimator_process.sh "%s" "%s" "%s" "%s" "%s"',
base_path(),
base_path() . '/app/Console/Commands/SpammerProcess',
$host,
$username,
$password,
$database
);
while ($process->isRunning()) {
Log::debug(sprintf("UserSpammerProcessor::handle running command %s", $command));
$process = new Process($command);
$process->setTimeout(PHP_INT_MAX);
$process->setIdleTimeout(PHP_INT_MAX);
$process->run();
while ($process->isRunning()) {
}
$csv_content = $process->getOutput();
Log::debug(sprintf("UserSpammerProcessor::handle output %s", $csv_content));
if (!$process->isSuccessful()) {
throw new Exception("Process Error!");
}
$rows = CSVReader::load($csv_content);
// send email with excerpt
$users = [];
foreach ($rows as $row) {
$user_id = intval($row["ID"]);
$type = $row["Type"];
$user = $this->user_repository->getById($user_id);
if (is_null($user) || !$user instanceof User) continue;
$users[] = [
'id' => $user->getId(),
'email' => $user->getEmail(),
'full_name' => $user->getFullName(),
'spam_type' => $type,
'edit_link' => URL::route("edit_user", ["user_id" => $user->getId()], true)
];
}
if (count($users) > 0 && !empty(Config::get('mail.user_spam_processor_to'))) {
Log::debug("UserSpammerProcessor::handle sending email");
Mail::queue(new UserSpammerProcessorResultsEmail($users));
}
}
$csv_content = $process->getOutput();
if (!$process->isSuccessful()) {
throw new Exception("Process Error!");
}
$rows = CSVReader::load($csv_content);
// send email with excerpt
$users = [];
foreach($rows as $row) {
$user_id = intval($row["ID"]);
$type = $row["Type"];
$user = $this->user_repository->getById($user_id);
if(is_null($user) || !$user instanceof User) continue;
$users[] = [
'id' => $user->getId(),
'email' => $user->getEmail(),
'full_name' => $user->getFullName(),
'spam_type' => $type,
'edit_link' => URL::route("edit_user", ["user_id" => $user->getId()], true)
];
}
if(count($users) > 0 && !empty(Config::get('mail.user_spam_processor_to'))){
Mail::queue(new UserSpammerProcessorResultsEmail($users));
catch (Exception $ex){
Log::error($ex);
}
}
}

View File

@ -27,6 +27,8 @@ class OAuth2ClientLocked extends Mailable
{
use Queueable, SerializesModels;
public $tries = 1;
/**
* @var string
*/

View File

@ -26,6 +26,8 @@ final class UserEmailVerificationRequest extends Mailable
{
use Queueable, SerializesModels;
public $tries = 2;
/**
* @var string
*/

View File

@ -24,6 +24,8 @@ class UserEmailVerificationSuccess extends Mailable
{
use Queueable, SerializesModels;
public $tries = 2;
/**
* @var string
*/

View File

@ -26,6 +26,8 @@ final class UserLockedEmail extends Mailable
{
use Queueable, SerializesModels;
public $tries = 2;
/**
* @var string
*/

View File

@ -25,6 +25,8 @@ final class UserPasswordResetMail extends Mailable
{
use Queueable, SerializesModels;
public $tries = 2;
/**
* @var string
*/

View File

@ -27,6 +27,8 @@ final class UserPasswordResetRequestMail extends Mailable
{
use Queueable, SerializesModels;
public $tries = 2;
/**
* @var string
*/

View File

@ -23,6 +23,8 @@ class UserSpammerProcessorResultsEmail extends Mailable
{
use Queueable, SerializesModels;
public $tries = 1;
/**
* @var array
*/
@ -42,9 +44,10 @@ class UserSpammerProcessorResultsEmail extends Mailable
{
$subject = sprintf("[%s] User Spammer Process Result", Config::get('app.app_name'));
$to = Config::get("mail.user_spam_processor_to");
$to = explode(',', $to);
return $this->from(Config::get("mail.from"))
->to(Config::get("mail.user_spam_processor_to"))
->to($to)
->subject($subject)
->view('emails.user_spammer_process_result');
}

View File

@ -25,6 +25,8 @@ final class WelcomeNewUserEmail extends Mailable
{
use Queueable, SerializesModels;
public $tries = 1;
/**
* @var string
*/