Skip to content

Add opt-in email notifications (new threads, mentions, replies) - #396

Draft
capocasa wants to merge 2 commits into
nim-lang:masterfrom
capocasa:email-notifications
Draft

capocasa wants to merge 2 commits into
nim-lang:masterfrom
capocasa:email-notifications

Conversation

@capocasa

Copy link
Copy Markdown

Summary

Adds opt-in email notifications with three switches in the user's profile settings:

  • New threads — mail when anyone starts a new thread
  • Mentions — mail when someone @mentions you in a post
  • Replies — mail when someone replies to a thread you posted in

Implementation

  • Three new person columns (emailNewThreads, emailMentions, emailReplies), added to fresh databases by setup_nimforum and to existing ones via an idempotent ALTER TABLE migration at startup.
  • /profile.json exposes the preferences; /saveProfile persists them.
  • After a post is committed, recipients are resolved synchronously (so the request object is still valid for URL building), then the SMTP transfers run in the background — posting never blocks or fails because of mail problems; send failures are logged and the existing 2-mails-per-hour-per-address rate limit applies.
  • Mentions are extracted from the rendered post HTML (extractMentions in utils.nim), so @user inside code blocks or links does not trigger mail — exactly matching what the page highlights.
  • A user qualifying for both a mention and a reply notification for the same post gets a single mail (mention wins). Banned/spammer ranks and unconfirmed email addresses never receive notifications.

Note for reviewers

replaceMentions needed its <>span htmlgen call replaced with an equivalent newXmlTree call because the former no longer compiles on Nim 2.2 (Invalid node kind nnkInfix for macros.$), and utils.nim must build for extractMentions. Happy to split that into a separate PR if preferred.

Testing

  • Backend and frontend build cleanly on Nim 2.2.12.
  • End-to-end verified on a local instance with a debugging SMTP sink: new-thread/mention/reply delivery, dedup, rank exclusion, rate limiting, and preference round-trip through both API and the settings UI (geckodriver).

Users get three switches in their profile settings: email on new
threads, on being @mentioned, and on replies to threads they posted
in. Preferences are stored in three new person columns, added via
ALTER TABLE migration on startup so existing databases keep working.

Mentions are extracted from the rendered post HTML, so @references
inside code blocks or links do not trigger mail, matching what the
page displays. A user qualifying for both a mention and a reply
notification for the same post receives only the mention mail.
Banned/spammer ranks and unconfirmed email addresses are excluded.

Recipients are resolved synchronously after the post is committed,
but SMTP transfers run in the background, so posting never blocks
or fails on mail errors; failures are logged and the existing
2-mails-per-hour-per-address rate limit applies.

Also replaces the htmlgen <>span call in replaceMentions with
newXmlTree: the former no longer compiles on Nim 2.2, and this file
needed to build for the new extractMentions proc.
The JS backend hoists the loop variable into one shared object that is
mutated in place per iteration, so capturing `category` in the closure
made every menu item select the last category. Only primitives are safe
to capture; hoist category.id and re-resolve the object on click.

This made the categories browser test fail deterministically
(categories.nim:147, expected "Announcements" got the last category).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant