<commit_before>const fs = require('fs')

const _ = require('lodash')
const fp = require('lodash/fp')
const KoaRouter = require('koa-router')
const escapeHtml = require('escape-html')

const titles = require('../../env/titles')
const config = require('../../env/config')
const logger = require('../../env/logger')
const isUuid = require('../../utils/isUuid')
const { getMessage } = require('../../postgres/queries/messages')

const router = new KoaRouter()

router.get('/*', async (ctx, next) => {
  const params = ctx.params[0].split('/')

  const clientPath = _.isObject(config.http.clientPath)
    ? config.http.clientPath[ctx.request.headers['x-client-app']]
    : config.http.clientPath

  if (!clientPath) {
    return next()
  }

  const indexPath = `${clientPath}/index.html`

  let indexHtml
  try {
    indexHtml = fs.readFileSync(indexPath, 'utf8')
  } catch (e) {
    logger.warn(`Could not find '${indexPath}'!`)
    return next()
  }

  let title

  const messageId = fp.findLast(isUuid, params)
  const message = messageId ? await getMessage(messageId) : null

  if (message) {
    title = titles.getMessageTitle(message)
  } else {
    const [serverId, channelName, nick] = params

    title = titles.getChannelTitle(serverId, channelName, nick)
  }

  indexHtml = indexHtml.replace(
    '<title>msks</title>',
    `<title>${escapeHtml(title)}</title>`,
  )

  ctx.body = indexHtml
})

module.exports = router<commit_msg>Check channel.isPublic for server titles<commit_after>const fs = require('fs')

const _ = require('lodash')
const fp = require('lodash/fp')
const KoaRouter = require('koa-router')
const escapeHtml = require('escape-html')

const titles = require('../../env/titles')
const config = require('../../env/config')
const logger = require('../../env/logger')
const isUuid = require('../../utils/isUuid')
const store = require('../../store')
const { getMessage } = require('../../postgres/queries/messages')

const router = new KoaRouter()

router.get('/*', async (ctx, next) => {
  const params = ctx.params[0].split('/')

  const clientPath = _.isObject(config.http.clientPath)
    ? config.http.clientPath[ctx.request.headers['x-client-app']]
    : config.http.clientPath

  if (!clientPath) {
    return next()
  }

  const indexPath = `${clientPath}/index.html`

  let indexHtml
  try {
    indexHtml = fs.readFileSync(indexPath, 'utf8')
  } catch (e) {
    logger.warn(`Could not find '${indexPath}'!`)
    return next()
  }

  let title

  const lastParam = fp.last(params)
  const messageId = isUuid(lastParam) ? lastParam : undefined

  const message = messageId ? await getMessage(messageId) : null

  if (message) {
    const state = store.getState()

    const channel = state.channels[message.channelId]

    if (!channel || !channel.isPublic) {
      return next()
    }

    title = titles.getMessageTitle(message)
  } else {
    const [serverId, channelName, nick] = params

    title = titles.getChannelTitle(serverId, channelName, nick)
  }

  indexHtml = indexHtml.replace(
    '<title>msks</title>',
    `<title>${escapeHtml(title)}</title>`,
  )

  ctx.body = indexHtml
})

module.exports = router
{'fraction_non_alphanumeric': 0.16719643992371266, 'fraction_numerical': 0.0012714558169103624, 'mean_word_length': 3.6211453744493394, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '3610847', 'n_tokens_mistral': 1019, 'n_tokens_neox': 998, 'n_words': 221}