Extract email from profile if necessary

This commit is contained in:
Stefan Bethke 2024-05-18 21:24:25 +02:00
parent 9bc4278c82
commit 359f308104

View file

@ -50,6 +50,13 @@ class EmailSender:
smtp_server.send_message(message)
def email_from_email_or_profile(row):
if row['email'] is not None:
return row['email']
profile = json.loads(row['profile'])
return profile['emails'][0]
def notes_to_be_expired(cutoff: datetime) -> list[any]:
notes = []
with db.prepare('''SELECT
@ -61,7 +68,8 @@ def notes_to_be_expired(cutoff: datetime) -> list[any]:
"Notes"."id",
"Notes"."title",
"Notes"."updatedAt",
"Users"."email"
"Users"."email",
"Users"."profile"
FROM "Notes", "Users"
WHERE "Notes"."updatedAt" < $1
AND "Notes"."ownerId" = "Users"."id"
@ -74,6 +82,7 @@ def notes_to_be_expired(cutoff: datetime) -> list[any]:
'email': row.email,
"id": row.id,
'ownerId': row.ownerId,
'profile': row.profile,
'shortid': row.shortid,
'title': row.title,
'updatedAt': row.updatedAt
@ -93,6 +102,7 @@ def revisions_to_be_expired(cutoff: datetime) -> list[any]:
"Notes"."alias",
"Revisions"."createdAt",
"Users"."email",
"Users"."profile",
"Revisions"."id" as "revisionId",
"Notes"."id" as "noteId",
"Notes"."shortid" as "shortid",
@ -108,6 +118,7 @@ def revisions_to_be_expired(cutoff: datetime) -> list[any]:
'createdAt': row.createdAt,
'email': row.email,
'noteId': row.noteId,
'profile': row.profile,
'revisionId': row.revisionId,
'shortid': row.shortid,
'title': row.title
@ -121,7 +132,7 @@ def check_notes_to_be_expired(age: timedelta, config: Config) -> None:
for note in notes_to_be_expired(cutoff):
age = datetime.now(timezone.utc) - datetime.fromisoformat(note['updatedAt'])
url = config.url + '/' + (note["alias"] if note["alias"] is not None else note["shortid"])
print(f' {note["email"]} ({humanize.naturaldelta(age)}) {url}: {note["title"]}')
print(f' {email_from_email_or_profile(note)} ({humanize.naturaldelta(age)}) {url}: {note["title"]}')
def check_revisions_to_be_expired(age: timedelta, config: Config) -> None:
@ -134,9 +145,9 @@ def check_revisions_to_be_expired(age: timedelta, config: Config) -> None:
notes[row['noteId']] = []
notes[row['noteId']].append(row)
for id, revisions in notes.items():
email = revisions[0]['email']
email = email_from_email_or_profile(revisions[0])
url = config.url + '/' + (revisions[0]["alias"] if revisions[0]["alias"] is not None else revisions[0]["shortid"])
print(f' {revisions[0]["email"]} {url}: {revisions[0]["title"]}')
print(f' {email} {url}: {revisions[0]["title"]}')
for rev in revisions:
print(f' {humanize.naturaldelta(rev["age"])}: {rev["revisionId"]}')
@ -149,7 +160,7 @@ def expire_old_notes(age: timedelta, config: Config, mail: EmailSender) -> None:
note_age = datetime.now(timezone.utc) - datetime.fromisoformat(note['updatedAt'])
msg = MIMEMultipart()
msg['From'] = mail.mail_from
msg['To'] = note["email"]
msg['To'] = email_from_email_or_profile(note)
msg['Subject'] = f'Your HedgeDoc Note "{note["title"]}" has been expired'
msg.attach(MIMEText(dedent(f'''\
You created the note titled "{note["title"]}" on {note["createdAt"]}.