AutoSubscribeIfHosted enrolls a freshly-created user in the newsletter when the instance is running in admin_mode (managed/SaaS deployments). On self-hosted instances (admin_mode off) this is a no-op so users keep their opt-in choice. Runs in a detached goroutine: the request handler returns immedia
(adminMode bool, users *store.UsersStore, log *slog.Logger, u *models.User)
| 70 | // |
| 71 | // Call after a successful user insert with a populated u.ID and u.Email. |
| 72 | func AutoSubscribeIfHosted(adminMode bool, users *store.UsersStore, log *slog.Logger, u *models.User) { |
| 73 | if !adminMode || users == nil || u == nil || u.Email == "" { |
| 74 | return |
| 75 | } |
| 76 | // Discord auto-creation synthesises `discord_<id>@discord.local` when |
| 77 | // the user hasn't shared a real email. Subscribing those produces hard |
| 78 | // bounces and hurts the marketing sender's domain reputation, so skip. |
| 79 | if strings.HasSuffix(strings.ToLower(u.Email), "@discord.local") { |
| 80 | return |
| 81 | } |
| 82 | // Snapshot the user fields the goroutine needs so it never races with |
| 83 | // later mutations of the caller's *models.User. |
| 84 | id := u.ID |
| 85 | email := u.Email |
| 86 | name := newsletterDisplayName(u) |
| 87 | |
| 88 | go func() { |
| 89 | // Detach from the request context (which dies when the handler |
| 90 | // returns) but keep a hard ceiling so a slow Listmonk can't pin a |
| 91 | // goroutine forever. |
| 92 | ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) |
| 93 | defer cancel() |
| 94 | |
| 95 | if err := SubscribeToList(ctx, name, email); err != nil { |
| 96 | if log != nil { |
| 97 | log.Warn("auto newsletter subscribe failed", "user_id", id, "error", err) |
| 98 | } |
| 99 | return |
| 100 | } |
| 101 | if err := users.SetNewsletterSubscribed(ctx, id); err != nil { |
| 102 | if log != nil { |
| 103 | log.Warn("auto newsletter flag update failed", "user_id", id, "error", err) |
| 104 | } |
| 105 | } |
| 106 | }() |
| 107 | } |
| 108 | |
| 109 | // Subscribe proxies the newsletter subscription to the external marketing system. |
| 110 | // Public endpoint - no auth required (used during first-time setup). |
no test coverage detected