Skip to content

Commit

Permalink
fix: messaging issue
Browse files Browse the repository at this point in the history
  • Loading branch information
alexindevs committed Oct 16, 2024
1 parent 140b9de commit 69edf88
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
14 changes: 5 additions & 9 deletions src/modules/direct-messaging/direct-messaging.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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({
Expand All @@ -29,7 +27,6 @@ import { AccessTokenService } from '../authentication/tokens/accesstoken.service
},
namespace: 'direct-messaging',
})
@UseWsGuards(JwtAuthGuard)
export class DirectMessagingGateway
implements OnGatewayConnection, OnGatewayDisconnect
{
Expand Down Expand Up @@ -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);
}
});
Expand All @@ -180,7 +177,6 @@ export class DirectMessagingGateway
data.conversationId,
data.before,
);
console.log(messages);

client.emit('getMessagesResponse', messages);
}
Expand Down
12 changes: 5 additions & 7 deletions src/modules/direct-messaging/direct-messaging.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export class DirectMessagingService {
});

if (existingConversation) {
console.log('Existing conversation found:', existingConversation);
return existingConversation;
}

Expand Down Expand Up @@ -75,13 +74,12 @@ export class DirectMessagingService {
page: number = 1,
limit: number = 20,
): Promise<ConversationDocument[]> {
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
},
},
{
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/modules/direct-messaging/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ <h3>Conversations:</h3>
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
}
Expand Down Expand Up @@ -122,7 +122,7 @@ <h3>Conversations:</h3>
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
});
Expand Down

0 comments on commit 69edf88

Please sign in to comment.