2019-11-20 18:52:19 +01:00
|
|
|
"""
|
2021-11-02 15:30:18 +01:00
|
|
|
Allow user to disable an alias or block a contact via the one click unsubscribe
|
2019-11-20 18:52:19 +01:00
|
|
|
"""
|
|
|
|
|
2021-11-02 15:30:18 +01:00
|
|
|
from app.db import Session
|
|
|
|
|
|
|
|
|
2019-11-20 19:18:24 +01:00
|
|
|
from flask import redirect, url_for, flash, request, render_template
|
2019-11-20 18:52:19 +01:00
|
|
|
from flask_login import login_required, current_user
|
|
|
|
|
|
|
|
from app.dashboard.base import dashboard_bp
|
2022-07-19 17:25:21 +02:00
|
|
|
from app.handler.unsubscribe_encoder import UnsubscribeAction
|
|
|
|
from app.handler.unsubscribe_handler import UnsubscribeHandler
|
2021-11-02 15:30:18 +01:00
|
|
|
from app.models import Alias, Contact
|
2019-11-20 18:52:19 +01:00
|
|
|
|
|
|
|
|
2022-07-19 17:25:21 +02:00
|
|
|
@dashboard_bp.route("/unsubscribe/<int:alias_id>", methods=["GET", "POST"])
|
2019-11-20 18:52:19 +01:00
|
|
|
@login_required
|
2020-03-17 11:51:40 +01:00
|
|
|
def unsubscribe(alias_id):
|
|
|
|
alias = Alias.get(alias_id)
|
|
|
|
if not alias:
|
2019-11-20 18:52:19 +01:00
|
|
|
flash("Incorrect link. Redirect you to the home page", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
if alias.user_id != current_user.id:
|
2019-11-20 18:52:19 +01:00
|
|
|
flash(
|
|
|
|
"You don't have access to this page. Redirect you to the home page",
|
|
|
|
"warning",
|
|
|
|
)
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
2019-11-20 19:18:24 +01:00
|
|
|
# automatic unsubscribe, according to https://tools.ietf.org/html/rfc8058
|
|
|
|
if request.method == "POST":
|
2020-03-17 11:51:40 +01:00
|
|
|
alias.enabled = False
|
|
|
|
flash(f"Alias {alias.email} has been blocked", "success")
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2019-11-20 18:52:19 +01:00
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
return redirect(url_for("dashboard.index", highlight_alias_id=alias.id))
|
2019-11-20 19:18:24 +01:00
|
|
|
else: # ask user confirmation
|
2020-03-17 11:51:40 +01:00
|
|
|
return render_template("dashboard/unsubscribe.html", alias=alias.email)
|
2021-11-02 15:30:18 +01:00
|
|
|
|
|
|
|
|
2022-07-19 17:25:21 +02:00
|
|
|
@dashboard_bp.route("/block_contact/<int:contact_id>", methods=["GET", "POST"])
|
2021-11-02 15:30:18 +01:00
|
|
|
@login_required
|
|
|
|
def block_contact(contact_id):
|
|
|
|
contact = Contact.get(contact_id)
|
|
|
|
if not contact:
|
|
|
|
flash("Incorrect link. Redirect you to the home page", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
|
|
|
if contact.user_id != current_user.id:
|
|
|
|
flash(
|
|
|
|
"You don't have access to this page. Redirect you to the home page",
|
|
|
|
"warning",
|
|
|
|
)
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
|
|
|
# automatic unsubscribe, according to https://tools.ietf.org/html/rfc8058
|
|
|
|
if request.method == "POST":
|
|
|
|
contact.block_forward = True
|
|
|
|
flash(f"Emails sent from {contact.website_email} are now blocked", "success")
|
|
|
|
Session.commit()
|
|
|
|
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"dashboard.alias_contact_manager",
|
|
|
|
alias_id=contact.alias_id,
|
|
|
|
highlight_contact_id=contact.id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else: # ask user confirmation
|
|
|
|
return render_template("dashboard/block_contact.html", contact=contact)
|
2022-07-19 17:25:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
@dashboard_bp.route("/unsubscribe/encoded/<encoded_request>", methods=["GET"])
|
|
|
|
@login_required
|
|
|
|
def encoded_unsubscribe(encoded_request: str):
|
|
|
|
unsub_data = UnsubscribeHandler().handle_unsubscribe_from_request(
|
|
|
|
current_user, encoded_request
|
|
|
|
)
|
|
|
|
if not unsub_data:
|
2023-11-21 16:42:18 +01:00
|
|
|
flash("Invalid unsubscribe request", "error")
|
2022-07-19 17:25:21 +02:00
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
if unsub_data.action == UnsubscribeAction.DisableAlias:
|
|
|
|
alias = Alias.get(unsub_data.data)
|
|
|
|
flash(f"Alias {alias.email} has been blocked", "success")
|
|
|
|
return redirect(url_for("dashboard.index", highlight_alias_id=alias.id))
|
|
|
|
if unsub_data.action == UnsubscribeAction.DisableContact:
|
|
|
|
contact = Contact.get(unsub_data.data)
|
|
|
|
flash(f"Emails sent from {contact.website_email} are now blocked", "success")
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"dashboard.alias_contact_manager",
|
|
|
|
alias_id=contact.alias_id,
|
|
|
|
highlight_contact_id=contact.id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if unsub_data.action == UnsubscribeAction.UnsubscribeNewsletter:
|
2023-11-21 16:42:18 +01:00
|
|
|
flash("You've unsubscribed from the newsletter", "success")
|
2022-07-19 17:25:21 +02:00
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"dashboard.index",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if unsub_data.action == UnsubscribeAction.OriginalUnsubscribeMailto:
|
2023-11-21 16:42:18 +01:00
|
|
|
flash("The original unsubscribe request has been forwarded", "success")
|
2022-07-19 17:25:21 +02:00
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"dashboard.index",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return redirect(url_for("dashboard.index"))
|