2016-10-12 05:18:50 +02:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
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
|
|
|
|
|
|
|
|
from confirmation.models import Confirmation
|
2017-07-12 03:17:17 +02:00
|
|
|
from zerver.lib.actions import do_change_notification_settings, 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-03-17 06:24:20 +01:00
|
|
|
def process_unsubscribe(request, token, subscription_type, unsubscribe_function):
|
2017-03-17 06:26:23 +01:00
|
|
|
# type: (HttpRequest, str, str, Callable[[UserProfile], None]) -> HttpResponse
|
2016-10-12 05:18:50 +02:00
|
|
|
try:
|
|
|
|
confirmation = Confirmation.objects.get(confirmation_key=token)
|
|
|
|
except Confirmation.DoesNotExist:
|
2017-03-17 06:26:23 +01:00
|
|
|
return render(request, 'zerver/unsubscribe_link_error.html')
|
2016-10-12 05:18:50 +02:00
|
|
|
|
|
|
|
user_profile = confirmation.content_object
|
|
|
|
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).
|
|
|
|
|
|
|
|
def do_missedmessage_unsubscribe(user_profile):
|
|
|
|
# type: (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
|
|
|
|
|
|
|
def do_welcome_unsubscribe(user_profile):
|
|
|
|
# type: (UserProfile) -> None
|
2017-07-12 03:17:17 +02:00
|
|
|
clear_scheduled_emails(user_profile.id, ScheduledEmail.WELCOME)
|
2016-10-12 05:18:50 +02:00
|
|
|
|
|
|
|
def do_digest_unsubscribe(user_profile):
|
|
|
|
# type: (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
|
|
|
|
|
|
|
# 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),
|
|
|
|
"digest": ("digest", do_digest_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-07-07 20:37:09 +02:00
|
|
|
def email_unsubscribe(request, email_type, confirmation_key):
|
2016-10-12 05:18:50 +02:00
|
|
|
# type: (HttpRequest, str, 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')
|