Display name sync

This commit is contained in:
binwiederhier 2022-12-25 22:29:55 -05:00
parent 7ae8049438
commit 2fb4bd4975
12 changed files with 897 additions and 793 deletions

View file

@ -175,6 +175,25 @@ class AccountApi {
return subscription;
}
async updateSubscription(remoteId, payload) {
const url = accountSubscriptionSingleUrl(config.baseUrl, remoteId);
const body = JSON.stringify(payload);
console.log(`[AccountApi] Updating user subscription ${url}: ${body}`);
const response = await fetch(url, {
method: "PATCH",
headers: maybeWithBearerAuth({}, session.token()),
body: body
});
if (response.status === 401 || response.status === 403) {
throw new UnauthorizedError();
} else if (response.status !== 200) {
throw new Error(`Unexpected server response ${response.status}`);
}
const subscription = await response.json();
console.log(`[AccountApi] Subscription`, subscription);
return subscription;
}
async deleteSubscription(remoteId) {
const url = accountSubscriptionSingleUrl(config.baseUrl, remoteId);
console.log(`[AccountApi] Removing user subscription ${url}`);

View file

@ -1,3 +1,5 @@
import routes from "../components/routes";
class Session {
store(username, token) {
localStorage.setItem("user", username);
@ -9,6 +11,11 @@ class Session {
localStorage.removeItem("token");
}
resetAndRedirect(url) {
this.reset();
window.location.href = url;
}
exists() {
return this.username() && this.token();
}

View file

@ -36,12 +36,15 @@ class SubscriptionManager {
}
async syncFromRemote(remoteSubscriptions) {
console.log(`[SubscriptionManager] Syncing subscriptions from remote`, remoteSubscriptions);
// Add remote subscriptions
let remoteIds = [];
for (let i = 0; i < remoteSubscriptions.length; i++) {
const remote = remoteSubscriptions[i];
const local = await this.add(remote.base_url, remote.topic);
await this.setRemoteId(local.id, remote.id);
await this.setDisplayName(local.id, remote.display_name);
remoteIds.push(remote.id);
}
@ -49,7 +52,7 @@ class SubscriptionManager {
const localSubscriptions = await db.subscriptions.toArray();
for (let i = 0; i < localSubscriptions.length; i++) {
const local = localSubscriptions[i];
if (local.remoteId && !remoteIds.includes(local.remoteId)) {
if (!local.remoteId || !remoteIds.includes(local.remoteId)) {
await this.remove(local.id);
}
}