Coverage for src/couchers/notifications/notify.py: 100%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from couchers.db import session_scope
2from couchers.jobs.enqueue import queue_job
3from couchers.models import BackgroundJobType, Notification
4from couchers.notifications.utils import enum_from_topic_action
5from proto.internal import jobs_pb2
8def notify(
9 *,
10 user_id,
11 topic,
12 key,
13 action,
14 title,
15 link,
16 avatar_key=None,
17 icon=None,
18 content=None,
19):
20 """
21 Queues a notification given the notification and a target, i.e. a tuple (user_id, topic, key), and an action.
23 Notifications are sent to user identified by user_id, and are collapsed/grouped based on the combination of
24 (topic, key).
26 For example, topic may be "chat" for a group chat/direct message, and the key might be the chat id; so that messages
27 in the same group chat show up in one group.
29 The action is a simple identifier describing the action that caused the notification. For the above example, the
30 action might be "add_admin" if the notification was caused by another user adding an admin into the gorup chat.
32 Each different notification type should have its own action.
33 """
34 with session_scope() as session:
35 notification = Notification(
36 user_id=user_id,
37 topic_action=enum_from_topic_action[topic, action],
38 key=key,
39 avatar_key=avatar_key,
40 icon=icon,
41 title=title,
42 content=content,
43 link=link,
44 )
45 session.add(notification)
46 session.flush()
47 notification_id = notification.id
49 queue_job(
50 job_type=BackgroundJobType.handle_notification,
51 payload=jobs_pb2.HandleNotificationPayload(
52 notification_id=notification_id,
53 ),
54 )