-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatController.js
224 lines (180 loc) · 5.08 KB
/
chatController.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
const Chat = require("../models/chatmodel")
const accessChat = async(req,res)=>{
const {userId} = req.body
try{
if(!userId){
return res.status(400).json({
status:"Failed",
error:"userId param not sent with requst"
})
}
//check chat exists with these userId
let isChat = await Chat.find({
isgroupchat:false,
users:{$all:[req.user._id,userId]}
}).populate("users", "-password").populate("latestMessage")
let chatdata;
console.log(isChat)
if(isChat.length>0){
return res.status(200).json({
isChat
})
}
else{
chatdata = {
chatName :"sender",
isgroupchat:false,
users:[req.user._id,userId]
}
}
console.log(chatdata)
const createdChat = await Chat.create(chatdata)
const Fullchat = await Chat.find({_id:createdChat._id}).populate("users", "-password")
return res.status(200).json({
Fullchat
})
}
catch(err){
return res.status(500).json({
status:"Failed",
error:err.message
})
}
}
const fetchChats = async(req,res)=>{
try{
const chats = await Chat.find({users:{$all:[req.user._id]}})
.populate("users", "-password")
.populate("isgroupAdmin", "-password")
.populate("latestMessage")
.sort({updatedAt : -1})
return res.json({
status:"success",
chats:chats
})
}
catch(err){
return res.json({
status:"Failed",
error:err.message
})
}
}
const createGroupChat = async(req,res) =>{
const {users, name} = req.body
//here we take bunch of users and name of groupchat from body
try{
if(!req.body.users || !req.body.name){
return res.status(400).json({
status:"Failed",
error:"Please fill all the fields"
})
}
//users coming through request is in json format so we parse it for actual array
let users = JSON.parse(req.body.users)
if(users.length>2){
return res.json({
status:"Failed",
error:"more than 2users are required to form a group"
})
}
// we push current loggedin user in to users
users.push(req.user)
// create groupchat
const groupChat = await Chat.create({
chatName: req.body.name,
users:users,
isgroupchat:true,
isgroupAdmin: req.user // logged in user is the admin of group
})
const fullGroupChat = await Chat.findOne({_id:groupChat.id})
.populate("users", "-password")
.populate("isgroupAdmin", "-password")
res.status(200).json({
status:"success",
fullGroupChat:fullGroupChat
})
}
catch(err){
return res.status(500).json({
status:"Failed",
error:err.message
})
}
}
const renameGroup = async(req,res)=>{
try{
const {chatId,chatname} = req.body
if(!chatId || !chatname){
return res.status(400).json({error:"all fields are mandatory"})
}
const updatedChat = await Chat.findByIdAndUpdate(chatId,
{chatName:chatname },{new:true})
.populate("users", "-password")
.populate('isgroupAdmin', "-password")
if(!updatedChat){
return res.status(404).json({
status:"Failed",
error:"Chat Not Found"
})
}
else{
return res.status(200).json({
status:"success",
updatedChat:updatedChat
})
}
}
catch(err){
return res.status(500).json({
status:"Failed",
error:err.message
})
}
}
const addToGroup = async(req,res)=>{
try{
const {chatId, userId} = req.body
const added = await Chat.findByIdAndUpdate(chatId,
{ $push:{users:userId}},
{new:true}).populate("users", "-password")
.populate('isgroupAdmin', "-password")
if(!added){
return res.status(400).json({error:"chat was not found"})
}
return res.json({
status:"success",
added:added
})
}
catch(err){
res.status(500).json({
status:"Failed",
error:err.message
})
}
}
const removeFromGroup = async(req,res)=>{
const {chatId, userId} = req.body
try{
const removed = await Chat.findByIdAndUpdate(chatId,
{$pull:{users:userId}},{
new:true
}).populate("users", "-password")
.populate("isgroupAdmin", "-password")
if(!removed){
return res.status(400).json({error:"chat was not found with that id"})
}
return res.json({
status:"success",
removed:removed
})
}
catch(err){
res.status(500).json({
status:"Failed",
error:err.message
})
}
}
module.exports = {accessChat, createGroupChat,fetchChats, renameGroup, addToGroup, removeFromGroup}