Sync email settings only when changed
Some checks failed
CI / lint-check-build (pull_request) Has been cancelled
Some checks failed
CI / lint-check-build (pull_request) Has been cancelled
This commit is contained in:
parent
83a8bb1572
commit
3e892d76f6
1 changed files with 25 additions and 5 deletions
|
|
@ -123,14 +123,22 @@ export class MailNotifications {
|
|||
sync() {
|
||||
return userSettingsValues.subscribe(
|
||||
throttle(500, async ({email, emailAddress, emailFrequency}) => {
|
||||
const current = emailNotificationState.get()
|
||||
|
||||
if (email && emailAddress) {
|
||||
const existing = await this._lookup()
|
||||
|
||||
if (
|
||||
!current.callback ||
|
||||
current.email !== emailAddress ||
|
||||
current.frequency !== emailFrequency
|
||||
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 {
|
||||
|
|
@ -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) =>
|
||||
String(hash(relay + key + device.get() + "email"))
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue