mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Replace expiration feature with startAt and endAt
This commit is contained in:
@@ -31,7 +31,8 @@ export class KeyValueSubscriptionStorage<T extends Record<string, unknown>> impl
|
||||
topic: subscription.topic,
|
||||
type: subscription.type,
|
||||
lastEmit: 0,
|
||||
expiration: subscription.expiration,
|
||||
startAt: subscription.startAt,
|
||||
endAt: subscription.endAt,
|
||||
accept: subscription.accept,
|
||||
rate: subscription.rate,
|
||||
state: subscription.state,
|
||||
@@ -42,7 +43,7 @@ export class KeyValueSubscriptionStorage<T extends Record<string, unknown>> impl
|
||||
public async get(id: string): Promise<SubscriptionInfo<T> | undefined> {
|
||||
const info = await this.storage.get(id);
|
||||
if (info && this.isSubscriptionInfo(info)) {
|
||||
if (typeof info.expiration === 'number' && info.expiration < Date.now()) {
|
||||
if (typeof info.endAt === 'number' && info.endAt < Date.now()) {
|
||||
this.logger.info(`Subscription ${id} has expired.`);
|
||||
await this.locker.withWriteLock(this.getLockKey(id), async(): Promise<void> => {
|
||||
await this.deleteInfo(info);
|
||||
|
||||
@@ -45,10 +45,16 @@ export class ListeningActivityHandler extends StaticHandler {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Don't emit if the previous notification was too recent according to the requested rate
|
||||
if (info.rate && info.rate > Date.now() - info.lastEmit) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Don't emit if we have not yet reached the requested starting time
|
||||
if (info.startAt && info.startAt > Date.now()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// No need to wait on this to resolve before going to the next subscription.
|
||||
// Prevent failed notification from blocking other notifications.
|
||||
this.handler.handleSafe({ info, activity, topic }).catch((error): void => {
|
||||
|
||||
@@ -79,9 +79,9 @@ export class NotificationSubscriber extends OperationHttpHandler {
|
||||
}
|
||||
|
||||
if (this.maxDuration) {
|
||||
const duration = (subscription.expiration ?? Number.POSITIVE_INFINITY) - Date.now();
|
||||
const duration = (subscription.endAt ?? Number.POSITIVE_INFINITY) - Date.now();
|
||||
if (duration > this.maxDuration) {
|
||||
subscription.expiration = Date.now() + this.maxDuration;
|
||||
subscription.endAt = Date.now() + this.maxDuration;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,10 @@ export const SUBSCRIBE_SCHEMA = object({
|
||||
type: string().required(),
|
||||
topic: string().required(),
|
||||
state: string().optional(),
|
||||
expiration: number().transform((value, original): number | undefined =>
|
||||
startAt: number().transform((value, original): number | undefined =>
|
||||
// Convert the date string to milliseconds
|
||||
Date.parse(original)).optional(),
|
||||
endAt: number().transform((value, original): number | undefined =>
|
||||
// Convert the date string to milliseconds
|
||||
Date.parse(original)).optional(),
|
||||
rate: number().transform((value, original): number | undefined =>
|
||||
|
||||
@@ -9,7 +9,8 @@ export type SubscriptionInfo<T = Record<string, unknown>> = {
|
||||
id: string;
|
||||
topic: string;
|
||||
type: string;
|
||||
expiration?: number;
|
||||
startAt?: number;
|
||||
endAt?: number;
|
||||
accept?: string;
|
||||
rate?: number;
|
||||
state?: string;
|
||||
|
||||
Reference in New Issue
Block a user