Sync email settings only when changed
Some checks failed
CI / lint-check-build (pull_request) Has been cancelled

This commit is contained in:
mplorentz 2026-09-01 15:59:30 -04:00
parent 83a8bb1572
commit 3e892d76f6

View file

@ -123,14 +123,22 @@ export class MailNotifications {
sync() { sync() {
return userSettingsValues.subscribe( return userSettingsValues.subscribe(
throttle(500, async ({email, emailAddress, emailFrequency}) => { throttle(500, async ({email, emailAddress, emailFrequency}) => {
const current = emailNotificationState.get()
if (email && emailAddress) { if (email && emailAddress) {
const existing = await this._lookup()
if ( if (
!current.callback || existing?.confirmed &&
current.email !== emailAddress || existing.email === emailAddress &&
current.frequency !== emailFrequency existing.frequency === emailFrequency
) { ) {
emailNotificationState.set({
key: existing.key,
callback: existing.callback,
email: existing.email,
frequency: existing.frequency,
error: undefined,
})
} else {
try { try {
await this.register(emailAddress, emailFrequency) await this.register(emailAddress, emailFrequency)
} catch { } catch {
@ -146,6 +154,18 @@ export class MailNotifications {
) )
} }
async _lookup() {
const {pubkey} = User.require(app.get())
const res = await fetch(
`${buildUrl(EMAIL_NOTIFICATION_SERVER, "subscription", "email")}?pubkey=${pubkey}`,
)
if (!res.ok) return undefined
return res.json()
}
_getSubscriptionIdentifier = (relay: string, key: string) => _getSubscriptionIdentifier = (relay: string, key: string) =>
String(hash(relay + key + device.get() + "email")) String(hash(relay + key + device.get() + "email"))