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

var ObjectID = require('mongodb').ObjectID

module.exports = function (app, resources) {
  var functions = resources.functions

  app.get('/admin', functions.isPrivileged, function (req, res) {
    var usersdb = resources.collections.users
    var pagesdb = resources.collections.pages
    var revisionsdb = resources.collections.revisions

    usersdb.count({}, function(err, users) {
      if (err) throw err

      pagesdb.count({}, function(err, pages) {
        if (err) throw err

        var today = new Date( functions.getISOdate() )
        var monday_this_week = today.setDate(today.getDate() - (today.getDay() + 6) % 7)
        monday_this_week = functions.newISOdate( new Date( monday_this_week) )

        //Pages this week
        pagesdb.count({"timestamp.created":{ $gte : monday_this_week }}, function(err, pages_this_week) {

          //Pages per month
          pagesdb.aggregate([
            {
              $group: {
                _id: {
                  id: "$_id", type: "$type", date: "$timestamp.created"
                }
              }
            },
            {
              $group: {
                _id: {
                  date: { $substr: ["$_id.date", 0, 10] }, type: "$_id.type"
                },
                pages: {
                  $sum: 1
                }
              }
            }
          ], function(err, pages_stats) {
            if (err) throw err

            revisionsdb.find({pending: { $gt: 0 } }).toArray(function(err, revisions) {
              pagesdb.find({remove: true}).toArray(function(err, removals) {
                var changes = []
                //Check if any of the removed pages has new revisions
                for (var i = 0; i < removals.length; i++) {
                  changes.push( String(removals[i]._id) )
                }

                for (var i = 0; i < revisions.length; i++) {
                  if( changes.indexOf( String(revisions[i].post_id ) ) == -1 ) {
                    changes.push( String(revisions[i].post_id ) )
                  }
                }

                res.render('admin/index', {
                  user: req.user,
                  active_page: 'index',
                  users: users,
                  pages_total: pages,
                  pages_this_week: pages_this_week,
                  pages_stats: pages_stats,
                  changes: changes,
                  loadPageResources: { highcharts: true },
                  loadAdminResources: { overview: true }
                })
              })
            })
          })
        })
      })
    })
  })

  app.get('/admin/borttagning/:post_id', functions.isPrivileged, function (req, res) {
    var usersdb = resources.collections.users
    var pagesdb = resources.collections.pages

    pagesdb.find({_id: new ObjectID(req.params.post_id)}).toArray(function(err, doc) {
      var page = doc[0]

      res.render('admin/remove', {
        user: req.user,
        active_page: 'remove',
        page: page
      })
    })
  })

  app.get('/admin/uppdateringar', functions.isPrivileged, function (req, res) {
    var revisionsdb = resources.collections.revisions
    var pagesdb = resources.collections.pages

    revisionsdb.find({pending: { $gt: 0 } }).toArray(function(err, revisions) {
      if (err) throw err

      var updated = []
      var changes = []
      var revisionIds = []

      for (var i = 0; i < revisions.length; i++) {
        updated.push(new ObjectID(revisions[i].post_id))
        revisionIds.push(String(revisions[i].post_id))
      }

      pagesdb.find({ $or: [ { _id: { $in: updated } }, { remove: true } ] } ).toArray(function(err, pages) {
        if (err) throw err

        if(pages.length > 0) {
          for (var i = 0; i < pages.length; i++) {
            var page_id = String(pages[i]._id)

            if( revisionIds.indexOf(page_id) >= 0 ) {
              for(var key in revisions) {
                var revision = revisions[key]
                var revision_id = String(revision.post_id)

                if(revision_id == page_id) {
                  changes.push({
                    title: pages[i].title,
                    url: pages[i].url,
                    id: pages[i]._id,
                    created: functions.getPrettyDateTime(pages[i].timestamp.created),
                    updated: functions.getPrettyDateTime(revision.modified),
                    revisions: Object.keys(revision.revisions).length,
                    remove: pages[i].hasOwnProperty('remove') ? pages[i].remove : false
                  })
                }
              }
            } else {
              if('remove' in pages[i]) {
                changes.push({
                  title: pages[i].title,
                  url: pages[i].url,
                  id: pages[i]._id,
                  created: functions.getPrettyDateTime(pages[i].timestamp.created),
                  updated: pages[i].timestamp.hasOwnProperty('updated') ? functions.getPrettyDateTime(pages[i].timestamp.updated) : 0,
                  revisions: 0,
                  remove: pages[i].remove
                })
              }
            }
          }
        }

        res.render('admin/changes', {
          user: req.user,
          active_page: 'changes',
          changes: changes,
          loadPageResources: { datatables: true }
        })
      })
    })
  })

  app.get('/admin/profil/:user_id', functions.isPrivileged, function (req, res) {
    var usersdb = resources.collections.users
    var pagesdb = resources.collections.pages<commit_msg>Rename 'remove' attribute to 'delete' instead, makes more sense now that we have 'removed' attribute<commit_after>/** privileged.js
* @file: /src/app/routes/pages/privileged.js
* @description: Handles express routing for the privileged page routes
* @parameters: Object(app), Object(resources)
* @exports: Express routes
*/

var ObjectID = require('mongodb').ObjectID

module.exports = function (app, resources) {
  var functions = resources.functions

  app.get('/admin', functions.isPrivileged, function (req, res) {
    var usersdb = resources.collections.users
    var pagesdb = resources.collections.pages
    var revisionsdb = resources.collections.revisions

    usersdb.count({}, function(err, users) {
      if (err) throw err

      pagesdb.count({}, function(err, pages) {
        if (err) throw err

        var today = new Date( functions.getISOdate() )
        var monday_this_week = today.setDate(today.getDate() - (today.getDay() + 6) % 7)
        monday_this_week = functions.newISOdate( new Date( monday_this_week) )

        //Pages this week
        pagesdb.count({"timestamp.created":{ $gte : monday_this_week }}, function(err, pages_this_week) {

          //Pages per month
          pagesdb.aggregate([
            {
              $group: {
                _id: {
                  id: "$_id", type: "$type", date: "$timestamp.created"
                }
              }
            },
            {
              $group: {
                _id: {
                  date: { $substr: ["$_id.date", 0, 10] }, type: "$_id.type"
                },
                pages: {
                  $sum: 1
                }
              }
            }
          ], function(err, pages_stats) {
            if (err) throw err

            revisionsdb.find({pending: { $gt: 0 } }).toArray(function(err, revisions) {
              pagesdb.find({delete: true}).toArray(function(err, removals) {
                var changes = []
                //Check if any of the removed pages has new revisions
                for (var i = 0; i < removals.length; i++) {
                  changes.push( String(removals[i]._id) )
                }

                for (var i = 0; i < revisions.length; i++) {
                  if( changes.indexOf( String(revisions[i].post_id ) ) == -1 ) {
                    changes.push( String(revisions[i].post_id ) )
                  }
                }

                res.render('admin/index', {
                  user: req.user,
                  active_page: 'index',
                  users: users,
                  pages_total: pages,
                  pages_this_week: pages_this_week,
                  pages_stats: pages_stats,
                  changes: changes,
                  loadPageResources: { highcharts: true },
                  loadAdminResources: { overview: true }
                })
              })
            })
          })
        })
      })
    })
  })

  app.get('/admin/borttagning/:post_id', functions.isPrivileged, function (req, res) {
    var usersdb = resources.collections.users
    var pagesdb = resources.collections.pages

    pagesdb.find({_id: new ObjectID(req.params.post_id)}).toArray(function(err, doc) {
      var page = doc[0]

      res.render('admin/remove', {
        user: req.user,
        active_page: 'remove',
        page: page
      })
    })
  })

  app.get('/admin/uppdateringar', functions.isPrivileged, function (req, res) {
    var revisionsdb = resources.collections.revisions
    var pagesdb = resources.collections.pages

    revisionsdb.find({pending: { $gt: 0 } }).toArray(function(err, revisions) {
      if (err) throw err

      var updated = []
      var changes = []
      var revisionIds = []

      for (var i = 0; i < revisions.length; i++) {
        updated.push(new ObjectID(revisions[i].post_id))
        revisionIds.push(String(revisions[i].post_id))
      }

      pagesdb.find({ $or: [ { _id: { $in: updated } }, { delete: true } ] } ).toArray(function(err, pages) {
        if (err) throw err

        if(pages.length > 0) {
          for (var i = 0; i < pages.length; i++) {
            var page_id = String(pages[i]._id)

            if( revisionIds.indexOf(page_id) >= 0 ) {
              for(var key in revisions) {
                var revision = revisions[key]
                var revision_id = String(revision.post_id)

                if(revision_id == page_id) {
                  changes.push({
                    title: pages[i].title,
                    url: pages[i].url,
                    id: pages[i]._id,
                    created: functions.getPrettyDateTime(pages[i].timestamp.created),
                    updated: functions.getPrettyDateTime(revision.modified),
                    revisions: Object.keys(revision.revisions).length,
                    delete: pages[i].hasOwnProperty('delete') ? pages[i].delete : false
                  })
                }
              }
            } else {
              if('delete' in pages[i]) {
                changes.push({
                  title: pages[i].title,
                  url: pages[i].url,
                  id: pages[i]._id,
                  created: functions.getPrettyDateTime(pages[i].timestamp.created),
                  updated: pages[i].timestamp.hasOwnProperty('updated') ? functions.getPrettyDateTime(pages[i].timestamp.updated) : 0,
                  revisions: 0,
                  delete: pages[i].delete
                })
              }
            }
          }
        }

        res.render('admin/changes', {
          user: req.user,
          active_page: 'changes',
          changes: changes,
          loadPageResources: { datatables: true }
        })
      })
    })
  })

  app.get('/admin/profil/:user_id', functions.isPrivileged, function (req, res) {
    var usersdb = resources.collections.users
    var pagesdb = resources.collections.pages
{'fraction_non_alphanumeric': 0.1241751649670066, 'fraction_numerical': 0.0030850972662610334, 'mean_word_length': 1.6136618141097425, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 7, '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': '4313482', 'n_tokens_mistral': 3461, 'n_tokens_neox': 3207, 'n_words': 681}