<commit_before>_                   = require 'underscore'
Backbone            = require 'backbone'
sd                  = require('sharify').data
moment              = require 'moment'
Profile             = require './profile.coffee'
Artwork             = require './artwork.coffee'
Artists             = require '../collections/artists.coffee'
Artworks            = require '../collections/artworks.coffee'
AdditionalImage     = require '../models/additional_image.coffee'
Attachments         = require '../components/post/collections/post_attachments.coffee'
Reposts             = require '../components/post/collections/reposts.coffee'
{ smartTruncate }   = require '../components/util/string.coffee'

module.exports = class Post extends Backbone.Model

  urlRoot: "#{sd.ARTSY_URL}/api/v1/post"
  href: -> "/post/#{@get('id')}"
  canonicalUrl: -> sd.APP_URL + @href()
  profile: -> new Profile @get('profile')

  onPostPage: (location)->
    location ?= window?.location?.pathname
    location == @href()

  metaImage: ->
    if @has 'image_url' and @get 'image_versions' and @hasImage 'large'
      @imageUrl 'large'
    else if @artworks?.length > 0 and @artworks.first().hasImage 'large'
      @artworks.first().imageUrl 'large'
    else
      null

  shareTitle: ->
    profile = @profile()
    if @get('title') and profile?.displayName()
      "#{profile.displayName()}: #{@get('title')}"
    else if profile?.displayName()
      "#{profile.displayName()} on Artsy"
    else if @get('title')
      "#{@get('title')}"
    else
      "Post on @artsy"

  titleOrBody: (length=60) ->
    if @get('title')
      smartTruncate @get('title'), length
    else if @get('summary')
      smartTruncate @get('summary'), length

  metaTitle: ->
    _.compact([
      @profile()?.displayName()
      @titleOrBody()
      "Artsy"
    ]).join(" | ")

  artworkTitles: ->
    return unless @get('artworks')
    _.compact(for artwork in @get('artworks')
      new Artwork(artwork).toOneLine()
    ).join(', ')

  metaDescription: ->
    _.compact([
      smartTruncate(@get('summary'), 200)
      @artworkTitles()
    ]).join(" | ")

  defaultImage: ->
    _.first(@artworks().models)?.defaultImage() or
    @images().first() or
    @attachmentImage()

  attachmentImage: ->
    if @attachments().length
      src = _.first(@attachments()).imageSrc()
      if src
        imageUrlFor : -> src
        imageUrl    : -> src

  artworks: -><commit_msg>Adds a comment explaining the awkwardness of the post attachment image fallback<commit_after>_                   = require 'underscore'
Backbone            = require 'backbone'
sd                  = require('sharify').data
moment              = require 'moment'
Profile             = require './profile.coffee'
Artwork             = require './artwork.coffee'
Artists             = require '../collections/artists.coffee'
Artworks            = require '../collections/artworks.coffee'
AdditionalImage     = require '../models/additional_image.coffee'
Attachments         = require '../components/post/collections/post_attachments.coffee'
Reposts             = require '../components/post/collections/reposts.coffee'
{ smartTruncate }   = require '../components/util/string.coffee'

module.exports = class Post extends Backbone.Model

  urlRoot: "#{sd.ARTSY_URL}/api/v1/post"
  href: -> "/post/#{@get('id')}"
  canonicalUrl: -> sd.APP_URL + @href()
  profile: -> new Profile @get('profile')

  onPostPage: (location)->
    location ?= window?.location?.pathname
    location == @href()

  metaImage: ->
    if @has 'image_url' and @get 'image_versions' and @hasImage 'large'
      @imageUrl 'large'
    else if @artworks?.length > 0 and @artworks.first().hasImage 'large'
      @artworks.first().imageUrl 'large'
    else
      null

  shareTitle: ->
    profile = @profile()
    if @get('title') and profile?.displayName()
      "#{profile.displayName()}: #{@get('title')}"
    else if profile?.displayName()
      "#{profile.displayName()} on Artsy"
    else if @get('title')
      "#{@get('title')}"
    else
      "Post on @artsy"

  titleOrBody: (length=60) ->
    if @get('title')
      smartTruncate @get('title'), length
    else if @get('summary')
      smartTruncate @get('summary'), length

  metaTitle: ->
    _.compact([
      @profile()?.displayName()
      @titleOrBody()
      "Artsy"
    ]).join(" | ")

  artworkTitles: ->
    return unless @get('artworks')
    _.compact(for artwork in @get('artworks')
      new Artwork(artwork).toOneLine()
    ).join(', ')

  metaDescription: ->
    _.compact([
      smartTruncate(@get('summary'), 200)
      @artworkTitles()
    ]).join(" | ")

  defaultImage: ->
    _.first(@artworks().models)?.defaultImage() or
    @images().first() or
    @attachmentImage()

  # This takes care of attachments that don't implement
  # the usual images interface (i.e. post videos or post links).
  # They *do* often have a single imageSrc, so this returns the
  # usual methods on a bare object so they can be called normally
  # (Useful for post thumbnails)
  attachmentImage: ->
    if @attachments().length
      src = _.first(@attachments()).imageSrc()
      if src
        imageUrlFor : -> src
        imageUrl    : -> src

  artworks: ->
{'fraction_non_alphanumeric': 0.15461155759663223, 'fraction_numerical': 0.0026789131266743206, 'mean_word_length': 2.6680701754385967, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, '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': '5587035', 'n_tokens_mistral': 1632, 'n_tokens_neox': 1577, 'n_words': 370}