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()
msg['From'] = self.email_sender.mail_from
msg['To'] = self.email_from_email_or_profile(note)
msg['Subject'] = f'Your HedgeDoc Note "{note["title"]}" has expired'
msg.attach(MIMEText(dedent(f'''\
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. if len(note["content"]) > 0:
note_age = datetime.now(timezone.utc) - datetime.fromisoformat(note['updatedAt'])
msg = MIMEMultipart()
msg['From'] = self.email_sender.mail_from
msg['To'] = self.email_from_email_or_profile(note)
msg['Subject'] = f'Your HedgeDoc Note "{note["title"]}" has expired'
msg.attach(MIMEText(dedent(f'''\
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)}.
The admin team for {self.config.url} Please find attached the contents of the latest revision of your note.
'''), 'plain', 'utf-8')) The admin team for {self.config.url}
md = MIMEText(note["content"], 'markdown', 'utf-8')
filename = note['title'].encode('ascii', 'ignore').decode('utf-8') '''), 'plain', 'utf-8'))
if len(filename) == 0: md = MIMEText(note["content"], 'markdown', 'utf-8')
filename = 'note' filename = note['title'].encode('ascii', 'ignore').decode('utf-8')
md.add_header('Content-Disposition', f'attachment; filename={filename}.md') if len(filename) == 0:
msg.attach(md) filename = 'note'
self.email_sender.send(msg) md.add_header('Content-Disposition', f'attachment; filename={filename}.md')
msg.attach(md)
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"])