From 95213b6d85215be164649f3976a76534823bd72a Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Sat, 16 May 2020 12:54:48 +0200 Subject: [PATCH 1/9] Add alias.disable_pgp column --- app/models.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/models.py b/app/models.py index b54b1d2c..4ac37205 100644 --- a/app/models.py +++ b/app/models.py @@ -654,6 +654,12 @@ class Alias(db.Model, ModelMixin): # To have the list of all mailboxes, should use AliasInfo instead _mailboxes = db.relationship("Mailbox", secondary="alias_mailbox", lazy="joined") + # If the mailbox has PGP-enabled, user can choose disable the PGP on the alias + # this is useful when some senders already support PGP + disable_pgp = db.Column( + db.Boolean, nullable=False, default=False, server_default="0" + ) + user = db.relationship(User) mailbox = db.relationship("Mailbox", lazy="joined") @@ -665,6 +671,13 @@ class Alias(db.Model, ModelMixin): return ret + def mailbox_support_pgp(self) -> bool: + """return True of one of the mailboxes support PGP""" + for mb in self.mailboxes: + if mb.pgp_finger_print: + return True + return False + @classmethod def create(cls, **kw): r = cls(**kw) From b167297808080a2315006f04fccd11c6d3674795 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Sat, 16 May 2020 12:55:21 +0200 Subject: [PATCH 2/9] Support disable_pgp in update alias endpoint --- README.md | 1 + app/api/views/alias.py | 5 +++++ tests/api/test_alias.py | 25 +++++++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/README.md b/README.md index b6ea063a..52383326 100644 --- a/README.md +++ b/README.md @@ -1043,6 +1043,7 @@ Input: - (optional) `mailbox_id` in request body - (optional) `name` in request body - (optional) `mailbox_ids` in request body: array of mailbox_id +- (optional) `disable_pgp` in request body: boolean Output: If success, return 200 diff --git a/app/api/views/alias.py b/app/api/views/alias.py index db7cb9ac..cb11f375 100644 --- a/app/api/views/alias.py +++ b/app/api/views/alias.py @@ -257,6 +257,7 @@ def update_alias(alias_id): note (optional): in body name (optional): in body mailbox_id (optional): in body + disable_pgp (optional): in body Output: 200 """ @@ -319,6 +320,10 @@ def update_alias(alias_id): alias.name = new_name changed = True + if "disable_pgp" in data: + alias.disable_pgp = data.get("disable_pgp") + changed = True + if changed: db.session.commit() diff --git a/tests/api/test_alias.py b/tests/api/test_alias.py index 9ee1217f..3d2eaf92 100644 --- a/tests/api/test_alias.py +++ b/tests/api/test_alias.py @@ -399,6 +399,31 @@ def test_update_alias_mailboxes(flask_client): assert r.status_code == 400 +def test_update_disable_pgp(flask_client): + user = User.create( + email="a@b.c", password="password", name="Test User", activated=True + ) + db.session.commit() + + # create api_key + api_key = ApiKey.create(user.id, "for test") + db.session.commit() + + alias = Alias.create_new_random(user) + db.session.commit() + assert not alias.disable_pgp + + r = flask_client.put( + url_for("api.update_alias", alias_id=alias.id), + headers={"Authentication": api_key.code}, + json={"disable_pgp": True}, + ) + + assert r.status_code == 200 + alias = Alias.get(alias.id) + assert alias.disable_pgp + + def test_alias_contacts(flask_client): user = User.create( email="a@b.c", password="password", name="Test User", activated=True From 80f614da6cf470c2c2ad646be532db5e871229cb Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Sat, 16 May 2020 12:55:53 +0200 Subject: [PATCH 3/9] refactor: remove unused var --- email_handler.py | 1 - 1 file changed, 1 deletion(-) diff --git a/email_handler.py b/email_handler.py index e356f4c6..9c4d3f00 100644 --- a/email_handler.py +++ b/email_handler.py @@ -379,7 +379,6 @@ def forward_email_to_mailbox( user, ) -> (bool, str): LOG.d("Forward %s -> %s -> %s", contact, alias, mailbox) - spam_check = True is_spam, spam_status = get_spam_info(msg) if is_spam: LOG.warning("Email detected as spam. Alias: %s, from: %s", alias, contact) From 5e6454e6de342ac8a58052d4b70e3448fa8f9119 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Sat, 16 May 2020 18:18:21 +0200 Subject: [PATCH 4/9] use a different class for alias toggle --- app/dashboard/templates/dashboard/index.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/dashboard/templates/dashboard/index.html b/app/dashboard/templates/dashboard/index.html index c90ba35c..fbb4a4f8 100644 --- a/app/dashboard/templates/dashboard/index.html +++ b/app/dashboard/templates/dashboard/index.html @@ -211,7 +211,7 @@ {% endif %} style="padding-left: 0px" > - @@ -527,7 +527,7 @@ }); - $(".custom-switch-input").change(async function (e) { + $(".enable-disable-alias").change(async function (e) { let aliasId = $(this).data("alias"); let alias = $(this).data("alias-email"); From 13bb9810b63421a56d711ae1a418f306a302981c Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Sat, 16 May 2020 18:19:47 +0200 Subject: [PATCH 5/9] use can disable PGP on an alias --- app/dashboard/templates/dashboard/index.html | 55 +++++++++++++++++++- app/models.py | 5 ++ server.py | 7 ++- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/app/dashboard/templates/dashboard/index.html b/app/dashboard/templates/dashboard/index.html index fbb4a4f8..e6b0d32a 100644 --- a/app/dashboard/templates/dashboard/index.html +++ b/app/dashboard/templates/dashboard/index.html @@ -372,10 +372,26 @@ Save - - + {% if alias.mailbox_support_pgp() %} +