check more detailed for email verification code resend, fix double send on register, fix double send in case of multiple email verification codes for user in db

This commit is contained in:
Dario 2020-07-10 09:47:31 +02:00
parent b3476c5d58
commit 8801d91ee7
4 changed files with 32 additions and 4 deletions

View File

@ -111,6 +111,9 @@ int EmailManager::ThreadFunction()
if (email->draft(&mailMessage, catalogs[lang_code])) {
mailClientSession.sendMessage(mailMessage);
// add for debugginh
auto user_model = email->getUser()->getModel();
printf("send email to %s\n", user_model->getEmail().data());
}
else {
// error drafting email, shouldn't happend

View File

@ -326,7 +326,9 @@ namespace controller {
select << "select u.id, v.created from users as u "
<< "LEFT JOIN email_opt_in as v ON(u.id = v.user_id) "
<< "where u.email_checked = ? "
<< "AND v.resend_count <= ?", Poco::Data::Keywords::use(email_checked), Poco::Data::Keywords::use(resend_count), Poco::Data::Keywords::into(results)
<< "AND v.resend_count <= ? "
<< "ORDER BY u.id, v.created " ,
Poco::Data::Keywords::use(email_checked), Poco::Data::Keywords::use(resend_count), Poco::Data::Keywords::into(results)
;
int result_count = 0;
try {
@ -344,22 +346,41 @@ namespace controller {
int count_scheduled_at_once = 0;
int count_scheduled = 0;
int last_user_id = 0;
// add 1 for resend task scheduled at once
// add 2 for resend task scheduled in the future
// reset if new user_id came up
int scheduledResendTask = 0;
// results sorted by user_id
//printf("results count: %d\n", results.size());
for (auto it = results.begin(); it != results.end(); it++) {
auto user_id = it->get<0>();
auto created = it->get<1>();
//auto created_str = Poco::DateTimeFormatter::format(created, "%f.%m.%y %H:%M");
//printf("user_id: %d, created: %s\n", user_id, created_str.data());
if (user_id != last_user_id) {
assert(user_id > last_user_id);
last_user_id = user_id;
scheduledResendTask = 0;
}
if (scheduledResendTask == 3) continue;
auto age = now - created;
// older than 7 days, schedule at once
if (age.days() > 7) {
if (age.days() > 7 && !(scheduledResendTask & 1)) {
UniLib::controller::TaskPtr verificationResendTask(new VerificationEmailResendTask(user_id));
verificationResendTask->scheduleTask(verificationResendTask);
count_scheduled_at_once++;
scheduledResendTask |= 1;
}
// younger than 7 days, schedule for created + 7 days
else {
else if(!(scheduledResendTask & 2)) {
auto runDateTime = created + Poco::Timespan(7, 0, 0, 0, 0);
ServerConfig::g_CronJobsTimer.schedule(new VerificationEmailResendTimerTask(user_id), Poco::Timestamp(runDateTime.timestamp()));
count_scheduled++;
scheduledResendTask |= 2;
}
}
if (count_scheduled_at_once) printf("scheduled %d verification email resend at once\n", count_scheduled_at_once);

View File

@ -378,7 +378,7 @@ bool Session::createUserDirect(const std::string& first_name, const std::string&
email_verification->getModel()->insertIntoDB(false);
auto _7days_later = Poco::DateTime() + Poco::Timespan(7, 0, 0, 0, 0);
ServerConfig::g_CronJobsTimer.schedule(new VerificationEmailResendTimerTask(user_id), Poco::Timestamp(_7days_later.microsecond()));
ServerConfig::g_CronJobsTimer.schedule(new VerificationEmailResendTimerTask(user_id), Poco::Timestamp(_7days_later.timestamp()));
email_manager->addEmail(new model::Email(email_verification, mNewUser, model::EMAIL_USER_VERIFICATION_CODE));

View File

@ -33,6 +33,10 @@ int VerificationEmailResendTask::run()
email_verification = controller::EmailVerificationCode::create(mUserId, model::table::EMAIL_OPT_IN_REGISTER_DIRECT);
email_verification->getModel()->insertIntoDB(false);
}
else if (email_verification->getModel()->getResendCount() > 1) {
// if email was already send maybe by another process, we can exit
return 1;
}
else {
email_verification->getModel()->addResendCountAndUpdate();
}