<commit_before>const cloudinary = require('cloudinary')
const config = require('config')
const User = require('../../app/models/user')
const Street = require('../../app/models/street')

module.exports = async function (req, res, next) {
  const userId = req.params.user_id
  const namespacedId = req.params.namespaced_id

  if (!userId || !namespacedId) {
    next()
  }

  // 1) Find user using userId.
  // 2) Find street using user._id and namespacedId.
  // 3) Find street thumbnail using street.id
  // 4a) Set res.locals.STREETMIX_TITLE if street found
  // 4b) Set res.locals.STREETMIX_IMAGE if thumbnail found

  const findUserById = async function (userId) {
    let user

    try {
      user = await User.findOne({ id: userId })
    } catch (error) {
      throw new Error('Error finding user.')
    }

    if (!user) {
      throw new Error('User not found.')
    }

    return user
  }

  const findStreetByNamespacedId = async function (user) {
    let street

    try {
      const creatorId = user._id.toString()
      street = await Street.findOne({ creator_id: creatorId, namespaced_id: namespacedId })
    } catch (error) {
      throw new Error('Error finding street.')
    }

    if (!street) {
      throw new Error('Street not found.')
    }

    const streetName = street.name || 'Unnamed Street'
    const title = `${streetName} - Streetmix`

    res.locals.STREETMIX_TITLE = title

    return street
  }

  const handleFindStreet = async function (street) {
    const streetName = street.name || 'Unnamed Street'
    const title = `${streetName} - Streetmix`

    res.locals.STREETMIX_TITLE = title

    let streetThumbnail

    try {
      const response = await cloudinary.v2.api.resource(`${config.env}/street_thumbnails/${street.id}`)
      streetThumbnail = response && response.secure_url
    } catch (error) {
      console.log(error)
      throw new Error('Error finding street thumbnail from cloudinary.')
    }

    if (streetThumbnail) {
      res.locals.STREETMIX_IMAGE = streetThumbnail
    }

    next()
  }

  const handleError = function (error) {
    console.log(error)
    next()
  }

  findUserById(userId)
    .then(findStreetByNamespacedId)
    .then(handleFindStreet)
    .catch(handleError)
}<commit_msg>make GET request for street thumbnail in metatags handler<commit_after>const request = require('request')
const config = require('config')
const User = require('../../app/models/user')
const Street = require('../../app/models/street')

module.exports = async function (req, res, next) {
  const userId = req.params.user_id
  const namespacedId = req.params.namespaced_id

  if (!userId || !namespacedId) {
    next()
  }

  // 1) Find user using userId.
  // 2) Find street using user._id and namespacedId.
  // 3) Find street thumbnail using street.id
  // 4a) Set res.locals.STREETMIX_TITLE if street found
  // 4b) Set res.locals.STREETMIX_IMAGE if thumbnail found

  const findUserById = async function (userId) {
    let user

    try {
      user = await User.findOne({ id: userId })
    } catch (error) {
      throw new Error('Error finding user.')
    }

    if (!user) {
      throw new Error('User not found.')
    }

    return user
  }

  const findStreetByNamespacedId = async function (user) {
    let street

    try {
      const creatorId = user._id.toString()
      street = await Street.findOne({ creator_id: creatorId, namespaced_id: namespacedId })
    } catch (error) {
      throw new Error('Error finding street.')
    }

    if (!street) {
      throw new Error('Street not found.')
    }

    const streetName = street.name || 'Unnamed Street'
    const title = `${streetName} - Streetmix`

    res.locals.STREETMIX_TITLE = title

    return street
  }

  const handleFindStreetThumbnail = function (error, response, body) {
    if (error) {
      console.log(error)
    }

    const results = JSON.parse(body)
    if (results && results.image) {
      res.locals.STREETMIX_IMAGE = results.image
    }

    next()
  }

  const handleFindStreet = async function (street) {
    const streetName = street.name || 'Unnamed Street'
    const title = `${streetName} - Streetmix`

    res.locals.STREETMIX_TITLE = title

    const endpoint = config.restapi.protocol + config.app_host_port + config.restapi.baseuri + `/v1/streets/images/${street.id}`
    request.get(endpoint, handleFindStreetThumbnail)
  }

  const handleError = function (error) {
    console.log(error)
    next()
  }

  findUserById(userId)
    .then(findStreetByNamespacedId)
    .then(handleFindStreet)
    .catch(handleError)
}
{'fraction_non_alphanumeric': 0.11655368467089439, 'fraction_numerical': 0.002624097966324076, 'mean_word_length': 2.9094017094017093, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1188701', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1421, 'n_words': 376}