77
88from owl .db import SCHEMA , async_session
99from owl .db .models import NotificationGroup
10- from owl .types import NotificationScope , NotificationType , ProductType , Role
10+ from owl .types import NotificationAudience , NotificationType , ProductType , Role
1111from owl .utils .dates import now
1212
1313# Mapping from ProductType to NotificationType for limit alerts
4949class NotificationIntent :
5050 """Plain data carrier for a notification to be dispatched in the background."""
5151
52- scope : NotificationScope
52+ audience : NotificationAudience
5353 event_type : NotificationType
5454 message : str
5555 actor_id : str | None = None
@@ -65,7 +65,7 @@ async def _fan_out_notifications(
6565 * ,
6666 group_id : str ,
6767 message : str ,
68- scope : NotificationScope ,
68+ audience : NotificationAudience ,
6969 organization_id : str | None = None ,
7070 project_id : str | None = None ,
7171 recipient_ids : list [str ] | None = None ,
@@ -76,27 +76,27 @@ async def _fan_out_notifications(
7676 base_vals = ":group_id, :message, '{}'::jsonb, :ts, :ts"
7777 params : dict = dict (group_id = group_id , message = message , ts = now ())
7878
79- if scope == NotificationScope .ORGANIZATION :
79+ if audience == NotificationAudience .ORGANIZATION :
8080 where = "om.organization_id = :org_id"
8181 params ["org_id" ] = organization_id
8282 if notif_admin_only :
8383 where += " AND om.role = :role"
8484 params ["role" ] = Role .ADMIN .value
8585 sql = f'INSERT INTO { base_cols } SELECT om.user_id, { base_vals } FROM { SCHEMA } ."OrgMember" om WHERE { where } '
8686
87- elif scope == NotificationScope .PROJECT :
87+ elif audience == NotificationAudience .PROJECT :
8888 where = "pm.project_id = :proj_id"
8989 params ["proj_id" ] = project_id
9090 if notif_admin_only :
9191 where += " AND pm.role = :role"
9292 params ["role" ] = Role .ADMIN .value
9393 sql = f'INSERT INTO { base_cols } SELECT pm.user_id, { base_vals } FROM { SCHEMA } ."ProjectMember" pm WHERE { where } '
9494
95- elif scope == NotificationScope .USER :
95+ elif audience == NotificationAudience .USER :
9696 params ["user_ids" ] = recipient_ids or []
9797 sql = f'INSERT INTO { base_cols } SELECT u.id, { base_vals } FROM { SCHEMA } ."User" u WHERE u.id = ANY(:user_ids)'
9898
99- elif scope == NotificationScope .SYSTEM :
99+ elif audience == NotificationAudience .SYSTEM :
100100 sql = f'INSERT INTO { base_cols } SELECT u.id, { base_vals } FROM { SCHEMA } ."User" u'
101101
102102 else :
@@ -115,7 +115,7 @@ async def dispatch_notification_intent(
115115 async with async_session () as session :
116116 if group_id is None :
117117 group = NotificationGroup (
118- scope = intent .scope ,
118+ audience = intent .audience ,
119119 event_type = intent .event_type ,
120120 organization_id = intent .organization_id ,
121121 project_id = intent .project_id ,
@@ -131,7 +131,7 @@ async def dispatch_notification_intent(
131131 session ,
132132 group_id = group_id ,
133133 message = intent .message ,
134- scope = intent .scope ,
134+ audience = intent .audience ,
135135 organization_id = intent .organization_id ,
136136 project_id = intent .project_id ,
137137 recipient_ids = intent .recipient_ids ,
@@ -142,7 +142,7 @@ async def dispatch_notification_intent(
142142 if row_count == 0 :
143143 logger .warning (
144144 f"No recipients resolved for notification { intent .event_type } "
145- f"(scope ={ intent .scope } , org={ intent .organization_id } , proj={ intent .project_id } )"
145+ f"(audience ={ intent .audience } , org={ intent .organization_id } , proj={ intent .project_id } )"
146146 )
147147 else :
148148 logger .bind (
@@ -165,7 +165,7 @@ def notify_org_invitation(
165165 role : str ,
166166) -> NotificationIntent :
167167 return NotificationIntent (
168- scope = NotificationScope .USER ,
168+ audience = NotificationAudience .USER ,
169169 event_type = NotificationType .ORG_INVITATION ,
170170 message = f"**{ actor_name } ** invited you to join organization **{ org_name } ** with role **{ role } **." ,
171171 actor_id = actor_id ,
@@ -186,7 +186,7 @@ def notify_project_invitation(
186186 role : str ,
187187) -> NotificationIntent :
188188 return NotificationIntent (
189- scope = NotificationScope .USER ,
189+ audience = NotificationAudience .USER ,
190190 event_type = NotificationType .PROJECT_INVITATION ,
191191 message = f"**{ actor_name } ** invited you to join project **{ project_name } ** with role **{ role } **." ,
192192 actor_id = actor_id ,
@@ -206,7 +206,7 @@ def notify_org_invitation_revoked(
206206 org_name : str ,
207207) -> NotificationIntent :
208208 return NotificationIntent (
209- scope = NotificationScope .USER ,
209+ audience = NotificationAudience .USER ,
210210 event_type = NotificationType .ORG_INVITATION_REVOKED ,
211211 message = f"**{ actor_name } ** revoked your invitation to organization **{ org_name } **." ,
212212 actor_id = actor_id ,
@@ -226,7 +226,7 @@ def notify_project_invitation_revoked(
226226 organization_id : str ,
227227) -> NotificationIntent :
228228 return NotificationIntent (
229- scope = NotificationScope .USER ,
229+ audience = NotificationAudience .USER ,
230230 event_type = NotificationType .PROJECT_INVITATION_REVOKED ,
231231 message = f"**{ actor_name } ** revoked your invitation to project **{ project_name } **." ,
232232 actor_id = actor_id ,
@@ -245,7 +245,7 @@ def notify_org_member_joined(
245245 subject_name : str ,
246246) -> NotificationIntent :
247247 return NotificationIntent (
248- scope = NotificationScope .ORGANIZATION ,
248+ audience = NotificationAudience .ORGANIZATION ,
249249 event_type = NotificationType .ORG_MEMBER_JOINED ,
250250 message = f"**{ subject_name } ** joined organization **{ org_name } **." ,
251251 subject_id = subject_id ,
@@ -262,7 +262,7 @@ def notify_project_member_joined(
262262 subject_name : str ,
263263) -> NotificationIntent :
264264 return NotificationIntent (
265- scope = NotificationScope .PROJECT ,
265+ audience = NotificationAudience .PROJECT ,
266266 event_type = NotificationType .PROJECT_MEMBER_JOINED ,
267267 message = f"**{ subject_name } ** joined project **{ project_name } **." ,
268268 subject_id = subject_id ,
@@ -281,7 +281,7 @@ def notify_org_role_updated(
281281 role : str ,
282282) -> NotificationIntent :
283283 return NotificationIntent (
284- scope = NotificationScope .USER ,
284+ audience = NotificationAudience .USER ,
285285 event_type = NotificationType .ORG_ROLE_UPDATED ,
286286 message = f"**{ actor_name } ** changed your role to **{ role } ** in organization **{ org_name } **." ,
287287 actor_id = actor_id ,
@@ -302,7 +302,7 @@ def notify_project_role_updated(
302302 role : str ,
303303) -> NotificationIntent :
304304 return NotificationIntent (
305- scope = NotificationScope .USER ,
305+ audience = NotificationAudience .USER ,
306306 event_type = NotificationType .PROJECT_ROLE_UPDATED ,
307307 message = f"**{ actor_name } ** changed your role to **{ role } ** in project **{ project_name } **." ,
308308 actor_id = actor_id ,
@@ -323,7 +323,7 @@ def notify_org_owner_updated(
323323 subject_name : str ,
324324) -> NotificationIntent :
325325 return NotificationIntent (
326- scope = NotificationScope .ORGANIZATION ,
326+ audience = NotificationAudience .ORGANIZATION ,
327327 event_type = NotificationType .ORG_OWNER_UPDATED ,
328328 message = f"**{ actor_name } ** transferred ownership of organization **{ org_name } ** to **{ subject_name } **." ,
329329 actor_id = actor_id ,
@@ -343,7 +343,7 @@ def notify_project_owner_updated(
343343 subject_name : str ,
344344) -> NotificationIntent :
345345 return NotificationIntent (
346- scope = NotificationScope .PROJECT ,
346+ audience = NotificationAudience .PROJECT ,
347347 event_type = NotificationType .PROJECT_OWNER_UPDATED ,
348348 message = f"**{ actor_name } ** transferred ownership of project **{ project_name } ** to **{ subject_name } **." ,
349349 actor_id = actor_id ,
@@ -365,7 +365,7 @@ def notify_quota_limit(
365365 event_type = _PRODUCT_TO_NOTIFICATION_TYPE [product_type ]
366366 label = _PRODUCT_LABEL [product_type ]
367367 return NotificationIntent (
368- scope = NotificationScope .ORGANIZATION ,
368+ audience = NotificationAudience .ORGANIZATION ,
369369 event_type = event_type ,
370370 message = f"{ label } usage has reached **{ threshold } %** of quota ({ usage :,.2f} /{ quota :,.2f} { unit } )." ,
371371 organization_id = organization_id ,
0 commit comments