 url = require 'url'
 request = require 'superagent'
 { ObjectId } = require 'mongojs'
-{ ARTSY_URL, API_MAX, API_PAGE_SIZE, SAILTHRU_KEY, SAILTHRU_SECRET, API_URL, EMBEDLY_KEY } = process.env
+{ ARTSY_URL, API_MAX, API_PAGE_SIZE, SAILTHRU_KEY, SAILTHRU_SECRET, API_URL, EMBEDLY_KEY, FORCE_URL } = process.env
 sailthru = require('sailthru-client').createSailthruClient(SAILTHRU_KEY,SAILTHRU_SECRET)
 { crop } = require('embedly-view-helpers')(EMBEDLY_KEY)
 

      article = _.extend article, input, updated_at: new Date
      article.author = User.denormalizedForArticle author if author
      cb null, article, author, publishing

updateSailthruContent = (article) =>
  images = {}
  tags = article.keywords
  tags = tags.concat ["artsy-editorial"] if article.author_id is "503f86e462d56000020002cc"
  if article.email_metadata?.image_url
    images =
      full: url: crop(article.email_metadata.image_url, { width: 1280, height: 960 } )
      thumb: url: crop(article.email_metadata.image_url, { width: 552, height: 392 } )
  response = sailthru.apiPost 'content',
    url: "https://artsy.net/article/#{article.slug}"
    date: article.published_at
    title: article.email_metadata?.headline or article.thumbnail_title
    author: article.email_metadata?.author or article.author.name
    tags: tags
    images: images
    spider: 0
    vars:
      credit_line: article.email_metadata?.credit_line
      credit_url: article.email_metadata?.credit_url
  , (err, response) =>
    return err if err

# After merging article & input


    biography_for_artist_id: ObjectId(article.biography_for_artist_id) if article.biography_for_artist_id
    super_article: if article.super_article?.related_articles then _.extend article.super_article, related_articles: article.super_article.related_articles.map(ObjectId) else {}

sendArticleToSailthru = (article, cb) =>
  images = {}
  tags = article.keywords.concat ['article']
  tags = tags.concat ['artsy-editorial'] if article.author_id is "503f86e462d56000020002cc"
  tags = tags.concat ['magazine'] if article.featured is true
  if article.email_metadata?.image_url
    images =
      full: url: crop(article.email_metadata.image_url, { width: 1200, height: 706 } )
      thumb: url: crop(article.email_metadata.image_url, { width: 900, height: 530 } )
  if NODE_ENV is 'production'
    response = sailthru.apiPost 'content',
      url: "#{FORCE_URL}/article/#{article.slug}"
      date: article.published_at
      title: article.email_metadata?.headline or article.thumbnail_title
      author: article.email_metadata?.author or article.author?.name
      tags: tags
      images: images
      spider: 0
      vars:
        credit_line: article.email_metadata?.credit_line
        credit_url: article.email_metadata?.credit_url
    , (err, response) =>
      return cb(err) if err
      cb(null)

sanitizeAndSave = (callback) -> (err, article) ->
  return callback err if err
  # Send new content call to Sailthru on any published article save
  if article.published
    sendArticleToSailthru article, (err) =>
      console.log 'Log an error' if err
      db.articles.save sanitize(typecastIds article), callback
  else
    db.articles.save sanitize(typecastIds article), callback

@destroy = (id, callback) ->
  db.articles.remove { _id: ObjectId(id) }, callback
