<commit_before>const Profiler = require("../models/Profiler")
const SiteData = require("../models/SiteData")
const updateDataCaches = require('../../middlewares/updateLocalCache')
const runTask = require('../helpers/tasks')
const Streamers = require("../models/Streamer")

module.exports = (fastify, opts, next) => {
  // get all blog posts
  fastify.get('/blogs', async (req, res) => {
    if (!req.user || !req.user.isAdmin.access || !(req.user.isAdmin.super || req.user.isAdmin.blog)) {
      return res.code(403).send({error: "forbidden"})
    }
    const blogs = await Blog.find({}).populate('_userId', 'account.username').select('_id title date publishStatus _userId').sort('-date').exec()
    res.send({blogs: blogs})
  })


  // creates or updates a blog post
  fastify.post('/blog', async (req, res) => {
    if (!req.user || !req.user.isAdmin.access || !(req.user.isAdmin.super || req.user.isAdmin.blog)) {
      return res.code(403).send({error: "forbidden"})
    }

    if (!req.body.title || !req.body.content) {
      return res.code(403).send({error: "no content"})
    }

    if (req.body.id) {
      var blog = await Blog.findById(req.body.id).exec()
      blog.title = req.body.title
      blog.content = req.body.content
      blog.publishStatus = req.body.publishStatus
      await blog.save()
      res.send({success: true, blog: blog})
    }
    else {
      var blog = new Blog()
      blog.title = req.body.title
      blog.content = req.body.content
      blog.publishStatus = req.body.publishStatus
      blog.date = Date.now()
      blog._userId = req.user._id
      await blog.save()
      res.send({success: true, blog: blog})
    }

    taskQueue.add('UpdateLatestNews')
  })

  // lists current task queue
  fastify.get('/status', async (req, res) => {
    if (!req.user || !req.user.isAdmin.access || !req.user.isAdmin.super) {
      return res.code(403).send({error: "forbidden"})
    }
    var data = {}
    data.waiting = await taskQueue.getWaiting(0, 50)
    data.active = await taskQueue.getActive(0, 50)
    data.completed = await taskQueue.getCompleted(0, 50)
    data.profiler = await Profiler.find({}).sort({timestamp: -1}).limit(1000)
    data.ratelimit = {}
    res.send(data)
  })

  // get rate limit by ip
  fastify.get('/ratelimit', async (req, res) => {
    if (!req.user || !req.user.isAdmin.access || !req.user.isAdmin.super) {
      return res.code(403).send({error: "forbidden"})
    }
    var data = {}
    data['Auth Keys'] = await new Promise((done) => {
      var count = 0
      const scanStreamUsers = redis2.scanStream({
        match: 'rate:auth:' + (req.query.q || '*')
      })
      scanStreamUsers.on('data', (data) => {
        count = count + data.length
      })
      scanStreamUsers.on('end', () => {
        done(count)
      })
    })

    data['Common Keys'] = await new Promise((done) => {
      var count = 0
      const scanStreamUsers = redis2.scanStream({
        match: 'rate:wago:' + (req.query.q || '*')
      })
      scanStreamUsers.on('data', (data) => {
        count = count + data.length
      })
      scanStreamUsers.on('end', () => {
        done(count)
      })
    })

    res.send(data)
  })

  fastify.get('/stream', async (req, res) => {
    if (!req.user || !req.user.isAdmin.access || !(req.user.isAdmin.super || req.user.isAdmin.config.embed)) {
      return res.code(403).send({error: "forbidden"})
    }
    var data = await SiteData.findById('EmbeddedStream').lean().exec()
    data = data.value || {}
    data.activeUsers = await redis.get('tally:active:users')
    if (!data.streams) data.streams = []
    for (let i = 0; i < data.streams.length; i++) {
      data.streams[i].online = await redis.get(`twitch:${data.streams[i].channel}:live`)
      data.streams[i].viewing = await redis.get('tally:active:embed:' + data.streams[i].channel)
    }
    res.send(data)
  })

  fastify.post('/stream', async (req, res) => {
    if (!req.user || !req.user.isAdmin.access || !(req.user.isAdmin.super || req.user.isAdmin.config.embed)) {
      return res.code(403).send({error: "forbidden"})
    }
    var streams = []
    if (req.body.streams) {
      req.body.streams.forEach(s => {
        streams.push({
          channel: s.channel,
          exposure: s.exposure,
          max: s.max
        })
      })
    }
    var data = {
      enabled: req.body.enabled,
      streams: streams
    }
    await SiteData.set('EmbeddedStream', data)
    const channelStatuses = await runTask('UpdateTwitchStatus', req.body.channel)
    await updateDataCaches.queue('EmbeddedStream')
    await streams.forEach(async (stream, i) => {
      streams[i].online = channelStatuses[stream.channel]
      streams[i].viewing = await redis.get('tally:active:embed:' + streams.channel)
    })
    res.send({success: true, streams: streams, enabled: data.enabled, activeUsers: await redis.get('tally:active:users')})
  })

  fastify.get('/getstreamers', async (req, res) => {
    if (!req.user || !req.user.isAdmin.access || !(req.user.isAdmin.super || req.user.isAdmin.config.embed)) {
      return res.code(403).send({error: "forbidden"})
    }
    const streams = await Streamers.find({}).sort({online: -1})
    res.send(streams)
  })

  fastify.post('/streamer/add', async (req, res) => {
    if (!req.user || !req.user.isAdmin.access || !(req.user.isAdmin.super || req.user.isAdmin.config.embed)) {
      return res.code(403).send({error: "forbidden"})
    }
    const name = req.body.name
    const exists = await Streamers.findOne({name: {$regex: new RegExp(name, 'i')}})
    if (exists) {<commit_msg>this too!<commit_after>const Profiler = require("../models/Profiler")
const SiteData = require("../models/SiteData")
const updateDataCaches = require('../../middlewares/updateLocalCache')
const runTask = require('../helpers/tasks')
const Streamers = require("../models/Streamer")

module.exports = (fastify, opts, next) => {
  // get all blog posts
  fastify.get('/blogs', async (req, res) => {
    if (!req.user || !req.user.isAdmin.access || !(req.user.isAdmin.super || req.user.isAdmin.blog)) {
      return res.code(403).send({error: "forbidden"})
    }
    const blogs = await Blog.find({}).populate('_userId', 'account.username').select('_id title date publishStatus _userId').sort('-date').exec()
    res.send({blogs: blogs})
  })


  // creates or updates a blog post
  fastify.post('/blog', async (req, res) => {
    if (!req.user || !req.user.isAdmin.access || !(req.user.isAdmin.super || req.user.isAdmin.blog)) {
      return res.code(403).send({error: "forbidden"})
    }

    if (!req.body.title || !req.body.content) {
      return res.code(403).send({error: "no content"})
    }

    if (req.body.id) {
      var blog = await Blog.findById(req.body.id).exec()
      blog.title = req.body.title
      blog.content = req.body.content
      blog.publishStatus = req.body.publishStatus
      await blog.save()
      res.send({success: true, blog: blog})
    }
    else {
      var blog = new Blog()
      blog.title = req.body.title
      blog.content = req.body.content
      blog.publishStatus = req.body.publishStatus
      blog.date = Date.now()
      blog._userId = req.user._id
      await blog.save()
      res.send({success: true, blog: blog})
    }

    taskQueue.add('UpdateLatestNews')
  })

  // lists current task queue
  fastify.get('/status', async (req, res) => {
    if (!req.user || !req.user.isAdmin.access || !req.user.isAdmin.super) {
      return res.code(403).send({error: "forbidden"})
    }
    var data = {}
    data.waiting = await taskQueue.getWaiting(0, 50)
    data.active = await taskQueue.getActive(0, 50)
    data.completed = await taskQueue.getCompleted(0, 50)
    data.profiler = await Profiler.find({}).sort({timestamp: -1}).limit(1000)
    data.ratelimit = {}
    res.send(data)
  })

  // get rate limit by ip
  fastify.get('/ratelimit', async (req, res) => {
    if (!req.user || !req.user.isAdmin.access || !req.user.isAdmin.super) {
      return res.code(403).send({error: "forbidden"})
    }
    var data = {}
    data['Auth Keys'] = await new Promise((done) => {
      var count = 0
      const scanStreamUsers = redis2.scanStream({
        match: 'rate:auth:' + (req.query.q || '*')
      })
      scanStreamUsers.on('data', (data) => {
        count = count + data.length
      })
      scanStreamUsers.on('end', () => {
        done(count)
      })
    })

    data['Common Keys'] = await new Promise((done) => {
      var count = 0
      const scanStreamUsers = redis2.scanStream({
        match: 'rate:wago:' + (req.query.q || '*')
      })
      scanStreamUsers.on('data', (data) => {
        count = count + data.length
      })
      scanStreamUsers.on('end', () => {
        done(count)
      })
    })

    res.send(data)
  })

  fastify.get('/stream', async (req, res) => {
    if (!req.user || !req.user.isAdmin.access || !(req.user.isAdmin.super || req.user.isAdmin.config.embed)) {
      return res.code(403).send({error: "forbidden"})
    }
    var data = await SiteData.findById('EmbeddedStream').lean().exec()
    data = data.value || {}
    data.activeUsers = await redis.get('tally:active:users')
    if (!data.streams) data.streams = []
    for (let i = 0; i < data.streams.length; i++) {
      data.streams[i].online = await redis.get(`twitch:${data.streams[i].channel}:live`)
      data.streams[i].viewing = await redis.get('tally:active:embed:' + data.streams[i].channel)
    }
    res.send(data)
  })

  fastify.post('/stream', async (req, res) => {
    if (!req.user || !req.user.isAdmin.access || !(req.user.isAdmin.super || req.user.isAdmin.config.embed)) {
      return res.code(403).send({error: "forbidden"})
    }
    var streams = []
    if (req.body.streams) {
      req.body.streams.forEach(s => {
        streams.push({
          channel: s.channel,
          exposure: s.exposure,
          max: s.max
        })
      })
    }
    var data = {
      enabled: req.body.enabled,
      streams: streams
    }
    await SiteData.set('EmbeddedStream', data)
    const channelStatuses = await runTask('UpdateTwitchStatus', req.body.channel)
    await updateDataCaches.queue('EmbeddedStream')
    await streams.forEach(async (stream, i) => {
      streams[i].online = channelStatuses[stream.channel]
      streams[i].viewing = await redis.get('tally:active:embed:' + streams.channel)
    })
    res.send({success: true, streams: streams, enabled: data.enabled, activeUsers: await redis.get('tally:active:users')})
  })

  fastify.get('/getstreamers', async (req, res) => {
    if (!req.user || !req.user.isAdmin.access || !(req.user.isAdmin.super || req.user.isAdmin.config.embed)) {
      return res.code(403).send({error: "forbidden"})
    }
    const streams = await Streamers.find({}).sort({online: -1, offline: -1})
    const users = {
      total: await redis.get('tally:active:users'),
      viewing: streams.map(c => c.wagoViewers || 0).reduce((acc, cur) => acc + cur)
    }
    res.send({streams, users})
  })

  fastify.post('/streamer/add', async (req, res) => {
    if (!req.user || !req.user.isAdmin.access || !(req.user.isAdmin.super || req.user.isAdmin.config.embed)) {
      return res.code(403).send({error: "forbidden"})
    }
    const name = req.body.name
    const exists = await Streamers.findOne({name: {$regex: new RegExp(name, 'i')}})
    if (exists) {
{'fraction_non_alphanumeric': 0.18097672778561355, 'fraction_numerical': 0.00846262341325811, 'mean_word_length': 2.844459505252457, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, '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': '3686127', 'n_tokens_mistral': 3866, 'n_tokens_neox': 3800, 'n_words': 712}