Passed
Push — main ( a29a2e...e3918a )
by Jochen
06:16
created

_get_user_address()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
"""
2
byceps.services.user_message.service
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
Send an e-mail message from one user to another.
6
7
:Copyright: 2006-2021 Jochen Kupperschmidt
8
:License: Revised BSD (see `LICENSE` file for details)
9
"""
10
11 1
from email.utils import formataddr
12 1
from typing import Optional
13
14 1
from flask import current_app
15
16 1
from ...typing import UserID
17 1
from ...util import templating
18
19 1
from ..email import service as email_service
20 1
from ..email.transfer.models import Message
21 1
from ..site import service as site_service
22 1
from ..site.transfer.models import SiteID
23 1
from ..user import service as user_service
24 1
from ..user.transfer.models import User
25
26
27 1
def send_message(
28
    sender_id: UserID,
29
    recipient_id: UserID,
30
    text: str,
31
    sender_contact_url: str,
32
    site_id: SiteID,
33
) -> None:
34
    """Create a message and send it."""
35 1
    message = create_message(
36
        sender_id, recipient_id, text, sender_contact_url, site_id
37
    )
38
39 1
    email_service.enqueue_message(message)
40
41
42 1
def create_message(
43
    sender_id: UserID,
44
    recipient_id: UserID,
45
    text: str,
46
    sender_contact_url: str,
47
    site_id: SiteID,
48
) -> Message:
49
    """Assemble an email message with the rendered template as its body."""
50 1
    sender = _get_user(sender_id)
51 1
    recipient = _get_user(recipient_id)
52 1
    site = site_service.get_site(site_id)
53 1
    email_config = email_service.get_config(site.brand_id)
54
55 1
    sender_screen_name = sender.screen_name or f'user-{sender.id}'
56 1
    website_server_name = site.server_name
57
58 1
    recipients = [_get_user_address(recipient)]
59 1
    subject = (
60
        f'Mitteilung von {sender.screen_name} (über {website_server_name})'
61
    )
62 1
    body = _get_body(
63
        recipient.screen_name,
64
        sender_screen_name,
65
        text,
66
        sender_contact_url,
67
        website_server_name,
68
        email_config.contact_address,
69
    )
70
71 1
    return Message(email_config.sender, recipients, subject, body)
72
73
74 1
def _get_user(user_id: UserID) -> User:
75 1
    user = user_service.find_active_user(user_id)
76
77 1
    if user is None:
78
        raise ValueError(f"Unknown user ID '{user_id}' or account not active")
79
80 1
    return user
81
82
83 1
def _get_user_address(user: User) -> str:
84 1
    email_address = user_service.get_email_address(user.id)
85
    # If `name` evaluates to `False`, just the address is returned.
86 1
    return formataddr((user.screen_name, email_address))
87
88
89 1
def _get_body(
90
    recipient_screen_name: Optional[str],
91
    sender_screen_name: str,
92
    text: str,
93
    sender_contact_url: str,
94
    website_server_name: str,
95
    website_contact_address: Optional[str],
96
) -> str:
97 1
    body = f'''\
98
Hallo {recipient_screen_name},
99
100
{sender_screen_name} möchte dir die folgende Mitteilung zukommen lassen.
101
102
Du kannst hier antworten: {sender_contact_url}
103
104
ACHTUNG: Antworte *nicht* auf diese E-Mail, sondern folge dem Link.
105
106
---8<-------------------------------------
107
108
{text.strip()}
109
110
---8<-------------------------------------
111
112
-- 
113
Diese Mitteilung wurde über die Website {website_server_name} gesendet.'''
114
115 1
    if website_contact_address:
116 1
        body += f'\nBei Fragen kontaktiere uns bitte per E-Mail an: {website_contact_address}'
117
118
    return body
119