discourse/db/fixtures/991_vapid_keys.rb
David Taylor 1aa8f702d8
DEV: Move bootstrap site settings from initializers to seeds (#39942)
Previously, the VAPID keypair, `push_api_secret_key`, and the
`global_notice` / `has_login_hint` "Congratulations, you installed
Discourse!" banner were generated by `to_prepare` blocks in three
different Rails initializers (`005-site_settings.rb`,
`006-ensure_login_hint.rb`, `100-push-notifications.rb`), which fired on
every boot and silently rescued when the DB schema wasn't ready.

This change moves each block into its own seed fixture. These are run on
each `db:migrate` invocation, which is more than frequent enough for all
these cases.

Extracted from https://github.com/discourse/discourse/pull/39788.
2026-05-13 10:07:54 +01:00

17 lines
759 B
Ruby
Vendored

# frozen_string_literal: true
# Regenerate the VAPID keypair whenever vapid_base_url drifts from Discourse.base_url —
# push subscriptions are bound to {URL, public_key}, so a hostname change invalidates them.
if SiteSetting.vapid_public_key.blank? || SiteSetting.vapid_private_key.blank? ||
SiteSetting.vapid_public_key_bytes.blank? || SiteSetting.vapid_base_url != Discourse.base_url
require "web-push"
vapid_key = WebPush.generate_key
SiteSetting.vapid_public_key = vapid_key.public_key
SiteSetting.vapid_private_key = vapid_key.private_key
SiteSetting.vapid_public_key_bytes =
Base64.urlsafe_decode64(SiteSetting.vapid_public_key).bytes.join("|")
SiteSetting.vapid_base_url = Discourse.base_url
PushSubscription.delete_all
end