This commit is contained in:
Philipp Heckel 2022-12-08 20:50:48 -05:00
parent 2e1ddc9ae1
commit 92bf7ebc52
11 changed files with 237 additions and 37 deletions

View file

@ -8,7 +8,7 @@ import {
topicUrlJsonPollWithSince,
userAccountUrl,
userTokenUrl,
userStatsUrl
userStatsUrl, userSubscriptionUrl, userSubscriptionDeleteUrl
} from "./utils";
import userManager from "./UserManager";
@ -186,6 +186,35 @@ class Api {
throw new Error(`Unexpected server response ${response.status}`);
}
}
async userSubscriptionAdd(baseUrl, token, payload) {
const url = userSubscriptionUrl(baseUrl);
const body = JSON.stringify(payload);
console.log(`[Api] Adding user subscription ${url}: ${body}`);
const response = await fetch(url, {
method: "POST",
headers: maybeWithBearerAuth({}, token),
body: body
});
if (response.status !== 200) {
throw new Error(`Unexpected server response ${response.status}`);
}
const subscription = await response.json();
console.log(`[Api] Subscription`, subscription);
return subscription;
}
async userSubscriptionDelete(baseUrl, token, remoteId) {
const url = userSubscriptionDeleteUrl(baseUrl, remoteId);
console.log(`[Api] Removing user subscription ${url}`);
const response = await fetch(url, {
method: "DELETE",
headers: maybeWithBearerAuth({}, token)
});
if (response.status !== 200) {
throw new Error(`Unexpected server response ${response.status}`);
}
}
}
const api = new Api();

View file

@ -18,17 +18,43 @@ class SubscriptionManager {
}
async add(baseUrl, topic) {
const id = topicUrl(baseUrl, topic);
const existingSubscription = await this.get(id);
if (existingSubscription) {
return existingSubscription;
}
const subscription = {
id: topicUrl(baseUrl, topic),
baseUrl: baseUrl,
topic: topic,
mutedUntil: 0,
last: null
last: null,
remoteId: null
};
await db.subscriptions.put(subscription);
return subscription;
}
async syncFromRemote(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);
remoteIds.push(remote.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];
if (local.remoteId && !remoteIds.includes(local.remoteId)) {
await this.remove(local.id);
}
}
}
async updateState(subscriptionId, state) {
db.subscriptions.update(subscriptionId, { state: state });
}
@ -139,6 +165,12 @@ class SubscriptionManager {
});
}
async setRemoteId(subscriptionId, remoteId) {
await db.subscriptions.update(subscriptionId, {
remoteId: remoteId
});
}
async pruneNotifications(thresholdTimestamp) {
await db.notifications
.where("time").below(thresholdTimestamp)

View file

@ -21,6 +21,8 @@ export const topicShortUrl = (baseUrl, topic) => shortUrl(topicUrl(baseUrl, topi
export const userStatsUrl = (baseUrl) => `${baseUrl}/user/stats`;
export const userTokenUrl = (baseUrl) => `${baseUrl}/user/token`;
export const userAccountUrl = (baseUrl) => `${baseUrl}/user/account`;
export const userSubscriptionUrl = (baseUrl) => `${baseUrl}/user/subscription`;
export const userSubscriptionDeleteUrl = (baseUrl, id) => `${baseUrl}/user/subscription/${id}`;
export const shortUrl = (url) => url.replaceAll(/https?:\/\//g, "");
export const expandUrl = (url) => [`https://${url}`, `http://${url}`];
export const expandSecureUrl = (url) => `https://${url}`;