From 39a7aa3c81b39364d18e1f36e3685f64d3b663e3 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Thu, 21 Feb 2013 22:18:57 +0100 Subject: [PATCH] Fix inaccuracies in thread stats. When a post dies, we need to decrease the filecount too if it had a file. But if a post dies and its file was also deleted previously, we don't need to decrease the filecount twice. Just count the entire thread each thread update. --- 4chan_x.user.js | 51 ++++++++++++++++++--------------------------- src/features.coffee | 28 +++++++++++-------------- 2 files changed, 32 insertions(+), 47 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 5738de993..c952bf07d 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -5041,7 +5041,6 @@ return; } this.dialog = UI.dialog('thread-stats', 'bottom: 0; left: 0;', "
0 / 0
"); - this.postCount = this.fileCount = 0; this.postCountEl = $('#post-count', this.dialog); this.fileCountEl = $('#file-count', this.dialog); this.fileLimit = (function() { @@ -5064,40 +5063,30 @@ }); }, node: function() { - var ID, post, _ref; - _ref = this.posts; - for (ID in _ref) { - post = _ref[ID]; - ThreadStats.postCount++; - if (post.file) { - ThreadStats.fileCount++; - } - } + ThreadStats.thread = this; ThreadStats.update(); - $.on(d, 'ThreadUpdate', ThreadStats.onUpdate); + $.on(d, 'ThreadUpdate', ThreadStats.update); return $.add(d.body, ThreadStats.dialog); }, - onUpdate: function(e) { - var post, _i, _len, _ref; - if (e.detail[404]) { - return; - } - _ref = e.detail.newPosts; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - post = _ref[_i]; - ThreadStats.postCount++; - if (post.file) { - ThreadStats.fileCount++; - } - } - ThreadStats.postCount -= e.detail.deletedPosts.length; - ThreadStats.fileCount -= e.detail.deletedFiles.length; - return ThreadStats.update(); - }, update: function() { - this.postCountEl.textContent = ThreadStats.postCount; - this.fileCountEl.textContent = ThreadStats.fileCount; - return (ThreadStats.fileCount > ThreadStats.fileLimit ? $.addClass : $.rmClass)(ThreadStats.fileCountEl, 'warning'); + var ID, fileCount, post, postCount, _ref; + postCount = 0; + fileCount = 0; + _ref = ThreadStats.thread.posts; + for (ID in _ref) { + post = _ref[ID]; + if (post.isDead) { + continue; + } + postCount++; + if (!post.file || post.file.isDead) { + continue; + } + fileCount++; + } + ThreadStats.postCountEl.textContent = postCount; + ThreadStats.fileCountEl.textContent = fileCount; + return (fileCount > ThreadStats.fileLimit ? $.addClass : $.rmClass)(ThreadStats.fileCountEl, 'warning'); } }; diff --git a/src/features.coffee b/src/features.coffee index 5d1a0cd1f..38e82afa9 100644 --- a/src/features.coffee +++ b/src/features.coffee @@ -3400,7 +3400,6 @@ ThreadStats =
0 / 0
""" - @postCount = @fileCount = 0 @postCountEl = $ '#post-count', @dialog @fileCountEl = $ '#file-count', @dialog @fileLimit = # XXX boards config, need up to date data on this, check browser @@ -3416,24 +3415,21 @@ ThreadStats = name: 'Thread Stats' cb: @node node: -> - for ID, post of @posts - ThreadStats.postCount++ - ThreadStats.fileCount++ if post.file + ThreadStats.thread = @ ThreadStats.update() - $.on d, 'ThreadUpdate', ThreadStats.onUpdate + $.on d, 'ThreadUpdate', ThreadStats.update $.add d.body, ThreadStats.dialog - onUpdate: (e) -> - return if e.detail[404] - for post in e.detail.newPosts - ThreadStats.postCount++ - ThreadStats.fileCount++ if post.file - ThreadStats.postCount -= e.detail.deletedPosts.length - ThreadStats.fileCount -= e.detail.deletedFiles.length - ThreadStats.update() update: -> - @postCountEl.textContent = ThreadStats.postCount - @fileCountEl.textContent = ThreadStats.fileCount - (if ThreadStats.fileCount > ThreadStats.fileLimit then $.addClass else $.rmClass) ThreadStats.fileCountEl, 'warning' + postCount = 0 + fileCount = 0 + for ID, post of ThreadStats.thread.posts + continue if post.isDead + postCount++ + continue if !post.file or post.file.isDead + fileCount++ + ThreadStats.postCountEl.textContent = postCount + ThreadStats.fileCountEl.textContent = fileCount + (if fileCount > ThreadStats.fileLimit then $.addClass else $.rmClass) ThreadStats.fileCountEl, 'warning' ThreadUpdater = init: ->