Always PUT email subscription (remove GET-first lookup)
Some checks failed
CI / lint-check-build (pull_request) Has been cancelled
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:
parent
3e892d76f6
commit
8047a4f084
1 changed files with 6 additions and 34 deletions
|
|
@ -28,7 +28,7 @@ export class MailNotifications {
|
||||||
|
|
||||||
let authEvent
|
let authEvent
|
||||||
try {
|
try {
|
||||||
authEvent = await $signer.sign(await makeHttpAuth(EMAIL_NOTIFICATION_SERVER, "POST"))
|
authEvent = await $signer.sign(await makeHttpAuth(EMAIL_NOTIFICATION_SERVER, "PUT"))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
emailNotificationState.set({error: "Failed to create authentication. Check your signer."})
|
emailNotificationState.set({error: "Failed to create authentication. Check your signer."})
|
||||||
throw new Error("Failed to sign NIP-98 auth", {cause: e})
|
throw new Error("Failed to sign NIP-98 auth", {cause: e})
|
||||||
|
|
@ -39,7 +39,7 @@ export class MailNotifications {
|
||||||
let res
|
let res
|
||||||
try {
|
try {
|
||||||
res = await fetch(buildUrl(EMAIL_NOTIFICATION_SERVER, "subscription", "email"), {
|
res = await fetch(buildUrl(EMAIL_NOTIFICATION_SERVER, "subscription", "email"), {
|
||||||
method: "POST",
|
method: "PUT",
|
||||||
body: JSON.stringify({email, frequency, pubkey: $user.pubkey}),
|
body: JSON.stringify({email, frequency, pubkey: $user.pubkey}),
|
||||||
headers: {Authorization: authHeader, "Content-Type": "application/json"},
|
headers: {Authorization: authHeader, "Content-Type": "application/json"},
|
||||||
})
|
})
|
||||||
|
|
@ -124,26 +124,10 @@ export class MailNotifications {
|
||||||
return userSettingsValues.subscribe(
|
return userSettingsValues.subscribe(
|
||||||
throttle(500, async ({email, emailAddress, emailFrequency}) => {
|
throttle(500, async ({email, emailAddress, emailFrequency}) => {
|
||||||
if (email && emailAddress) {
|
if (email && emailAddress) {
|
||||||
const existing = await this._lookup()
|
try {
|
||||||
|
await this.register(emailAddress, emailFrequency)
|
||||||
if (
|
} catch {
|
||||||
existing?.confirmed &&
|
return
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.enable()
|
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) =>
|
_getSubscriptionIdentifier = (relay: string, key: string) =>
|
||||||
String(hash(relay + key + device.get() + "email"))
|
String(hash(relay + key + device.get() + "email"))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue