diff --git a/src/modules/direct-messaging/direct-messaging.gateway.ts b/src/modules/direct-messaging/direct-messaging.gateway.ts index de748f1..a6023da 100644 --- a/src/modules/direct-messaging/direct-messaging.gateway.ts +++ b/src/modules/direct-messaging/direct-messaging.gateway.ts @@ -8,7 +8,6 @@ import { MessageBody, } from '@nestjs/websockets'; import { Server, Socket } from 'socket.io'; -import { JwtAuthGuard } from '../authentication/guards/jwt-auth.guard'; import { DirectMessagingService } from './direct-messaging.service'; import { ConnectionService } from '../connections/connections.service'; import { ListingType } from './direct-messaging.schema'; @@ -17,7 +16,6 @@ import { Logger, UnauthorizedException, } from '@nestjs/common'; -import { UseWsGuards } from 'src/shared/decorators/use-ws-guard'; import { AccessTokenService } from '../authentication/tokens/accesstoken.service'; @WebSocketGateway({ @@ -29,7 +27,6 @@ import { AccessTokenService } from '../authentication/tokens/accesstoken.service }, namespace: 'direct-messaging', }) -@UseWsGuards(JwtAuthGuard) export class DirectMessagingGateway implements OnGatewayConnection, OnGatewayDisconnect { @@ -153,16 +150,16 @@ export class DirectMessagingGateway data.conversationId, ); - conversation.users.forEach(async (participantId) => { - if (participantId.toString() !== userId) { - this.server.to(participantId.toString()).emit('newMessage', message); + conversation.users.forEach(async (participant) => { + if (participant._id.toString() !== userId) { + this.server.to(participant._id.toString()).emit('newMessage', message); const unreadCount = await this.directMessagingService.getUnreadMessageCount( - participantId.toString(), + participant._id.toString(), ); this.server - .to(participantId.toString()) + .to(participant._id.toString()) .emit('unreadMessageCount', unreadCount); } }); @@ -180,7 +177,6 @@ export class DirectMessagingGateway data.conversationId, data.before, ); - console.log(messages); client.emit('getMessagesResponse', messages); } diff --git a/src/modules/direct-messaging/direct-messaging.service.ts b/src/modules/direct-messaging/direct-messaging.service.ts index 5727c82..64a3a70 100644 --- a/src/modules/direct-messaging/direct-messaging.service.ts +++ b/src/modules/direct-messaging/direct-messaging.service.ts @@ -34,7 +34,6 @@ export class DirectMessagingService { }); if (existingConversation) { - console.log('Existing conversation found:', existingConversation); return existingConversation; } @@ -75,13 +74,12 @@ export class DirectMessagingService { page: number = 1, limit: number = 20, ): Promise { - const skip = (page - 1) * limit; // Calculate how many documents to skip - + const skip = (page - 1) * limit; return this.conversationModel .aggregate([ { $match: { - user_ids: new Types.ObjectId(userId), // Match conversations involving the user + users: new Types.ObjectId(userId), // Match conversations involving the user }, }, { @@ -119,9 +117,9 @@ export class DirectMessagingService { // Project the required fields (you can add/remove fields as needed) $project: { _id: 1, - user_ids: 1, - room_listing_id: 1, - roommate_listing_id: 1, + users: 1, + room_listing: 1, + roommate_listing: 1, listing_type: 1, created_at: 1, updated_at: 1, diff --git a/src/modules/direct-messaging/test.html b/src/modules/direct-messaging/test.html index 91ae7bf..a4579c1 100644 --- a/src/modules/direct-messaging/test.html +++ b/src/modules/direct-messaging/test.html @@ -76,7 +76,7 @@

Conversations:

function connectToWebSocket() { const token = document.getElementById('token').value; - socket = io('http://localhost:3000/direct-messaging', { + socket = io('https://tgbsldrj-3000.uks1.devtunnels.ms/direct-messaging', { auth: { token: token } @@ -122,7 +122,7 @@

Conversations:

const listingId = document.getElementById('listingId').value; socket.emit('createConversation', { - userIds: [userId, '670fbaf758cfd6a751e911f1'], // example userId to create conversation with + userIds: [userId, '66e34873662945c5adfa7b86'], // example userId to create conversation with listingType: listingType, listingId: listingId });