Fixing the issue with complete deletion from the database

This commit is contained in:
2026-09-29 21:46:41 +03:30
parent 88dc7c5a1c
commit 5bb8e68521
6 changed files with 276 additions and 52 deletions
@@ -1,4 +1,4 @@
import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common';
import { Body, Controller, Get, Post, Query, UseGuards } from '@nestjs/common';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { CurrentUser } from '../auth/current-user.decorator';
import { RequireOrgModule } from '../billing/org-module.decorator';
@@ -6,6 +6,7 @@ import { OrgModuleGuard } from '../billing/org-module.guard';
import {
CreateConversationDmDto,
CreateConversationGroupDto,
HideConversationMessagesDto,
} from './dto';
import { ConversationsService } from './conversations.service';
@@ -61,4 +62,31 @@ export class ConversationsController {
dto.memberUserIds,
);
}
@Post('messages/hide')
hideMessages(
@CurrentUser() user: { userId: string; organizationId: string; role: string },
@Body() dto: HideConversationMessagesDto,
) {
return this.conversations.hideMessages(
user.userId,
user.organizationId,
user.role,
dto.externalRoomId,
dto.messageIds,
);
}
@Get('messages/hidden')
listHidden(
@CurrentUser() user: { userId: string; organizationId: string; role: string },
@Query('externalRoomId') externalRoomId: string,
) {
return this.conversations.listHiddenMessageIds(
user.userId,
user.organizationId,
user.role,
externalRoomId || '',
);
}
}
@@ -443,6 +443,9 @@ export class ConversationsService {
let preview = lastMsg
? String(lastMsg.content ?? lastMsg.text ?? lastMsg.body ?? '').trim()
: '';
if (/^__MENTO_HIDE__:/i.test(preview) || /MENTO_DEL/i.test(preview)) {
preview = '';
}
if (lastType === 'image' || lastType === 'photo' || lastType === 'img') {
if (/(?:^|[\\/._-])vid-\d+/i.test(preview) || /\.(mp4|mov|m4v|mkv|avi|3gp)(?:\?|$)/i.test(preview)) {
preview = 'ویدیو';
@@ -490,4 +493,54 @@ export class ConversationsService {
peerUserId,
};
}
async hideMessages(
userId: string,
organizationId: string,
role: string,
externalRoomId: string,
messageIds: string[],
): Promise<{ messageIds: string[] }> {
await this.loadAccess(userId, organizationId, role);
const roomId = externalRoomId.trim();
if (!roomId) throw new BadRequestException('شناسه اتاق خالی است');
const ids = [
...new Set(
messageIds
.map((id) => String(id || '').trim())
.filter((id) => id.length > 0 && id.length <= 200),
),
].slice(0, 100);
if (!ids.length) throw new BadRequestException('شناسه پیام خالی است');
await this.prisma.conversationHiddenMessage.createMany({
data: ids.map((messageId) => ({
organizationId,
externalRoomId: roomId,
messageId,
hiddenByUserId: userId,
})),
skipDuplicates: true,
});
return { messageIds: ids };
}
async listHiddenMessageIds(
userId: string,
organizationId: string,
role: string,
externalRoomId: string,
): Promise<{ messageIds: string[] }> {
await this.loadAccess(userId, organizationId, role);
const roomId = externalRoomId.trim();
if (!roomId) throw new BadRequestException('شناسه اتاق خالی است');
const rows = await this.prisma.conversationHiddenMessage.findMany({
where: { organizationId, externalRoomId: roomId },
select: { messageId: true },
take: 5000,
});
return { messageIds: rows.map((r) => r.messageId) };
}
}
+13
View File
@@ -49,3 +49,16 @@ export class CreateConversationGroupDto {
@IsString({ each: true })
memberUserIds!: string[];
}
export class HideConversationMessagesDto {
@IsString()
@MinLength(1)
@MaxLength(200)
externalRoomId!: string;
@IsArray()
@ArrayMinSize(1)
@ArrayMaxSize(100)
@IsString({ each: true })
messageIds!: string[];
}