Преглед изворни кода

send email notifying user that alias creation works only in premium plan

Son NK пре 5 година
родитељ
комит
3f0aae6f02

+ 38 - 0
app/email_utils.py

@@ -101,6 +101,44 @@ def send_test_email_alias(email, name):
     )
 
 
+def send_cannot_create_directory_alias(user, alias, directory):
+    """when user cancels their subscription, they cannot create alias on the fly.
+    If this happens, send them an email to notify
+    """
+    send_email(
+        user.email,
+        f"Alias {alias} cannot be created",
+        _render(
+            "cannot-create-alias-directory.txt",
+            name=user.name,
+            alias=alias,
+            directory=directory,
+        ),
+        _render(
+            "cannot-create-alias-directory.txt",
+            name=user.name,
+            alias=alias,
+            directory=directory,
+        ),
+    )
+
+
+def send_cannot_create_domain_alias(user, alias, domain):
+    """when user cancels their subscription, they cannot create alias on the fly with custom domain.
+    If this happens, send them an email to notify
+    """
+    send_email(
+        user.email,
+        f"Alias {alias} cannot be created",
+        _render(
+            "cannot-create-alias-domain.txt", name=user.name, alias=alias, domain=domain
+        ),
+        _render(
+            "cannot-create-alias-domain.txt", name=user.name, alias=alias, domain=domain
+        ),
+    )
+
+
 def send_email(to_email, subject, plaintext, html):
     if NOT_SEND_EMAIL:
         LOG.d(

+ 2 - 0
app/models.py

@@ -720,6 +720,8 @@ class CustomDomain(db.Model, ModelMixin):
     # an alias is created automatically the first time it receives an email
     catch_all = db.Column(db.Boolean, nullable=False, default=False, server_default="0")
 
+    user = db.relationship(User)
+
     def nb_alias(self):
         return GenEmail.filter_by(custom_domain_id=self.id).count()
 

+ 48 - 19
email_handler.py

@@ -47,6 +47,8 @@ from app.email_utils import (
     get_email_domain_part,
     add_or_replace_header,
     delete_header,
+    send_cannot_create_directory_alias,
+    send_cannot_create_domain_alias,
 )
 from app.extensions import db
 from app.log import LOG
@@ -110,7 +112,9 @@ class MailHandler:
 
         gen_email = GenEmail.get_by(email=alias)
         if not gen_email:
-            LOG.d("alias %s not exist. Try to see if it can created on the fly", alias)
+            LOG.d(
+                "alias %s not exist. Try to see if it can be created on the fly", alias
+            )
 
             # try to see if alias could be created on-the-fly
             on_the_fly = False
@@ -122,31 +126,56 @@ class MailHandler:
                     LOG.d("directory_name %s", directory_name)
 
                     directory = Directory.get_by(name=directory_name)
+
+                    # Only premium user can continue using the directory feature
                     if directory:
-                        LOG.d("create alias %s for directory %s", alias, directory)
+                        dir_user = directory.user
+                        if dir_user.is_premium():
+                            LOG.d("create alias %s for directory %s", alias, directory)
+                            on_the_fly = True
+
+                            gen_email = GenEmail.create(
+                                email=alias,
+                                user_id=directory.user_id,
+                                directory_id=directory.id,
+                            )
+                            db.session.commit()
+                        else:
+                            LOG.error(
+                                "User %s is not premium anymore and cannot create alias with directory",
+                                dir_user,
+                            )
+                            send_cannot_create_directory_alias(
+                                dir_user, alias, directory_name
+                            )
+            else:
+                # check if alias is custom-domain alias and if the custom-domain has catch-all enabled
+                alias_domain = get_email_domain_part(alias)
+                custom_domain = CustomDomain.get_by(domain=alias_domain)
+
+                # Only premium user can continue using the catch-all feature
+                if custom_domain and custom_domain.catch_all:
+                    domain_user = custom_domain.user
+                    if domain_user.is_premium():
+                        LOG.d("create alias %s for domain %s", alias, custom_domain)
                         on_the_fly = True
 
                         gen_email = GenEmail.create(
                             email=alias,
-                            user_id=directory.user_id,
-                            directory_id=directory.id,
+                            user_id=custom_domain.user_id,
+                            custom_domain_id=custom_domain.id,
+                            automatic_creation=True,
                         )
                         db.session.commit()
-            else:
-                # check if alias is custom-domain alias and if the custom-domain has catch-all enabled
-                alias_domain = get_email_domain_part(alias)
-                custom_domain = CustomDomain.get_by(domain=alias_domain)
-                if custom_domain and custom_domain.catch_all:
-                    LOG.d("create alias %s for domain %s", alias, custom_domain)
-                    on_the_fly = True
-
-                    gen_email = GenEmail.create(
-                        email=alias,
-                        user_id=custom_domain.user_id,
-                        custom_domain_id=custom_domain.id,
-                        automatic_creation=True,
-                    )
-                    db.session.commit()
+                    else:
+                        LOG.error(
+                            "User %s is not premium anymore and cannot create alias with domain %s",
+                            domain_user,
+                            alias_domain,
+                        )
+                        send_cannot_create_domain_alias(
+                            domain_user, alias, alias_domain
+                        )
 
             if not on_the_fly:
                 LOG.d("alias %s not exist, return 510", alias)

+ 7 - 0
templates/emails/cannot-create-alias-directory.txt

@@ -0,0 +1,7 @@
+Hi {{name}}
+
+An email has been sent to the alias {{alias}} that would be created automatically as you own the directory {{directory}}. However as your plan is no longer premium, this creation cannot happen.
+
+Please upgrade to premium plan in order to use this feature.
+Best,
+SimpleLogin team.

+ 7 - 0
templates/emails/cannot-create-alias-domain.txt

@@ -0,0 +1,7 @@
+Hi {{name}}
+
+An email has been sent to the alias {{alias}} that would be created automatically as you own the domain {{domain}}. However as your plan is no longer premium, this creation cannot happen.
+
+Please upgrade to premium plan in order to use this feature.
+Best,
+SimpleLogin team.