Persist Sessions

This commit is contained in:
Josef 2025-11-16 14:30:12 +01:00
parent c1e65a2594
commit d8ebe48a76
7 changed files with 118 additions and 9 deletions

View File

@ -0,0 +1,30 @@
import { iTaskScheduler } from "../../scheduler/iTaskScheduler";
import { Task } from "../../scheduler/Task";
import { UserManagementServer } from "../../UserManagementServer";
import { UserApp } from "../UserApp";
import { AdminData } from "./data/AdminData";
export class AdminApp extends UserApp<AdminData> implements iTaskScheduler
{
static readonly id = "administration";
constructor( ums:UserManagementServer )
{
super( AdminApp.id, ums );
}
async getTasksToSchedule( maxDate:Date ):Promise<Task[]>
{
let tasks = [];
// await this.iterateUserData(
// ( userData:UserData, reminderData:ReminderData )=>
// {
// this.grabTasks( userData, reminderData, maxDate, tasks );
// return Promise.resolve();
// }
// );
return Promise.resolve( tasks );
}
}

View File

@ -0,0 +1,4 @@
export class AdminData
{
}

View File

@ -64,13 +64,14 @@ export class ReminderApp extends UserApp<ReminderData> implements iTaskScheduler
if ( DateMath.isBefore( date, maxDate ) )
{
let task = Task.createAt( date,
let task = Task.createAt( me.id, date,
()=>
{
this.ums.sendEmail( userData.email, me.subject, me.message );
}
);
task.setUserContext( this.id, userData.id );
tasks.push( task )
}
}

View File

@ -7,6 +7,7 @@ import { LogoutHandler } from "./_/logout";
import { RequestPasswordChangeHandler } from "./_/request-password-change";
import { SignUpHandler } from "./_/signup";
import { AppsCanUseHandler } from "./apps/can-use";
import { AppsDeleteTaskHandler } from "./apps/delete-task";
import { AppsLoadHandler } from "./apps/load";
import { AppsSaveHandler } from "./apps/save";
@ -29,6 +30,7 @@ export class HandlerGroups
new AppsCanUseHandler(),
new AppsSaveHandler(),
new AppsLoadHandler(),
new AppsDeleteTaskHandler()
];

View File

@ -0,0 +1,33 @@
import { RequestHandler, RequestType } from "../../RequestHandler";
import { FastifyRequest, FastifyReply } from 'fastify';
import { UserIsLoggedIn } from "../../requirements/user/UserIsLoggedIn";
import { UserCanUseApp } from "./UserCanUseApp";
import { FilesSync } from "../../../files/FilesSync";
import { Files } from "../../../files/Files";
export class AppsDeleteTaskHandler extends RequestHandler
{
static url = "/apps/delete-task";
constructor()
{
super( RequestType.POST, AppsDeleteTaskHandler.url, [ new UserIsLoggedIn(), new UserCanUseApp() ] );
}
async _handle( request:FastifyRequest, reply:FastifyReply )
{
let requestBody = request.body;
let appData = requestBody as { taskID: string, appID:string };
let user = await this.getUser();
let taskRemoved = this.ums.scheduler.cancelUserAppTask( appData.taskID, user.id, appData.appID );
if ( taskRemoved )
{
return this.sendInfo( "Task deleted: " + appData.appID + " " + appData.taskID );
}
return this.sendError( "Task not found: " + appData.appID + " " + appData.taskID );
}
}

View File

@ -5,6 +5,8 @@ import { DateMath } from '../../../browser/date/DateMath';
import { Task } from './Task';
import { iTaskScheduler } from './iTaskScheduler';
import { RJLog } from '../../log/RJLog';
import { DateFormatter } from '../../../browser/date/DateFormatter';
import { Duration } from '../../../browser/date/Duration';
export class Scheduler
{
@ -98,7 +100,7 @@ export class Scheduler
let delay = Math.max( 0, task.date.getTime() - Date.now() );
RJLog.log( "delaying:", delay );
RJLog.log( task.id, DateFormatter.HMS( task.date ), "delaying:", Duration.fromMilliSeconds( delay ) + " sec" );
this._taskTimerCallback = setTimeout(
() =>
@ -124,18 +126,45 @@ export class Scheduler
);
}
cancelTask( id:string ): void
cancelUserAppTask( taskID:string, userID:string, appID:string ):boolean
{
let index = this._queue.findIndex( e => e.id === id );
if ( ! taskID || ! userID || ! appID )
{
return false;
}
let index = this._queue.findIndex( e => e.id === taskID );
if ( index == -1 )
{
RJLog.log( "Task not found:", taskID );
return false;
}
let foundTask = this._queue[ index ];
if ( foundTask.userID !== userID || foundTask.appID !== appID )
{
RJLog.log( "Found task has not matching user:", foundTask.userID, foundTask.appID );
return false;
}
if ( index === -1 )
{
return;
return false;
}
Arrays.removeAt( this._queue, index );
if ( taskID !== this._nextTaskID )
{
return true;
}
this._resetTimer();
this._updateTimer();
return true;
}
}

View File

@ -6,20 +6,30 @@ import { DateMath } from '../../../browser/date/DateMath';
export class Task
{
id:string;
userID:string;
appID:string;
date:Date;
action:()=>void;
static createIn( duration:number, action:()=>void )
setUserContext( appID:string, userID:string )
{
let date = DateMath.fromNowAddSeconds( duration );
return Task.createAt( date, action );
this.appID = appID;
this.userID = userID;
return this;
}
static createAt( date:Date, action:()=>void )
static createIn( id:string, duration:number, action:()=>void )
{
let date = DateMath.fromNowAddSeconds( duration );
return Task.createAt( id, date, action );
}
static createAt( id:string, date:Date, action:()=>void )
{
let task = new Task();
task.id = crypto.randomUUID();
task.id = id;
task.action = action;
task.date = date;