<commit_before>'use strict';

var path = require('path'),
  mongoose = require('mongoose'),
  Poll = mongoose.model('Poll'),
  Opt = mongoose.model('Opt'),
  Cmt = mongoose.model('Cmt'),
  Vote = mongoose.model('Vote'),
  Voteopt = mongoose.model('Voteopt'),
  Polltag = mongoose.model('Polltag'),
  Polluser = mongoose.model('Polluser'),
  Notif = mongoose.model('Notif'),
  Like = mongoose.model('Like'),
  errorHandler = require(path.resolve(
    './modules/core/server/controllers/errors.server.controller'
  )),
  _ = require('underscore');

// Create the chat configuration
module.exports = function (io, socket) {
  // On subscribe
  socket.on('subscribe_poll', req => {
    socket.join(req.pollId);
  });
  socket.on('subscribe_public', req => {
    socket.join('public');
  });
  // On unsubscribe
  socket.on('unsubscribe_poll', req => {
    socket.leave(req.pollId);
  });
  // On unsubscribe
  socket.on('unsubscribe_public', req => {
    socket.leave('public');
  });

  // On like poll
  socket.on('poll_like', req => {
    io.sockets.in(req.pollId).emit('poll_like', req.report);
    if (req.type === 0) {
      Notif.remove({ poll: req.pollId, type: { $in: [0, 1] }, from: req.from });
      return;
    }
    // Create notifis
    var action = (req.type === 1) ? 'liked' : 'disliked';
    var type = req.type - 1;
    Notif.findOne({ poll: req.pollId, type: { $in: [0, 1] }, from: req.from })
      .then(_nof => {
        if (_nof) {
          if (_nof.type !== type) {
            _nof.type = type;
            _nof.content = action + ' your poll:';
            _nof.status = 0;
            _nof.save().then(notif => {
              var socketIds = _.where(global.socketUsers, { user: req.to });
                  console.log('socketIds', socketIds);
              socketIds.forEach(item => {
                io.sockets.connected[item.socket].emit('notifs', notif._id);
              });
            });
          }
        } else {
          Notif.findOne({ poll: req.pollId, type: type, status: 0 })
            .then(_nof => {
              if (_nof) {
                _nof.from = req.from;
                _nof.count += 1;
                _nof.content = 'and ' + _nof.count + ' other people recently ' + action + ' your poll:';
                _nof.save().then(notif => {
                  var socketIds = _.where(global.socketUsers, { user: req.to });
                  console.log('socketIds', socketIds);
                  socketIds.forEach(item => {
                    io.sockets.connected[item.socket].emit('notifs', notif._id);
                  });
                }, handlerError);
              } else {
                _nof = new Notif({
                  from: req.from,
                  to: req.to,
                  content: action + ' your poll:',
                  poll: req.pollId
                });
                _nof.save().then(notif => {
                  var socketIds = _.where(global.socketUsers, { user: req.to });
                  console.log('socketIds', socketIds);
                  socketIds.forEach(item => {
                    io.sockets.connected[item.socket].emit('notifs', notif._id);
                  });
                }, handlerError);
              }
            }, handlerError);
        }
      });
  });
  // On vote poll
  socket.on('poll_vote', req => {
    io.sockets.in(req.pollId).emit('poll_vote');
  });
  // On delete poll
  socket.on('poll_delete', req => {
    io.sockets.in(req.pollId).emit('poll_delete');
  });
  // On delete poll
  socket.on('poll_update', req => {
    io.sockets.in(req.pollId).emit('poll_update');
  });
  // On delete poll
  socket.on('poll_create', req => {
    io.sockets.in('public').emit('poll_create');
  });

  // On comment added
  socket.on('cmt_add', req => {
    io.sockets.in(req.pollId).emit('cmt_add', req.cmtId);
    if (req.to) {
      var notif = new Notif({
        from: req.from,
        to: req.to,
        content: 'has replied your comment on',
        poll: req.pollId
      });
      notif.save().then(notif => {
        var socketIds = _.where(global.socketUsers, { user: req.to });
        socketIds.forEach(item => {
          io.sockets.connected[item.socket].emit('notifs', notif._id);
        });
      });
    } else {
      // Tìm toàn bộ các member đang theo dõi poll
      Polluser.find({ poll: req.pollId, following: true }).then(
        pollusers => {
          var notif;
          // Tạo notif cho toàn bộ các member đang theo dõi
          pollusers.forEach((polluser, index) => {
            if (polluser.user.toString() !== req.from.toString()) {
              notif = new Notif({
                from: req.from,
                to: polluser.user,
                content: 'has posted a comment on',
                poll: req.pollId
              });
              notif.save().then(
                _notif => {
                  var socketIds = _.where(global.socketUsers, {
                    user: polluser.user.toString()
                  });
                  socketIds.forEach(item => {
                    io.sockets.connected[item.socket].emit(
                      'notifs',
                      _notif._id
                    );
                  });
                },
                err => {
                  console.log('Has error when save notif comment');
                }
              );
            }
          });
        },
        err => {
          console.log('Has error when get user in poll for comment');
        }
      );
    }
  });
  // On comment deleted
  socket.on('cmt_del', req => {
    io.sockets.in(req.pollId).emit('cmt_del', req.cmtId);
  });
  // On like poll
  socket.on('cmt_like', req => {
    io.sockets
      .in(req.pollId)
      .emit('cmt_like', { cmtId: req.cmtId, likeCnt: req.likeCnt });
    if (req.type === 0) {
      return;
    }
    var action = (req.type === 1) ? 'liked' : 'disliked';
    var notif = new Notif({
      from: req.from,
      to: req.to,
      content: 'has ' + action + ' your comment on',
      poll: req.pollId
    });
    notif.save().then(notif => {
      var socketIds = _.where(global.socketUsers, { user: req.to });
      socketIds.forEach(item => {
        io.sockets.connected[item.socket].emit('notifs', notif._id);
      });
    });
  });

  // On delete poll
  socket.on('opts_update', req => {
    io.sockets.in(req.pollId).emit('opts_update');
  });
  // On delete poll
  socket.on('opts_request', req => {
    io.sockets.in(req.pollId).emit('opts_request');
  });

  // io.sockets.connected[socket.id].emit('comment_result', { success: true });
  function next() {
    return new Promise((resolve, reject) => {
      return resolve();
    });
  }

  function handlerError(err) {
    console.log(err);
  }
};
<commit_msg>Commited from lenh at Fri, Jul 14, 2017 11:00:16 AM<commit_after>'use strict';

var path = require('path'),
  mongoose = require('mongoose'),
  Poll = mongoose.model('Poll'),
  Opt = mongoose.model('Opt'),
  Cmt = mongoose.model('Cmt'),
  Vote = mongoose.model('Vote'),
  Voteopt = mongoose.model('Voteopt'),
  Polltag = mongoose.model('Polltag'),
  Polluser = mongoose.model('Polluser'),
  Notif = mongoose.model('Notif'),
  Like = mongoose.model('Like'),
  errorHandler = require(path.resolve(
    './modules/core/server/controllers/errors.server.controller'
  )),
  _ = require('underscore');

// Create the chat configuration
module.exports = function (io, socket) {
  // On subscribe
  socket.on('subscribe_poll', req => {
    socket.join(req.pollId);
  });
  socket.on('subscribe_public', req => {
    socket.join('public');
  });
  // On unsubscribe
  socket.on('unsubscribe_poll', req => {
    socket.leave(req.pollId);
  });
  // On unsubscribe
  socket.on('unsubscribe_public', req => {
    socket.leave('public');
  });

  // On like poll
  socket.on('poll_like', req => {
    io.sockets.in(req.pollId).emit('poll_like', req.report);
    if (req.type === 0) {
      Notif.findOne({ poll: req.pollId, type: { $in: [0, 1] }, from: req.from })
        .then(_nof => {
          _nof.remove();
        });
      return;
    }
    // Create notifis
    var action = (req.type === 1) ? 'liked' : 'disliked';
    var type = req.type - 1;
    Notif.findOne({ poll: req.pollId, type: { $in: [0, 1] }, from: req.from })
      .then(_nof => {
        if (_nof) {
          if (_nof.type !== type) {
            _nof.type = type;
            _nof.content = action + ' your poll:';
            _nof.status = 0;
            _nof.save().then(notif => {
              var socketIds = _.where(global.socketUsers, { user: req.to });
                  console.log('socketIds', socketIds);
              socketIds.forEach(item => {
                io.sockets.connected[item.socket].emit('notifs', notif._id);
              });
            });
          }
        } else {
          Notif.findOne({ poll: req.pollId, type: type, status: 0 })
            .then(_nof => {
              if (_nof) {
                _nof.from = req.from;
                _nof.count += 1;
                _nof.content = 'and ' + _nof.count + ' other people recently ' + action + ' your poll:';
                _nof.save().then(notif => {
                  var socketIds = _.where(global.socketUsers, { user: req.to });
                  console.log('socketIds', socketIds);
                  socketIds.forEach(item => {
                    io.sockets.connected[item.socket].emit('notifs', notif._id);
                  });
                }, handlerError);
              } else {
                _nof = new Notif({
                  from: req.from,
                  to: req.to,
                  content: action + ' your poll:',
                  poll: req.pollId
                });
                _nof.save().then(notif => {
                  var socketIds = _.where(global.socketUsers, { user: req.to });
                  console.log('socketIds', socketIds);
                  socketIds.forEach(item => {
                    io.sockets.connected[item.socket].emit('notifs', notif._id);
                  });
                }, handlerError);
              }
            }, handlerError);
        }
      });
  });
  // On vote poll
  socket.on('poll_vote', req => {
    io.sockets.in(req.pollId).emit('poll_vote');
  });
  // On delete poll
  socket.on('poll_delete', req => {
    io.sockets.in(req.pollId).emit('poll_delete');
  });
  // On delete poll
  socket.on('poll_update', req => {
    io.sockets.in(req.pollId).emit('poll_update');
  });
  // On delete poll
  socket.on('poll_create', req => {
    io.sockets.in('public').emit('poll_create');
  });

  // On comment added
  socket.on('cmt_add', req => {
    io.sockets.in(req.pollId).emit('cmt_add', req.cmtId);
    if (req.to) {
      var notif = new Notif({
        from: req.from,
        to: req.to,
        content: 'has replied your comment on',
        poll: req.pollId
      });
      notif.save().then(notif => {
        var socketIds = _.where(global.socketUsers, { user: req.to });
        socketIds.forEach(item => {
          io.sockets.connected[item.socket].emit('notifs', notif._id);
        });
      });
    } else {
      // Tìm toàn bộ các member đang theo dõi poll
      Polluser.find({ poll: req.pollId, following: true }).then(
        pollusers => {
          var notif;
          // Tạo notif cho toàn bộ các member đang theo dõi
          pollusers.forEach((polluser, index) => {
            if (polluser.user.toString() !== req.from.toString()) {
              notif = new Notif({
                from: req.from,
                to: polluser.user,
                content: 'has posted a comment on',
                poll: req.pollId
              });
              notif.save().then(
                _notif => {
                  var socketIds = _.where(global.socketUsers, {
                    user: polluser.user.toString()
                  });
                  socketIds.forEach(item => {
                    io.sockets.connected[item.socket].emit(
                      'notifs',
                      _notif._id
                    );
                  });
                },
                err => {
                  console.log('Has error when save notif comment');
                }
              );
            }
          });
        },
        err => {
          console.log('Has error when get user in poll for comment');
        }
      );
    }
  });
  // On comment deleted
  socket.on('cmt_del', req => {
    io.sockets.in(req.pollId).emit('cmt_del', req.cmtId);
  });
  // On like poll
  socket.on('cmt_like', req => {
    io.sockets
      .in(req.pollId)
      .emit('cmt_like', { cmtId: req.cmtId, likeCnt: req.likeCnt });
    if (req.type === 0) {
      return;
    }
    var action = (req.type === 1) ? 'liked' : 'disliked';
    var notif = new Notif({
      from: req.from,
      to: req.to,
      content: 'has ' + action + ' your comment on',
      poll: req.pollId
    });
    notif.save().then(notif => {
      var socketIds = _.where(global.socketUsers, { user: req.to });
      socketIds.forEach(item => {
        io.sockets.connected[item.socket].emit('notifs', notif._id);
      });
    });
  });

  // On delete poll
  socket.on('opts_update', req => {
    io.sockets.in(req.pollId).emit('opts_update');
  });
  // On delete poll
  socket.on('opts_request', req => {
    io.sockets.in(req.pollId).emit('opts_request');
  });

  // io.sockets.connected[socket.id].emit('comment_result', { success: true });
  function next() {
    return new Promise((resolve, reject) => {
      return resolve();
    });
  }

  function handlerError(err) {
    console.log(err);
  }
};

{'fraction_non_alphanumeric': 0.16208085699611124, 'fraction_numerical': 0.0026414263702399295, 'mean_word_length': 1.721102016370533, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '134924', 'n_tokens_mistral': 4392, 'n_tokens_neox': 4274, 'n_words': 809}