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
|
2016-10-12 05:18:50 +02:00
|
|
|
from typing import Callable
|
|
|
|
|
2017-11-01 21:16:23 +01:00
|
|
|
from confirmation.models import Confirmation, get_object_from_key, \
|
2019-02-02 23:53:22 +01:00
|
|
|
ConfirmationKeyException
|
2019-03-15 18:48:01 +01:00
|
|
|
from zerver.lib.actions import do_change_notification_settings
|
|
|
|
from zerver.lib.send_email import clear_scheduled_emails
|
2017-07-12 03:09:54 +02:00
|
|
|
from zerver.models import UserProfile, ScheduledEmail
|
2016-11-08 10:07:47 +01:00
|
|
|
from zerver.context_processors import common_context
|
2016-10-12 05:18:50 +02:00
|
|
|
|
2017-11-01 21:16:23 +01:00
|
|
|
def process_unsubscribe(request: HttpRequest, confirmation_key: str, subscription_type: str,
|
2017-10-27 02:18:49 +02:00
|
|
|
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:
|
2017-11-07 20:29:37 +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)
|
|
|
|
context.update({"subscription_type": subscription_type})
|
2017-03-17 06:26:23 +01:00
|
|
|
return render(request, 'zerver/unsubscribe_success.html', context=context)
|
2016-10-12 05:18:50 +02:00
|
|
|
|
|
|
|
# Email unsubscribe functions. All have the function signature
|
|
|
|
# processor(user_profile).
|
|
|
|
|
2017-10-27 02:18:49 +02:00
|
|
|
def do_missedmessage_unsubscribe(user_profile: UserProfile) -> None:
|
2017-05-23 03:19:21 +02:00
|
|
|
do_change_notification_settings(user_profile, 'enable_offline_email_notifications', False)
|
2016-10-12 05:18:50 +02:00
|
|
|
|
2017-10-27 02:18:49 +02:00
|
|
|
def do_welcome_unsubscribe(user_profile: UserProfile) -> None:
|
2019-01-04 01:50:21 +01:00
|
|
|
clear_scheduled_emails([user_profile.id], ScheduledEmail.WELCOME)
|
2016-10-12 05:18:50 +02:00
|
|
|
|
2017-10-27 02:18:49 +02:00
|
|
|
def do_digest_unsubscribe(user_profile: UserProfile) -> None:
|
2017-05-23 03:19:21 +02:00
|
|
|
do_change_notification_settings(user_profile, 'enable_digest_emails', False)
|
2016-10-12 05:18:50 +02:00
|
|
|
|
2018-11-07 16:54:23 +01:00
|
|
|
def do_login_unsubscribe(user_profile: UserProfile) -> None:
|
|
|
|
do_change_notification_settings(user_profile, 'enable_login_emails', False)
|
|
|
|
|
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),
|
|
|
|
"login": ("login", do_login_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.
|
2017-10-27 02:18:49 +02: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
|
|
|
|
2017-03-17 06:26:23 +01:00
|
|
|
return render(request, 'zerver/unsubscribe_link_error.html')
|