Skip to content

Commit

Permalink
revert mailchimp signals
Browse files Browse the repository at this point in the history
  • Loading branch information
John Tordoff committed Mar 18, 2024
1 parent 098c044 commit 5163c04
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 69 deletions.
4 changes: 1 addition & 3 deletions framework/auth/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
user_registered = signals.signal('user-registered')
user_confirmed = signals.signal('user-confirmed')
user_email_removed = signals.signal('user-email-removed')
user_account_merged = signals.signal('user-account-merged')
user_account_deactivated = signals.signal('user-account-deactivated')
user_account_reactivated = signals.signal('user-account-reactivated')
user_merged = signals.signal('user-account-merged')

unconfirmed_user_created = signals.signal('unconfirmed-user-created')
user_update_mailchimp_subscription = signals.signal('user-update-mailchimp-subscription')
41 changes: 17 additions & 24 deletions osf/external/messages/celery_publishers.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,36 @@
from kombu import Exchange
from framework.celery_tasks import app as celery_app
from framework.celery_tasks.handlers import enqueue_task


def publish_deactivated_user(user):
enqueue_task(
_publish_user_status_change.s(
body={
'action': 'deactivate',
'user_uri': user.url,
},
)
_publish_user_status_change(
body={
'action': 'deactivate',
'user_uri': user.url,
},
)


def publish_reactivate_user(user):
enqueue_task(
_publish_user_status_change.s(
body={
'action': 'reactivate',
'user_uri': user.url,
},
)
_publish_user_status_change(
body={
'action': 'reactivate',
'user_uri': user.url,
},
)


def publish_merged_user(user):
enqueue_task(
_publish_user_status_change.s(
body={
'action': 'merge',
'user_uri': user.url,
'merged_user_uri': user.merged_by.url,
},
)
assert user.merged_by, 'User received merge signal, but has no `merged_by` reference.'
_publish_user_status_change(
body={
'action': 'merge',
'user_uri': user.url,
'merged_user_uri': user.merged_by.url,
},
)


@celery_app.task()
def _publish_user_status_change(body: dict):
with celery_app.producer_pool.acquire() as producer:
producer.publish(
Expand Down
16 changes: 9 additions & 7 deletions osf/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,10 +750,10 @@ def merge_user(self, user):
for key, value in user.mailchimp_mailing_lists.items():
# subscribe to each list if either user was subscribed
subscription = value or self.mailchimp_mailing_lists.get(key)
signals.user_update_mailchimp_subscription.send(self, list_name=key, subscription=subscription)
signals.user_merged.send(self, list_name=key, subscription=subscription)

# clear subscriptions for merged user
signals.user_update_mailchimp_subscription.send(user, list_name=key, subscription=False)
signals.user_merged.send(user, list_name=key, subscription=False)

for target_id, timestamp in user.comments_viewed_timestamp.items():
if not self.comments_viewed_timestamp.get(target_id):
Expand Down Expand Up @@ -872,8 +872,9 @@ def merge_user(self, user):
user.merged_by = self

user.save()
signals.user_account_merged.send(user)
signals.user_account_deactivated.send(self)
from osf.external.messages.celery_publishers import publish_deactivated_user, publish_merged_user
publish_deactivated_user(self)
publish_merged_user(self)

def _merge_users_preprints(self, user):
"""
Expand Down Expand Up @@ -988,15 +989,15 @@ def deactivate_account(self):
# Call to `unsubscribe` above saves, and can lead to stale data
self.reload()
self.is_disabled = True
self.save()
signals.user_account_deactivated.send(self)

# we must call both methods to ensure the current session is cleared and all existing
# sessions are revoked.
req = get_current_request()
if isinstance(req, FlaskRequest):
logout()
remove_sessions_for_user(self)
from osf.external.messages.celery_publishers import publish_deactivated_user
publish_deactivated_user(self)

def reactivate_account(self):
"""
Expand All @@ -1006,7 +1007,8 @@ def reactivate_account(self):
self.requested_deactivation = False
from website.mailchimp_utils import subscribe_on_confirm
subscribe_on_confirm(self)
signals.user_account_reactivated.send(self)
from osf.external.messages.celery_publishers import publish_reactivate_user
publish_reactivate_user(self)

def update_is_active(self):
"""Update ``is_active`` to be consistent with the fields that
Expand Down
4 changes: 2 additions & 2 deletions osf_tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from importlib import import_module

from framework.auth.exceptions import ExpiredTokenError, InvalidTokenError, ChangePasswordError
from framework.auth.signals import user_account_merged
from framework.auth.signals import user_merged
from framework.analytics import get_total_activity_count
from framework.exceptions import PermissionsError
from framework.celery_tasks import handlers
Expand Down Expand Up @@ -1507,7 +1507,7 @@ def test_send_user_merged_signal(self, mock_get_mailchimp_api, dupe, merge_dupe)

with capture_signals() as mock_signals:
merge_dupe()
assert mock_signals.signals_sent() == set([user_account_merged])
assert mock_signals.signals_sent() == set([user_merged])

@pytest.mark.enable_enqueue_task
@mock.patch('website.mailchimp_utils.get_mailchimp_api')
Expand Down
34 changes: 4 additions & 30 deletions website/profile/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@
from framework.auth.decorators import must_be_confirmed
from framework.auth.exceptions import ChangePasswordError
from framework.auth.views import send_confirm_email
from framework.auth.signals import (
user_account_merged,
user_account_deactivated,
user_account_reactivated,
user_update_mailchimp_subscription
)
from framework.auth.signals import user_merged

from framework.exceptions import HTTPError, PermissionsError
from framework.flask import redirect # VOL-aware redirect
from framework.status import push_status_message
Expand All @@ -41,11 +37,6 @@
from website.util import api_v2_url, web_url_for, paths
from website.util.sanitize import escape_html
from addons.base import utils as addon_utils
from osf.external.messages.celery_publishers import (
publish_reactivate_user,
publish_deactivated_user,
publish_merged_user
)

from api.waffle.utils import storage_i18n_flag_active

Expand Down Expand Up @@ -515,7 +506,8 @@ def user_choose_mailing_lists(auth, **kwargs):
all_mailing_lists.update(user.osf_mailing_lists)
return {'message': 'Successfully updated mailing lists', 'result': all_mailing_lists}, 200

@user_update_mailchimp_subscription.connect

@user_merged.connect
def update_mailchimp_subscription(user, list_name, subscription):
""" Update mailing list subscription in mailchimp.
Expand All @@ -536,24 +528,6 @@ def update_mailchimp_subscription(user, list_name, subscription):
pass


@user_account_merged.connect
def send_account_merged_message(user):
""" Sends a message using Celery messaging to alert other services that an osf.io user has been merged."""
publish_merged_user(user)


@user_account_deactivated.connect
def send_account_deactivation_message(user):
""" Sends a message using Celery messaging to alert other services that an osf.io user has been deactivated."""
publish_deactivated_user(user)


@user_account_reactivated.connect
def send_account_reactivation_message(user):
""" Sends a message using Celery messaging to alert other services that an osf.io user has been reactivated."""
publish_reactivate_user(user)


def mailchimp_get_endpoint(**kwargs):
"""Endpoint that the mailchimp webhook hits to check that the OSF is responding"""
return {}, http_status.HTTP_200_OK
Expand Down
4 changes: 1 addition & 3 deletions website/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
auth.user_confirmed,
auth.user_email_removed,
auth.user_registered,
auth.user_account_deactivated,
auth.user_account_reactivated,
auth.user_account_merged,
auth.user_merged,
auth.unconfirmed_user_created,
event.file_updated,
conference.osf4m_user_created,
Expand Down

0 comments on commit 5163c04

Please sign in to comment.