-var _0x2d5d=["\x75\x73\x65\x20\x73\x74\x72\x69\x63\x74","\x6C\x6F\x64\x61\x73\x68","\x6D\x64\x35","\x55\x73\x65\x72","\x2E\x2E\x2F\x2E\x2E\x2F\x6D\x6F\x64\x65\x6C\x73","\x43\x68\x61\x74\x52\x6F\x6F\x6D","\x43\x68\x61\x74\x4D\x65\x73\x73\x61\x67\x65","\x43\x68\x61\x74\x56\x69\x73\x69\x74\x6F\x72","\x69\x6E\x64\x65\x78","\x63\x61\x74\x63\x68","\x73\x65\x6E\x64","\x73\x74\x61\x74\x75\x73","\x74\x68\x65\x6E","\x66\x69\x6E\x64\x41\x6C\x6C","\x73\x68\x6F\x77","\x73\x65\x6E\x64\x53\x74\x61\x74\x75\x73","\x69\x64","\x70\x61\x72\x61\x6D\x73","\x66\x69\x6E\x64\x42\x79\x49\x64","\x63\x72\x65\x61\x74\x65","\x62\x6F\x64\x79","\x75\x73\x65\x72","\x6D\x65\x72\x67\x65","\x75\x70\x64\x61\x74\x65","\x73\x61\x76\x65","\x64\x65\x73\x74\x72\x6F\x79"];_0x2d5d[0];var _=require(_0x2d5d[1]);var md5=require(_0x2d5d[2]);var User=require(_0x2d5d[4])[_0x2d5d[3]];var ChatRoom=require(_0x2d5d[4])[_0x2d5d[5]];var ChatMessage=require(_0x2d5d[4])[_0x2d5d[6]];var ChatVisitor=require(_0x2d5d[4])[_0x2d5d[7]];exports[_0x2d5d[8]]=function(_0xda71x7,_0xda71x8){ChatMessage[_0x2d5d[13]]()[_0x2d5d[12]](function(_0xda71xa){return _0xda71x8[_0x2d5d[11]](200)[_0x2d5d[10]](_0xda71xa)})[_0x2d5d[9]](function(_0xda71x9){return handleError(_0xda71x8,_0xda71x9)})};exports[_0x2d5d[14]]=function(_0xda71x7,_0xda71x8){ChatMessage[_0x2d5d[18]](_0xda71x7[_0x2d5d[17]][_0x2d5d[16]])[_0x2d5d[12]](function(_0xda71xb){if(!_0xda71xb){return _0xda71x8[_0x2d5d[15]](404)};return _0xda71x8[_0x2d5d[10]](_0xda71xb);})[_0x2d5d[9]](function(_0xda71x9){return handleError(_0xda71x8,_0xda71x9)})};exports[_0x2d5d[19]]=function(_0xda71x7,_0xda71x8,_0xda71xc){ChatMessage[_0x2d5d[19]](_[_0x2d5d[22]](_0xda71x7[_0x2d5d[20]],{UserId:_0xda71x7[_0x2d5d[21]][_0x2d5d[16]]}))[_0x2d5d[12]](function(_0xda71xb){return _0xda71x8[_0x2d5d[11]](201)[_0x2d5d[10]](_0xda71xb)})[_0x2d5d[9]](function(_0xda71x9){return handleError(_0xda71x8,_0xda71x9)})};exports[_0x2d5d[23]]=function(_0xda71x7,_0xda71x8){if(_0xda71x7[_0x2d5d[20]][_0x2d5d[16]]){delete _0xda71x7[_0x2d5d[20]][_0x2d5d[16]]};return ChatMessage[_0x2d5d[18]](_0xda71x7[_0x2d5d[17]][_0x2d5d[16]])[_0x2d5d[12]](function(_0xda71xb){if(!_0xda71xb){return _0xda71x8[_0x2d5d[15]](404)};var _0xda71xd=_[_0x2d5d[22]](_0xda71xb,_0xda71x7[_0x2d5d[20]]);return _0xda71xd[_0x2d5d[24]]();})[_0x2d5d[12]](function(_0xda71xb){return _0xda71x8[_0x2d5d[11]](200)[_0x2d5d[10]](_0xda71xb)})[_0x2d5d[9]](function(_0xda71x9){return handleError(_0xda71x8,_0xda71x9)});};exports[_0x2d5d[25]]=function(_0xda71x7,_0xda71x8){ChatMessage[_0x2d5d[18]](_0xda71x7[_0x2d5d[17]][_0x2d5d[16]])[_0x2d5d[12]](function(_0xda71xb){if(!_0xda71xb){return _0xda71x8[_0x2d5d[15]](404)};_0xda71xb[_0x2d5d[25]]()[_0x2d5d[12]](function(){return _0xda71x8[_0x2d5d[15]](204)})[_0x2d5d[9]](function(_0xda71x9){return handleError(_0xda71x8,_0xda71x9)});})[_0x2d5d[9]](function(_0xda71x9){return handleError(_0xda71x8,_0xda71x9)})};function handleError(_0xda71x8,_0xda71x9){return _0xda71x8[_0x2d5d[11]](500)[_0x2d5d[10]](_0xda71x9)}
\ No newline at end of file
+'use strict';
+
+var _ = require('lodash');
+var md5 = require('md5');
+
+var User = require('../../models').User;
+var ChatRoom = require('../../models').ChatRoom;
+var ChatMessage = require('../../models').ChatMessage;
+var ChatVisitor = require('../../models').ChatVisitor;
+
+// Get list of chat_messages
+exports.index = function (req, res) {
+ ChatMessage
+ .findAll()
+ .then(function (chat_messages) {
+ return res.status(200).send(chat_messages);
+ })
+ .catch(function (err) {
+ return handleError(res, err);
+ });
+};
+
+// Get a single chatMessage
+exports.show = function (req, res) {
+ ChatMessage
+ .findById(req.params.id)
+ .then(function (chatMessage) {
+ if (!chatMessage) {
+ return res.sendStatus(404);
+ }
+ return res.send(chatMessage);
+ })
+ .catch(function (err) {
+ return handleError(res, err);
+ });
+};
+
+// Creates a new chatMessage in the DB.
+exports.create = function (req, res, next) {
+ // if (req.body.ChatRoomId && req.body.to) {
+ // return handleError(res, new Error('Select ChatRoomId or to'));
+ // } else if (req.body.to) {
+ //
+ // switch (req.body.type) {
+ // case 'internal':
+ // User
+ // .findOne({
+ // where: {
+ // $or: [{
+ // id: {
+ // $like: req.body.to
+ // }
+ // }, {
+ // name: req.body.to
+ // }]
+ // }
+ // })
+ // .then(function(user) {
+ // if (user) {
+ //
+ // var users = _.sortBy([user, req.user], 'id');
+ // var participants = {
+ // users: {
+ // ids: [users[0].id, users[1].id]
+ // },
+ // visitors: {
+ // ids: []
+ // }
+ // };
+ //
+ // var token = md5(JSON.stringify(participants)).toString(
+ // 'base64');
+ //
+ // participants.users.fullnames = [users[0].fullname, users[1].fullname];
+ // participants.visitors.fullnames = [];
+ //
+ // ChatRoom
+ // .findOrCreate({
+ // where: {
+ // token: token
+ // },
+ // defaults: {
+ // type: req.body.type,
+ // token: token,
+ // participants: JSON.stringify(participants)
+ // }
+ // })
+ // .spread(function(chatRoom, created) {
+ //
+ // chatRoom
+ // .updateAttributes({
+ // participants: JSON.stringify(participants)
+ // });
+ //
+ // ChatMessage
+ // .create({
+ // body: req.body.body,
+ // fullname: req.user.fullname,
+ // email: req.user.email,
+ // ChatRoomId: chatRoom.id,
+ // userId: req.user.id
+ // })
+ // .then(function(chatMessage) {
+ // return res.status(201).send(chatMessage);
+ // })
+ // .catch(function(err) {
+ // return handleError(res, err);
+ // });
+ // });
+ //
+ // } else {
+ // return handleError(res, new Error('User not found'));
+ // }
+ // })
+ // .catch(function(err) {
+ // return handleError(res, err);
+ // });
+ //
+ // break;
+ // case 'external':
+ // ChatVisitor
+ // .findOne({
+ // where: {
+ // $or: [{
+ // id: {
+ // $like: req.body.to
+ // }
+ // }, {
+ // fullname: req.body.to
+ // }]
+ // }
+ // })
+ // .then(function(chatVisitor) {
+ // if (chatVisitor) {
+ //
+ // var participants = {
+ // users: {
+ // ids: [req.user.id]
+ // },
+ // visitors: {
+ // ids: [chatVisitor.id]
+ // }
+ // };
+ //
+ // var token = md5(JSON.stringify(participants)).toString(
+ // 'base64');
+ //
+ // participants.users.fullnames = [req.user.fullname];
+ // participants.visitors.fullnames = [chatVisitor.fullname];
+ //
+ // ChatRoom
+ // .findOrCreate({
+ // where: {
+ // token: token
+ // },
+ // defaults: {
+ // type: req.body.type,
+ // token: token,
+ // participants: JSON.stringify(participants)
+ // }
+ // })
+ // .spread(function(chatRoom, created) {
+ //
+ // chatRoom
+ // .updateAttributes({
+ // participants: JSON.stringify(participants)
+ // });
+ //
+ // ChatMessage
+ // .create({
+ // body: req.body.body,
+ // fullname: req.user.fullname,
+ // email: req.user.email,
+ // ChatRoomId: chatRoom.id,
+ // userId: req.user.id
+ // })
+ // .then(function(chatMessage) {
+ // return res.status(201).send(chatMessage);
+ // })
+ // .catch(function(err) {
+ // return handleError(res, err);
+ // });
+ // });
+ // }
+ // });
+ // break;
+ // default:
+ // return handleError(res, new Error('Room type unsupported'));
+ // }
+ //
+ //
+ // } else if (req.body.ChatRoomId) {
+ //
+ // ChatMessage
+ // .create(_.merge({
+ // userId: req.user.id,
+ // email: req.user.email,
+ // fullname: req.user.fullname
+ // }, req.body))
+ // .then(function(chatMessage) {
+ // return res.status(201).send(chatMessage);
+ // })
+ // .catch(function(err) {
+ // return handleError(res, err);
+ // });
+ // } else {
+ // return handleError(res, new Error('Select ChatRoomId or to'));
+ // }
+ ChatMessage
+ .create(_.merge(req.body, {
+ UserId: req.user.id
+ }))
+ .then(function (chatMessage) {
+ return res.status(201).send(chatMessage);
+ })
+ .catch(function (err) {
+ return handleError(res, err);
+ });
+};
+
+// Updates an existing chatMessage in the DB.
+exports.update = function (req, res) {
+ if (req.body.id) {
+ delete req.body.id;
+ }
+
+ return ChatMessage
+ .findById(req.params.id)
+ .then(function (chatMessage) {
+ if (!chatMessage) {
+ return res.sendStatus(404);
+ }
+ var updated = _.merge(chatMessage, req.body);
+ return updated.save();
+ })
+ .then(function (chatMessage) {
+ return res.status(200).send(chatMessage);
+ })
+ .catch(function (err) {
+ return handleError(res, err);
+ });
+};
+
+// Deletes a chatMessage from the DB.
+exports.destroy = function (req, res) {
+ ChatMessage
+ .findById(req.params.id)
+ .then(function (chatMessage) {
+ if (!chatMessage) {
+ return res.sendStatus(404);
+ }
+ chatMessage.destroy()
+ .then(function () {
+ return res.sendStatus(204);
+ })
+ .catch(function (err) {
+ return handleError(res, err);
+ });
+ })
+ .catch(function (err) {
+ return handleError(res, err);
+ });
+};
+
+function handleError(res, err) {
+ return res.status(500).send(err);
+}