2026-07-25 21:03:46 +02:00
|
|
|
"""Public contract for the mail plugin.
|
|
|
|
|
|
|
|
|
|
Exposes only the symbols that other builtins plugins need.
|
|
|
|
|
Currently the only cross-plugin consumer is ai_proactive, which imports
|
|
|
|
|
the ``Mail`` model for querying recent emails by contact.
|
|
|
|
|
|
|
|
|
|
Importers should use::
|
|
|
|
|
|
|
|
|
|
from app.plugins.builtins.contracts import get_contract
|
|
|
|
|
mail = get_contract("mail")
|
|
|
|
|
if mail:
|
|
|
|
|
result = await db.execute(select(mail.Mail).where(...))
|
|
|
|
|
|
|
|
|
|
instead of importing from internal modules directly.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-08-27 18:09:15 +02:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
2026-07-25 21:03:46 +02:00
|
|
|
from app.plugins.builtins.contracts import get_contract_registry
|
2026-08-27 17:38:06 +02:00
|
|
|
from app.plugins.builtins.mail.models import Mail, MailAccount
|
2026-07-25 21:03:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class MailContract:
|
|
|
|
|
"""Public API surface for the mail plugin.
|
|
|
|
|
|
2026-08-27 17:38:06 +02:00
|
|
|
Exposes ORM models so that other plugins can query the mail tables
|
|
|
|
|
without importing from ``mail.models`` directly.
|
2026-07-25 21:03:46 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
contract_name = "mail"
|
|
|
|
|
|
|
|
|
|
# ─── models ───
|
|
|
|
|
Mail = Mail
|
2026-08-27 17:38:06 +02:00
|
|
|
MailAccount = MailAccount
|
2026-07-25 21:03:46 +02:00
|
|
|
|
2026-08-27 18:09:15 +02:00
|
|
|
@staticmethod
|
|
|
|
|
async def dsar_collect(
|
|
|
|
|
db: AsyncSession, tenant_id: Any, user_id: Any
|
|
|
|
|
) -> dict[str, Any]:
|
|
|
|
|
"""GDPR Art. 15: collect mail account data owned by the user."""
|
|
|
|
|
mail_accounts = (
|
|
|
|
|
await db.execute(
|
|
|
|
|
select(MailAccount).where(
|
|
|
|
|
MailAccount.tenant_id == tenant_id,
|
|
|
|
|
MailAccount.user_id == user_id,
|
|
|
|
|
).limit(500)
|
|
|
|
|
)
|
|
|
|
|
).scalars().all()
|
|
|
|
|
return {
|
|
|
|
|
"mail_accounts": [
|
|
|
|
|
{
|
|
|
|
|
"id": str(a.id),
|
|
|
|
|
"email_address": a.email_address,
|
|
|
|
|
"display_name": a.display_name,
|
|
|
|
|
"is_shared": a.is_shared,
|
|
|
|
|
"is_active": a.is_active,
|
|
|
|
|
}
|
|
|
|
|
for a in mail_accounts
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-23 12:50:53 +02:00
|
|
|
@classmethod
|
|
|
|
|
def get_function(cls, name: str):
|
|
|
|
|
"""Return a callable exposed by this contract, or None if absent."""
|
|
|
|
|
return getattr(cls, name, None)
|
|
|
|
|
|
2026-07-25 21:03:46 +02:00
|
|
|
|
|
|
|
|
# ─── self-registration ───
|
|
|
|
|
|
|
|
|
|
_contract = MailContract()
|
|
|
|
|
get_contract_registry().register("mail", _contract)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
|
"MailContract",
|
|
|
|
|
"Mail",
|
|
|
|
|
]
|