human handoffs
This commit is contained in:
parent
79188e0f59
commit
36a55930f9
35 changed files with 4039 additions and 139 deletions
49
backend/app/services/reply_recipients.py
Normal file
49
backend/app/services/reply_recipients.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
from app.schemas import MailAddress
|
||||
|
||||
|
||||
def _addresses(items: list[dict] | list[MailAddress] | None) -> list[str]:
|
||||
if not items:
|
||||
return []
|
||||
values: list[str] = []
|
||||
for item in items:
|
||||
if isinstance(item, dict):
|
||||
address = item.get("address", "")
|
||||
else:
|
||||
address = item.address
|
||||
if address:
|
||||
values.append(address.lower())
|
||||
return values
|
||||
|
||||
|
||||
def reply_recipients(
|
||||
*,
|
||||
message: dict,
|
||||
viewer_email: str,
|
||||
reply_all: bool = False,
|
||||
) -> tuple[list[str], list[str]]:
|
||||
viewer = viewer_email.lower()
|
||||
own_addresses = {viewer}
|
||||
|
||||
reply_to = _addresses(message.get("reply_to"))
|
||||
sender = _addresses([message.get("sender", {})])
|
||||
to = _addresses(message.get("to") or message.get("recipients"))
|
||||
cc = _addresses(message.get("cc"))
|
||||
|
||||
primary = reply_to or sender
|
||||
primary = [address for address in primary if address not in own_addresses]
|
||||
|
||||
if not reply_all:
|
||||
return primary, []
|
||||
|
||||
recipients = []
|
||||
for address in [*primary, *to, *cc]:
|
||||
if address in own_addresses or address in recipients:
|
||||
continue
|
||||
recipients.append(address)
|
||||
|
||||
if not recipients and primary:
|
||||
recipients = primary
|
||||
|
||||
to_out = recipients[:1] if recipients else []
|
||||
cc_out = recipients[1:] if len(recipients) > 1 else []
|
||||
return to_out, cc_out
|
||||
Loading…
Add table
Add a link
Reference in a new issue