File size: 4,834 Bytes
b734a17
5e463e3
 
 
d94e719
 
5e463e3
 
 
 
b734a17
 
 
 
 
 
 
5e463e3
 
 
 
 
b734a17
 
5e463e3
 
 
b734a17
5e463e3
 
b734a17
5e463e3
 
 
b734a17
 
d94e719
 
 
 
b734a17
 
 
b728b04
 
 
b734a17
d94e719
 
 
b734a17
 
 
b728b04
 
 
d94e719
 
 
b728b04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d94e719
b734a17
 
 
 
b728b04
b734a17
661a924
b734a17
b728b04
 
 
 
b734a17
 
 
 
 
 
 
 
 
 
b728b04
b734a17
d94e719
 
b734a17
b728b04
b734a17
 
d94e719
 
 
 
 
 
b734a17
d94e719
 
 
 
 
661a924
b734a17
5e463e3
b734a17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""Email delivery via EmailJS for project collaboration invites."""

from dataclasses import dataclass
from datetime import datetime
import json
from urllib import request


@dataclass(frozen=True)
class InviteEmailPayload:
    """Payload for EmailJS invite notification.
    
    At least one of invitee_username or invitee_email must be present.
    - Only username: internal app invite only (no email sent).
    - Only email: external email-based invite (no prior HF account required).
    - Both: dual invite (internal + email).
    """
    project_slug: str
    role: str
    invited_by: str
    expires_at: datetime
    login_url: str
    invitee_username: str | None = None
    invitee_email: str | None = None


class InviteEmailNotifier:
    """Abstract notifier interface for invite emails."""

    def send(self, payload: InviteEmailPayload) -> tuple[bool, str]:
        """Send an invite notification. Returns (success, message)."""
        raise NotImplementedError


class EmailJSInviteEmailNotifier(InviteEmailNotifier):
    """EmailJS-backed notifier for invite emails (only transport available)."""

    def __init__(
        self,
        sender_email: str,
        service_id: str,
        template_id: str,
        public_key: str,
        template_id_username_only: str | None = None,
        template_id_email_only: str | None = None,
        template_id_dual: str | None = None,
        endpoint: str = "https://api.emailjs.com/api/v1.0/email/send",
        timeout_seconds: int = 20,
    ):
        self._sender_email = sender_email
        self._service_id = service_id
        self._template_id = template_id
        self._public_key = public_key
        self._template_id_username_only = (template_id_username_only or "").strip() or None
        self._template_id_email_only = (template_id_email_only or "").strip() or None
        self._template_id_dual = (template_id_dual or "").strip() or None
        self._endpoint = endpoint
        self._timeout_seconds = max(5, int(timeout_seconds))

    def _resolve_invite_mode(self, payload: InviteEmailPayload) -> str:
        if payload.invitee_username and payload.invitee_email:
            return "dual"
        if payload.invitee_email:
            return "email_only"
        return "username_only"

    def _resolve_template_id(self, mode: str) -> str:
        if mode == "username_only" and self._template_id_username_only:
            return self._template_id_username_only
        if mode == "email_only" and self._template_id_email_only:
            return self._template_id_email_only
        if mode == "dual" and self._template_id_dual:
            return self._template_id_dual
        return self._template_id

    def send(self, payload: InviteEmailPayload) -> tuple[bool, str]:
        """Send invite email via EmailJS.
        
        If invitee_email is missing, returns success with message indicating internal-only invite.
        """
        invite_mode = self._resolve_invite_mode(payload)
        if not payload.invitee_email:
            return True, f"Internal invite created for {payload.invitee_username or 'pending'}"

        template_id = self._resolve_template_id(invite_mode)
        if not template_id:
            return False, "Internal invite created, but email send failed: EmailJS template ID is not configured"

        template_params = {
            "from_name": self._sender_email,
            "to_email": payload.invitee_email,
            "invitee_username": payload.invitee_username or "(to be claimed)",
            "project_slug": payload.project_slug,
            "role": payload.role,
            "invited_by": payload.invited_by,
            "expires_at": payload.expires_at.isoformat(),
            "login_url": payload.login_url or "",
            "invite_link": payload.login_url or "",
            "invite_mode": invite_mode,
        }

        req_payload = {
            "service_id": self._service_id,
            "template_id": template_id,
            "user_id": self._public_key,
            "template_params": template_params,
        }

        try:
            req = request.Request(
                self._endpoint,
                data=json.dumps(req_payload).encode("utf-8"),
                headers={"Content-Type": "application/json"},
                method="POST",
            )
            with request.urlopen(req, timeout=self._timeout_seconds) as response:
                status_code = int(getattr(response, "status", 0) or response.getcode())
                if 200 <= status_code < 300:
                    return True, f"Invite sent to {payload.invitee_email}"
                return False, f"Internal invite created, but email send failed (status {status_code})"
        except Exception as exc:
            return False, f"Internal invite created, but email send failed: {exc}"