|
5 | 5 | from website import settings |
6 | 6 | from enum import Enum |
7 | 7 | from osf.utils.caching import ttl_cached_property |
| 8 | +from framework import sentry |
8 | 9 |
|
9 | 10 |
|
10 | 11 | def get_default_frequency_choices(): |
@@ -222,14 +223,31 @@ def emit( |
222 | 223 | _is_digest=is_digest, |
223 | 224 | ) |
224 | 225 | else: |
225 | | - subscription, created = NotificationSubscription.objects.get_or_create( |
226 | | - notification_type=self, |
227 | | - user=user, |
228 | | - content_type=content_type, |
229 | | - object_id=subscribed_object.pk if subscribed_object else None, |
230 | | - defaults={'message_frequency': message_frequency}, |
231 | | - _is_digest=is_digest, |
232 | | - ) |
| 226 | + try: |
| 227 | + subscription, created = NotificationSubscription.objects.get_or_create( |
| 228 | + notification_type=self, |
| 229 | + user=user, |
| 230 | + content_type=content_type, |
| 231 | + object_id=subscribed_object.pk if subscribed_object else None, |
| 232 | + defaults={'message_frequency': message_frequency}, |
| 233 | + _is_digest=is_digest, |
| 234 | + ) |
| 235 | + except NotificationSubscription.MultipleObjectsReturned as e: |
| 236 | + # Temporary fix before we deduplicate and add constraints: use the last created one if duplicates found |
| 237 | + subscriptions_qs = NotificationSubscription.objects.filter( |
| 238 | + notification_type=self, |
| 239 | + user=user, |
| 240 | + content_type=content_type, |
| 241 | + object_id=subscribed_object.pk if subscribed_object else None, |
| 242 | + defaults={'message_frequency': message_frequency}, |
| 243 | + _is_digest=is_digest, |
| 244 | + ).order_by('id') |
| 245 | + sentry.log_exception(e) |
| 246 | + count = subscriptions_qs.count() |
| 247 | + subscription = subscriptions_qs.last() |
| 248 | + sentry.log_message(f'Multiple notification subscriptions found, using the last created one: ' |
| 249 | + f'[count={count}, user={user._id}, type={self.name}, last={subscription.id}]') |
| 250 | + |
233 | 251 | subscription.emit( |
234 | 252 | destination_address=destination_address, |
235 | 253 | event_context=event_context, |
|
0 commit comments