<commit_before>
# ------------------------------------------------------------------------------
# BLOG INDEX


Template.blogIndex.onCreated ->
  @tag = new ReactiveVar null
  @autorun =>
    @tag.set Blog.Router.getParam 'tag'

  if not Session.get('blog.postLimit')
    if Blog.settings.pageSize
      Session.set 'blog.postLimit', Blog.settings.pageSize

  authorsSub = Blog.subs.subscribe 'blog.authors'
  postsSub = null

  @autorun =>
    if @tag.get()
      postsSub = Blog.subs.subscribe 'blog.taggedPosts', @tag.get()
    else
      postsSub = Blog.subs.subscribe 'blog.posts', Session.get('blog.postLimit')

  @subsReady = new ReactiveVar false
  @autorun =>
    if authorsSub.ready() and postsSub.ready()
      @subsReady.set true

Template.blogIndex.onRendered ->
  # Page Title
  document.title = "Blog"
  if Blog.settings.title
    document.title += " | #{Blog.settings.title}"

Template.blogIndex.helpers
  subsReady: -> Template.instance().subsReady.get()
  posts: ->
    tag = Template.instance().tag.get()
    if tag
      Blog.Post.where({ tags: tag }, { sort: publishedAt: -1 })
    else
      Blog.Post.where({}, { sort: publishedAt: -1 })


# ------------------------------------------------------------------------------
# SHOW BLOG


Template.blogShow.onCreated ->
  @slug = new ReactiveVar null
  @autorun =>
    @slug.set Blog.Router.getParam 'slug'

  postSub = Blog.subs.subscribe 'blog.singlePostBySlug', @slug.get()
  commentsSub = Blog.subs.subscribe 'blog.commentsBySlug', @slug.get()
  authorsSub = Blog.subs.subscribe 'blog.authors'

  @subsReady = new ReactiveVar false
  @autorun =>
    if postSub.ready() and commentsSub.ready() and authorsSub.ready() and !Meteor.loggingIn()
      @subsReady.set true

Template.blogShow.helpers
  subsReady: -> Template.instance().subsReady.get()
  post: -> Blog.Post.first slug: Template.instance().slug.get()
  notFound: ->
    if Blog.settings.blogNotFoundTemplate
      Blog.settings.blogNotFoundTemplate
    else
      notFound = Blog.Router.getNotFoundTemplate()
      if notFound
        return notFound
      else
        Blog.Router.notFound()


renderSideComments = null


Template.blogShowBody.onRendered ->
  Meteor.call 'isBlogAuthorized', @data.id, (err, authorized) =>
    # Can view?
    if @data.mode is 'draft'
      return Blog.Router.go('/blog') unless authorized

    # Can edit?
    if authorized
      Session.set 'blog.canEditPost', authorized

  # Page Title
  document.title = "#{@data.title}"
  if Blog.settings.title
    document.title += " | #{Blog.settings.title}"

  # Hide draft/private posts from crawlers
  if @data.mode isnt 'public'
    $('<meta>', { name: 'robots', content: 'noindex,nofollow' }).appendTo 'head'

  # Featured image resize
  if @data.featuredImage
    $(window).resize =><commit_msg>Fix bug where sometimes Flow Router would run notFound too soon<commit_after>
# ------------------------------------------------------------------------------
# BLOG INDEX


Template.blogIndex.onCreated ->
  @tag = new ReactiveVar null
  @autorun =>
    @tag.set Blog.Router.getParam 'tag'

  if not Session.get('blog.postLimit')
    if Blog.settings.pageSize
      Session.set 'blog.postLimit', Blog.settings.pageSize

  authorsSub = Blog.subs.subscribe 'blog.authors'
  postsSub = null

  @autorun =>
    if @tag.get()
      postsSub = Blog.subs.subscribe 'blog.taggedPosts', @tag.get()
    else
      postsSub = Blog.subs.subscribe 'blog.posts', Session.get('blog.postLimit')

  @subsReady = new ReactiveVar false
  @autorun =>
    if authorsSub.ready() and postsSub.ready()
      @subsReady.set true

Template.blogIndex.onRendered ->
  # Page Title
  document.title = "Blog"
  if Blog.settings.title
    document.title += " | #{Blog.settings.title}"

Template.blogIndex.helpers
  subsReady: -> Template.instance().subsReady.get()
  posts: ->
    tag = Template.instance().tag.get()
    if tag
      Blog.Post.where({ tags: tag }, { sort: publishedAt: -1 })
    else
      Blog.Post.where({}, { sort: publishedAt: -1 })


# ------------------------------------------------------------------------------
# SHOW BLOG


Template.blogShow.onCreated ->
  @slug = new ReactiveVar null
  @autorun =>
    @slug.set Blog.Router.getParam 'slug'

  postSub = Blog.subs.subscribe 'blog.singlePostBySlug', @slug.get()
  commentsSub = Blog.subs.subscribe 'blog.commentsBySlug', @slug.get()
  authorsSub = Blog.subs.subscribe 'blog.authors'

  @subsReady = new ReactiveVar false
  @autorun =>
    if postSub.ready() and commentsSub.ready() and authorsSub.ready() and !Meteor.loggingIn()
      @subsReady.set true

Template.blogShow.helpers
  subsReady: -> Template.instance().subsReady.get()
  post: -> Blog.Post.first slug: Template.instance().slug.get()
  notFound: ->
    if Blog.settings.blogNotFoundTemplate
      Blog.settings.blogNotFoundTemplate
    else
      notFound = Blog.Router.getNotFoundTemplate()
      if notFound
        return notFound
      else if Blog.Router.getParam('slug')
        Blog.Router.notFound()


renderSideComments = null


Template.blogShowBody.onRendered ->
  Meteor.call 'isBlogAuthorized', @data.id, (err, authorized) =>
    # Can view?
    if @data.mode is 'draft'
      return Blog.Router.go('/blog') unless authorized

    # Can edit?
    if authorized
      Session.set 'blog.canEditPost', authorized

  # Page Title
  document.title = "#{@data.title}"
  if Blog.settings.title
    document.title += " | #{Blog.settings.title}"

  # Hide draft/private posts from crawlers
  if @data.mode isnt 'public'
    $('<meta>', { name: 'robots', content: 'noindex,nofollow' }).appendTo 'head'

  # Featured image resize
  if @data.featuredImage
    $(window).resize =>
{'fraction_non_alphanumeric': 0.18146718146718147, 'fraction_numerical': 0.000702000702000702, 'mean_word_length': 2.6230133502860777, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, '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': '7364710', 'n_tokens_mistral': 1777, 'n_tokens_neox': 1751, 'n_words': 388}