const fs = require('fs')
const path = require('path')
const YAML = require('yaml')
const util = require('./util')

module.exports.getBlogPosts = async (req, res, next) => {
  res.locals.blogPosts = await util.getBlogPosts({ publishedOnly: true })
  next()
}

module.exports.getNotes = async (req, res, next) => {
  res.locals.notes = await util.getNotes({ publishedOnly: true })
  next()
}

module.exports.getPostBySlug = async (req, res, next, slug) => {
  let post = await util.getPost(slug)

  if (post) {
    post.webmentions = await util.getWebmentions(post.url)
    post.hasWebmentions = post.webmentions.length > 0
  }
  
  res.locals.post = post
  next()
}

module.exports.getVinyl = async (req, res, next) => {
  const file = fs.readFileSync(path.join(__dirname, '../', 'content', 'music', 'vinyl.yaml'), 'utf8')
  res.locals.vinyl = YAML.parse(file)
  next()
}
