2020-06-11 00:54:34 +02:00
|
|
|
from typing import Callable
|
|
|
|
|
2016-10-12 05:18:50 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2017-03-17 06:26:23 +01:00
|
|
|
from django.shortcuts import render
|
2021-04-29 00:24:17 +02:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2016-10-12 05:18:50 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from confirmation.models import Confirmation, ConfirmationKeyException, get_object_from_key
|
|
|
|
from zerver.context_processors import common_context
|
2021-09-08 15:36:08 +02:00
|
|
|
from zerver.lib.actions import do_change_user_setting
|
2019-03-15 18:48:01 +01:00
|
|
|
from zerver.lib.send_email import clear_scheduled_emails
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.models import ScheduledEmail, UserProfile
|
|
|
|
|
2016-10-12 05:18:50 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
def process_unsubscribe(
|
|
|
|
request: HttpRequest,
|
|
|
|
confirmation_key: str,
|
|
|
|
subscription_type: str,
|
|
|
|
unsubscribe_function: Callable[[UserProfile], None],
|
|
|
|
) -> HttpResponse:
|
2016-10-12 05:18:50 +02:00
|
|
|
try:
|
2017-11-01 21:16:23 +01:00
|
|
|
user_profile = get_object_from_key(confirmation_key, Confirmation.UNSUBSCRIBE)
|
2018-05-24 16:41:34 +02:00
|
|
|
except ConfirmationKeyException:
|
2021-02-12 08:20:45 +01:00
|
|
|
return render(request, "zerver/unsubscribe_link_error.html")
|
2016-10-12 05:18:50 +02:00
|
|
|
|
|
|
|
unsubscribe_function(user_profile)
|
2016-11-08 10:07:47 +01:00
|
|
|
context = common_context(user_profile)
|
2020-09-03 05:32:15 +02:00
|
|
|
context.update(subscription_type=subscription_type)
|
2021-02-12 08:20:45 +01:00
|
|
|
return render(request, "zerver/unsubscribe_success.html", context=context)
|
2016-10-12 05:18:50 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2016-10-12 05:18:50 +02:00
|
|
|
# Email unsubscribe functions. All have the function signature
|
|
|
|
# processor(user_profile).
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-10-27 02:18:49 +02:00
|
|
|
def do_missedmessage_unsubscribe(user_profile: UserProfile) -> None:
|
2021-09-08 15:36:08 +02:00
|
|
|
do_change_user_setting(
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile, "enable_offline_email_notifications", False, acting_user=user_profile
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
|
2016-10-12 05:18:50 +02:00
|
|
|
|
2017-10-27 02:18:49 +02:00
|
|
|
def do_welcome_unsubscribe(user_profile: UserProfile) -> None:
|
2021-08-14 02:54:46 +02:00
|
|
|
clear_scheduled_emails(user_profile.id, ScheduledEmail.WELCOME)
|
2016-10-12 05:18:50 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-10-27 02:18:49 +02:00
|
|
|
def do_digest_unsubscribe(user_profile: UserProfile) -> None:
|
2021-09-08 15:36:08 +02:00
|
|
|
do_change_user_setting(user_profile, "enable_digest_emails", False, acting_user=user_profile)
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2016-10-12 05:18:50 +02:00
|
|
|
|
2018-11-07 16:54:23 +01:00
|
|
|
def do_login_unsubscribe(user_profile: UserProfile) -> None:
|
2021-09-08 15:36:08 +02:00
|
|
|
do_change_user_setting(user_profile, "enable_login_emails", False, acting_user=user_profile)
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-11-07 16:54:23 +01:00
|
|
|
|
2021-04-28 00:25:27 +02:00
|
|
|
def do_marketing_unsubscribe(user_profile: UserProfile) -> None:
|
2021-09-08 15:36:08 +02:00
|
|
|
do_change_user_setting(user_profile, "enable_marketing_emails", False, acting_user=user_profile)
|
2021-04-28 00:25:27 +02:00
|
|
|
|
|
|
|
|
2016-10-12 05:18:50 +02:00
|
|
|
# The keys are part of the URL for the unsubscribe link and must be valid
|
|
|
|
# without encoding.
|
|
|
|
# The values are a tuple of (display name, unsubscribe function), where the
|
|
|
|
# display name is what we call this class of email in user-visible text.
|
|
|
|
email_unsubscribers = {
|
|
|
|
"missed_messages": ("missed messages", do_missedmessage_unsubscribe),
|
|
|
|
"welcome": ("welcome", do_welcome_unsubscribe),
|
2018-11-07 16:54:23 +01:00
|
|
|
"digest": ("digest", do_digest_unsubscribe),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
"login": ("login", do_login_unsubscribe),
|
2021-04-28 00:25:27 +02:00
|
|
|
"marketing": ("marketing", do_marketing_unsubscribe),
|
2017-01-24 06:34:26 +01:00
|
|
|
}
|
2016-10-12 05:18:50 +02:00
|
|
|
|
|
|
|
# Login NOT required. These are for one-click unsubscribes.
|
2021-04-29 00:24:17 +02:00
|
|
|
@csrf_exempt
|
2021-02-12 08:19:30 +01:00
|
|
|
def email_unsubscribe(request: HttpRequest, email_type: str, confirmation_key: str) -> HttpResponse:
|
2017-07-07 20:37:09 +02:00
|
|
|
if email_type in email_unsubscribers:
|
|
|
|
display_name, unsubscribe_function = email_unsubscribers[email_type]
|
|
|
|
return process_unsubscribe(request, confirmation_key, display_name, unsubscribe_function)
|
2016-10-12 05:18:50 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
return render(request, "zerver/unsubscribe_link_error.html")
|