File size: 4,544 Bytes
851e5ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

from dataclasses import dataclass
from html import escape
from typing import Optional

from ..config import Config
from .email_service import EmailContent, SesEmailService, first_name_for


@dataclass(frozen=True)
class TaskCompletionRecipient:
    email: str
    name: Optional[str] = None
    first_name: Optional[str] = None


class TaskCompletionEmailService:
    def __init__(self, config: Optional[Config] = None):
        self.config = config or Config()
        self.email_service = SesEmailService(self.config)
        self.app_url = self.config.app_base_url

    @property
    def is_configured(self) -> bool:
        return self.email_service.is_configured

    async def send_task_completed_email(
        self,
        *,
        recipient: TaskCompletionRecipient,
        task_id: str,
        source_title: Optional[str],
        clips_count: int,
        workflow: str = "highlights",
        topic: str = "",
    ) -> dict:
        content = self._build_task_completed_email(
            recipient=recipient,
            task_id=task_id,
            source_title=source_title,
            clips_count=clips_count,
            workflow=workflow,
            topic=topic,
        )
        return await self.email_service.send_email(recipient.email, content)

    def _build_task_completed_email(
        self,
        *,
        recipient: TaskCompletionRecipient,
        task_id: str,
        source_title: Optional[str],
        clips_count: int,
        workflow: str = "highlights",
        topic: str = "",
    ) -> EmailContent:
        first_name = first_name_for(
            first_name=recipient.first_name,
            full_name=recipient.name,
        )
        task_url = f"{self.app_url}/tasks/{task_id}"
        clips_label = f"{clips_count} clip" if clips_count == 1 else f"{clips_count} clips"
        safe_source_title = escape(source_title.strip()) if source_title else "your video"
        subject = "Your SupoClip clips are ready"
        # What the generation made, as (headline, plain-text headline, count line).
        headline_html = f"Your clips for <strong>{safe_source_title}</strong> are ready."
        headline_text = f"Your clips for {source_title or 'your video'} are ready."
        count_line = f"We generated {clips_label} for you."
        link_label = "Open your finished clips"
        if workflow == "series":
            subject = f"Your {clips_count}-part series is ready"
            headline_html = f"Your {clips_count}-part series from <strong>{safe_source_title}</strong> is ready."
            headline_text = f"Your {clips_count}-part series from {source_title or 'your video'} is ready."
            count_line = "Every part picks up where the last one ended."
            link_label = "Open your series"
        elif workflow in ("recap", "trailer"):
            # "Your recap of the talk", "Your trailer for the talk".
            preposition = "of" if workflow == "recap" else "for"
            subject = f"Your {workflow} is ready"
            headline_html = f"Your {workflow} {preposition} <strong>{safe_source_title}</strong> is ready."
            headline_text = f"Your {workflow} {preposition} {source_title or 'your video'} is ready."
            count_line = (
                "It covers the main points in one short video."
                if workflow == "recap"
                else "A short preview that makes people want to watch the whole video."
            )
            link_label = f"Open your {workflow}"
        elif workflow == "topic" and topic.strip():
            subject = f"Your clips about {topic.strip()} are ready"
            headline_html = (
                f"Your clips about <strong>{escape(topic.strip())}</strong> "
                f"from <strong>{safe_source_title}</strong> are ready."
            )
            headline_text = f"Your clips about {topic.strip()} from {source_title or 'your video'} are ready."

        return EmailContent(
            subject=subject,
            html=(
                f"<p>Hi {escape(first_name)},</p>"
                f"<p>{headline_html}</p>"
                f"<p>{escape(count_line)}</p>"
                f'<p><a href="{escape(task_url)}">{escape(link_label)}</a></p>'
                "<p>Team SupoClip</p>"
            ),
            text=(
                f"Hi {first_name},\n\n"
                f"{headline_text}\n"
                f"{count_line}\n\n"
                f"{link_label}: {task_url}\n\n"
                "Team SupoClip"
            ),
        )