Remove awkward subscription id
This commit is contained in:
parent
9131d3d521
commit
cc309e87e9
12 changed files with 67 additions and 104 deletions
|
@ -173,9 +173,12 @@ class AccountApi {
|
|||
});
|
||||
}
|
||||
|
||||
async addSubscription(payload) {
|
||||
async addSubscription(baseUrl, topic) {
|
||||
const url = accountSubscriptionUrl(config.base_url);
|
||||
const body = JSON.stringify(payload);
|
||||
const body = JSON.stringify({
|
||||
base_url: baseUrl,
|
||||
topic: topic
|
||||
});
|
||||
console.log(`[AccountApi] Adding user subscription ${url}: ${body}`);
|
||||
const response = await fetchOrThrow(url, {
|
||||
method: "POST",
|
||||
|
@ -187,9 +190,13 @@ class AccountApi {
|
|||
return subscription;
|
||||
}
|
||||
|
||||
async updateSubscription(remoteId, payload) {
|
||||
const url = accountSubscriptionSingleUrl(config.base_url, remoteId);
|
||||
const body = JSON.stringify(payload);
|
||||
async updateSubscription(baseUrl, topic, payload) {
|
||||
const url = accountSubscriptionUrl(config.base_url);
|
||||
const body = JSON.stringify({
|
||||
base_url: baseUrl,
|
||||
topic: topic,
|
||||
...payload
|
||||
});
|
||||
console.log(`[AccountApi] Updating user subscription ${url}: ${body}`);
|
||||
const response = await fetchOrThrow(url, {
|
||||
method: "PATCH",
|
||||
|
@ -201,12 +208,16 @@ class AccountApi {
|
|||
return subscription;
|
||||
}
|
||||
|
||||
async deleteSubscription(remoteId) {
|
||||
const url = accountSubscriptionSingleUrl(config.base_url, remoteId);
|
||||
async deleteSubscription(baseUrl, topic) {
|
||||
const url = accountSubscriptionUrl(config.base_url);
|
||||
console.log(`[AccountApi] Removing user subscription ${url}`);
|
||||
const headers = {
|
||||
"X-BaseURL": baseUrl,
|
||||
"X-Topic": topic,
|
||||
}
|
||||
await fetchOrThrow(url, {
|
||||
method: "DELETE",
|
||||
headers: withBearerAuth({}, session.token())
|
||||
headers: withBearerAuth(headers, session.token()),
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -29,7 +29,6 @@ class SubscriptionManager {
|
|||
topic: topic,
|
||||
mutedUntil: 0,
|
||||
last: null,
|
||||
remoteId: null,
|
||||
internal: internal || false
|
||||
};
|
||||
await db.subscriptions.put(subscription);
|
||||
|
@ -40,24 +39,23 @@ class SubscriptionManager {
|
|||
console.log(`[SubscriptionManager] Syncing subscriptions from remote`, remoteSubscriptions);
|
||||
|
||||
// Add remote subscriptions
|
||||
let remoteIds = [];
|
||||
let remoteIds = []; // = topicUrl(baseUrl, topic)
|
||||
for (let i = 0; i < remoteSubscriptions.length; i++) {
|
||||
const remote = remoteSubscriptions[i];
|
||||
const local = await this.add(remote.base_url, remote.topic);
|
||||
const local = await this.add(remote.base_url, remote.topic, false);
|
||||
const reservation = remoteReservations?.find(r => remote.base_url === config.base_url && remote.topic === r.topic) || null;
|
||||
await this.update(local.id, {
|
||||
remoteId: remote.id,
|
||||
displayName: remote.display_name, // May be undefined
|
||||
reservation: reservation // May be null!
|
||||
});
|
||||
remoteIds.push(remote.id);
|
||||
remoteIds.push(local.id);
|
||||
}
|
||||
|
||||
// Remove local subscriptions that do not exist remotely
|
||||
const localSubscriptions = await db.subscriptions.toArray();
|
||||
for (let i = 0; i < localSubscriptions.length; i++) {
|
||||
const local = localSubscriptions[i];
|
||||
const remoteExists = local.remoteId && remoteIds.includes(local.remoteId);
|
||||
const remoteExists = remoteIds.includes(local.id);
|
||||
if (!local.internal && !remoteExists) {
|
||||
await this.remove(local.id);
|
||||
}
|
||||
|
@ -174,12 +172,6 @@ class SubscriptionManager {
|
|||
});
|
||||
}
|
||||
|
||||
async setRemoteId(subscriptionId, remoteId) {
|
||||
await db.subscriptions.update(subscriptionId, {
|
||||
remoteId: remoteId
|
||||
});
|
||||
}
|
||||
|
||||
async setReservation(subscriptionId, reservation) {
|
||||
await db.subscriptions.update(subscriptionId, {
|
||||
reservation: reservation
|
||||
|
|
|
@ -23,7 +23,6 @@ export const accountPasswordUrl = (baseUrl) => `${baseUrl}/v1/account/password`;
|
|||
export const accountTokenUrl = (baseUrl) => `${baseUrl}/v1/account/token`;
|
||||
export const accountSettingsUrl = (baseUrl) => `${baseUrl}/v1/account/settings`;
|
||||
export const accountSubscriptionUrl = (baseUrl) => `${baseUrl}/v1/account/subscription`;
|
||||
export const accountSubscriptionSingleUrl = (baseUrl, id) => `${baseUrl}/v1/account/subscription/${id}`;
|
||||
export const accountReservationUrl = (baseUrl) => `${baseUrl}/v1/account/reservation`;
|
||||
export const accountReservationSingleUrl = (baseUrl, topic) => `${baseUrl}/v1/account/reservation/${topic}`;
|
||||
export const accountBillingSubscriptionUrl = (baseUrl) => `${baseUrl}/v1/account/billing/subscription`;
|
||||
|
|
|
@ -299,11 +299,7 @@ export const subscribeTopic = async (baseUrl, topic) => {
|
|||
const subscription = await subscriptionManager.add(baseUrl, topic);
|
||||
if (session.exists()) {
|
||||
try {
|
||||
const remoteSubscription = await accountApi.addSubscription({
|
||||
base_url: baseUrl,
|
||||
topic: topic
|
||||
});
|
||||
await subscriptionManager.setRemoteId(subscription.id, remoteSubscription.id);
|
||||
await accountApi.addSubscription(baseUrl, topic);
|
||||
} catch (e) {
|
||||
console.log(`[SubscribeDialog] Subscribing to topic ${topic} failed`, e);
|
||||
if (e instanceof UnauthorizedError) {
|
||||
|
|
|
@ -109,9 +109,9 @@ export const SubscriptionPopup = (props) => {
|
|||
const handleUnsubscribe = async () => {
|
||||
console.log(`[SubscriptionPopup] Unsubscribing from ${props.subscription.id}`, props.subscription);
|
||||
await subscriptionManager.remove(props.subscription.id);
|
||||
if (session.exists() && props.subscription.remoteId) {
|
||||
if (session.exists() && !subscription.internal) {
|
||||
try {
|
||||
await accountApi.deleteSubscription(props.subscription.remoteId);
|
||||
await accountApi.deleteSubscription(props.subscription.baseUrl, props.subscription.topic);
|
||||
} catch (e) {
|
||||
console.log(`[SubscriptionPopup] Error unsubscribing`, e);
|
||||
if (e instanceof UnauthorizedError) {
|
||||
|
@ -198,10 +198,10 @@ const DisplayNameDialog = (props) => {
|
|||
|
||||
const handleSave = async () => {
|
||||
await subscriptionManager.setDisplayName(subscription.id, displayName);
|
||||
if (session.exists() && subscription.remoteId) {
|
||||
if (session.exists() && !subscription.internal) {
|
||||
try {
|
||||
console.log(`[SubscriptionSettingsDialog] Updating subscription display name to ${displayName}`);
|
||||
await accountApi.updateSubscription(subscription.remoteId, { display_name: displayName });
|
||||
await accountApi.updateSubscription(subscription.baseUrl, subscription.topic, { display_name: displayName });
|
||||
} catch (e) {
|
||||
console.log(`[SubscriptionSettingsDialog] Error updating subscription`, e);
|
||||
if (e instanceof UnauthorizedError) {
|
||||
|
|
|
@ -100,11 +100,7 @@ export const useAutoSubscribe = (subscriptions, selected) => {
|
|||
const subscription = await subscriptionManager.add(baseUrl, params.topic);
|
||||
if (session.exists()) {
|
||||
try {
|
||||
const remoteSubscription = await accountApi.addSubscription({
|
||||
base_url: baseUrl,
|
||||
topic: params.topic
|
||||
});
|
||||
await subscriptionManager.setRemoteId(subscription.id, remoteSubscription.id);
|
||||
await accountApi.addSubscription(baseUrl, params.topic);
|
||||
} catch (e) {
|
||||
console.log(`[Hooks] Auto-subscribing failed`, e);
|
||||
if (e instanceof UnauthorizedError) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue