Get vapid key to instance config
This commit is contained in:
parent
51e3341e82
commit
1d69a1da71
2 changed files with 11 additions and 4 deletions
|
@ -1,6 +1,6 @@
|
||||||
// Utils for push notifications
|
// Utils for push notifications
|
||||||
import { api } from './api';
|
import { api } from './api';
|
||||||
import { getCurrentAccount } from './store-utils';
|
import { getVapidKey } from './store-utils';
|
||||||
|
|
||||||
// Subscription is an object with the following structure:
|
// Subscription is an object with the following structure:
|
||||||
// {
|
// {
|
||||||
|
@ -113,7 +113,7 @@ export async function initSubscription() {
|
||||||
// Check if the subscription changed
|
// Check if the subscription changed
|
||||||
if (backendSubscription && subscription) {
|
if (backendSubscription && subscription) {
|
||||||
const sameEndpoint = backendSubscription.endpoint === subscription.endpoint;
|
const sameEndpoint = backendSubscription.endpoint === subscription.endpoint;
|
||||||
const { vapidKey } = getCurrentAccount();
|
const vapidKey = getVapidKey();
|
||||||
const sameKey = backendSubscription.serverKey === vapidKey;
|
const sameKey = backendSubscription.serverKey === vapidKey;
|
||||||
if (!sameEndpoint) {
|
if (!sameEndpoint) {
|
||||||
throw new Error('Backend subscription endpoint changed');
|
throw new Error('Backend subscription endpoint changed');
|
||||||
|
@ -146,7 +146,7 @@ export async function initSubscription() {
|
||||||
|
|
||||||
if (subscription && !backendSubscription) {
|
if (subscription && !backendSubscription) {
|
||||||
// check if account's vapidKey is same as subscription's applicationServerKey
|
// check if account's vapidKey is same as subscription's applicationServerKey
|
||||||
const { vapidKey } = getCurrentAccount();
|
const vapidKey = getVapidKey();
|
||||||
if (vapidKey) {
|
if (vapidKey) {
|
||||||
const { applicationServerKey } = subscription.options;
|
const { applicationServerKey } = subscription.options;
|
||||||
const vapidKeyStr = urlBase64ToUint8Array(vapidKey).toString();
|
const vapidKeyStr = urlBase64ToUint8Array(vapidKey).toString();
|
||||||
|
@ -210,7 +210,7 @@ export async function updateSubscription({ data, policy }) {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// User is not subscribed
|
// User is not subscribed
|
||||||
const { vapidKey } = getCurrentAccount();
|
const vapidKey = getVapidKey();
|
||||||
if (!vapidKey) throw new Error('No server key found');
|
if (!vapidKey) throw new Error('No server key found');
|
||||||
subscription = await registration.pushManager.subscribe({
|
subscription = await registration.pushManager.subscribe({
|
||||||
userVisibleOnly: true,
|
userVisibleOnly: true,
|
||||||
|
|
|
@ -154,6 +154,13 @@ export function getCurrentInstanceConfiguration() {
|
||||||
return getInstanceConfiguration(instance);
|
return getInstanceConfiguration(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getVapidKey() {
|
||||||
|
// Vapid key has moved from account to instance config
|
||||||
|
const config = getCurrentInstanceConfiguration();
|
||||||
|
const vapidKey = config?.vapid?.publicKey || config?.vapid?.public_key;
|
||||||
|
return vapidKey || getCurrentAccount()?.vapidKey;
|
||||||
|
}
|
||||||
|
|
||||||
export function isMediaFirstInstance() {
|
export function isMediaFirstInstance() {
|
||||||
const instance = getCurrentInstance();
|
const instance = getCurrentInstance();
|
||||||
return /pixelfed/i.test(instance?.version);
|
return /pixelfed/i.test(instance?.version);
|
||||||
|
|
Loading…
Add table
Reference in a new issue