5
5
from typing import TYPE_CHECKING , Any
6
6
7
7
from django .apps import apps
8
+ from django .contrib .auth .models import AnonymousUser
8
9
from django .utils .functional import SimpleLazyObject
9
10
10
11
from sentry .db .models import Model
19
20
from sentry .notifications .notifications .base import BaseNotification
20
21
21
22
23
+ ANONYMOUS_USER_KEY = "anonymoususer"
22
24
LAZY_OBJECT_KEY = "lazyobjectrpcuser"
23
25
MODEL_KEY = "model"
24
26
@@ -50,6 +52,14 @@ def serialize_model(arg: Model, key: str | None = None) -> dict[str, Any]:
50
52
}
51
53
52
54
55
+ def serialize_anonymous_user (arg : AnonymousUser , key : str | None = None ) -> dict [str , Any ]:
56
+ return {
57
+ "type" : ANONYMOUS_USER_KEY ,
58
+ "data" : {},
59
+ "key" : key ,
60
+ }
61
+
62
+
53
63
@region_silo_function
54
64
def async_send_notification (
55
65
NotificationClass : type [BaseNotification ], * args : Any , ** kwargs : Any
@@ -70,6 +80,8 @@ def async_send_notification(
70
80
task_args .append (serialize_model (arg ))
71
81
elif isinstance (arg , SimpleLazyObject ):
72
82
task_args .append (serialize_lazy_object_user (arg ))
83
+ elif isinstance (arg , AnonymousUser ):
84
+ task_args .append (serialize_anonymous_user (arg ))
73
85
# maybe we need an explicit check if it's a primitive?
74
86
else :
75
87
task_args .append ({"type" : "other" , "value" : arg , "key" : None })
@@ -78,6 +90,8 @@ def async_send_notification(
78
90
task_args .append (serialize_model (val , key ))
79
91
elif isinstance (val , SimpleLazyObject ):
80
92
task_args .append (serialize_lazy_object_user (val , key ))
93
+ elif isinstance (val , AnonymousUser ):
94
+ task_args .append (serialize_anonymous_user (val , key ))
81
95
# maybe we need an explicit check if it's a primitive?
82
96
else :
83
97
task_args .append ({"type" : "other" , "value" : val , "key" : key })
@@ -113,6 +127,12 @@ def _send_notification(notification_class_name: str, arg_list: Iterable[Mapping[
113
127
output_kwargs [arg ["key" ]] = user
114
128
else :
115
129
output_args .append (user )
130
+ elif arg ["type" ] == ANONYMOUS_USER_KEY :
131
+ anon_user = AnonymousUser ()
132
+ if arg ["key" ]:
133
+ output_kwargs [arg ["key" ]] = anon_user
134
+ else :
135
+ output_args .append (anon_user )
116
136
elif arg ["key" ]:
117
137
output_kwargs [arg ["key" ]] = arg ["value" ]
118
138
else :
0 commit comments