Make web push toggle global
This commit is contained in:
parent
a8db08c7d4
commit
46798ac322
10 changed files with 99 additions and 91 deletions
|
@ -45,15 +45,11 @@ class ConnectionManager {
|
|||
return;
|
||||
}
|
||||
console.log(`[ConnectionManager] Refreshing connections`);
|
||||
const subscriptionsWithUsersAndConnectionId = subscriptions
|
||||
.map((s) => {
|
||||
const [user] = users.filter((u) => u.baseUrl === s.baseUrl);
|
||||
const connectionId = makeConnectionId(s, user);
|
||||
return { ...s, user, connectionId };
|
||||
})
|
||||
// background notifications don't need this as they come over web push.
|
||||
// however, if they are muted, we again need the ws while the page is active
|
||||
.filter((s) => !s.webPushEnabled && s.mutedUntil !== 1);
|
||||
const subscriptionsWithUsersAndConnectionId = subscriptions.map((s) => {
|
||||
const [user] = users.filter((u) => u.baseUrl === s.baseUrl);
|
||||
const connectionId = makeConnectionId(s, user);
|
||||
return { ...s, user, connectionId };
|
||||
});
|
||||
|
||||
console.log();
|
||||
const targetIds = subscriptionsWithUsersAndConnectionId.map((s) => s.connectionId);
|
||||
|
|
|
@ -114,6 +114,11 @@ class Notifier {
|
|||
return this.pushSupported() && this.contextSupported() && this.granted() && !this.iosSupportedButInstallRequired();
|
||||
}
|
||||
|
||||
async pushEnabled() {
|
||||
const enabled = await prefs.webPushEnabled();
|
||||
return this.pushPossible() && enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this is a HTTPS site, or served over localhost. Otherwise the Notification API
|
||||
* is not supported, see https://developer.mozilla.org/en-US/docs/Web/API/notification
|
||||
|
|
|
@ -31,6 +31,15 @@ class Prefs {
|
|||
const deleteAfter = await this.db.prefs.get("deleteAfter");
|
||||
return deleteAfter ? Number(deleteAfter.value) : 604800; // Default is one week
|
||||
}
|
||||
|
||||
async webPushEnabled() {
|
||||
const obj = await this.db.prefs.get("webPushEnabled");
|
||||
return obj?.value ?? false;
|
||||
}
|
||||
|
||||
async setWebPushEnabled(enabled) {
|
||||
await this.db.prefs.put({ key: "webPushEnabled", value: enabled });
|
||||
}
|
||||
}
|
||||
|
||||
const prefs = new Prefs(getDb());
|
||||
|
|
|
@ -21,8 +21,16 @@ class SubscriptionManager {
|
|||
}
|
||||
|
||||
async webPushTopics() {
|
||||
const subscriptions = await this.db.subscriptions.where({ webPushEnabled: 1, mutedUntil: 0 }).toArray();
|
||||
return subscriptions.map(({ topic }) => topic);
|
||||
// the Promise.resolve wrapper is not superfluous, without it the live query breaks:
|
||||
// https://dexie.org/docs/dexie-react-hooks/useLiveQuery()#calling-non-dexie-apis-from-querier
|
||||
if (!(await Promise.resolve(notifier.pushEnabled()))) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const subscriptions = await this.db.subscriptions.where({ mutedUntil: 0, baseUrl: config.base_url }).toArray();
|
||||
|
||||
// internal is currently a bool, it could be a 0/1 to be indexable, but for now just filter them out here
|
||||
return subscriptions.filter(({ internal }) => !internal).map(({ topic }) => topic);
|
||||
}
|
||||
|
||||
async get(subscriptionId) {
|
||||
|
@ -49,7 +57,6 @@ class SubscriptionManager {
|
|||
* @param {string} topic
|
||||
* @param {object} opts
|
||||
* @param {boolean} opts.internal
|
||||
* @param {boolean} opts.webPushEnabled
|
||||
* @returns
|
||||
*/
|
||||
async add(baseUrl, topic, opts = {}) {
|
||||
|
@ -67,7 +74,6 @@ class SubscriptionManager {
|
|||
topic,
|
||||
mutedUntil: 0,
|
||||
last: null,
|
||||
webPushEnabled: opts.webPushEnabled ? 1 : 0,
|
||||
};
|
||||
|
||||
await this.db.subscriptions.put(subscription);
|
||||
|
@ -211,12 +217,6 @@ class SubscriptionManager {
|
|||
});
|
||||
}
|
||||
|
||||
async toggleBackgroundNotifications(subscription) {
|
||||
await this.db.subscriptions.update(subscription.id, {
|
||||
webPushEnabled: subscription.webPushEnabled === 1 ? 0 : 1,
|
||||
});
|
||||
}
|
||||
|
||||
async setDisplayName(subscriptionId, displayName) {
|
||||
await this.db.subscriptions.update(subscriptionId, {
|
||||
displayName,
|
||||
|
|
|
@ -14,7 +14,7 @@ const getDbBase = (username) => {
|
|||
const db = new Dexie(dbName);
|
||||
|
||||
db.version(2).stores({
|
||||
subscriptions: "&id,baseUrl,[webPushEnabled+mutedUntil]",
|
||||
subscriptions: "&id,baseUrl,[baseUrl+mutedUntil]",
|
||||
notifications: "&id,subscriptionId,time,new,[subscriptionId+new]", // compound key for query performance
|
||||
users: "&baseUrl,username",
|
||||
prefs: "&key",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue