Skip to content

Commit 4065812

Browse files
Switched to from_notification
1 parent 7177ef2 commit 4065812

2 files changed

Lines changed: 54 additions & 46 deletions

File tree

app/backend/src/couchers/email/emails.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Defines data models for each email we sent out to users.
33
"""
44

5+
import re
56
from abc import ABC, abstractmethod
67
from dataclasses import dataclass
78
from datetime import UTC, date, datetime
@@ -274,6 +275,25 @@ def build_body(self, builder: EmailBlocksBuilder, loc_context: LocalizationConte
274275
builder.quote(self.text, markdown=False)
275276
builder.action(self.view_url, "view_action")
276277

278+
@classmethod
279+
def from_notification(cls, data: notification_data_pb2.ChatMessage, *, user_name: str) -> Self:
280+
group_chat_title: str | None = data.group_chat_title
281+
if not group_chat_title:
282+
# Backcompat (2026-05): The group name previously was formatted in the message string
283+
# msg = f"{message.author.name} sent a message in {group_chat.title}"
284+
if match := re.search(" sent a message in (.+)$", data.message or ""):
285+
group_chat_title = match[1]
286+
else:
287+
group_chat_title = None
288+
289+
return cls(
290+
user_name,
291+
author=UserInfo.from_protobuf(data.author),
292+
text=data.text,
293+
group_chat_title=group_chat_title,
294+
view_url=urls.chat_link(chat_id=data.group_chat_id),
295+
)
296+
277297
@classmethod
278298
def dummy_data(cls) -> ChatMessageReceivedEmail:
279299
return ChatMessageReceivedEmail(
@@ -318,6 +338,38 @@ def build_body(self, builder: EmailBlocksBuilder, loc_context: LocalizationConte
318338
builder.quote(entry.latest_message_text, markdown=False)
319339
builder.action(entry.view_url, "view_action")
320340

341+
@classmethod
342+
def from_notification(cls, data: notification_data_pb2.ChatMissedMessages, *, user_name: str) -> Self:
343+
missed_entries = []
344+
for message in data.messages:
345+
group_chat_title: str | None = message.group_chat_title
346+
missed_count: int = message.unseen_count
347+
348+
# Backcompat (2026-05): The group name and unseen count were previously was formatted in the message string
349+
# msg = f"You missed {unseen_count} message(s) in {group_chat.title}"
350+
if not group_chat_title or not missed_count:
351+
if match := re.search(" message(s) in (.+)$", message.message or ""):
352+
group_chat_title = match[1]
353+
else:
354+
group_chat_title = None
355+
356+
if match := re.search(r"^You missed (\d+) message(s)", message.message or ""):
357+
missed_count = int(match[1])
358+
else:
359+
missed_count = 1
360+
361+
missed_entries.append(
362+
cls.Entry(
363+
group_chat_title=group_chat_title,
364+
missed_count=missed_count,
365+
latest_message_author=UserInfo.from_protobuf(message.author),
366+
latest_message_text=message.text,
367+
view_url=urls.chat_link(chat_id=message.group_chat_id),
368+
)
369+
)
370+
371+
return cls(user_name, entries=missed_entries)
372+
321373
@classmethod
322374
def dummy_data(cls) -> ChatMessagesMissedEmail:
323375
return ChatMessagesMissedEmail(

app/backend/src/couchers/notifications/render_email.py

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import logging
2-
import re
32
from dataclasses import dataclass, field
43
from typing import Any
54

@@ -124,52 +123,9 @@ def _get_generic_templated_email(user_name: str, notification: Notification) ->
124123
case NotificationTopicAction.birthdate__change:
125124
return emails.BirthdateChangedEmail.from_notification(data, user_name=user_name)
126125
case NotificationTopicAction.chat__message:
127-
group_chat_title: str | None = data.group_chat_title
128-
if not group_chat_title:
129-
# Backcompat (2026-05): The group name previously was formatted in the message string
130-
# msg = f"{message.author.name} sent a message in {group_chat.title}"
131-
if match := re.search(" sent a message in (.+)$", data.message or ""):
132-
group_chat_title = match[1]
133-
else:
134-
group_chat_title = None
135-
136-
return emails.ChatMessageReceivedEmail(
137-
user_name,
138-
author=_user_info(data.author),
139-
text=data.text,
140-
group_chat_title=group_chat_title,
141-
view_url=urls.chat_link(chat_id=data.group_chat_id),
142-
)
126+
return emails.ChatMessageReceivedEmail.from_notification(data, user_name=user_name)
143127
case NotificationTopicAction.chat__missed_messages:
144-
missed_entries = []
145-
for message in data.messages:
146-
group_chat_title = message.group_chat_title
147-
missed_count: int = message.unseen_count
148-
149-
# Backcompat (2026-05): The group name and unseen count were previously was formatted in the message string
150-
# msg = f"You missed {unseen_count} message(s) in {group_chat.title}"
151-
if not group_chat_title or not missed_count:
152-
if match := re.search(" message(s) in (.+)$", message.message or ""):
153-
group_chat_title = match[1]
154-
else:
155-
group_chat_title = None
156-
157-
if match := re.search(r"^You missed (\d+) message(s)", message.message or ""):
158-
missed_count = int(match[1])
159-
else:
160-
missed_count = 1
161-
162-
missed_entries.append(
163-
emails.ChatMessagesMissedEmail.Entry(
164-
group_chat_title=group_chat_title,
165-
missed_count=missed_count,
166-
latest_message_author=_user_info(message.author),
167-
latest_message_text=message.text,
168-
view_url=urls.chat_link(chat_id=message.group_chat_id),
169-
)
170-
)
171-
172-
return emails.ChatMessagesMissedEmail(user_name, entries=missed_entries)
128+
return emails.ChatMessagesMissedEmail.from_notification(data, user_name=user_name)
173129
case NotificationTopicAction.discussion__create:
174130
return emails.DiscussionCreatedEmail.from_notification(data, user_name=user_name)
175131
case NotificationTopicAction.discussion__comment:

0 commit comments

Comments
 (0)