Moved all unsub logic to the encoder
This commit is contained in:
parent
f1eacb18fc
commit
ffd1bdc20c
3 changed files with 16 additions and 22 deletions
|
@ -27,9 +27,7 @@ class UnsubscribeEncoder:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def encode(action: UnsubscribeAction, data: int) -> UnsubscribeLink:
|
def encode(action: UnsubscribeAction, data: int) -> UnsubscribeLink:
|
||||||
if config.UNSUBSCRIBER:
|
if config.UNSUBSCRIBER:
|
||||||
return UnsubscribeLink(
|
return UnsubscribeLink(UnsubscribeEncoder.encode_mailto(action, data), True)
|
||||||
UnsubscribeEncoder.encode_subject(action, data), True
|
|
||||||
)
|
|
||||||
return UnsubscribeLink(UnsubscribeEncoder.encode_url(action, data), False)
|
return UnsubscribeLink(UnsubscribeEncoder.encode_url(action, data), False)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -41,6 +39,11 @@ class UnsubscribeEncoder:
|
||||||
if action == UnsubscribeAction.UnsubscribeNewsletter:
|
if action == UnsubscribeAction.UnsubscribeNewsletter:
|
||||||
return f"{data}*"
|
return f"{data}*"
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def encode_mailto(action: UnsubscribeAction, data: int) -> str:
|
||||||
|
subject = UnsubscribeEncoder.encode_subject(action, data)
|
||||||
|
return f"mailto:{config.UNSUBSCRIBER}?subject={subject}"
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def encode_url(action: UnsubscribeAction, data: int) -> str:
|
def encode_url(action: UnsubscribeAction, data: int) -> str:
|
||||||
if action == UnsubscribeAction.DisableAlias:
|
if action == UnsubscribeAction.DisableAlias:
|
||||||
|
|
|
@ -892,7 +892,9 @@ class User(Base, ModelMixin, UserMixin, PasswordOracle):
|
||||||
# use * as suffix instead of = as for alias unsubscribe
|
# use * as suffix instead of = as for alias unsubscribe
|
||||||
return (
|
return (
|
||||||
self.email,
|
self.email,
|
||||||
f"mailto:{config.UNSUBSCRIBER}?subject={self.id}*",
|
UnsubscribeEncoder.encode_mailto(
|
||||||
|
UnsubscribeAction.UnsubscribeNewsletter, self.id
|
||||||
|
),
|
||||||
True,
|
True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1480,22 +1482,6 @@ class Alias(Base, ModelMixin):
|
||||||
else:
|
else:
|
||||||
return self.user.email
|
return self.user.email
|
||||||
|
|
||||||
def unsubscribe_link(self, contact: Optional["Contact"] = None) -> (str, bool):
|
|
||||||
"""
|
|
||||||
return the unsubscribe link along with whether this is via email (mailto:) or Http POST
|
|
||||||
The mailto: method is preferred
|
|
||||||
"""
|
|
||||||
if contact:
|
|
||||||
if config.UNSUBSCRIBER:
|
|
||||||
return f"mailto:{config.UNSUBSCRIBER}?subject={contact.id}_", True
|
|
||||||
else:
|
|
||||||
return f"{config.URL}/dashboard/block_contact/{contact.id}", False
|
|
||||||
else:
|
|
||||||
if config.UNSUBSCRIBER:
|
|
||||||
return f"mailto:{config.UNSUBSCRIBER}?subject={self.id}=", True
|
|
||||||
else:
|
|
||||||
return f"{config.URL}/dashboard/unsubscribe/{self.id}", False
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"<Alias {self.id} {self.email}>"
|
return f"<Alias {self.id} {self.email}>"
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ legacy_mail_or_link_test_data = [
|
||||||
UnsubscribeData(UnsubscribeAction.DisableAlias, 3),
|
UnsubscribeData(UnsubscribeAction.DisableAlias, 3),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"9=",
|
"mailto:me@nowhere.net?subject=9=",
|
||||||
True,
|
True,
|
||||||
UnsubscribeData(UnsubscribeAction.DisableAlias, 9),
|
UnsubscribeData(UnsubscribeAction.DisableAlias, 9),
|
||||||
),
|
),
|
||||||
|
@ -59,10 +59,15 @@ legacy_mail_or_link_test_data = [
|
||||||
UnsubscribeData(UnsubscribeAction.DisableContact, 8),
|
UnsubscribeData(UnsubscribeAction.DisableContact, 8),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"8_",
|
"mailto:me@nowhere.net?subject=8_",
|
||||||
True,
|
True,
|
||||||
UnsubscribeData(UnsubscribeAction.DisableContact, 8),
|
UnsubscribeData(UnsubscribeAction.DisableContact, 8),
|
||||||
),
|
),
|
||||||
|
(
|
||||||
|
"mailto:me@nowhere.net?subject=83*",
|
||||||
|
True,
|
||||||
|
UnsubscribeData(UnsubscribeAction.UnsubscribeNewsletter, 83),
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue