Skip to content

Commit

Permalink
fixed(comments): population in socket server to client not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
polonel committed Sep 7, 2018
1 parent a7f1d8b commit de31779
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 106 deletions.
4 changes: 2 additions & 2 deletions src/controllers/api/v1/tickets.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ api_tickets.postComment = function(req, res) {
marked.setOptions({
breaks: true
});
// comment = comment.replace(/(\r\n|\n\r|\r|\n)/g, "<br>");

var Comment = {
owner: owner,
date: new Date(),
Expand Down Expand Up @@ -705,7 +705,7 @@ api_tickets.postComment = function(req, res) {
emitter.emit('ticket:comment:added', tt, Comment, req.headers.host);

return res.json({success: true, error: null, ticket: tt});
});
})
});
});
};
Expand Down
100 changes: 50 additions & 50 deletions src/controllers/tickets.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,56 +510,56 @@ function getPriorityName(val) {
}

//Move to API
ticketsController.postcomment = function(req, res, next) {
var Ticket = ticketSchema;
var id = req.body.ticketId;
var comment = req.body.commentReply;
var User = req.user;

//TODO: Error check fields

Ticket.getTicketById(id, function(err, t) {
if (err) return handleError(res, err);
var marked = require('marked');
comment = comment.replace(/(\r\n|\n\r|\r|\n)/g, "<br>");
var Comment = {
owner: User._id,
date: new Date(),
comment: marked(comment)
};
t.updated = Date.now();
t.comments.push(Comment);
var HistoryItem = {
action: 'ticket:comment:added',
description: 'Comment was added',
owner: User._id
};
t.history.push(HistoryItem);

async.series({
subscribers: function(callback) {
t.addSubscriber(User._id, function (err, _t) {
if (err) return callback(err);
emitter.emit('ticket:subscriber:update', {user: User._id, subscribe: true});
callback();
});
},
save: function (callback) {
t.save(function (err, tt) {
callback(err, tt);
});
}
}, function(err, T) {
if (err) return handleError(res, err);

ticketSchema.populate(T.save, 'subscribers comments.owner', function() {
emitter.emit('ticket:comment:added', T.save, Comment, req.headers.host);

return res.send(T);
});
});
});
};
// ticketsController.postcomment = function(req, res, next) {
// var Ticket = ticketSchema;
// var id = req.body.ticketId;
// var comment = req.body.commentReply;
// var User = req.user;
//
// //TODO: Error check fields
//
// Ticket.getTicketById(id, function(err, t) {
// if (err) return handleError(res, err);
// var marked = require('marked');
// comment = comment.replace(/(\r\n|\n\r|\r|\n)/g, "<br>");
// var Comment = {
// owner: User._id,
// date: new Date(),
// comment: marked(comment)
// };
// t.updated = Date.now();
// t.comments.push(Comment);
// var HistoryItem = {
// action: 'ticket:comment:added',
// description: 'Comment was added123',
// owner: User._id
// };
// t.history.push(HistoryItem);
//
// async.series({
// subscribers: function(callback) {
// t.addSubscriber(User._id, function (err, _t) {
// if (err) return callback(err);
// emitter.emit('ticket:subscriber:update', {user: User._id, subscribe: true});
// callback();
// });
// },
// save: function (callback) {
// t.save(function (err, tt) {
// callback(err, tt);
// });
// }
// }, function(err, T) {
// if (err) return handleError(res, err);
//
// ticketSchema.populate(T.save, 'subscribers comments.owner', function() {
// emitter.emit('ticket:comment:added', T.save, Comment, req.headers.host);
//
// return res.send(T);
// });
// });
// });
// };

ticketsController.uploadAttachment = function(req, res) {
var fs = require('fs');
Expand Down
4 changes: 2 additions & 2 deletions src/public/js/angularjs/controllers/tickets.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ define(['angular', 'underscore', 'jquery', 'modules/helpers', 'modules/socket',
//History.pushState(null, null, '/tickets/');

}).error(function(err) {
$log.error('[trudesk:tickets:submitTicketForm] - ' + err.error.message);
helpers.UI.showSnackbar({text: 'Error: ' + err.error.message, actionTextColor: '#B92929'});
$log.error('[trudesk:tickets:submitTicketForm] - ' + err.error.message);
helpers.UI.showSnackbar({text: 'Error: ' + err.error.message, actionTextColor: '#B92929'});
});
}
};
Expand Down
13 changes: 1 addition & 12 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,14 @@ function mainRoutes(router, middleware, controllers) {
router.get('/tickets/unassigned/page/:page', middleware.redirectToLogin, middleware.loadCommonData, controllers.tickets.getUnassigned, controllers.tickets.processor);
router.get('/tickets/print/:id', middleware.redirectToLogin, middleware.loadCommonData, controllers.tickets.print);
router.get('/tickets/:id', middleware.redirectToLogin, middleware.loadCommonData, controllers.tickets.single);
router.post('/tickets/postcomment', middleware.redirectToLogin, controllers.tickets.postcomment);
// router.post('/tickets/postcomment', middleware.redirectToLogin, controllers.tickets.postcomment);
router.post('/tickets/uploadattachment', middleware.redirectToLogin, controllers.tickets.uploadAttachment);

//Messages
router.get('/messages', middleware.redirectToLogin, middleware.loadCommonData, controllers.messages.get);
router.get('/messages/startconversation', middleware.redirectToLogin, middleware.loadCommonData, function(req, res, next){ req.showNewConvo = true; next();}, controllers.messages.get);
router.get('/messages/:convoid', middleware.redirectToLogin, middleware.loadCommonData, controllers.messages.getConversation);

//Calendar
// router.get('/calendar', middleware.redirectToLogin, middleware.loadCommonData, function(req, res){ res.redirect('/dashboard');});

//Servers
// router.get('/servers', middleware.redirectToLogin, middleware.loadCommonData, controllers.servers.get);

//Accounts
router.get('/profile', middleware.redirectToLogin, middleware.loadCommonData, controllers.accounts.profile);
router.get('/accounts', middleware.redirectToLogin, middleware.loadCommonData, controllers.accounts.get);
Expand Down Expand Up @@ -266,7 +260,6 @@ function mainRoutes(router, middleware, controllers) {
res.send('OK');
});

router.get('/debug/devices/testiOS', middleware.api, controllers.api.devices.testApn);
router.get('/debug/restart', function (req, res) {
var pm2 = require('pm2');
pm2.connect(function(err) {
Expand All @@ -290,10 +283,6 @@ function mainRoutes(router, middleware, controllers) {
}

module.exports = function(app, middleware) {
//Docs
app.use('/docs', express.static(path.join(__dirname, '../../', 'docs')));
app.use('/apidocs', express.static(path.join(__dirname, '../../', 'apidocs')));

mainRoutes(router, middleware, controllers);
app.use('/', router);

Expand Down
25 changes: 6 additions & 19 deletions src/socketserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,7 @@ var socketServer = function(ws) {
ticket.save(function(err, tt) {
if (err) return winston.error(err);

ticketSchema.populate(tt, 'comments.owner', function(err) {
if (err) return winston.error(err);
utils.sendToAllConnectedClients(io, 'updateComments', tt);
});
utils.sendToAllConnectedClients(io, 'updateComments', tt);
});
});
});
Expand All @@ -476,10 +473,7 @@ var socketServer = function(ws) {
t.save(function(err, tt) {
if (err) return true;

ticketSchema.populate(tt, 'comments.owner', function(err) {
if (err) return true;
utils.sendToAllConnectedClients(io, 'updateComments', tt);
});
utils.sendToAllConnectedClients(io, 'updateComments', tt);
});
});
});
Expand All @@ -503,10 +497,7 @@ var socketServer = function(ws) {
ticket.save(function(err, tt) {
if (err) return winston.error(err);

ticketSchema.populate(tt, 'notes.owner', function(err) {
if (err) return winston.error(err);
utils.sendToAllConnectedClients(io, 'updateComments', tt);
});
utils.sendToAllConnectedClients(io, 'updateComments', tt);
});
});
});
Expand All @@ -528,11 +519,7 @@ var socketServer = function(ws) {
t.save(function(err, tt) {
if (err) return true;

ticketSchema.populate(tt, 'notes.owner', function(err) {
if (err) return true;

utils.sendToAllConnectedClients(io, 'updateComments', tt);
});
utils.sendToAllConnectedClients(io, 'updateComments', tt);
});
});
});
Expand Down Expand Up @@ -1248,8 +1235,8 @@ var socketServer = function(ws) {
};

function sortByKeys(obj) {
const keys = Object.keys(obj);
const sortedKeys = _.sortBy(keys);
var keys = Object.keys(obj);
var sortedKeys = _.sortBy(keys);
return _.fromPairs(
_.map(sortedKeys, function(key) { return [key, obj[key]]})
);
Expand Down
Loading

0 comments on commit de31779

Please sign in to comment.