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) 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]: def notes_to_be_expired(cutoff: datetime) -> list[any]:
notes = [] notes = []
with db.prepare('''SELECT with db.prepare('''SELECT
@ -61,7 +68,8 @@ def notes_to_be_expired(cutoff: datetime) -> list[any]:
"Notes"."id", "Notes"."id",
"Notes"."title", "Notes"."title",
"Notes"."updatedAt", "Notes"."updatedAt",
"Users"."email" "Users"."email",
"Users"."profile"
FROM "Notes", "Users" FROM "Notes", "Users"
WHERE "Notes"."updatedAt" < $1 WHERE "Notes"."updatedAt" < $1
AND "Notes"."ownerId" = "Users"."id" AND "Notes"."ownerId" = "Users"."id"
@ -74,6 +82,7 @@ def notes_to_be_expired(cutoff: datetime) -> list[any]:
'email': row.email, 'email': row.email,
"id": row.id, "id": row.id,
'ownerId': row.ownerId, 'ownerId': row.ownerId,
'profile': row.profile,
'shortid': row.shortid, 'shortid': row.shortid,
'title': row.title, 'title': row.title,
'updatedAt': row.updatedAt 'updatedAt': row.updatedAt
@ -93,6 +102,7 @@ def revisions_to_be_expired(cutoff: datetime) -> list[any]:
"Notes"."alias", "Notes"."alias",
"Revisions"."createdAt", "Revisions"."createdAt",
"Users"."email", "Users"."email",
"Users"."profile",
"Revisions"."id" as "revisionId", "Revisions"."id" as "revisionId",
"Notes"."id" as "noteId", "Notes"."id" as "noteId",
"Notes"."shortid" as "shortid", "Notes"."shortid" as "shortid",
@ -108,6 +118,7 @@ def revisions_to_be_expired(cutoff: datetime) -> list[any]:
'createdAt': row.createdAt, 'createdAt': row.createdAt,
'email': row.email, 'email': row.email,
'noteId': row.noteId, 'noteId': row.noteId,
'profile': row.profile,
'revisionId': row.revisionId, 'revisionId': row.revisionId,
'shortid': row.shortid, 'shortid': row.shortid,
'title': row.title '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): for note in notes_to_be_expired(cutoff):
age = datetime.now(timezone.utc) - datetime.fromisoformat(note['updatedAt']) age = datetime.now(timezone.utc) - datetime.fromisoformat(note['updatedAt'])
url = config.url + '/' + (note["alias"] if note["alias"] is not None else note["shortid"]) 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: 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']] = []
notes[row['noteId']].append(row) notes[row['noteId']].append(row)
for id, revisions in notes.items(): 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"]) 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: for rev in revisions:
print(f' {humanize.naturaldelta(rev["age"])}: {rev["revisionId"]}') 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']) note_age = datetime.now(timezone.utc) - datetime.fromisoformat(note['updatedAt'])
msg = MIMEMultipart() msg = MIMEMultipart()
msg['From'] = mail.mail_from 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['Subject'] = f'Your HedgeDoc Note "{note["title"]}" has been expired'
msg.attach(MIMEText(dedent(f'''\ msg.attach(MIMEText(dedent(f'''\
You created the note titled "{note["title"]}" on {note["createdAt"]}. You created the note titled "{note["title"]}" on {note["createdAt"]}.