|
3 | 3 | from framework import sentry |
4 | 4 | from framework.celery_tasks import app as celery_app |
5 | 5 |
|
| 6 | +from osf.models import OSFUser |
| 7 | +from osf.models.notification_type import NotificationTypeEnum |
| 8 | + |
6 | 9 | logger = logging.getLogger(__name__) |
7 | 10 |
|
8 | 11 |
|
@@ -32,3 +35,37 @@ def merge_users(merger_guid: str, mergee_guid: str): |
32 | 35 | except Exception as exc: |
33 | 36 | logger.exception(f'Unexpected error during background user merge: merger={merger_guid}, mergee={mergee_guid}') |
34 | 37 | sentry.log_exception(exc) |
| 38 | + |
| 39 | + |
| 40 | +@celery_app.task(bind=True, name='api.users.tasks.confirm_user_ham') |
| 41 | +def confirm_user_ham(self, user_guid: str, initiator_guid: str | None = None): |
| 42 | + initiator_user = OSFUser.load(initiator_guid) if initiator_guid else None |
| 43 | + failed_ham = [] |
| 44 | + try: |
| 45 | + user = OSFUser.objects.get(guids___id=user_guid) |
| 46 | + except OSFUser.DoesNotExist as exc: |
| 47 | + sentry.log_exception(exc) |
| 48 | + return str(exc) |
| 49 | + else: |
| 50 | + try: |
| 51 | + user.is_registered = True # back in the day spam users were de-registered |
| 52 | + failed_ham = user.confirm_ham(save=True, train_spam_services=False) |
| 53 | + except Exception as exc: |
| 54 | + sentry.log_exception(exc) |
| 55 | + |
| 56 | + try: |
| 57 | + if initiator_user: |
| 58 | + NotificationTypeEnum.USER_CONFIRM_HAM_REPORT.instance.emit( |
| 59 | + user=initiator_user, |
| 60 | + message_frequency='instantly', |
| 61 | + event_context={ |
| 62 | + 'user_guid': user._id, |
| 63 | + 'failed_ham': ', '.join(failed_ham), |
| 64 | + }, |
| 65 | + save=False, |
| 66 | + ) |
| 67 | + except Exception as exc: |
| 68 | + logger.exception('Failed to send HAM confirmation report email') |
| 69 | + sentry.log_exception(exc) |
| 70 | + |
| 71 | + return True |
0 commit comments