<commit_before>/** authenticated.js
* @file: /src/app/routes/ajax/authenticated.js
* @description: Handles express routing for the authenticated ajax routes
* @parameters: Object(app), Object(resources)
* @exports: Express routes
*/

var ObjectID = require('mongodb').ObjectID

module.exports = function (app, resources) {
  /** /ajax/addVote
  * @type: GET
  * @description: Ajax route for handling user votes.
  */
  app.get('/ajax/addVote', function (req, res) {
    if(req.query.id != undefined && req.query.content != undefined) {
     if (req.isAuthenticated()) {
        var votesdb = resources.collections.votes
        var pagesdb = resources.collections.pages

        votesdb.count({ "post.id": new ObjectID(req.query.id), "user.id": req.user._id }, function(err, count) {
          if(count < 1) { // Användaren har inte röstat
            var isodate = resources.functions.getISOdate()
            var data = {
              content: parseInt(req.query.content),
              timestamp: isodate,
              post: { id: new ObjectID(req.query.id) },
              user: { id: req.user._id }
            }

            votesdb.insert(data, function (err) {
              if(err) throw err
              votesdb.aggregate(
              [ { $match: { "post.id": new ObjectID(req.query.id) } },
                { $group: {
                  _id: new ObjectID(req.query.id),
                  avg: { $avg: "$content" },
                }
              } ], function(err, result) {
                votesdb.count({ "post.id": new ObjectID(req.query.id) }, function (err, total_count) {
                  result[0].count = total_count
                  pagesdb.update({ "_id": new ObjectID(req.query.id) }, {$set: { "rating.votes_sum": result[0].avg}, $inc: { "rating.votes": 1 }}, function (err) {
                    if(err) throw err
                    res.send(result)
                  })
                })
              })
            })
          } else { // Användaren har röstat
            res.send('3') // Vote already found!
          }
        })
      } else {
        res.send('1') // Not logged in
      }
    } else {
      res.send('2') // All required variables not found in url
    }
  })

  /** /ajax/like
  * @type: GET
  * @description: Ajax route for handling user likes.
  */
  app.get('/ajax/like', function (req, res) {
    if(req.query.id != undefined) {

      if (req.isAuthenticated()) {
        var likesdb = resources.collections.likes
        likesdb.count({ "post.id": new ObjectID(req.query.id), "user.id": req.user._id }, function (err, count) {
          if(count > 0) { // Already liked, remove it
            likesdb.remove({ "post.id": new ObjectID(req.query.id), "user.id": req.user._id }, function (err) {
              if (err) throw err
            })

            var pagesdb = resources.collections.pages
            pagesdb.update({ "_id": new ObjectID(req.query.id) }, {$inc: { "rating.likes": -1, }}, function (err) {
              if(err) throw err
            })

            likesdb.count({ "post.id": new ObjectID(req.query.id) }, function (err, sum) {
              if (err) throw err
              var response = { 'action':0, 'new_value':sum }
              res.send(response)
            })
          } else { // First time pressing, add it
            var data = {
              post: { id: new ObjectID(req.query.id) },
              user: { id: req.user._id }
            }

            likesdb.insert(data, function (err) {
              if (err) throw err
            })

            var pagesdb = resources.collections.pages
            pagesdb.update({ "_id": new ObjectID(req.query.id) }, {$inc: { "rating.likes": 1, }}, function (err) {
              if (err) throw err
            })

            likesdb.count({ "post.id": new ObjectID(req.query.id) }, function (err, sum) {
              if (err) throw err
              var response = { 'action':1, 'new_value':sum }
              res.send(response)
            })
          }
        })
      } else {
        res.redirect('/login/like')
      }
    } else {
      res.send('2') // All variables isnt set
    }
  })

  app.get('/ajax/remove/:post_id', function(req, res) {
    var post_id = req.params.post_id
    var userid = req.params.user._id

    var pagesdb = resources.collections.pages
    var usersdb = resources.collections.users

    if(typeof(req.user) !== 'undefined') {
      //Make sure user exists
      usersdb.find({_id: new ObjectID(userid)}).toArray(function(err, users) {
        if (err) throw err

        if(users.length > 0) {
          var user = users[0]

          var pageQuery = {
            _id: new ObjectID(post_id)
          }
          //TODO move this into functions.js
          var privileged = ['admin','moderator']
          if( privileged.indexOf(user.info.permission) == -1 ) {
            //Lacking privileges, check that the user is the creator of the page
            pageQuery["user_info.id"] = new ObjectID(user._id)
          }

          pagesdb.update( pageQuery, { $set: { remove: true, "timestamp.updated": functions.getISOdate() } }, function(err, status) {
            if (err) throw err
            if(status.result.n > 0) {
              res.json({
                success: status.result.nModified,
                message: (status.result.nModified) ? 'Page has already been scheduled for deletion' : 'Page has been scheduled for deletion'
              })
            } else {
              res.json({<commit_msg>Fix remove page submission<commit_after>/** authenticated.js
* @file: /src/app/routes/ajax/authenticated.js
* @description: Handles express routing for the authenticated ajax routes
* @parameters: Object(app), Object(resources)
* @exports: Express routes
*/

var ObjectID = require('mongodb').ObjectID

module.exports = function (app, resources) {
  var functions = resources.functions
  /** /ajax/addVote
  * @type: GET
  * @description: Ajax route for handling user votes.
  */
  app.get('/ajax/addVote', function (req, res) {
    if(req.query.id != undefined && req.query.content != undefined) {
     if (req.isAuthenticated()) {
        var votesdb = resources.collections.votes
        var pagesdb = resources.collections.pages

        votesdb.count({ "post.id": new ObjectID(req.query.id), "user.id": req.user._id }, function(err, count) {
          if(count < 1) { // Användaren har inte röstat
            var isodate = resources.functions.getISOdate()
            var data = {
              content: parseInt(req.query.content),
              timestamp: isodate,
              post: { id: new ObjectID(req.query.id) },
              user: { id: req.user._id }
            }

            votesdb.insert(data, function (err) {
              if(err) throw err
              votesdb.aggregate(
              [ { $match: { "post.id": new ObjectID(req.query.id) } },
                { $group: {
                  _id: new ObjectID(req.query.id),
                  avg: { $avg: "$content" },
                }
              } ], function(err, result) {
                votesdb.count({ "post.id": new ObjectID(req.query.id) }, function (err, total_count) {
                  result[0].count = total_count
                  pagesdb.update({ "_id": new ObjectID(req.query.id) }, {$set: { "rating.votes_sum": result[0].avg}, $inc: { "rating.votes": 1 }}, function (err) {
                    if(err) throw err
                    res.send(result)
                  })
                })
              })
            })
          } else { // Användaren har röstat
            res.send('3') // Vote already found!
          }
        })
      } else {
        res.send('1') // Not logged in
      }
    } else {
      res.send('2') // All required variables not found in url
    }
  })

  /** /ajax/like
  * @type: GET
  * @description: Ajax route for handling user likes.
  */
  app.get('/ajax/like', function (req, res) {
    if(req.query.id != undefined) {

      if (req.isAuthenticated()) {
        var likesdb = resources.collections.likes
        likesdb.count({ "post.id": new ObjectID(req.query.id), "user.id": req.user._id }, function (err, count) {
          if(count > 0) { // Already liked, remove it
            likesdb.remove({ "post.id": new ObjectID(req.query.id), "user.id": req.user._id }, function (err) {
              if (err) throw err
            })

            var pagesdb = resources.collections.pages
            pagesdb.update({ "_id": new ObjectID(req.query.id) }, {$inc: { "rating.likes": -1, }}, function (err) {
              if(err) throw err
            })

            likesdb.count({ "post.id": new ObjectID(req.query.id) }, function (err, sum) {
              if (err) throw err
              var response = { 'action':0, 'new_value':sum }
              res.send(response)
            })
          } else { // First time pressing, add it
            var data = {
              post: { id: new ObjectID(req.query.id) },
              user: { id: req.user._id }
            }

            likesdb.insert(data, function (err) {
              if (err) throw err
            })

            var pagesdb = resources.collections.pages
            pagesdb.update({ "_id": new ObjectID(req.query.id) }, {$inc: { "rating.likes": 1, }}, function (err) {
              if (err) throw err
            })

            likesdb.count({ "post.id": new ObjectID(req.query.id) }, function (err, sum) {
              if (err) throw err
              var response = { 'action':1, 'new_value':sum }
              res.send(response)
            })
          }
        })
      } else {
        res.redirect('/login/like')
      }
    } else {
      res.send('2') // All variables isnt set
    }
  })

  app.get('/ajax/remove/:post_id', function(req, res) {
    var post_id = req.params.post_id
    var userid = req.user._id

    var pagesdb = resources.collections.pages
    var usersdb = resources.collections.users

    if(typeof(req.user) !== 'undefined') {
      //Make sure user exists
      usersdb.find({_id: new ObjectID(userid)}).toArray(function(err, users) {
        if (err) throw err

        if(users.length > 0) {
          var user = users[0]

          var pageQuery = {
            _id: new ObjectID(post_id)
          }
          //TODO move this into functions.js
          var privileged = ['admin','moderator']
          if( privileged.indexOf(user.info.permission) == -1 ) {
            //Lacking privileges, check that the user is the creator of the page
            pageQuery["user_info.id"] = new ObjectID(user._id)
          }

          pagesdb.update( pageQuery, { $set: { remove: true, "timestamp.updated": functions.getISOdate() } }, function(err, status) {
            if (err) throw err
            if(status.result.n > 0) {
              res.json({
                success: status.result.nModified,
                message: (status.result.nModified) ? 'Page has already been scheduled for deletion' : 'Page has been scheduled for deletion'
              })
            } else {
              res.json({
{'fraction_non_alphanumeric': 0.13961273977560623, 'fraction_numerical': 0.003076366268548679, 'mean_word_length': 1.7413194444444444, 'pattern_counts': {'":': 36, '<': 5, '<?xml version=': 0, '>': 9, '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': '913696', 'n_tokens_mistral': 3318, 'n_tokens_neox': 3243, 'n_words': 785}