2020-02-03 14:11:11 +08:00
|
|
|
"""
|
|
|
|
Run scheduled jobs.
|
|
|
|
Not meant for running job at precise time (+- 1h)
|
|
|
|
"""
|
|
|
|
import time
|
|
|
|
|
|
|
|
import arrow
|
|
|
|
|
2020-04-03 05:26:17 +08:00
|
|
|
from app.config import (
|
|
|
|
JOB_ONBOARDING_1,
|
|
|
|
JOB_ONBOARDING_2,
|
|
|
|
JOB_ONBOARDING_4,
|
2020-09-11 02:14:55 +08:00
|
|
|
JOB_BATCH_IMPORT,
|
2021-04-23 17:50:26 +08:00
|
|
|
JOB_DELETE_ACCOUNT,
|
2021-10-13 17:40:15 +08:00
|
|
|
JOB_DELETE_MAILBOX,
|
2021-10-13 17:43:44 +08:00
|
|
|
JOB_DELETE_DOMAIN,
|
2020-09-11 02:14:55 +08:00
|
|
|
)
|
2021-10-12 20:36:47 +08:00
|
|
|
from app.db import Session
|
2020-09-11 02:14:55 +08:00
|
|
|
from app.email_utils import (
|
|
|
|
send_email,
|
|
|
|
render,
|
2020-04-03 05:26:17 +08:00
|
|
|
)
|
2021-03-29 16:56:42 +08:00
|
|
|
from app.import_utils import handle_batch_import
|
2020-02-03 14:11:11 +08:00
|
|
|
from app.log import LOG
|
2021-10-13 17:43:44 +08:00
|
|
|
from app.models import User, Job, BatchImport, Mailbox, CustomDomain
|
2021-03-29 16:56:42 +08:00
|
|
|
from server import create_light_app
|
2020-02-03 14:11:11 +08:00
|
|
|
|
|
|
|
|
2020-03-25 04:01:38 +08:00
|
|
|
def onboarding_send_from_alias(user):
|
2020-10-22 16:44:05 +08:00
|
|
|
to_email, unsubscribe_link, via_email = user.get_communication_email()
|
2020-09-12 20:33:27 +08:00
|
|
|
if not to_email:
|
|
|
|
return
|
|
|
|
|
2020-02-03 14:11:11 +08:00
|
|
|
send_email(
|
2020-09-12 20:33:27 +08:00
|
|
|
to_email,
|
2020-12-06 18:25:41 +08:00
|
|
|
"SimpleLogin Tip: Send emails from your alias",
|
2021-10-11 23:08:48 +08:00
|
|
|
render("com/onboarding/send-from-alias.txt.j2", user=user, to_email=to_email),
|
2020-09-12 21:51:43 +08:00
|
|
|
render("com/onboarding/send-from-alias.html", user=user, to_email=to_email),
|
2020-10-22 16:44:05 +08:00
|
|
|
unsubscribe_link,
|
|
|
|
via_email,
|
2020-02-03 14:11:11 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-03-25 04:01:38 +08:00
|
|
|
def onboarding_pgp(user):
|
2020-10-22 16:44:05 +08:00
|
|
|
to_email, unsubscribe_link, via_email = user.get_communication_email()
|
2020-09-12 20:33:27 +08:00
|
|
|
if not to_email:
|
|
|
|
return
|
|
|
|
|
2020-03-25 04:01:38 +08:00
|
|
|
send_email(
|
2020-09-12 20:33:27 +08:00
|
|
|
to_email,
|
2020-12-06 18:25:41 +08:00
|
|
|
"SimpleLogin Tip: Secure your emails with PGP",
|
2020-09-12 21:51:43 +08:00
|
|
|
render("com/onboarding/pgp.txt", user=user, to_email=to_email),
|
|
|
|
render("com/onboarding/pgp.html", user=user, to_email=to_email),
|
2020-10-22 16:44:05 +08:00
|
|
|
unsubscribe_link,
|
|
|
|
via_email,
|
2020-03-25 04:01:38 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-04-03 05:26:17 +08:00
|
|
|
def onboarding_browser_extension(user):
|
2020-10-22 16:44:05 +08:00
|
|
|
to_email, unsubscribe_link, via_email = user.get_communication_email()
|
2020-09-12 20:33:27 +08:00
|
|
|
if not to_email:
|
|
|
|
return
|
|
|
|
|
2020-04-03 05:26:17 +08:00
|
|
|
send_email(
|
2020-09-12 20:33:27 +08:00
|
|
|
to_email,
|
2020-12-06 18:25:41 +08:00
|
|
|
"SimpleLogin Tip: Chrome/Firefox/Safari extensions and Android/iOS apps",
|
2020-09-12 21:51:43 +08:00
|
|
|
render("com/onboarding/browser-extension.txt", user=user, to_email=to_email),
|
|
|
|
render("com/onboarding/browser-extension.html", user=user, to_email=to_email),
|
2020-10-22 16:44:05 +08:00
|
|
|
unsubscribe_link,
|
|
|
|
via_email,
|
2020-04-03 05:26:17 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-03-25 04:19:45 +08:00
|
|
|
def onboarding_mailbox(user):
|
2020-10-22 16:44:05 +08:00
|
|
|
to_email, unsubscribe_link, via_email = user.get_communication_email()
|
2020-09-12 20:33:27 +08:00
|
|
|
if not to_email:
|
|
|
|
return
|
|
|
|
|
2020-03-25 04:19:45 +08:00
|
|
|
send_email(
|
2020-09-12 20:33:27 +08:00
|
|
|
to_email,
|
2020-12-06 18:25:41 +08:00
|
|
|
"SimpleLogin Tip: Multiple mailboxes",
|
2020-09-12 21:51:43 +08:00
|
|
|
render("com/onboarding/mailbox.txt", user=user, to_email=to_email),
|
|
|
|
render("com/onboarding/mailbox.html", user=user, to_email=to_email),
|
2020-10-22 16:44:05 +08:00
|
|
|
unsubscribe_link,
|
|
|
|
via_email,
|
2020-03-25 04:19:45 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-02-03 14:11:11 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
while True:
|
2021-10-26 16:52:28 +08:00
|
|
|
# wrap in an app context to benefit from app setup like database cleanup, sentry integration, etc
|
|
|
|
with create_light_app().app_context():
|
|
|
|
# run a job 1h earlier or later is not a big deal ...
|
|
|
|
min_dt = arrow.now().shift(hours=-1)
|
|
|
|
max_dt = arrow.now().shift(hours=1)
|
|
|
|
|
|
|
|
for job in Job.filter(
|
|
|
|
Job.taken.is_(False), Job.run_at > min_dt, Job.run_at <= max_dt
|
|
|
|
).all():
|
|
|
|
LOG.d("Take job %s", job)
|
|
|
|
|
|
|
|
# mark the job as taken, whether it will be executed successfully or not
|
|
|
|
job.taken = True
|
2021-10-13 17:43:44 +08:00
|
|
|
Session.commit()
|
|
|
|
|
2021-10-26 16:52:28 +08:00
|
|
|
if job.name == JOB_ONBOARDING_1:
|
|
|
|
user_id = job.payload.get("user_id")
|
|
|
|
user = User.get(user_id)
|
|
|
|
|
|
|
|
# user might delete their account in the meantime
|
|
|
|
# or disable the notification
|
|
|
|
if user and user.notification and user.activated:
|
|
|
|
LOG.d("send onboarding send-from-alias email to user %s", user)
|
|
|
|
onboarding_send_from_alias(user)
|
|
|
|
elif job.name == JOB_ONBOARDING_2:
|
|
|
|
user_id = job.payload.get("user_id")
|
|
|
|
user = User.get(user_id)
|
|
|
|
|
|
|
|
# user might delete their account in the meantime
|
|
|
|
# or disable the notification
|
|
|
|
if user and user.notification and user.activated:
|
|
|
|
LOG.d("send onboarding mailbox email to user %s", user)
|
|
|
|
onboarding_mailbox(user)
|
|
|
|
elif job.name == JOB_ONBOARDING_4:
|
|
|
|
user_id = job.payload.get("user_id")
|
|
|
|
user = User.get(user_id)
|
|
|
|
|
|
|
|
# user might delete their account in the meantime
|
|
|
|
# or disable the notification
|
|
|
|
if user and user.notification and user.activated:
|
|
|
|
LOG.d("send onboarding pgp email to user %s", user)
|
|
|
|
onboarding_pgp(user)
|
|
|
|
|
|
|
|
elif job.name == JOB_BATCH_IMPORT:
|
|
|
|
batch_import_id = job.payload.get("batch_import_id")
|
|
|
|
batch_import = BatchImport.get(batch_import_id)
|
|
|
|
handle_batch_import(batch_import)
|
|
|
|
elif job.name == JOB_DELETE_ACCOUNT:
|
|
|
|
user_id = job.payload.get("user_id")
|
|
|
|
user = User.get(user_id)
|
|
|
|
|
|
|
|
if not user:
|
|
|
|
LOG.i("No user found for %s", user_id)
|
|
|
|
continue
|
|
|
|
|
|
|
|
user_email = user.email
|
|
|
|
LOG.w("Delete user %s", user)
|
|
|
|
User.delete(user.id)
|
|
|
|
Session.commit()
|
|
|
|
|
|
|
|
send_email(
|
|
|
|
user_email,
|
|
|
|
"Your SimpleLogin account has been deleted",
|
|
|
|
render("transactional/account-delete.txt"),
|
|
|
|
render("transactional/account-delete.html"),
|
|
|
|
)
|
|
|
|
elif job.name == JOB_DELETE_MAILBOX:
|
|
|
|
mailbox_id = job.payload.get("mailbox_id")
|
|
|
|
mailbox = Mailbox.get(mailbox_id)
|
|
|
|
if not mailbox:
|
|
|
|
continue
|
|
|
|
|
|
|
|
mailbox_email = mailbox.email
|
|
|
|
user = mailbox.user
|
|
|
|
|
|
|
|
Mailbox.delete(mailbox_id)
|
|
|
|
Session.commit()
|
|
|
|
LOG.d("Mailbox %s %s deleted", mailbox_id, mailbox_email)
|
|
|
|
|
|
|
|
send_email(
|
|
|
|
user.email,
|
|
|
|
f"Your mailbox {mailbox_email} has been deleted",
|
|
|
|
f"""Mailbox {mailbox_email} along with its aliases are deleted successfully.
|
2021-11-15 18:55:22 +08:00
|
|
|
Regards,
|
|
|
|
SimpleLogin team.
|
|
|
|
""",
|
2021-10-26 16:52:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
elif job.name == JOB_DELETE_DOMAIN:
|
|
|
|
custom_domain_id = job.payload.get("custom_domain_id")
|
|
|
|
custom_domain = CustomDomain.get(custom_domain_id)
|
|
|
|
if not custom_domain:
|
|
|
|
continue
|
|
|
|
|
|
|
|
domain_name = custom_domain.domain
|
|
|
|
user = custom_domain.user
|
|
|
|
|
|
|
|
CustomDomain.delete(custom_domain.id)
|
|
|
|
Session.commit()
|
|
|
|
|
|
|
|
LOG.d("Domain %s deleted", domain_name)
|
|
|
|
|
|
|
|
send_email(
|
|
|
|
user.email,
|
|
|
|
f"Your domain {domain_name} has been deleted",
|
|
|
|
f"""Domain {domain_name} along with its aliases are deleted successfully.
|
2021-11-15 18:55:22 +08:00
|
|
|
|
|
|
|
Regards,
|
|
|
|
SimpleLogin team.
|
|
|
|
""",
|
2021-10-26 16:52:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
else:
|
|
|
|
LOG.e("Unknown job name %s", job.name)
|
|
|
|
|
|
|
|
time.sleep(10)
|