3 var _ = require('lodash');
4 var md5 = require('md5');
6 var User = require('../../models').User;
7 var ChatRoom = require('../../models').ChatRoom;
8 var ChatMessage = require('../../models').ChatMessage;
9 var ChatVisitor = require('../../models').ChatVisitor;
11 // Get list of chat_messages
12 exports.index = function (req, res) {
15 .then(function (chat_messages) {
16 return res.status(200).send(chat_messages);
18 .catch(function (err) {
19 return handleError(res, err);
23 // Get a single chatMessage
24 exports.show = function (req, res) {
26 .findById(req.params.id)
27 .then(function (chatMessage) {
29 return res.sendStatus(404);
31 return res.send(chatMessage);
33 .catch(function (err) {
34 return handleError(res, err);
38 // Creates a new chatMessage in the DB.
39 exports.create = function (req, res, next) {
40 // if (req.body.ChatRoomId && req.body.to) {
41 // return handleError(res, new Error('Select ChatRoomId or to'));
42 // } else if (req.body.to) {
44 // switch (req.body.type) {
58 // .then(function(user) {
61 // var users = _.sortBy([user, req.user], 'id');
62 // var participants = {
64 // ids: [users[0].id, users[1].id]
71 // var token = md5(JSON.stringify(participants)).toString(
74 // participants.users.fullnames = [users[0].fullname, users[1].fullname];
75 // participants.visitors.fullnames = [];
83 // type: req.body.type,
85 // participants: JSON.stringify(participants)
88 // .spread(function(chatRoom, created) {
91 // .updateAttributes({
92 // participants: JSON.stringify(participants)
97 // body: req.body.body,
98 // fullname: req.user.fullname,
99 // email: req.user.email,
100 // ChatRoomId: chatRoom.id,
101 // userId: req.user.id
103 // .then(function(chatMessage) {
104 // return res.status(201).send(chatMessage);
106 // .catch(function(err) {
107 // return handleError(res, err);
112 // return handleError(res, new Error('User not found'));
115 // .catch(function(err) {
116 // return handleError(res, err);
126 // $like: req.body.to
129 // fullname: req.body.to
133 // .then(function(chatVisitor) {
134 // if (chatVisitor) {
136 // var participants = {
138 // ids: [req.user.id]
141 // ids: [chatVisitor.id]
145 // var token = md5(JSON.stringify(participants)).toString(
148 // participants.users.fullnames = [req.user.fullname];
149 // participants.visitors.fullnames = [chatVisitor.fullname];
157 // type: req.body.type,
159 // participants: JSON.stringify(participants)
162 // .spread(function(chatRoom, created) {
165 // .updateAttributes({
166 // participants: JSON.stringify(participants)
171 // body: req.body.body,
172 // fullname: req.user.fullname,
173 // email: req.user.email,
174 // ChatRoomId: chatRoom.id,
175 // userId: req.user.id
177 // .then(function(chatMessage) {
178 // return res.status(201).send(chatMessage);
180 // .catch(function(err) {
181 // return handleError(res, err);
188 // return handleError(res, new Error('Room type unsupported'));
192 // } else if (req.body.ChatRoomId) {
196 // userId: req.user.id,
197 // email: req.user.email,
198 // fullname: req.user.fullname
200 // .then(function(chatMessage) {
201 // return res.status(201).send(chatMessage);
203 // .catch(function(err) {
204 // return handleError(res, err);
207 // return handleError(res, new Error('Select ChatRoomId or to'));
210 .create(_.merge(req.body, {
213 .then(function (chatMessage) {
214 return res.status(201).send(chatMessage);
216 .catch(function (err) {
217 return handleError(res, err);
221 // Updates an existing chatMessage in the DB.
222 exports.update = function (req, res) {
228 .findById(req.params.id)
229 .then(function (chatMessage) {
231 return res.sendStatus(404);
233 var updated = _.merge(chatMessage, req.body);
234 return updated.save();
236 .then(function (chatMessage) {
237 return res.status(200).send(chatMessage);
239 .catch(function (err) {
240 return handleError(res, err);
244 // Deletes a chatMessage from the DB.
245 exports.destroy = function (req, res) {
247 .findById(req.params.id)
248 .then(function (chatMessage) {
250 return res.sendStatus(404);
252 chatMessage.destroy()
254 return res.sendStatus(204);
256 .catch(function (err) {
257 return handleError(res, err);
260 .catch(function (err) {
261 return handleError(res, err);
265 function handleError(res, err) {
266 return res.status(500).send(err);