only send email when a note actually has content #2

Merged
stb merged 1 commit from dont-notify-empty-notes into main 2026-02-09 14:37:47 +01:00

View file

@ -215,28 +215,30 @@ class HedgedocExpire:
with db.prepare('DELETE FROM "Notes" WHERE "id" = $1') as delete_statement: with db.prepare('DELETE FROM "Notes" WHERE "id" = $1') as delete_statement:
for note in self.notes_to_be_expired(db): for note in self.notes_to_be_expired(db):
try: try:
note_age = datetime.now(timezone.utc) - datetime.fromisoformat(note['updatedAt'])
msg = MIMEMultipart() if len(note["content"]) > 0:
msg['From'] = self.email_sender.mail_from note_age = datetime.now(timezone.utc) - datetime.fromisoformat(note['updatedAt'])
msg['To'] = self.email_from_email_or_profile(note) msg = MIMEMultipart()
msg['Subject'] = f'Your HedgeDoc Note "{note["title"]}" has expired' msg['From'] = self.email_sender.mail_from
msg.attach(MIMEText(dedent(f'''\ msg['To'] = self.email_from_email_or_profile(note)
You created the note titled "{note["title"]}" on {note["createdAt"]}. msg['Subject'] = f'Your HedgeDoc Note "{note["title"]}" has expired'
It was lasted updated {note['updatedAt']}, {humanize.naturaldelta(note_age)} ago. We expire all notes msg.attach(MIMEText(dedent(f'''\
that have not been updated within {humanize.naturaldelta(self.config.note_age)}. You created the note titled "{note["title"]}" on {note["createdAt"]}.
It was lasted updated {note['updatedAt']}, {humanize.naturaldelta(note_age)} ago. We expire all notes
that have not been updated within {humanize.naturaldelta(self.config.note_age)}.
Please find attached the contents of the latest revision of your note. Please find attached the contents of the latest revision of your note.
The admin team for {self.config.url} The admin team for {self.config.url}
'''), 'plain', 'utf-8')) '''), 'plain', 'utf-8'))
md = MIMEText(note["content"], 'markdown', 'utf-8') md = MIMEText(note["content"], 'markdown', 'utf-8')
filename = note['title'].encode('ascii', 'ignore').decode('utf-8') filename = note['title'].encode('ascii', 'ignore').decode('utf-8')
if len(filename) == 0: if len(filename) == 0:
filename = 'note' filename = 'note'
md.add_header('Content-Disposition', f'attachment; filename={filename}.md') md.add_header('Content-Disposition', f'attachment; filename={filename}.md')
msg.attach(md) msg.attach(md)
self.email_sender.send(msg) self.email_sender.send(msg)
# email backup of the note sent, now we can delete it # email backup of the note sent, now we can delete it
delete_statement(note["id"]) delete_statement(note["id"])