-
Notifications
You must be signed in to change notification settings - Fork 2
/
socket.js
192 lines (146 loc) · 6.3 KB
/
socket.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
const Conversation = require('./models/chat/conversation');
const Account = require('./models/account');
const Member = require('./models/member');
const Message = require('./models/chat/message');
const Participant = require('./models/chat/participant');
const Contact = require('./models/contact');
const Property = require('./models/property');
const Department = require('./models/department');
const mailer = require('./mailer/index');
//const async = require('async');
var mongoose = require('mongoose');
module.exports = function (io) {
var clients = [];
// nsp.emit('hi', 'everyone!');
io.on("connection", async function (socket) {
clients.push(socket.id);
// console.log("new connection");
var sessionId = socket.request.sessionID;
//console.log(sessionId);
// todo use above
if (socket.request.session.passport) {
var userId = socket.request.session.passport.user;
//console.log("Your User ID is", userId);
user = await Account.findOne({email: userId});
socket.user = user;
// todo save socket and user_id
}
// console.log(socket.user);
socket.on('new message', async function (msg) {
// console.log('new message: ' + msg);
var conversation;
var participant;
var property;
var department;
conversation = await Conversation.findOne({_id: msg['conversation']});
department = await Department.findOne({_id: conversation.department});
property = await Property.findOne({_id: conversation.property});
let participantIds = conversation.participants.map(p => new mongoose.Types.ObjectId(p.id));
if (socket.user){
var member = await Member.findOne({account: socket.user._id});
participant = await Participant.findOne({modelRef: mongoose.Types.ObjectId(member._id)})
.where('_id')
.in(participantIds)
.exec();
if (participant == null) {
participant = await Participant.create({
refModel: 'Member',
modelRef: member._id
});
conversation.participants.push(participant);
await conversation.save();
//
// await Conversation.updateOne(
// { _id: conversation._id },
// { $push: { participants: participant } }
// );
}
} else {
participant = await Participant.findOne({refModel: 'Contact'})
.where('_id')
.in(participantIds)
.exec();
}
var message = await Message.create({
body: msg['text'],
conversation: conversation._id,
owner: participant._id,
});
if (conversation.mail_notifications) {
var contact = await Participant.findOne({refModel: 'Contact'})
.where('_id')
.in(participantIds)
.populate('modelRef')
.exec();
let mailOptions = {
from: department.support_email, // sender address
to: contact['modelRef']['email'], // list of receivers
replyTo: 'notifications-' + conversation._id + '@'+process.env.MAIL_SERVER_HOST,
subject: `Re: ${conversation.title}`, // Subject line
text: message['body'], // plain text body
// html: '<b>Hello world?</b>' // html body
};
mailer.sendNotification(mailOptions,function (err, info) {
console.log('Message sent: %s', info.messageId);
});
}
message['owner'] = participant;
var room = 'c/' + conversation._id;
// console.log(io.sockets.adapter.rooms[room]);
io.sockets.in(room).emit('chat message', message);
});
socket.on('init chat', async function (msg) {
var conversation;
conversation = await Conversation.findOne({_id: msg['conversation']}).populate('participants').exec();
if (conversation == null) {
console.trace('SHOULD NOT HAPPEN')
} else {
conversation.recentMessages(function (err, messages) {
msg['participants'] = conversation['participants'];
msg['messages'] = messages;
var room = 'c/' + conversation._id;
socket.join(room);
//io.sockets.in(room).emit('init chat', msg);
//io.to(
socket.emit('init chat', msg);
});
}
});
socket.on('start chat', async function (msg) {
var conversation;
var participant;
var contact = await Contact.findOne({email: msg['email'], property: msg['property']});
if (contact == null) {
contact = await Contact.create({
name: msg['name'],
email: msg['email'],
property: msg['property']
});
}
// create participant
participant = await Participant.create({
refModel: 'Contact',
modelRef: contact._id
});
conversation = await Conversation.create({
title: msg['text'],
participants: [participant._id],
department: msg['department'],
property: msg['property']
});
msg['conversation'] = conversation._id;
// msg['participant'] = contact._id;
var message = await Message.create({
body: msg['text'],
conversation: conversation._id,
owner: participant._id,
});
msg['_id'] = message._id;
var room = 'c/' + conversation._id;
socket.join(room);
//io.to(
socket.emit('start chat', msg);
});
});
return io;
};