This commit is contained in:
@@ -13,7 +13,8 @@ from django.utils import timezone
|
||||
from apps.jobs.services.normalization import normalize_extracted_job
|
||||
from apps.jobs.services.pipeline import persist_draft
|
||||
from apps.sources.adapters.email_alert import EmailAlertAdapter
|
||||
from apps.sources.models import EmailMessageRecord, RawDocument, Source
|
||||
from apps.sources.models import EmailMessageRecord, MailboxConnection, RawDocument, Source
|
||||
from apps.sources.platforms import platform_label
|
||||
|
||||
|
||||
def message_identity(raw_message: bytes) -> str:
|
||||
@@ -24,22 +25,32 @@ def message_identity(raw_message: bytes) -> str:
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def ingest_email(raw_message: bytes, *, mailbox: str = "INBOX") -> EmailMessageRecord:
|
||||
def ingest_email(
|
||||
raw_message: bytes,
|
||||
*,
|
||||
mailbox: str = "INBOX",
|
||||
mailbox_connection: MailboxConnection | None = None,
|
||||
platform: str | None = None,
|
||||
) -> EmailMessageRecord:
|
||||
parsed = BytesParser(policy=policy.default).parsebytes(raw_message)
|
||||
message_id = message_identity(raw_message)
|
||||
existing = EmailMessageRecord.objects.filter(message_id=message_id).first()
|
||||
existing = EmailMessageRecord.objects.filter(
|
||||
message_id=message_id, mailbox_connection=mailbox_connection
|
||||
).first()
|
||||
if existing:
|
||||
return existing
|
||||
|
||||
source_domain = f"{platform}.mailbox.local" if platform else "mailbox.local"
|
||||
source_name = f"{platform_label(platform)} vacaturemailbox" if platform else "Vacaturemailbox"
|
||||
source, _ = Source.objects.get_or_create(
|
||||
domain="mailbox.local",
|
||||
domain=source_domain,
|
||||
source_type=Source.Type.EMAIL,
|
||||
defaults={
|
||||
"name": "Vacaturemailbox",
|
||||
"name": source_name,
|
||||
"status": Source.Status.ACTIVE,
|
||||
"policy": Source.Policy.ALLOW,
|
||||
"parser_key": "email-alert",
|
||||
"crawl_interval_minutes": 10,
|
||||
"crawl_interval_minutes": 1440,
|
||||
},
|
||||
)
|
||||
content_hash = hashlib.sha256(raw_message).hexdigest()
|
||||
@@ -52,7 +63,11 @@ def ingest_email(raw_message: bytes, *, mailbox: str = "INBOX") -> EmailMessageR
|
||||
body_text=raw_message.decode("utf-8", errors="replace"),
|
||||
byte_length=len(raw_message),
|
||||
retain_until=retain_until,
|
||||
metadata={"mailbox": mailbox},
|
||||
metadata={
|
||||
"mailbox": mailbox,
|
||||
"mailbox_connection_id": mailbox_connection.pk if mailbox_connection else None,
|
||||
"alert_provider": platform or "unknown",
|
||||
},
|
||||
)
|
||||
received_at = None
|
||||
if parsed.get("date"):
|
||||
@@ -64,12 +79,28 @@ def ingest_email(raw_message: bytes, *, mailbox: str = "INBOX") -> EmailMessageR
|
||||
received_at = None
|
||||
|
||||
adapter = EmailAlertAdapter()
|
||||
result = adapter.extract_message(raw_message)
|
||||
result = adapter.extract_message(raw_message, expected_provider=platform)
|
||||
detected_provider = next(
|
||||
(
|
||||
str(extracted.raw.get("alert_provider"))
|
||||
for extracted in result.jobs
|
||||
if extracted.raw.get("alert_provider")
|
||||
),
|
||||
"unknown",
|
||||
)
|
||||
alert_provider = platform or detected_provider
|
||||
document.parser_key = result.parser_key
|
||||
document.parser_version = result.parser_version
|
||||
document.extraction_confidence = result.confidence
|
||||
document.metadata = {**document.metadata, "alert_provider": alert_provider}
|
||||
document.save(
|
||||
update_fields=["parser_key", "parser_version", "extraction_confidence", "updated_at"]
|
||||
update_fields=[
|
||||
"parser_key",
|
||||
"parser_version",
|
||||
"extraction_confidence",
|
||||
"metadata",
|
||||
"updated_at",
|
||||
]
|
||||
)
|
||||
links: list[str] = []
|
||||
errors: list[str] = []
|
||||
@@ -87,6 +118,7 @@ def ingest_email(raw_message: bytes, *, mailbox: str = "INBOX") -> EmailMessageR
|
||||
except Exception as exc:
|
||||
errors.append(f"{exc.__class__.__name__}: {exc}")
|
||||
return EmailMessageRecord.objects.create(
|
||||
mailbox_connection=mailbox_connection,
|
||||
message_id=message_id,
|
||||
mailbox=mailbox,
|
||||
sender=str(parsed.get("from") or "")[:500],
|
||||
|
||||
Reference in New Issue
Block a user