-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatController.js
More file actions
173 lines (128 loc) · 4.69 KB
/
chatController.js
File metadata and controls
173 lines (128 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const { isValidObjectId, Types } = require('mongoose')
const { catchAsync, appError } = require('./errorController')
const Chat = require('../models/chatModel')
const { filterObjectByArray, apiFeatures } = require('../utils')
const Message = require('../models/messageModal')
// GET /api/chats/ + protected
exports.getAllChats = catchAsync( async (req, res, next) => {
const userId = req.session.user._id
// Shows all chat's of current user
const filter = { users: { $elemMatch: { $eq: userId }} }
// // Shows all chat of Group Users
// const filter = {
// isGroup: true, // Only show group chat
// users: {
// $elemMatch: { $eq: userId } // of currentUser
// }
// }
let chats = await apiFeatures(Chat, req.query, filter).populate('users latestMessage')
// // Find which users has logedIn user._id ==> Find Group of user self exists
// const chats = await Chat.find({ users: { $elemMatch: { $eq: userId }} })
// .sort('createdAt: -1')
// .populate('users latestMessage') // direct property
// chats = await Message.populate(chats, 'latestMessage.sender') // populated by others, then get sender
//- We need chats[0].latestMessage.users, But why ?
/* For Notification Badge: fetch('/api/chats?unreadOnly="true"')
those latestMessage (message) don't have us, just ignore those messages for notification badge
*/
// if(req.query.unreadOnly === 'true') {
// // chats = chats.filter( chat => chat.latestMessage.users.includes( userId ))
// chats.filter( (chat) => {
// chat.latestMessage.users.includes( userId )
// console.log(chat.latestMessage.users, userId)
// // return
// })
// }
// console.log(chats)
// console.log(chats[0].latestMessage.users)
// console.log(chats)
res.status(200).json({
status: 'success',
count: chats.length,
data: chats
})
})
// GET /api/chats/:id + protected
exports.getChatById = catchAsync( async (req, res, next) => {
const chatId = req.params.id
const logedInUserId = req.session.user._id
if( !isValidObjectId(chatId) ) return next(appError('invalid id'))
// handle group chat by chatId
const groupChat = await Chat.findOne({
_id: chatId,
users: { $elemMatch: { $eq: logedInUserId }}
}).populate('users')
if(!groupChat) {
/* if groupChat not exists, then it must be user-to-user single chat
and the chat id will be userid instead: [ chatId === userId ]
if logedInUser already have created chat with same user that
find that chat, else create new user to user chat
instead of creating 2 condition we can solve that by mongoDB
update method with `upsert` keywork, which will create if update failed
*/
const otherUserId = chatId
const userChat = await Chat.findOneAndUpdate({
isGroup: false,
users: {
$size: 2,
$all: [
{ $elemMatch: { $eq: new Types.ObjectId( logedInUserId ) } },
{ $elemMatch: { $eq: new Types.ObjectId( otherUserId ) } },
]
}
},
{
$setOnInsert: {
users: [ logedInUserId, otherUserId ]
}
}, {
new: true, // return new modified doc
upsert: true // Allow to create if not exists
})
.populate('users') //
return res.status(200).json({
status: 'success',
data: userChat
})
}
res.status(200).json({
status: 'success',
data: groupChat
})
})
// POST /api/chats + protected
exports.createChat = catchAsync( async (req, res, next) => {
if(!req.body.length) return next(appError('please send user._id as array '))
const selectedUsersIds = req.body // body: [ user1._id, user2._id, ... ]
const userIds = [ ...selectedUsersIds, req.session.user._id ]
const body = { users: userIds, isGroup: true, isOpened: true }
const chat = await Chat.create( body )
res.status(201).json({
status: 'success',
data: chat
})
})
// PATCH /api/chats/:id + protected
exports.updateChatById = catchAsync( async (req, res, next) => {
const chatId = req.params.id
if( !isValidObjectId(chatId) ) return next(appError('invalid id'))
const filteredBody = filterObjectByArray(req.body, ['name', 'isGroup', 'isOpened'] )
const groupChat = await Chat.findByIdAndUpdate( chatId, filteredBody, { new: true } )
console.log({ isOpened: filteredBody.isOpened })
res.status(200).json({
status: 'success',
data: groupChat
})
})
// DELETE /api/chats/:id + protected
exports.deleteChatById = catchAsync( async(req, res, next) => {
const chatId = req.params.id
if( !isValidObjectId(chatId) ) return next(appError('invalid chatId'))
const groupChat = await Chat.findByIdAndDelete( chatId )
await Message.deleteMany({ chat: new Types.ObjectId(chatId) }) // must be awaited
// => Only delete group chat's messages, not user-to-user messages
res.status(200).json({
status: 'success',
data: groupChat
})
})