[core] Add dedicated POST /api/{session}/groups/refresh endpoint

This commit is contained in:
devlikepro committed 2024-12-09 10:19:38 +07:00
1 parent 767d1bb3e3
commit 56009e482b
8 files changed
+46 -41

No files matched your search

+9 -3
View File
@@ -22,7 +22,6 @@ import { WhatsappSession } from '../core/abc/session.abc';
import {
CreateGroupRequest,
DescriptionRequest,
GetGroupsParams,
GroupsPaginationParams,
ParticipantsRequest,
SettingsSecurityChangeInfo,
@@ -52,9 +51,16 @@ export class GroupsController {
getGroups(
@WorkingSessionParam session: WhatsappSession,
@Query() pagination: GroupsPaginationParams,
@Query() params: GetGroupsParams,
) {
return session.getGroups(pagination, params.refresh);
return session.getGroups(pagination);
}
@Post('refresh')
@SessionApiParam
@ApiOperation({ summary: 'Refresh groups from the server.' })
@UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
async refreshGroups(@WorkingSessionParam session: WhatsappSession) {
return { success: await session.refreshGroups() };
}
@Get(':id')
+5 -1
View File
@@ -523,7 +523,11 @@ export abstract class WhatsappSession {
throw new NotImplementedByEngineError();
}
public getGroups(pagination: PaginationParams, refresh: boolean) {
public getGroups(pagination: PaginationParams) {
throw new NotImplementedByEngineError();
}
public refreshGroups(): Promise<boolean> {
throw new NotImplementedByEngineError();
}
+9 -3
View File
@@ -1084,14 +1084,20 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
return this.sock.groupCreate(request.name, participants);
}
public async getGroups(pagination: PaginationParams, refresh: boolean) {
const groups = await this.store.getGroups(pagination, refresh);
public async getGroups(pagination: PaginationParams) {
const groups = await this.store.getGroups(pagination);
// return {id: group} mapping for backward compatability
return lodash.keyBy(groups, 'id');
}
public async refreshGroups(): Promise<boolean> {
this.store.resetGroupsCache();
await this.store.getGroups({});
return true;
}
public async getGroup(id) {
const groups = await this.getGroups({}, false);
const groups = await this.getGroups({});
return groups[id];
}
+3 -4
View File
@@ -42,8 +42,7 @@ export interface INowebStore {
getChatLabels(chatId: string): Promise<Label[]>;
getGroups(
pagination: PaginationParams,
refresh: boolean,
): Promise<GroupMetadata[]>;
getGroups(pagination: PaginationParams): Promise<GroupMetadata[]>;
resetGroupsCache(): void;
}
@@ -89,13 +89,14 @@ export class NowebInMemoryStore implements INowebStore {
throw new BadRequestException(this.errorMessage);
}
async getGroups(
pagination: PaginationParams,
refresh: boolean,
): Promise<GroupMetadata[]> {
async getGroups(pagination: PaginationParams): Promise<GroupMetadata[]> {
const response = await this.socket?.groupFetchAllParticipating();
const groups = Object.values(response);
const paginator = new PaginatorInMemory(pagination);
return paginator.apply(groups);
}
resetGroupsCache() {
return;
}
}
@@ -57,6 +57,7 @@ export class NowebPersistentStore implements INowebStore {
});
private lastTimeGroupUpdate: Date = new Date(0);
private lastTimeGroupFetch: Date = new Date(0);
private GROUP_METADATA_CACHE_TIME = 24 * HOUR;
constructor(
@@ -453,15 +454,14 @@ export class NowebPersistentStore implements INowebStore {
return this.chatRepo.getAllWithMessages(pagination);
}
private shouldUpdateGroup(): boolean {
const timePassed =
new Date().getTime() - this.lastTimeGroupUpdate.getTime();
private shouldFetchGroup(): boolean {
const timePassed = new Date().getTime() - this.lastTimeGroupFetch.getTime();
return timePassed > this.GROUP_METADATA_CACHE_TIME;
}
private async fetchGroups() {
await this.groupsFetchLock.acquire('groups-fetch', async () => {
if (!this.shouldUpdateGroup()) {
if (!this.shouldFetchGroup()) {
// Update has been done by another request
return;
}
@@ -474,19 +474,16 @@ export class NowebPersistentStore implements INowebStore {
100,
5_000,
);
this.lastTimeGroupFetch = new Date();
});
}
async getGroups(
pagination: PaginationParams,
refresh: boolean,
): Promise<GroupMetadata[]> {
if (refresh) {
// Reset the last update time
this.lastTimeGroupUpdate = new Date(0);
}
resetGroupsCache() {
this.lastTimeGroupFetch = new Date(0);
}
if (this.shouldUpdateGroup()) {
async getGroups(pagination: PaginationParams): Promise<GroupMetadata[]> {
if (this.shouldFetchGroup()) {
await this.fetchGroups();
}
return this.groupRepo.getAll(pagination);
+5 -1
View File
@@ -790,13 +790,17 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
return groupChat.setMessagesAdminsOnly(value);
}
public async getGroups(pagination: PaginationParams, refresh: boolean) {
public async getGroups(pagination: PaginationParams) {
const chats = await this.whatsapp.getChats();
const groups = lodash.filter(chats, (chat) => chat.isGroup);
const paginator = new PaginatorInMemory(pagination);
return paginator.apply(groups);
}
public async refreshGroups(): Promise<boolean> {
return true;
}
public getGroup(id) {
return this.whatsapp.getChatById(id);
}
-12
View File
@@ -71,15 +71,3 @@ export class GroupsPaginationParams extends PaginationParams {
@IsEnum(GroupSortField)
sortBy?: string;
}
export class GetGroupsParams {
@ApiProperty({
description: 'Refresh the groups list and participants from the server',
example: false,
required: false,
})
@Transform(BooleanString)
@IsBoolean()
@IsOptional()
refresh: boolean = false;
}