/
messages.js
241 lines (204 loc) · 7.04 KB
/
messages.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* . .o8 oooo
* .o8 "888 `888
* .o888oo oooo d8b oooo oooo .oooo888 .ooooo. .oooo.o 888 oooo
* 888 `888""8P `888 `888 d88' `888 d88' `88b d88( "8 888 .8P'
* 888 888 888 888 888 888 888ooo888 `"Y88b. 888888.
* 888 . 888 888 888 888 888 888 .o o. )88b 888 `88b.
* "888" d888b `V88V"V8P' `Y8bod88P" `Y8bod8P' 8""888P' o888o o888o
* ========================================================================
* Author: Chris Brame
* Updated: 1/20/19 4:43 PM
* Copyright (c) 2014-2019. All rights reserved.
*/
const _ = require('lodash')
const async = require('async')
const winston = require('../logger')
const conversationSchema = require('../models/chat/conversation')
const messageSchema = require('../models/chat/message')
const messagesController = {}
messagesController.content = {}
messagesController.get = function (req, res) {
const content = {}
content.title = 'Messages'
content.nav = 'messages'
content.data = {}
content.data.user = req.user
content.data.common = req.viewdata
content.data.conversations = []
content.data.showNewConvo = req.showNewConvo
conversationSchema.getConversationsWithLimit(req.user._id, undefined, function (err, convos) {
if (err) {
winston.debug(err)
return handleError(res, err)
}
async.eachSeries(
convos,
function (convo, done) {
const c = convo.toObject()
const userMeta =
convo.userMeta[
_.findIndex(convo.userMeta, function (item) {
return item.userId.toString() === req.user._id.toString()
})
]
if (!_.isUndefined(userMeta) && !_.isUndefined(userMeta.deletedAt) && userMeta.deletedAt > convo.updatedAt) {
return done()
}
messageSchema.getMostRecentMessage(c._id, function (err, rm) {
if (err) return done(err)
_.each(c.participants, function (p) {
if (p._id.toString() !== req.user._id.toString()) {
c.partner = p
}
})
rm = _.first(rm)
if (!_.isUndefined(rm)) {
if (String(c.partner._id) === String(rm.owner._id)) {
c.recentMessage = c.partner.fullname + ': ' + rm.body
} else {
c.recentMessage = 'You: ' + rm.body
}
} else {
c.recentMessage = 'New Conversation'
}
content.data.conversations.push(c)
return done()
})
},
function (err) {
if (err) {
winston.debug(err)
return handleError(res, err)
}
return res.render('messages', content)
}
)
})
}
messagesController.getConversation = async (req, res) => {
const cid = req.params.convoid
if (_.isUndefined(cid)) return handleError(res, 'Invalid Conversation ID!')
const content = {}
content.title = 'Messages'
content.nav = 'messages'
content.data = {}
content.data.user = req.user
content.data.common = req.viewdata
content.data.conversations = []
async.parallel(
[
function (next) {
conversationSchema.getConversationsWithLimit(req.user._id, undefined, function (err, convos) {
if (err) return next(err)
async.eachSeries(
convos,
function (convo, done) {
const userMeta =
convo.userMeta[
_.findIndex(convo.userMeta, function (item) {
return item.userId.toString() === req.user._id.toString()
})
]
if (
!_.isUndefined(userMeta) &&
!_.isUndefined(userMeta.deletedAt) &&
userMeta.deletedAt > convo.updatedAt &&
req.params.convoid.toString() !== convo._id.toString()
) {
return done()
}
const c = convo.toObject()
messageSchema.getMostRecentMessage(c._id, function (err, rm) {
if (err) return done(err)
_.each(c.participants, function (p) {
if (p._id.toString() !== req.user._id.toString()) {
c.partner = p
}
})
rm = _.first(rm)
if (!_.isUndefined(rm)) {
if (String(c.partner._id) === String(rm.owner._id)) {
c.recentMessage = c.partner.fullname + ': ' + rm.body
} else {
c.recentMessage = 'You: ' + rm.body
}
} else {
c.recentMessage = 'New Conversation'
}
if (
!_.isUndefined(userMeta) &&
!_.isUndefined(userMeta.deletedAt) &&
!_.isUndefined(rm) &&
rm.createdAt < userMeta.deletedAt
) {
c.recentMessage = 'New Conversation'
}
content.data.conversations.push(c)
return done()
})
},
function (err) {
if (err) return next(err)
return next()
}
)
})
},
function (next) {
content.data.page = 2
conversationSchema.getConversation(cid, function (err, convo) {
if (err) return next(err)
if (convo === null || convo === undefined) {
return res.redirect('/messages')
}
const c = convo.toObject()
let isPart = false
_.each(c.participants, function (p) {
if (p._id.toString() === req.user._id.toString()) isPart = true
})
if (!isPart) {
return res.redirect('/messages')
}
messageSchema.getConversationWithObject(
{ cid: c._id, userMeta: convo.userMeta, requestingUser: req.user },
function (err, messages) {
if (err) return next(err)
_.each(c.participants, function (p) {
if (p._id.toString() !== req.user._id.toString()) {
c.partner = p
}
})
c.requestingUserMeta =
convo.userMeta[
_.findIndex(convo.userMeta, function (item) {
return item.userId.toString() === req.user._id.toString()
})
]
content.data.conversation = c
content.data.conversation.messages = messages.reverse()
return next()
}
)
})
}
],
function (err) {
if (err) return handleError(res, err)
return res.render('messages', content)
}
)
}
function handleError (res, err) {
if (err) {
winston.warn(err)
if (!err.status) res.status = 500
else res.status = err.status
return res.render('error', {
layout: false,
error: err,
message: err.message
})
}
}
module.exports = messagesController