-
Notifications
You must be signed in to change notification settings - Fork 52
/
notification.py
1632 lines (1383 loc) · 61.9 KB
/
notification.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Notification primitives.
Notification models and support classes for implementing notifications, best understood
using examples:
Scenario: Notification about an update
Given: User A is a participant in project P When: Project P's editor E posts an
update U Then: User A receives a notification about update U And: The notification
is attributed to editor E And: User A is informed of being a recipient for being a
participant in project P And: User A can choose to unsubscribe from notifications
about updates
How it works:
1. The view handler that creates the Update triggers an UpdateNotification on it. This
is a subclass of Notification. The UpdateNotification class specifies the roles that
must receive the notification.
2. Roles? Yes. UpdateNotification says it should be delivered to users possessing the
roles 'project_crew' and 'project_participant' on the Update object, in that order.
That means a user who is both crew and participant will only get the version meant
for crew members and won't be notified twice. Versions will have minor differences
such as in language: "the project you're a crew member of had an update", versus
"the project you're a participant of had an update".
3. The view calls `dispatch_notification` with an instance of UpdateNotification
referring to the Update instance. The dispatcher can process multiple such
notifications at once, tagging them with a common eventid. It queues a background
worker in RQ to process the notifications.
4. The background worker calls `UpdateNotification.dispatch` to find all recipients and
create `UserNotification` instances for each of them. The worker can be given
multiple notifications linked to the same event. If a user is identified as a
recipient to more than one notification, only the first match is used. To find
these recipients, the default notification-level dispatch method calls
`Update.actors_for({roles})`. The default implementation in RoleMixin is aware that
these roles are inherited from Project (using granted_via declarations), and so
it calls `Update.project.actors_for`. The obtained UserNotification instances are
batched and handed off to a second round of background workers.
5. Each second background worker receives a batch of UserNotification instances and
discovers user preferences for the particular notification. Some notifications are
defined as being for a fragment of a larger document, like for an individual
comment in a large comment thread. In such a case, a scan is performed for previous
unread instances of UserNotification referring to the same document, determined
from `UserNotification.notification.document_uuid`, and those are revoked to remove
them from the user's feed. A rollup is presented instead, showing all fragments
since the last view or last day, whichever is greater. The second background worker
now queues a third series of background workers, for each of the supported
transports if at least one recipient in that batch wants to use that transport.
6. A separate render view class named RenderNewUpdateNotification contains methods named
like `web`, `email`, `sms` and others. These are expected to return a rendered
message. The `web` render is used for the notification feed page on the website.
7. Views are registered to the model, so the dispatch mechanism only needs to call
``view.email()`` etc to get the rendered content. The dispatch mechanism then calls
the appropriate transport helper (``send_email``, etc) to do the actual sending. The
message id returned by these functions is saved to the ``messageid_*`` columns in
UserNotification, as a record that the notification was sent. If the transport
doesn't support message ids, a random non-None value is used. Accurate message ids
are only required when user interaction over the same transport is expected, such
as reply emails.
10. The ``/updates`` endpoint on the website shows a feed of UserNotification items and
handles the ability to mark each as read.
It is possible to have two separate notifications for the same event. For example, a
comment replying to another comment will trigger a CommentReplyNotification to the user
being replied to, and a ProjectCommentNotification or ProposalCommentNotification for
the project or proposal. The same user may be a recipient of both notifications. To
de-duplicate this, a random "eventid" is shared across both notifications, and is
required to be unique per user, so that the second notification will be skipped. This is
supported using an unusual primary and foreign key structure in :class:`Notification`
and :class:`UserNotification`:
1. Notification has pkey ``(eventid, id)``, where `id` is local to the instance
2. UserNotification has pkey ``(eventid, user_id)`` combined with a fkey to Notification
using ``(eventid, notification_id)``.
Notifications can also be delivered to a group chat via a hook mechanism. This is
documented in :class:`NotificationHook`.
"""
from __future__ import annotations
from dataclasses import dataclass
from datetime import datetime
from types import SimpleNamespace
from typing import (
Any,
Callable,
ClassVar,
Dict,
Generator,
Optional,
Sequence,
Set,
Tuple,
Type,
Union,
cast,
)
from uuid import UUID, uuid4
from sqlalchemy import event
from sqlalchemy.orm import column_keyed_dict
from sqlalchemy.orm.exc import NoResultFound
from werkzeug.utils import cached_property
from typing_extensions import Protocol
from baseframe import __
from coaster.sqlalchemy import (
DynamicAssociationProxy,
Query,
Registry,
SqlUuidB58Comparator,
auto_init_default,
immutable,
with_roles,
)
from coaster.utils import LabeledEnum, uuid_from_base58, uuid_to_base58
from ..typing import OptionalMigratedTables, T, UuidModelType
from . import (
BaseMixin,
Mapped,
NoIdMixin,
UuidMixin,
db,
hybrid_property,
postgresql,
sa,
)
from .helpers import reopen
from .membership_mixin import ImmutableUserMembershipMixin
from .phone_number import PhoneNumber, PhoneNumberMixin
from .user import User, UserEmail, UserPhone
__all__ = [
'SMS_STATUS',
'notification_categories',
'SmsMessage',
'NotificationType',
'Notification',
'PreviewNotification',
'NotificationPreferences',
'UserNotification',
'NotificationFor',
'notification_type_registry',
'notification_web_types',
]
# --- Registries -----------------------------------------------------------------------
#: Registry of Notification subclasses for user preferences, automatically populated.
#: Inactive types and types that shadow other types are excluded from this registry
notification_type_registry: Dict[str, Notification] = {}
#: Registry of notification types that allow web renders
notification_web_types: Set[Notification] = set()
@dataclass
class NotificationCategory:
"""Category for a notification."""
priority_id: int
title: str
available_for: Callable[[User], bool]
@dataclass
class Role:
"""User-facing name and description for a role within a specific context."""
#: Role identifier
name: str
#: Role title
title: str
#: Optional role description
description: Optional[str] = None
#: Registry of notification categories
notification_categories: SimpleNamespace = SimpleNamespace(
none=NotificationCategory(0, __("Uncategorized"), lambda user: False),
account=NotificationCategory(1, __("My account"), lambda user: True),
subscriptions=NotificationCategory(
2, __("My subscriptions and billing"), lambda user: False
),
participant=NotificationCategory(
3,
__("Projects I am participating in"),
# Criteria: User has registered or proposed
lambda user: (
db.session.query(user.rsvps.exists()).scalar() # type: ignore[has-type]
or db.session.query( # type: ignore[has-type]
user.proposal_memberships.exists()
).scalar()
),
),
project_crew=NotificationCategory(
4,
__("Projects I am a crew member in"),
# Criteria: user has ever been a project crew member
lambda user: db.session.query( # type: ignore[has-type]
user.projects_as_crew_memberships.exists()
).scalar(),
),
account_admin=NotificationCategory(
5,
__("Accounts I manage"),
# Criteria: user has ever been an organization admin
lambda user: db.session.query( # type: ignore[has-type]
user.organization_admin_memberships.exists()
).scalar(),
),
site_admin=NotificationCategory(
6,
__("As a website administrator"),
# Criteria: User has a currently active site membership
lambda user: bool(user.active_site_membership),
),
)
# --- Flags ----------------------------------------------------------------------------
class SMS_STATUS(LabeledEnum): # noqa: N801
"""SMS delivery status."""
QUEUED = (1, __("Queued"))
PENDING = (2, __("Pending"))
DELIVERED = (3, __("Delivered"))
FAILED = (4, __("Failed"))
UNKNOWN = (5, __("Unknown"))
# --- Legacy models --------------------------------------------------------------------
class SmsMessage(PhoneNumberMixin, BaseMixin, db.Model): # type: ignore[name-defined]
"""An outbound SMS message."""
__tablename__ = 'sms_message'
__allow_unmapped__ = True
__phone_optional__ = False
__phone_unique__ = False
__phone_is_exclusive__ = False
phone_number_reference_is_active: bool = False
transactionid: Mapped[Optional[str]] = immutable(
sa.orm.mapped_column(sa.UnicodeText, unique=True, nullable=True)
)
# The message itself
message: Mapped[str] = immutable(
sa.orm.mapped_column(sa.UnicodeText, nullable=False)
)
# Flags
status: Mapped[int] = sa.orm.mapped_column(
sa.Integer, default=SMS_STATUS.QUEUED, nullable=False
)
status_at: Mapped[Optional[datetime]] = sa.orm.mapped_column(
sa.TIMESTAMP(timezone=True), nullable=True
)
fail_reason: Mapped[Optional[str]] = sa.orm.mapped_column(
sa.UnicodeText, nullable=True
)
def __init__(self, **kwargs):
phone = kwargs.pop('phone', None)
if phone:
kwargs['phone_number'] = PhoneNumber.add(phone)
super().__init__(**kwargs)
# --- Notification models --------------------------------------------------------------
class NotificationType(Protocol):
"""Protocol for :class:`Notification` and :class:`PreviewNotification`."""
type: str # noqa: A003
eventid: UUID
id: UUID # noqa: A003
eventid_b58: str
document: Any
document_uuid: UUID
fragment: Any
fragment_uuid: Optional[UUID]
user_id: Optional[int]
user: Optional[User]
class Notification(NoIdMixin, db.Model): # type: ignore[name-defined]
"""
Holds a single notification for an activity on a document object.
Notifications are fanned out to recipients using :class:`UserNotification` and
may be accessed through the website and delivered over email, push notification, SMS
or other transport.
Notifications cannot hold any data and must source everything from the linked
document and fragment.
"""
__tablename__ = 'notification'
__allow_unmapped__ = True
#: Flag indicating this is an active notification type. Can be False for draft
#: and retired notification types to hide them from preferences UI
active: ClassVar[bool] = True
#: Random identifier for the event that triggered this notification. Event ids can
#: be shared across notifications, and will be used to enforce a limit of one
#: instance of a UserNotification per-event rather than per-notification
eventid: Mapped[UUID] = immutable(
sa.orm.mapped_column(
postgresql.UUID, primary_key=True, nullable=False, default=uuid4
)
)
#: Notification id
id: Mapped[UUID] = immutable( # noqa: A003
sa.orm.mapped_column(
postgresql.UUID, primary_key=True, nullable=False, default=uuid4
)
)
#: Default category of notification. Subclasses MUST override
category: ClassVar[NotificationCategory] = notification_categories.none
#: Default description for notification. Subclasses MUST override
title: ClassVar[str] = __("Unspecified notification type")
#: Default description for notification. Subclasses MUST override
description: ClassVar[str] = ''
#: Type of Notification subclass (auto-populated from subclass's `type=` parameter)
cls_type: ClassVar[str] = ''
#: Type for user preferences, in case a notification type is a shadow of
#: another type (auto-populated from subclass's `shadow=` parameter)
pref_type: ClassVar[str] = ''
#: Document model, must be specified in subclasses
document_model: ClassVar[Type[UuidModelType]]
#: SQL table name for document type, auto-populated from the document model
document_type: ClassVar[str]
#: Fragment model, optional for subclasses
fragment_model: ClassVar[Optional[Type[UuidModelType]]] = None
#: SQL table name for fragment type, auto-populated from the fragment model
fragment_type: ClassVar[Optional[str]]
#: Roles to send notifications to. Roles must be in order of priority for situations
#: where a user has more than one role on the document.
roles: ClassVar[Sequence[str]] = []
#: Roles that are typically held by multiple actors, to enable notifications sent to
#: all of them at once via the notification hook mechanism
shared_roles: ClassVar[Dict[str, Role]] = {}
#: Exclude triggering actor from receiving notifications? Subclasses may override
exclude_actor: ClassVar[bool] = False
#: If this notification is typically for a single recipient, views will need to be
#: careful about leaking out recipient identifiers such as a utm_source tracking tag
for_private_recipient: ClassVar[bool] = False
#: The preference context this notification is being served under. Users may have
#: customized preferences per account (nee profile) or project
preference_context: ClassVar[db.Model] = None # type: ignore[name-defined]
#: Notification type (identifier for subclass of :class:`NotificationType`)
type_: Mapped[str] = immutable(
sa.orm.mapped_column('type', sa.Unicode, nullable=False)
)
#: Id of user that triggered this notification
user_id: Mapped[Optional[int]] = sa.orm.mapped_column(
sa.Integer, sa.ForeignKey('user.id', ondelete='SET NULL'), nullable=True
)
#: User that triggered this notification. Optional, as not all notifications are
#: caused by user activity. Used to optionally exclude user from receiving
#: notifications of their own activity
user: Mapped[Optional[User]] = sa.orm.relationship(User)
#: UUID of document that the notification refers to
document_uuid: Mapped[UUID] = immutable(
sa.orm.mapped_column(postgresql.UUID, nullable=False, index=True)
)
#: Optional fragment within document that the notification refers to. This may be
#: the document itself, or something within it, such as a comment. Notifications for
#: multiple fragments are collapsed into a single notification
fragment_uuid: Mapped[Optional[UUID]] = immutable(
sa.orm.mapped_column(postgresql.UUID, nullable=True)
)
__table_args__ = (
# This could have been achieved with a UniqueConstraint on all three columns.
# When the third column (fragment_uuid) is null it has the same effect as the
# PostgreSQL-specific where clause. We use the clause here to make clear our
# intent of only enforcing a one-notification limit when the fragment is
# present. Hence the naming convention of `_key` suffix rather than `ix_` prefix
sa.Index(
'notification_type_document_uuid_fragment_uuid_key',
type_,
document_uuid,
fragment_uuid,
unique=True,
postgresql_where=fragment_uuid.isnot(None),
),
)
__mapper_args__ = {
# 'polymorphic_identity' from subclasses is stored in the type column
'polymorphic_on': type_,
# When querying the Notification model, cast automatically to all subclasses
'with_polymorphic': '*',
}
__roles__ = {
'all': {
'read': {'document_type', 'fragment_type'},
}
}
__datasets__ = {
'primary': {
'eventid',
'eventid_b58',
'document_type',
'fragment_type',
'document',
'fragment',
'type',
'user',
},
'related': {
'eventid',
'eventid_b58',
'document_type',
'fragment_type',
'document',
'fragment',
'type',
},
}
# Flags to control whether this notification can be delivered over a particular
# transport. Subclasses can disable these if they consider notifications unsuitable
# for particular transports.
#: This notification class may be seen on the website
allow_web: ClassVar[bool] = True
#: This notification class may be delivered by email
allow_email: ClassVar[bool] = True
#: This notification class may be delivered by SMS
allow_sms: ClassVar[bool] = True
#: This notification class may be delivered by push notification
allow_webpush: ClassVar[bool] = True
#: This notification class may be delivered by Telegram message
allow_telegram: ClassVar[bool] = True
#: This notification class may be delivered by WhatsApp message
allow_whatsapp: ClassVar[bool] = True
# Flags to set defaults for transports, in case the user has not made a choice
#: By default, turn on/off delivery by email
default_email: ClassVar[bool] = True
#: By default, turn on/off delivery by SMS
default_sms: ClassVar[bool] = True
#: By default, turn on/off delivery by push notification
default_webpush: ClassVar[bool] = True
#: By default, turn on/off delivery by Telegram message
default_telegram: ClassVar[bool] = True
#: By default, turn on/off delivery by WhatsApp message
default_whatsapp: ClassVar[bool] = True
#: Ignore transport errors? If True, an error will be ignored silently. If False,
#: an error report will be logged for the user or site administrator. TODO
ignore_transport_errors: ClassVar[bool] = False
#: Registry of per-class renderers ``{cls_type: CustomNotificationView}``
renderers: ClassVar[Dict[str, Type]] = {}
# Can't import RenderNotification from views here, so it's typed to just Type
def __init_subclass__(
cls,
type: str, # noqa: A002 # pylint: disable=redefined-builtin
shadows: Optional[Type[Notification]] = None,
**kwargs,
) -> None:
# For SQLAlchemy's polymorphic support
if '__mapper_args__' not in cls.__dict__:
cls.__mapper_args__ = {}
cls.__mapper_args__['polymorphic_identity'] = type
# For notification type identification and preference management
cls.cls_type = type
if shadows is not None:
if {'category', 'title', 'description'} & cls.__dict__.keys():
raise TypeError(
"Shadow notification types cannot have category, title or"
" description as they are not shown in UI"
)
if shadows.cls_type != shadows.pref_type:
raise TypeError(
f"{cls!r} cannot shadow {shadows!r} as it shadows yet another"
" notification type"
)
cls.pref_type = shadows.pref_type
else:
cls.pref_type = type
return super().__init_subclass__(**kwargs)
def __init__(self, document=None, fragment=None, **kwargs) -> None:
if document is not None:
if not isinstance(document, self.document_model):
raise TypeError(f"{document!r} is not of type {self.document_model!r}")
kwargs['document_uuid'] = document.uuid
if fragment is not None:
if self.fragment_model is None:
raise TypeError(f"{self.__class__} is not expecting a fragment")
if not isinstance(fragment, self.fragment_model): # pylint: disable=W1116
raise TypeError(f"{fragment!r} is not of type {self.fragment_model!r}")
kwargs['fragment_uuid'] = fragment.uuid
super().__init__(**kwargs)
@property
def identity(self) -> Tuple[UUID, UUID]:
"""Primary key of this object."""
return (self.eventid, self.id)
@hybrid_property
def eventid_b58(self) -> str:
"""URL-friendly UUID representation, using Base58 with the Bitcoin alphabet."""
return uuid_to_base58(self.eventid)
@eventid_b58.setter # type: ignore[no-redef]
def eventid_b58(self, value: str) -> None:
self.eventid = uuid_from_base58(value)
@eventid_b58.comparator # type: ignore[no-redef]
def eventid_b58(cls): # pylint: disable=no-self-argument
"""Return SQL comparator for Base58 rendering."""
return SqlUuidB58Comparator(cls.eventid)
@cached_property
def document(self) -> Optional[UuidModelType]:
"""
Retrieve the document referenced by this Notification, if any.
This assumes the underlying object won't disappear, as there is no SQL foreign
key constraint enforcing a link. This risk is mitigated in the view layer.
"""
if self.document_uuid and self.document_model:
return self.document_model.query.filter_by(uuid=self.document_uuid).one()
return None
@cached_property
def fragment(self) -> Optional[UuidModelType]:
"""
Retrieve the fragment within a document referenced by this Notification, if any.
This assumes the underlying object won't disappear, as there is no SQL foreign
key constraint enforcing a link. As with :prop:`document`, the risk of a missing
database entry is mitigated in the view layer.
"""
if self.fragment_uuid and self.fragment_model:
return self.fragment_model.query.filter_by(uuid=self.fragment_uuid).one()
return None
@classmethod
def renderer(cls, view: Type[T]) -> Type[T]:
"""
Register a view class containing render methods.
Usage in views::
from ..models import MyNotificationType
from .views import NotificationView
@MyNotificationType.renderer
class MyNotificationView(NotificationView):
...
"""
if cls.cls_type in cls.renderers:
raise TypeError(
f"A renderer has already been registered for {cls.cls_type}"
)
cls.renderers[cls.cls_type] = view
return view
@classmethod
def allow_transport(cls, transport) -> bool:
"""Return ``cls.allow_<transport>``."""
return getattr(cls, 'allow_' + transport)
@property
def role_provider_obj(self):
"""Return fragment if exists, document otherwise, indicating role provider."""
return self.fragment or self.document
@classmethod
def hook_context_models(cls) -> Sequence[db.Model]: # type: ignore[name-defined]
"""
Return models in which hooks may be located for this notification type.
See :meth:`hook_context_uuids` for an explanation.
"""
return [cls.document_model]
def hook_context_uuids(self) -> Dict[str, UUID]:
"""
Return UUIDs of documents to find notification hooks in.
Notifications are typically dispatched to a single user at a time, but it is
possible to send them to a role all at once using a notification hook. Hooks are
defined on a document or one of its parents, are set to trigger on specified
notification types, roles and contexts, and will only actually trigger if a
consenting user attached to the hook is eligible for the notification.
"""
return {self.document_type: self.document_uuid}
def dispatch(self) -> Generator[UserNotification, None, None]:
"""
Create :class:`UserNotification` instances and yield in an iterator.
This is a heavy method and must be called from a background job. It creates
instances of :class:`UserNotification` for each discovered recipient and yields
them, skipping over pre-existing instances (typically caused by a second
dispatch on the same event, such as when a background job fails midway and is
restarted).
Subclasses wanting more control over how their notifications are dispatched
should override this method.
"""
for user, role in self.role_provider_obj.actors_with(
self.roles, with_role=True
):
# If this notification requires that it not be sent to the actor that
# triggered the notification, don't notify them. For example, a user
# who leaves a comment should not be notified of their own comment.
# This `if` condition uses `user_id` instead of the recommended `user`
# for faster processing in a loop.
if (
self.exclude_actor
and self.user_id is not None
and self.user_id == user.id
):
continue
# Don't notify inactive (suspended, merged) users
if not user.state.ACTIVE:
continue
# Was a notification already sent to this user? If so:
# 1. The user has multiple roles
# 2. We're being dispatched a second time, possibly because a background
# job failed and is re-queued.
# In either case, don't notify the user a second time.
# Since this query uses SQLAlchemy's session cache, we don't have to
# bother with a local cache for the first case.
existing_notification = UserNotification.query.get((user.id, self.eventid))
if existing_notification is None:
user_notification = UserNotification(
eventid=self.eventid,
user_id=user.id,
notification_id=self.id,
role=role,
)
db.session.add(user_notification)
yield user_notification
def eligible_hooks(self): # -> Generator[NotificationHook, None, None]:
"""Find eligible hooks for this notification."""
# TODO: Find hooks, check if any of their consenting users have a matching
# UserNotification instance (ie, they are eligible), then yield them
# Make :attr:`type_` available under the name `type`, but declare this at the very
# end of the class to avoid conflicts with the Python `type` global that is
# used for type-hinting
type: Mapped[str] = sa.orm.synonym('type_') # noqa: A003
class PreviewNotification(NotificationType):
"""
Mimics a Notification subclass without instantiating it, for providing a preview.
To be used with :class:`NotificationFor`::
NotificationFor(PreviewNotification(NotificationType), user)
"""
def __init__( # pylint: disable=super-init-not-called
self,
cls: Type[Notification],
document: UuidModelType,
fragment: Optional[UuidModelType] = None,
user: Optional[User] = None,
) -> None:
self.eventid = uuid4()
self.id = uuid4()
self.eventid_b58 = uuid_to_base58(self.eventid)
self.cls = cls
self.type = cls.cls_type
self.document = document
self.document_uuid = document.uuid
self.fragment = fragment
self.fragment_uuid = fragment.uuid if fragment is not None else None
self.user = user
self.user_id = cast(int, user.id) if user is not None else None
def __getattr__(self, attr: str) -> Any:
"""Get an attribute."""
return getattr(self.cls, attr)
class UserNotificationMixin:
"""Shared mixin for :class:`UserNotification` and :class:`NotificationFor`."""
notification: Union[Notification, PreviewNotification]
@cached_property
def notification_type(self) -> str:
"""Return the notification type identifier."""
return self.notification.type
with_roles(notification_type, read={'owner'})
@cached_property
def notification_pref_type(self) -> str:
"""Return the notification preference type identifier."""
# This is dependent on SQLAlchemy using the appropriate subclass of
# :class:`Notification` so that :attr:`~Notification.pref_type` has the correct
# value
return self.notification.pref_type
with_roles(notification_pref_type, read={'owner'})
@cached_property
def document(self) -> Optional[db.Model]: # type: ignore[name-defined]
"""Document that this notification is for."""
return self.notification.document
with_roles(document, read={'owner'})
@cached_property
def fragment(self) -> Optional[db.Model]: # type: ignore[name-defined]
"""Fragment within this document that this notification is for."""
return self.notification.fragment
with_roles(fragment, read={'owner'})
# This dummy property is required because of a pending mypy issue:
# https://github.com/python/mypy/issues/4125
@property
def is_revoked(self) -> bool:
"""Test if notification has been revoked."""
raise NotImplementedError("Subclass must provide this property")
@is_revoked.setter
def is_revoked(self, value: bool) -> None:
raise NotImplementedError("Subclass must provide this property")
def is_not_deleted(self, revoke: bool = False) -> bool:
"""
Return True if the document and optional fragment are still present.
:param bool revoke: Mark the notification as revoked if document or fragment
is missing
"""
try:
return bool(self.fragment and self.document or self.document)
except NoResultFound:
pass
if revoke:
self.is_revoked = True
# Do not set self.rollupid because this is not a rollup
return False
class UserNotification(
UserNotificationMixin,
NoIdMixin,
db.Model, # type: ignore[name-defined]
):
"""
The recipient of a notification.
Contains delivery metadata and helper methods to render the notification.
"""
__tablename__ = 'user_notification'
__allow_unmapped__ = True
# Primary key is a compound of (user_id, eventid).
#: Id of user being notified
user_id: Mapped[int] = immutable(
sa.orm.mapped_column(
sa.Integer,
sa.ForeignKey('user.id', ondelete='CASCADE'),
primary_key=True,
nullable=False,
)
)
#: User being notified (backref defined below, outside the model)
user: Mapped[User] = with_roles(
sa.orm.relationship(User), read={'owner'}, grants={'owner'}
)
#: Random eventid, shared with the Notification instance
eventid: Mapped[UUID] = with_roles(
immutable(
sa.orm.mapped_column(postgresql.UUID, primary_key=True, nullable=False)
),
read={'owner'},
)
#: Id of notification that this user received (fkey in __table_args__ below)
notification_id: Mapped[UUID] = sa.orm.mapped_column(
postgresql.UUID, nullable=False
)
#: Notification that this user received
notification = with_roles(
sa.orm.relationship(
Notification, backref=sa.orm.backref('recipients', lazy='dynamic')
),
read={'owner'},
)
#: The role they held at the time of receiving the notification, used for
#: customizing the template.
#:
#: Note: This column represents the first instance of a role shifting from being an
#: entirely in-app symbol (i.e., code refactorable) to being data in the database
#: (i.e., requiring a data migration alongside a code refactor)
role: Mapped[str] = with_roles(
immutable(sa.orm.mapped_column(sa.Unicode, nullable=False)), read={'owner'}
)
#: Timestamp for when this notification was marked as read
read_at: Mapped[Optional[datetime]] = with_roles(
sa.orm.mapped_column(sa.TIMESTAMP(timezone=True), default=None, nullable=True),
read={'owner'},
)
#: Timestamp when/if the notification is revoked. This can happen if:
#: 1. The action that caused the notification has been undone (future use)
#: 2. A new notification has been raised for the same document and this user was
#: a recipient of the new notification
#: 3. The underlying document or fragment has been deleted
revoked_at: Mapped[Optional[datetime]] = with_roles(
sa.orm.mapped_column(sa.TIMESTAMP(timezone=True), nullable=True, index=True),
read={'owner'},
)
#: When a roll-up is performed, record an identifier for the items rolled up
rollupid: Mapped[Optional[UUID]] = with_roles(
sa.orm.mapped_column(postgresql.UUID, nullable=True, index=True),
read={'owner'},
)
#: Message id for email delivery
messageid_email: Mapped[Optional[str]] = sa.orm.mapped_column(
sa.Unicode, nullable=True
)
#: Message id for SMS delivery
messageid_sms: Mapped[Optional[str]] = sa.orm.mapped_column(
sa.Unicode, nullable=True
)
#: Message id for web push delivery
messageid_webpush: Mapped[Optional[str]] = sa.orm.mapped_column(
sa.Unicode, nullable=True
)
#: Message id for Telegram delivery
messageid_telegram: Mapped[Optional[str]] = sa.orm.mapped_column(
sa.Unicode, nullable=True
)
#: Message id for WhatsApp delivery
messageid_whatsapp: Mapped[Optional[str]] = sa.orm.mapped_column(
sa.Unicode, nullable=True
)
__table_args__ = (
sa.ForeignKeyConstraint(
[eventid, notification_id], # type: ignore[list-item]
[Notification.eventid, Notification.id],
ondelete='CASCADE',
name='user_notification_eventid_notification_id_fkey',
),
)
__roles__ = {'owner': {'read': {'created_at'}}}
__datasets__ = {
'primary': {
'created_at',
'eventid',
'eventid_b58',
'role',
'read_at',
'is_read',
'is_revoked',
'rollupid',
'notification_type',
},
'related': {
'created_at',
'eventid',
'eventid_b58',
'role',
'read_at',
'is_read',
'is_revoked',
'rollupid',
},
}
# --- User notification properties -------------------------------------------------
@property
def identity(self) -> Tuple[int, UUID]:
"""Primary key of this object."""
return (self.user_id, self.eventid)
@hybrid_property
def eventid_b58(self) -> str:
"""URL-friendly UUID representation, using Base58 with the Bitcoin alphabet."""
return uuid_to_base58(self.eventid)
@eventid_b58.setter # type: ignore[no-redef]
def eventid_b58(self, value: str):
self.eventid = uuid_from_base58(value)
@eventid_b58.comparator # type: ignore[no-redef]
def eventid_b58(cls): # pylint: disable=no-self-argument
"""Return SQL comparator for Base58 representation."""
return SqlUuidB58Comparator(cls.eventid)
with_roles(eventid_b58, read={'owner'})
@hybrid_property
def is_read(self) -> bool:
"""Whether this notification has been marked as read."""
return self.read_at is not None
@is_read.setter # type: ignore[no-redef]
def is_read(self, value: bool) -> None:
if value:
if not self.read_at:
self.read_at = sa.func.utcnow()
else:
self.read_at = None
@is_read.expression # type: ignore[no-redef]
def is_read(cls): # pylint: disable=no-self-argument
"""Test if notification has been marked as read, as a SQL expression."""
return cls.read_at.isnot(None)
with_roles(is_read, rw={'owner'})
@hybrid_property
def is_revoked(self) -> bool: # pylint: disable=invalid-overridden-method
"""Whether this notification has been marked as revoked."""
return self.revoked_at is not None
@is_revoked.setter # type: ignore[no-redef]
def is_revoked(self, value: bool) -> None:
if value:
if not self.revoked_at:
self.revoked_at = sa.func.utcnow()
else:
self.revoked_at = None
# PyLint complains because the hybrid property doesn't resemble the mixin's property
# pylint: disable=no-self-argument,arguments-renamed,invalid-overridden-method
@is_revoked.expression # type: ignore[no-redef]
def is_revoked(cls):
return cls.revoked_at.isnot(None)
# pylint: enable=no-self-argument,arguments-renamed,invalid-overridden-method
with_roles(is_revoked, rw={'owner'})
# --- Dispatch helper methods ------------------------------------------------------
def user_preferences(self) -> NotificationPreferences:
"""Return the user's notification preferences for this notification type."""
prefs = self.user.notification_preferences.get(self.notification_pref_type)
if prefs is None:
prefs = NotificationPreferences(
notification_type=self.notification_pref_type, user=self.user