Skip to content

Commit 3f14b5f

Browse files
author
Rishabh Garg
committed
Show Last Message in conversation
user online status in conversation
1 parent c4720d3 commit 3f14b5f

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

app/config/socket.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import { AuthUtil } from "../utils/auth.util";
22
import { MessageRepository } from "../repositories/message.repository";
33
import { MessageModel } from "../models/message.model";
4+
import { UserRepository } from "../repositories/user.repository";
45

56
const io = require('socket.io')();
67
export const SocketConf: any = {};
78
export class Socket {
89
private auth: AuthUtil;
910
private messageRepository: MessageRepository;
11+
private userRepository: UserRepository;
1012
constructor() {
1113
this.auth = new AuthUtil();
1214
this.messageRepository = new MessageRepository();
15+
this.userRepository = new UserRepository();
1316
}
1417
init(server) {
1518
SocketConf.io = io.listen(server, {
@@ -37,9 +40,14 @@ export class Socket {
3740
SocketConf.io.on('connect', socket => {
3841
console.log('Client Connectted');
3942
socket.join(socket.handshake.query.loggedUser._id);
40-
socket.on('join', room => {
43+
socket.on('join', async (room) => {
4144
socket.join(room.roomName);
4245
io.to(room.roomName).emit('New User Connected...');
46+
await this.userRepository.updateWithoutSet(
47+
{
48+
"_id": { "$eq": socket.handshake.query.loggedUser._id },
49+
}, { "$addToSet": { sockets: socket.id } }, {}
50+
);
4351
});
4452

4553
socket.on('message', async (messageObject: MessageModel) => {
@@ -52,7 +60,7 @@ export class Socket {
5260
messageObject.members.filter(user => user._id !== messageObject.sender).forEach(user => {
5361
if (io.sockets.adapter.rooms[user._id] && io.sockets.adapter.rooms[user._id].length) {
5462
io.to(user._id).emit('unseen-message', message);
55-
}
63+
}
5664
});
5765
});
5866

@@ -69,11 +77,16 @@ export class Socket {
6977
}, { "$addToSet": { seen: socket.handshake.query.loggedUser._id } }, { multi: true }
7078
);
7179
});
72-
socket.on('disconnect', function () {
80+
socket.on('disconnect', async () => {
7381
console.log('disconnected event');
82+
await this.userRepository.updateWithoutSet(
83+
{
84+
"_id": { "$eq": socket.handshake.query.loggedUser._id },
85+
}, { "$pull": { sockets: socket.id } }, { multi: true }
86+
);
7487
//socket.manager.onClientDisconnect(socket.id); --> endless loop with this disconnect event on server side
7588
//socket.disconnect(); --> same here
76-
});
89+
});
7790
});
7891
// Added Disconnection FUnction of socket
7992
// Remove client from all joined roomes once socket is disconnected

app/controllers/conversation.controller.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,29 @@ export class ConversationController {
4545
}
4646
},
4747
{ $project: { seenObject: { $arrayElemAt: ["$messageUnread", 0] }, "members": 1, "createdAt": 1, "isGroup": 1, groupName: 1 } },
48-
{ $project: { unreadCount: "$seenObject.count", "members": 1, "createdAt": 1, "isGroup": 1, groupName: 1 } },
48+
{
49+
$lookup: {
50+
from: "messages",
51+
let: {
52+
conversation_id: "$_id"
53+
},
54+
pipeline: [
55+
{
56+
$match: {
57+
$expr: {
58+
$and: [
59+
{ $eq: ["$$conversation_id", "$conversation"] },
60+
]
61+
}
62+
}
63+
},
64+
{ $sort: { _id: -1 } },
65+
{ $limit: 1 }
66+
67+
],
68+
as: "lastMessages"
69+
}
70+
},
4971
{
5072
$lookup: {
5173
from: "users",
@@ -54,6 +76,8 @@ export class ConversationController {
5476
as: "members"
5577
}
5678
},
79+
{ $project: { lastMessage: { $arrayElemAt: ["$lastMessages", 0] }, unreadCount: "$seenObject.count", "members": 1, "createdAt": 1, "isGroup": 1, groupName: 1 } },
80+
5781
]);
5882
console.log(conversations);
5983
response.send(CumtomResponse.success(conversations, 'Conversations fetched'));

app/models/user.model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export interface UserModel extends mongoose.Document {
44
fullName?: string;
55
gender?: string;
66
profile?: string;
7+
sockets?: string[];
78
}

app/schemas/user.schema.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class UserSchema {
1515
profile: {
1616
type: String
1717
},
18+
sockets: [{
19+
type: String
20+
}]
1821
});
1922
return schema;
2023
}

0 commit comments

Comments
 (0)