shared/common/online-state.service.ts
Properties |
|
Methods |
|
constructor(redisService: IOnlineStateRedisService)
|
||||||||
Defined in shared/common/online-state.service.ts:7
|
||||||||
Parameters :
|
Private Readonly KEY_PREFIX |
Type : string
|
Default value : 'online_state'
|
Defined in shared/common/online-state.service.ts:6
|
Private Readonly redisService |
Type : IOnlineStateRedisService
|
Defined in shared/common/online-state.service.ts:7
|
Private getKey |
getKey(context: string, contextId: string)
|
Defined in shared/common/online-state.service.ts:16
|
Returns :
string
|
Async getOnlineList |
getOnlineList(context: string, contextId: string)
|
Defined in shared/common/online-state.service.ts:43
|
Returns :
Promise<OnlineState[]>
|
Async getOnlineStatus |
getOnlineStatus(context: string, contextId: string, userId: string)
|
Defined in shared/common/online-state.service.ts:49
|
Returns :
Promise<OnlineState | null>
|
Async setOffline |
setOffline(context: string, contextId: string, userId: string)
|
Defined in shared/common/online-state.service.ts:38
|
Returns :
Promise<void>
|
Async setOnline | ||||||||||||||||||||||||
setOnline(context: string, contextId: string, userId: string, data, ttlSeconds: number)
|
||||||||||||||||||||||||
Defined in shared/common/online-state.service.ts:20
|
||||||||||||||||||||||||
Parameters :
Returns :
Promise<void>
|
import { OnlineState } from '../interfaces/online-state';
import { OnlineStateStatus } from './enums/online-state-status.enum';
import { IOnlineStateRedisService } from './redis-service.interface';
export class OnlineStateService {
private readonly KEY_PREFIX = 'online_state';
private readonly redisService: IOnlineStateRedisService;
/**
* @param redisService An instance of AppRedisService (must implement the required Redis hash methods)
*/
constructor(redisService: IOnlineStateRedisService) {
this.redisService = redisService;
}
private getKey(context: string, contextId: string): string {
return `${this.KEY_PREFIX}_${context}_${contextId}`;
}
async setOnline(
context: string,
contextId: string,
userId: string,
data: Omit<OnlineState, 'status'> & { status?: OnlineStateStatus },
ttlSeconds = 3600,
): Promise<void> {
const key = this.getKey(context, contextId);
const value: OnlineState = {
...data,
userId,
status: data.status || OnlineStateStatus.ONLINE,
lastActivityAt: new Date().toISOString(),
};
await this.redisService.rhset(key, userId, value);
await this.redisService.rexpirein(key, ttlSeconds);
}
async setOffline(context: string, contextId: string, userId: string): Promise<void> {
const key = this.getKey(context, contextId);
await this.redisService.rhdel(key, userId);
}
async getOnlineList(context: string, contextId: string): Promise<OnlineState[]> {
const key = this.getKey(context, contextId);
const values = await this.redisService.rhvals(key);
return values.map((val: string) => JSON.parse(val));
}
async getOnlineStatus(context: string, contextId: string, userId: string): Promise<OnlineState | null> {
const key = this.getKey(context, contextId);
const value = await this.redisService.rhget(key, userId);
return value ? JSON.parse(value) : null;
}
}