feat: implement per-user chat clearing by filtering messages based on new clearedAt timestamps
This commit is contained in:
@@ -503,6 +503,10 @@ model Conversation {
|
|||||||
userFavorited Boolean @default(false)
|
userFavorited Boolean @default(false)
|
||||||
agentFavorited Boolean @default(false)
|
agentFavorited Boolean @default(false)
|
||||||
|
|
||||||
|
// Clear chat (per-user) — messages before this timestamp are hidden for that user
|
||||||
|
userClearedAt DateTime?
|
||||||
|
agentClearedAt DateTime?
|
||||||
|
|
||||||
// Timestamps
|
// Timestamps
|
||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
updatedAt DateTime @updatedAt
|
updatedAt DateTime @updatedAt
|
||||||
|
|||||||
@@ -343,8 +343,12 @@ export class MessagesService {
|
|||||||
// Map to include unread count from agent perspective
|
// Map to include unread count from agent perspective
|
||||||
return conversations.map((conv) => {
|
return conversations.map((conv) => {
|
||||||
const showStatus = this.shouldShowOnlineStatus(conv.user.privacyPreferences);
|
const showStatus = this.shouldShowOnlineStatus(conv.user.privacyPreferences);
|
||||||
|
const cleared = conv.agentClearedAt;
|
||||||
|
const isCleared = cleared && conv.lastMessageAt && new Date(cleared) >= new Date(conv.lastMessageAt);
|
||||||
return {
|
return {
|
||||||
...conv,
|
...conv,
|
||||||
|
lastMessageText: isCleared ? null : conv.lastMessageText,
|
||||||
|
lastMessageAt: isCleared ? null : conv.lastMessageAt,
|
||||||
unreadCount: conv.agentUnreadCount,
|
unreadCount: conv.agentUnreadCount,
|
||||||
otherParty: {
|
otherParty: {
|
||||||
id: conv.user.id,
|
id: conv.user.id,
|
||||||
@@ -388,8 +392,12 @@ export class MessagesService {
|
|||||||
// Map to include unread count from user perspective
|
// Map to include unread count from user perspective
|
||||||
return conversations.map((conv) => {
|
return conversations.map((conv) => {
|
||||||
const showStatus = this.shouldShowOnlineStatus(conv.agentProfile.user.privacyPreferences);
|
const showStatus = this.shouldShowOnlineStatus(conv.agentProfile.user.privacyPreferences);
|
||||||
|
const cleared = conv.userClearedAt;
|
||||||
|
const isCleared = cleared && conv.lastMessageAt && new Date(cleared) >= new Date(conv.lastMessageAt);
|
||||||
return {
|
return {
|
||||||
...conv,
|
...conv,
|
||||||
|
lastMessageText: isCleared ? null : conv.lastMessageText,
|
||||||
|
lastMessageAt: isCleared ? null : conv.lastMessageAt,
|
||||||
unreadCount: conv.userUnreadCount,
|
unreadCount: conv.userUnreadCount,
|
||||||
otherParty: {
|
otherParty: {
|
||||||
id: conv.agentProfile.id,
|
id: conv.agentProfile.id,
|
||||||
@@ -437,9 +445,16 @@ export class MessagesService {
|
|||||||
|
|
||||||
const skip = (page - 1) * limit;
|
const skip = (page - 1) * limit;
|
||||||
|
|
||||||
|
// Filter out messages cleared by this user
|
||||||
|
const clearedAt = isUser ? conversation.userClearedAt : conversation.agentClearedAt;
|
||||||
|
const messageWhere: any = { conversationId };
|
||||||
|
if (clearedAt) {
|
||||||
|
messageWhere.createdAt = { gt: clearedAt };
|
||||||
|
}
|
||||||
|
|
||||||
const [messages, total] = await Promise.all([
|
const [messages, total] = await Promise.all([
|
||||||
this.prisma.message.findMany({
|
this.prisma.message.findMany({
|
||||||
where: { conversationId },
|
where: messageWhere,
|
||||||
include: {
|
include: {
|
||||||
sender: {
|
sender: {
|
||||||
select: {
|
select: {
|
||||||
@@ -466,7 +481,7 @@ export class MessagesService {
|
|||||||
skip,
|
skip,
|
||||||
take: limit,
|
take: limit,
|
||||||
}),
|
}),
|
||||||
this.prisma.message.count({ where: { conversationId } }),
|
this.prisma.message.count({ where: messageWhere }),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -704,7 +719,8 @@ export class MessagesService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear all messages in a conversation (keep the conversation itself)
|
* Clear chat for the requesting user only (messages are hidden, not deleted)
|
||||||
|
* The other party still sees all messages.
|
||||||
*/
|
*/
|
||||||
async clearChat(conversationId: string, userId: string) {
|
async clearChat(conversationId: string, userId: string) {
|
||||||
const conversation = await this.prisma.conversation.findUnique({
|
const conversation = await this.prisma.conversation.findUnique({
|
||||||
@@ -727,19 +743,14 @@ export class MessagesService {
|
|||||||
throw new ForbiddenException('You are not part of this conversation');
|
throw new ForbiddenException('You are not part of this conversation');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete all messages
|
const now = new Date();
|
||||||
await this.prisma.message.deleteMany({
|
|
||||||
where: { conversationId },
|
|
||||||
});
|
|
||||||
|
|
||||||
// Reset conversation preview
|
// Set clearedAt timestamp for the requesting user only
|
||||||
await this.prisma.conversation.update({
|
await this.prisma.conversation.update({
|
||||||
where: { id: conversationId },
|
where: { id: conversationId },
|
||||||
data: {
|
data: {
|
||||||
lastMessageAt: null,
|
...(isUser ? { userClearedAt: now, userUnreadCount: 0 } : {}),
|
||||||
lastMessageText: null,
|
...(isAgent ? { agentClearedAt: now, agentUnreadCount: 0 } : {}),
|
||||||
userUnreadCount: 0,
|
|
||||||
agentUnreadCount: 0,
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user