Always PUT email subscription (remove GET-first lookup)
Some checks failed
CI / lint-check-build (pull_request) Has been cancelled

Mailship's /subscription/email is now an idempotent PUT upsert. Drop the
lookup-before-register dance and always PUT the settings; mailship skips the
confirmation email when nothing changed or only the frequency changed.
This commit is contained in:
mplorentz 2026-09-03 11:52:31 -04:00
parent 3e892d76f6
commit 8047a4f084

View file

@ -28,7 +28,7 @@ export class MailNotifications {
let authEvent
try {
authEvent = await $signer.sign(await makeHttpAuth(EMAIL_NOTIFICATION_SERVER, "POST"))
authEvent = await $signer.sign(await makeHttpAuth(EMAIL_NOTIFICATION_SERVER, "PUT"))
} catch (e) {
emailNotificationState.set({error: "Failed to create authentication. Check your signer."})
throw new Error("Failed to sign NIP-98 auth", {cause: e})
@ -39,7 +39,7 @@ export class MailNotifications {
let res
try {
res = await fetch(buildUrl(EMAIL_NOTIFICATION_SERVER, "subscription", "email"), {
method: "POST",
method: "PUT",
body: JSON.stringify({email, frequency, pubkey: $user.pubkey}),
headers: {Authorization: authHeader, "Content-Type": "application/json"},
})
@ -124,26 +124,10 @@ export class MailNotifications {
return userSettingsValues.subscribe(
throttle(500, async ({email, emailAddress, emailFrequency}) => {
if (email && emailAddress) {
const existing = await this._lookup()
if (
existing?.confirmed &&
existing.email === emailAddress &&
existing.frequency === emailFrequency
) {
emailNotificationState.set({
key: existing.key,
callback: existing.callback,
email: existing.email,
frequency: existing.frequency,
error: undefined,
})
} else {
try {
await this.register(emailAddress, emailFrequency)
} catch {
return
}
try {
await this.register(emailAddress, emailFrequency)
} catch {
return
}
await this.enable()
@ -154,18 +138,6 @@ 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) =>
String(hash(relay + key + device.get() + "email"))