Skip to content

Commit

Permalink
refactor: remove Mail Server Sync History
Browse files Browse the repository at this point in the history
  • Loading branch information
s-aga-r committed Nov 5, 2024
1 parent 60c486e commit db3d9ef
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 237 deletions.
68 changes: 13 additions & 55 deletions mail_server/api/inbound.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,33 @@
from datetime import datetime
from typing import TYPE_CHECKING

import frappe
import pytz
from frappe import _
from frappe.utils import convert_utc_to_system_timezone, now

from mail_server.mail_server.doctype.mail_server_sync_history.mail_server_sync_history import (
get_mail_server_sync_history,
)
from mail_server.utils.validation import (
is_domain_registry_exists,
validate_user_has_domain_owner_role,
validate_user_is_domain_owner,
)

if TYPE_CHECKING:
from mail_server.mail_server.doctype.mail_server_sync_history.mail_server_sync_history import (
MailServerSyncHistory,
)


from mail_server.utils import convert_to_utc
from mail_server.utils.cache import get_user_owned_domains
from mail_server.utils.validation import validate_user_has_domain_owner_role


@frappe.whitelist(methods=["GET"])
def fetch(
domain_name: str, limit: int = 100, last_synced_at: str | None = None
) -> dict[str, list[dict] | str]:
"""Returns the incoming mails for the given domain."""
def fetch(limit: int = 100, last_synced_at: str | None = None) -> dict[str, list[dict] | str]:
"""Returns the incoming mails for the given domains."""

user = frappe.session.user
validate_user_has_domain_owner_role(user)
is_domain_registry_exists(domain_name, raise_exception=True)
validate_user_is_domain_owner(user, domain_name)
mail_domains = get_user_owned_domains(user)

if not mail_domains:
frappe.throw(_("User {0} does not associated with any domain.").format(user))

source = get_source()
last_synced_at = convert_to_system_timezone(last_synced_at)
sync_history = get_mail_server_sync_history(source, frappe.session.user, domain_name)
result = get_incoming_mails(domain_name, limit, last_synced_at or sync_history.last_synced_at)
update_mail_server_sync_history(sync_history, result["last_synced_at"], result["last_synced_mail"])
result = get_incoming_mails(mail_domains, limit, last_synced_at)
result["last_synced_at"] = convert_to_utc(result["last_synced_at"])

return result


def get_source() -> str:
"""Returns the source of the request."""

return frappe.request.headers.get("X-Frappe-Mail-Site") or frappe.local.request_ip


def convert_to_system_timezone(last_synced_at: str) -> datetime | None:
"""Converts the last_synced_at to system timezone."""

Expand All @@ -61,11 +38,11 @@ def convert_to_system_timezone(last_synced_at: str) -> datetime | None:


def get_incoming_mails(
domain_name: str,
mail_domains: list[str],
limit: int,
last_synced_at: str | None = None,
last_synced_at: str | datetime | None = None,
) -> dict[str, list[dict] | str]:
"""Returns the incoming mails for the given domain."""
"""Returns the incoming mails for the given domains."""

IML = frappe.qb.DocType("Incoming Mail Log")
query = (
Expand All @@ -76,7 +53,7 @@ def get_incoming_mails(
IML.is_spam,
IML.message,
)
.where((IML.is_rejected == 0) & (IML.status == "Accepted") & (IML.domain_name == domain_name))
.where((IML.is_rejected == 0) & (IML.status == "Accepted") & (IML.domain_name.isin(mail_domains)))
.orderby(IML.processed_at)
.limit(limit)
)
Expand All @@ -86,27 +63,8 @@ def get_incoming_mails(

mails = query.run(as_dict=True)
last_synced_at = mails[-1].processed_at if mails else now()
last_synced_mail = mails[-1].oml if mails else None

return {
"mails": mails,
"last_synced_at": last_synced_at,
"last_synced_mail": last_synced_mail,
}


def update_mail_server_sync_history(
sync_history: "MailServerSyncHistory",
last_synced_at: str,
last_synced_mail: str | None = None,
) -> None:
"""Update the last_synced_at in the Mail Server Sync History."""

kwargs = {
"last_synced_at": last_synced_at or now(),
}

if last_synced_mail:
kwargs["last_synced_mail"] = last_synced_mail

sync_history._db_set(**kwargs, commit=True)
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def deliver_email_to_mail_client(self):
"is_spam": self.is_spam,
"message": self.message,
"domain_name": self.domain_name,
"processed_at": self.processed_at,
"inbound_token": domain_registry.get_password("inbound_token"),
}

Expand Down
Empty file.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit db3d9ef

Please sign in to comment.