timecs 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- app.py +3014 -0
- ast_scanner.py +36187 -0
- banner.py +146 -0
- ci_reporter.py +570 -0
- cli.py +2347 -0
- config_loader.py +307 -0
- cross_file_engine.py +1781 -0
- delivery_models.py +145 -0
- delivery_repository.py +78 -0
- diff_scan.py +127 -0
- durable_delivery_sink.py +103 -0
- event_bus.py +155 -0
- event_publisher.py +345 -0
- event_taxonomy.py +313 -0
- function_summarizer.py +354 -0
- html_auditor.py +274 -0
- iac_auditor.py +400 -0
- in_app_adapter.py +115 -0
- js_scanner.py +895 -0
- manifest_parser.py +960 -0
- master_rules_bank.py +389 -0
- notification_models.py +346 -0
- notification_policy.py +309 -0
- notification_repository.py +118 -0
- notification_routes.py +394 -0
- notification_service.py +199 -0
- osv_client.py +437 -0
- remediation/__init__.py +39 -0
- remediation/base.py +102 -0
- remediation/contracts.py +196 -0
- remediation/cwe1188_binding.py +78 -0
- remediation/cwe1336_jinja.py +91 -0
- remediation/cwe22_path_traversal.py +348 -0
- remediation/cwe295_ssl.py +87 -0
- remediation/cwe327_weak_hash.py +78 -0
- remediation/cwe377_tempfile.py +77 -0
- remediation/cwe489_debug.py +80 -0
- remediation/cwe502_yaml.py +89 -0
- remediation/cwe614_cookie.py +91 -0
- remediation/cwe78_cmdi.py +415 -0
- remediation/cwe798_credentials.py +153 -0
- remediation/cwe89_sqli.py +425 -0
- remediation/cwe916_password_hash.py +131 -0
- remediation/cwe95_eval.py +77 -0
- remediation/dispatcher.py +114 -0
- remediation/orchestrator.py +541 -0
- remediation/patch_engine.py +213 -0
- remediation/verification.py +253 -0
- rule_engine.py +1535 -0
- sarif_adapter.py +719 -0
- sarif_exporter.py +88 -0
- sca_reachability/__init__.py +28 -0
- sca_reachability/ast_analyzer.py +366 -0
- sca_reachability/call_graph.py +203 -0
- sca_reachability/contracts.py +365 -0
- sca_reachability/dist_import_map.py +46 -0
- sca_reachability/engine.py +603 -0
- sca_reachability/resolver.py +169 -0
- scanners/__init__.py +70 -0
- scanners/container_scanner.py +323 -0
- scanners/cve_index.py +521 -0
- scanners/iac_scanner.py +328 -0
- scanners/sbom.py +183 -0
- scanners/sca_scanner.py +667 -0
- scanners/secret_scanner.py +345 -0
- scanners/yaml_lite.py +217 -0
- secret_filters.py +435 -0
- secret_scanner.py +943 -0
- sqlite_delivery_repository.py +277 -0
- sqlite_notification_repository.py +441 -0
- ssrf_guard.py +276 -0
- staged_scanner.py +492 -0
- suppression_resolver.py +282 -0
- symbol_indexer.py +424 -0
- tcs/__init__.py +1 -0
- tcs/analysis/__init__.py +1 -0
- tcs/analysis/def_use.py +408 -0
- tcs/analysis/function_contracts.py +290 -0
- tcs/parallel_analyzer.py +276 -0
- tcs/parallel_scanner.py +630 -0
- tcs/treesitter_packer.py +1446 -0
- tcs_cli.py +2086 -0
- tcs_config.py +218 -0
- tcs_gui.py +2123 -0
- timecs-2.0.0.dist-info/METADATA +142 -0
- timecs-2.0.0.dist-info/RECORD +94 -0
- timecs-2.0.0.dist-info/WHEEL +5 -0
- timecs-2.0.0.dist-info/entry_points.txt +4 -0
- timecs-2.0.0.dist-info/licenses/LICENSE +201 -0
- timecs-2.0.0.dist-info/top_level.txt +49 -0
- version_matcher.py +934 -0
- webhook_adapter.py +495 -0
- webhook_client.py +483 -0
- webhook_formatters.py +245 -0
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
"""
|
|
2
|
+
TimeCodeSecurity SQLite/SQLAlchemy Notification Repository.
|
|
3
|
+
|
|
4
|
+
Concrete implementation of NotificationRepository using SQLAlchemy and SQLite.
|
|
5
|
+
Enforces multi-tenant isolation, SQLite pragmas (WAL mode, busy timeout, foreign keys),
|
|
6
|
+
authoritative database unique constraints, and atomic state transitions.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from contextlib import contextmanager
|
|
12
|
+
from datetime import datetime
|
|
13
|
+
from enum import Enum
|
|
14
|
+
from typing import Any, Generator, List, Optional, Union
|
|
15
|
+
|
|
16
|
+
from sqlalchemy import and_, event, func, or_
|
|
17
|
+
from sqlalchemy.engine import Engine
|
|
18
|
+
from sqlalchemy.exc import IntegrityError
|
|
19
|
+
from sqlalchemy.orm import Session, sessionmaker
|
|
20
|
+
|
|
21
|
+
from notification_models import (
|
|
22
|
+
Base,
|
|
23
|
+
Notification,
|
|
24
|
+
NotificationCreate,
|
|
25
|
+
RecipientScope,
|
|
26
|
+
)
|
|
27
|
+
from notification_repository import NotificationRepository
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def configure_sqlite_pragmas(target_engine: Engine) -> None:
|
|
31
|
+
"""
|
|
32
|
+
Configures critical SQLite pragmas:
|
|
33
|
+
- PRAGMA foreign_keys=ON: Relational integrity.
|
|
34
|
+
- PRAGMA busy_timeout=5000: Prevents 'database is locked' errors during concurrency.
|
|
35
|
+
- PRAGMA journal_mode=WAL: Concurrency optimization (readers do not block writers).
|
|
36
|
+
"""
|
|
37
|
+
if target_engine is not None and target_engine.dialect.name == "sqlite":
|
|
38
|
+
@event.listens_for(target_engine, "connect")
|
|
39
|
+
def _set_sqlite_pragmas(dbapi_connection, connection_record):
|
|
40
|
+
cursor = dbapi_connection.cursor()
|
|
41
|
+
try:
|
|
42
|
+
cursor.execute("PRAGMA foreign_keys=ON")
|
|
43
|
+
cursor.execute("PRAGMA busy_timeout=5000")
|
|
44
|
+
try:
|
|
45
|
+
cursor.execute("PRAGMA journal_mode=WAL")
|
|
46
|
+
except Exception:
|
|
47
|
+
pass # In-memory SQLite safely degrades without error
|
|
48
|
+
except Exception:
|
|
49
|
+
pass
|
|
50
|
+
finally:
|
|
51
|
+
cursor.close()
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class SqliteNotificationRepository(NotificationRepository):
|
|
55
|
+
"""
|
|
56
|
+
SQLAlchemy-backed repository implementation for SQLite.
|
|
57
|
+
Supports dependency-injected sessions, sessionmakers, or custom engines.
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
def __init__(
|
|
61
|
+
self,
|
|
62
|
+
session: Optional[Session] = None,
|
|
63
|
+
session_factory: Optional[sessionmaker] = None,
|
|
64
|
+
engine: Optional[Engine] = None,
|
|
65
|
+
) -> None:
|
|
66
|
+
self._session = session
|
|
67
|
+
self._engine = engine
|
|
68
|
+
self._session_factory = session_factory
|
|
69
|
+
|
|
70
|
+
if self._engine is not None:
|
|
71
|
+
configure_sqlite_pragmas(self._engine)
|
|
72
|
+
if self._session_factory is None and self._session is None:
|
|
73
|
+
self._session_factory = sessionmaker(
|
|
74
|
+
autocommit=False,
|
|
75
|
+
autoflush=False,
|
|
76
|
+
bind=self._engine,
|
|
77
|
+
expire_on_commit=False,
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
if self._session is None and self._session_factory is None:
|
|
81
|
+
# Fallback to existing application infrastructure
|
|
82
|
+
try:
|
|
83
|
+
from app import SessionLocal, engine as app_engine
|
|
84
|
+
self._engine = app_engine
|
|
85
|
+
self._session_factory = SessionLocal
|
|
86
|
+
if self._engine is not None:
|
|
87
|
+
configure_sqlite_pragmas(self._engine)
|
|
88
|
+
except Exception:
|
|
89
|
+
pass
|
|
90
|
+
|
|
91
|
+
@contextmanager
|
|
92
|
+
def _session_scope(self) -> Generator[Session, None, None]:
|
|
93
|
+
"""Provides a transactional session scope."""
|
|
94
|
+
if self._session is not None:
|
|
95
|
+
yield self._session
|
|
96
|
+
elif self._session_factory is not None:
|
|
97
|
+
session = self._session_factory()
|
|
98
|
+
try:
|
|
99
|
+
yield session
|
|
100
|
+
finally:
|
|
101
|
+
session.close()
|
|
102
|
+
else:
|
|
103
|
+
raise RuntimeError(
|
|
104
|
+
"No SQLAlchemy session, session_factory, or engine configured for SqliteNotificationRepository."
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
def init_db(self) -> None:
|
|
108
|
+
"""Initializes tables in the bound engine if available."""
|
|
109
|
+
if self._engine is not None:
|
|
110
|
+
Base.metadata.create_all(bind=self._engine)
|
|
111
|
+
elif self._session is not None:
|
|
112
|
+
bind = self._session.get_bind()
|
|
113
|
+
if bind is not None:
|
|
114
|
+
Base.metadata.create_all(bind=bind)
|
|
115
|
+
|
|
116
|
+
def _find_existing(
|
|
117
|
+
self,
|
|
118
|
+
session: Session,
|
|
119
|
+
org_id: int,
|
|
120
|
+
scope_target: int,
|
|
121
|
+
dedupe_key: str,
|
|
122
|
+
notif_type: str,
|
|
123
|
+
) -> Optional[Notification]:
|
|
124
|
+
"""Lookup existing record by dedupe identity."""
|
|
125
|
+
return session.query(Notification).filter(
|
|
126
|
+
Notification.organization_id == org_id,
|
|
127
|
+
Notification.scope_target == scope_target,
|
|
128
|
+
Notification.dedupe_key == dedupe_key,
|
|
129
|
+
Notification.type == notif_type,
|
|
130
|
+
).first()
|
|
131
|
+
|
|
132
|
+
def create(self, payload: Union[NotificationCreate, dict]) -> Notification:
|
|
133
|
+
"""
|
|
134
|
+
Idempotently creates a notification record.
|
|
135
|
+
On concurrent conflict (IntegrityError), rolls back and returns the winning record.
|
|
136
|
+
"""
|
|
137
|
+
if isinstance(payload, dict):
|
|
138
|
+
payload = NotificationCreate(**payload)
|
|
139
|
+
elif not isinstance(payload, NotificationCreate):
|
|
140
|
+
raise TypeError("payload must be a NotificationCreate instance or dictionary.")
|
|
141
|
+
|
|
142
|
+
scope_target = payload.scope_target
|
|
143
|
+
org_id = payload.organization_id
|
|
144
|
+
dedupe_key = payload.dedupe_key
|
|
145
|
+
notif_type = payload.type
|
|
146
|
+
|
|
147
|
+
with self._session_scope() as session:
|
|
148
|
+
# 1. Optional application-level check optimization
|
|
149
|
+
existing = self._find_existing(
|
|
150
|
+
session, org_id, scope_target, dedupe_key, notif_type
|
|
151
|
+
)
|
|
152
|
+
if existing is not None:
|
|
153
|
+
return existing
|
|
154
|
+
|
|
155
|
+
# 2. Construct record
|
|
156
|
+
record = Notification(
|
|
157
|
+
organization_id=org_id,
|
|
158
|
+
recipient_scope=payload.recipient_scope.value if isinstance(payload.recipient_scope, Enum) else str(payload.recipient_scope),
|
|
159
|
+
user_id=payload.user_id,
|
|
160
|
+
scope_target=scope_target,
|
|
161
|
+
event_id=payload.event_id,
|
|
162
|
+
scan_id=payload.scan_id,
|
|
163
|
+
dedupe_key=dedupe_key,
|
|
164
|
+
type=notif_type,
|
|
165
|
+
severity=payload.severity.value if isinstance(payload.severity, Enum) else str(payload.severity),
|
|
166
|
+
title=payload.title,
|
|
167
|
+
message=payload.message,
|
|
168
|
+
link=payload.link,
|
|
169
|
+
is_read=False,
|
|
170
|
+
read_at=None,
|
|
171
|
+
created_at=datetime.utcnow(),
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
# 3. Attempt insert/commit with race-safe rollback
|
|
175
|
+
try:
|
|
176
|
+
session.add(record)
|
|
177
|
+
session.commit()
|
|
178
|
+
session.refresh(record)
|
|
179
|
+
return record
|
|
180
|
+
except IntegrityError:
|
|
181
|
+
session.rollback()
|
|
182
|
+
winning = session.query(Notification).filter(
|
|
183
|
+
Notification.organization_id == org_id,
|
|
184
|
+
Notification.scope_target == scope_target,
|
|
185
|
+
Notification.dedupe_key == dedupe_key,
|
|
186
|
+
Notification.type == notif_type,
|
|
187
|
+
).first()
|
|
188
|
+
if winning is not None:
|
|
189
|
+
return winning
|
|
190
|
+
raise
|
|
191
|
+
except Exception:
|
|
192
|
+
session.rollback()
|
|
193
|
+
raise
|
|
194
|
+
|
|
195
|
+
def get_by_id(
|
|
196
|
+
self,
|
|
197
|
+
notification_id: int,
|
|
198
|
+
organization_id: int,
|
|
199
|
+
user_id: Optional[int] = None,
|
|
200
|
+
) -> Optional[Notification]:
|
|
201
|
+
"""Fetch notification by ID enforcing tenant and user isolation."""
|
|
202
|
+
if organization_id is None or not isinstance(organization_id, int) or organization_id <= 0:
|
|
203
|
+
raise ValueError("organization_id must be a positive integer.")
|
|
204
|
+
if user_id is not None and (not isinstance(user_id, int) or user_id <= 0):
|
|
205
|
+
raise ValueError("user_id must be a positive integer or None.")
|
|
206
|
+
|
|
207
|
+
with self._session_scope() as session:
|
|
208
|
+
query = session.query(Notification).filter(
|
|
209
|
+
Notification.id == notification_id,
|
|
210
|
+
Notification.organization_id == organization_id,
|
|
211
|
+
)
|
|
212
|
+
if user_id is not None:
|
|
213
|
+
query = query.filter(
|
|
214
|
+
or_(
|
|
215
|
+
and_(
|
|
216
|
+
Notification.recipient_scope == RecipientScope.USER.value,
|
|
217
|
+
Notification.user_id == user_id,
|
|
218
|
+
),
|
|
219
|
+
Notification.recipient_scope == RecipientScope.ORGANIZATION.value,
|
|
220
|
+
)
|
|
221
|
+
)
|
|
222
|
+
else:
|
|
223
|
+
query = query.filter(
|
|
224
|
+
Notification.recipient_scope == RecipientScope.ORGANIZATION.value
|
|
225
|
+
)
|
|
226
|
+
return query.first()
|
|
227
|
+
|
|
228
|
+
def list_notifications(
|
|
229
|
+
self,
|
|
230
|
+
organization_id: int,
|
|
231
|
+
user_id: Optional[int] = None,
|
|
232
|
+
recipient_scope: Optional[Union[RecipientScope, str]] = None,
|
|
233
|
+
unread_only: bool = False,
|
|
234
|
+
limit: int = 50,
|
|
235
|
+
offset: int = 0,
|
|
236
|
+
) -> List[Notification]:
|
|
237
|
+
"""List notifications ordered by created_at DESC with tenant isolation."""
|
|
238
|
+
if organization_id is None or not isinstance(organization_id, int) or organization_id <= 0:
|
|
239
|
+
raise ValueError("organization_id must be a positive integer.")
|
|
240
|
+
if user_id is not None and (not isinstance(user_id, int) or user_id <= 0):
|
|
241
|
+
raise ValueError("user_id must be a positive integer or None.")
|
|
242
|
+
if limit < 0 or offset < 0:
|
|
243
|
+
raise ValueError("limit and offset must be non-negative integers.")
|
|
244
|
+
|
|
245
|
+
with self._session_scope() as session:
|
|
246
|
+
query = session.query(Notification).filter(
|
|
247
|
+
Notification.organization_id == organization_id
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
if unread_only:
|
|
251
|
+
query = query.filter(Notification.is_read == False)
|
|
252
|
+
|
|
253
|
+
if recipient_scope is not None:
|
|
254
|
+
scope_val = recipient_scope.value if isinstance(recipient_scope, Enum) else str(recipient_scope)
|
|
255
|
+
if scope_val not in [s.value for s in RecipientScope]:
|
|
256
|
+
raise ValueError(f"Invalid recipient_scope '{recipient_scope}'.")
|
|
257
|
+
query = query.filter(Notification.recipient_scope == scope_val)
|
|
258
|
+
if scope_val == RecipientScope.USER.value and user_id is not None:
|
|
259
|
+
query = query.filter(Notification.user_id == user_id)
|
|
260
|
+
else:
|
|
261
|
+
if user_id is not None:
|
|
262
|
+
query = query.filter(
|
|
263
|
+
or_(
|
|
264
|
+
and_(
|
|
265
|
+
Notification.recipient_scope == RecipientScope.USER.value,
|
|
266
|
+
Notification.user_id == user_id,
|
|
267
|
+
),
|
|
268
|
+
Notification.recipient_scope == RecipientScope.ORGANIZATION.value,
|
|
269
|
+
)
|
|
270
|
+
)
|
|
271
|
+
else:
|
|
272
|
+
query = query.filter(
|
|
273
|
+
Notification.recipient_scope == RecipientScope.ORGANIZATION.value
|
|
274
|
+
)
|
|
275
|
+
|
|
276
|
+
query = query.order_by(Notification.created_at.desc(), Notification.id.desc())
|
|
277
|
+
if offset:
|
|
278
|
+
query = query.offset(offset)
|
|
279
|
+
if limit:
|
|
280
|
+
query = query.limit(limit)
|
|
281
|
+
|
|
282
|
+
return query.all()
|
|
283
|
+
|
|
284
|
+
def count_unread(
|
|
285
|
+
self,
|
|
286
|
+
organization_id: int,
|
|
287
|
+
user_id: Optional[int] = None,
|
|
288
|
+
recipient_scope: Optional[Union[RecipientScope, str]] = None,
|
|
289
|
+
) -> int:
|
|
290
|
+
"""Count unread notifications matching tenant/recipient scope."""
|
|
291
|
+
if organization_id is None or not isinstance(organization_id, int) or organization_id <= 0:
|
|
292
|
+
raise ValueError("organization_id must be a positive integer.")
|
|
293
|
+
if user_id is not None and (not isinstance(user_id, int) or user_id <= 0):
|
|
294
|
+
raise ValueError("user_id must be a positive integer or None.")
|
|
295
|
+
|
|
296
|
+
with self._session_scope() as session:
|
|
297
|
+
query = session.query(func.count(Notification.id)).filter(
|
|
298
|
+
Notification.organization_id == organization_id,
|
|
299
|
+
Notification.is_read == False,
|
|
300
|
+
)
|
|
301
|
+
if recipient_scope is not None:
|
|
302
|
+
scope_val = recipient_scope.value if isinstance(recipient_scope, Enum) else str(recipient_scope)
|
|
303
|
+
if scope_val not in [s.value for s in RecipientScope]:
|
|
304
|
+
raise ValueError(f"Invalid recipient_scope '{recipient_scope}'.")
|
|
305
|
+
query = query.filter(Notification.recipient_scope == scope_val)
|
|
306
|
+
if scope_val == RecipientScope.USER.value and user_id is not None:
|
|
307
|
+
query = query.filter(Notification.user_id == user_id)
|
|
308
|
+
else:
|
|
309
|
+
if user_id is not None:
|
|
310
|
+
query = query.filter(
|
|
311
|
+
or_(
|
|
312
|
+
and_(
|
|
313
|
+
Notification.recipient_scope == RecipientScope.USER.value,
|
|
314
|
+
Notification.user_id == user_id,
|
|
315
|
+
),
|
|
316
|
+
Notification.recipient_scope == RecipientScope.ORGANIZATION.value,
|
|
317
|
+
)
|
|
318
|
+
)
|
|
319
|
+
else:
|
|
320
|
+
query = query.filter(
|
|
321
|
+
Notification.recipient_scope == RecipientScope.ORGANIZATION.value
|
|
322
|
+
)
|
|
323
|
+
return query.scalar() or 0
|
|
324
|
+
|
|
325
|
+
def mark_read(
|
|
326
|
+
self,
|
|
327
|
+
notification_id: int,
|
|
328
|
+
organization_id: int,
|
|
329
|
+
user_id: Optional[int] = None,
|
|
330
|
+
read_at: Optional[datetime] = None,
|
|
331
|
+
) -> bool:
|
|
332
|
+
"""Marks a single notification as read, enforcing invariant and tenant boundary."""
|
|
333
|
+
if organization_id is None or not isinstance(organization_id, int) or organization_id <= 0:
|
|
334
|
+
raise ValueError("organization_id must be a positive integer.")
|
|
335
|
+
|
|
336
|
+
with self._session_scope() as session:
|
|
337
|
+
query = session.query(Notification).filter(
|
|
338
|
+
Notification.id == notification_id,
|
|
339
|
+
Notification.organization_id == organization_id,
|
|
340
|
+
)
|
|
341
|
+
if user_id is not None:
|
|
342
|
+
query = query.filter(
|
|
343
|
+
or_(
|
|
344
|
+
and_(
|
|
345
|
+
Notification.recipient_scope == RecipientScope.USER.value,
|
|
346
|
+
Notification.user_id == user_id,
|
|
347
|
+
),
|
|
348
|
+
Notification.recipient_scope == RecipientScope.ORGANIZATION.value,
|
|
349
|
+
)
|
|
350
|
+
)
|
|
351
|
+
else:
|
|
352
|
+
query = query.filter(
|
|
353
|
+
Notification.recipient_scope == RecipientScope.ORGANIZATION.value
|
|
354
|
+
)
|
|
355
|
+
|
|
356
|
+
record = query.first()
|
|
357
|
+
if record is None:
|
|
358
|
+
return False
|
|
359
|
+
|
|
360
|
+
if record.is_read:
|
|
361
|
+
return True
|
|
362
|
+
|
|
363
|
+
ts = read_at or datetime.utcnow()
|
|
364
|
+
try:
|
|
365
|
+
record.is_read = True
|
|
366
|
+
record.read_at = ts
|
|
367
|
+
session.commit()
|
|
368
|
+
return True
|
|
369
|
+
except Exception:
|
|
370
|
+
session.rollback()
|
|
371
|
+
raise
|
|
372
|
+
|
|
373
|
+
def mark_all_read(
|
|
374
|
+
self,
|
|
375
|
+
organization_id: int,
|
|
376
|
+
user_id: Optional[int] = None,
|
|
377
|
+
read_at: Optional[datetime] = None,
|
|
378
|
+
) -> int:
|
|
379
|
+
"""Marks all unread notifications matching scope as read."""
|
|
380
|
+
if organization_id is None or not isinstance(organization_id, int) or organization_id <= 0:
|
|
381
|
+
raise ValueError("organization_id must be a positive integer.")
|
|
382
|
+
|
|
383
|
+
ts = read_at or datetime.utcnow()
|
|
384
|
+
|
|
385
|
+
with self._session_scope() as session:
|
|
386
|
+
query = session.query(Notification).filter(
|
|
387
|
+
Notification.organization_id == organization_id,
|
|
388
|
+
Notification.is_read == False,
|
|
389
|
+
)
|
|
390
|
+
if user_id is not None:
|
|
391
|
+
query = query.filter(
|
|
392
|
+
or_(
|
|
393
|
+
and_(
|
|
394
|
+
Notification.recipient_scope == RecipientScope.USER.value,
|
|
395
|
+
Notification.user_id == user_id,
|
|
396
|
+
),
|
|
397
|
+
Notification.recipient_scope == RecipientScope.ORGANIZATION.value,
|
|
398
|
+
)
|
|
399
|
+
)
|
|
400
|
+
else:
|
|
401
|
+
query = query.filter(
|
|
402
|
+
Notification.recipient_scope == RecipientScope.ORGANIZATION.value
|
|
403
|
+
)
|
|
404
|
+
|
|
405
|
+
try:
|
|
406
|
+
unread_records = query.all()
|
|
407
|
+
count = len(unread_records)
|
|
408
|
+
if count > 0:
|
|
409
|
+
for rec in unread_records:
|
|
410
|
+
rec.is_read = True
|
|
411
|
+
rec.read_at = ts
|
|
412
|
+
session.commit()
|
|
413
|
+
return count
|
|
414
|
+
except Exception:
|
|
415
|
+
session.rollback()
|
|
416
|
+
raise
|
|
417
|
+
|
|
418
|
+
def exists_by_dedupe_key(
|
|
419
|
+
self,
|
|
420
|
+
dedupe_key: str,
|
|
421
|
+
organization_id: int,
|
|
422
|
+
user_id: Optional[int] = None,
|
|
423
|
+
type: Optional[str] = None,
|
|
424
|
+
) -> bool:
|
|
425
|
+
"""Check whether a notification with this dedupe identity already exists."""
|
|
426
|
+
if organization_id is None or not isinstance(organization_id, int) or organization_id <= 0:
|
|
427
|
+
raise ValueError("organization_id must be a positive integer.")
|
|
428
|
+
if not dedupe_key or not isinstance(dedupe_key, str):
|
|
429
|
+
raise ValueError("dedupe_key must be a non-empty string.")
|
|
430
|
+
|
|
431
|
+
scope_target = user_id if user_id is not None else 0
|
|
432
|
+
|
|
433
|
+
with self._session_scope() as session:
|
|
434
|
+
query = session.query(Notification.id).filter(
|
|
435
|
+
Notification.organization_id == organization_id,
|
|
436
|
+
Notification.scope_target == scope_target,
|
|
437
|
+
Notification.dedupe_key == dedupe_key,
|
|
438
|
+
)
|
|
439
|
+
if type is not None:
|
|
440
|
+
query = query.filter(Notification.type == type)
|
|
441
|
+
return query.first() is not None
|