Make Thread Stats work on Tinyboard/vichan sites. #2171
This commit is contained in:
parent
64d1e5b21d
commit
8091065f03
@ -1,15 +1,22 @@
|
||||
ThreadStats =
|
||||
postCount: 0
|
||||
fileCount: 0
|
||||
postIndex: 0
|
||||
|
||||
init: ->
|
||||
return if g.VIEW isnt 'thread' or !Conf['Thread Stats']
|
||||
|
||||
if Conf['Page Count in Stats']
|
||||
@[if g.SITE.isPrunedByAge?(g.BOARD) then 'showPurgePos' else 'showPage'] = true
|
||||
|
||||
statsHTML = <%= html(
|
||||
'<span id="post-count">?</span> / <span id="file-count">?</span>' +
|
||||
'?{Conf["IP Count in Stats"]}{ / <span id="ip-count">?</span>}' +
|
||||
'?{Conf["IP Count in Stats"] && g.SITE.hasIPCount}{ / <span id="ip-count">?</span>}' +
|
||||
'?{Conf["Page Count in Stats"]}{ / <span id="page-count">?</span>}'
|
||||
) %>
|
||||
statsTitle = 'Posts / Files'
|
||||
statsTitle += ' / IPs' if Conf['IP Count in Stats']
|
||||
statsTitle += (if g.BOARD.ID is 'f' then ' / Purge Position' else ' / Page') if Conf['Page Count in Stats']
|
||||
statsTitle += ' / IPs' if Conf['IP Count in Stats'] and g.SITE.hasIPCount
|
||||
statsTitle += (if @showPurgePos then ' / Purge Position' else ' / Page') if Conf['Page Count in Stats']
|
||||
|
||||
if Conf['Updater and Stats in Header']
|
||||
@dialog = sc = $.el 'span',
|
||||
@ -37,35 +44,46 @@ ThreadStats =
|
||||
cb: @node
|
||||
|
||||
node: ->
|
||||
postCount = 0
|
||||
fileCount = 0
|
||||
@posts.forEach (post) ->
|
||||
postCount++
|
||||
fileCount++ if post.file
|
||||
(ThreadStats.lastPost = post.info.date if ThreadStats.pageCountEl)
|
||||
ThreadStats.thread = @
|
||||
ThreadStats.count()
|
||||
ThreadStats.update()
|
||||
ThreadStats.fetchPage()
|
||||
ThreadStats.update postCount, fileCount, @ipCount
|
||||
$.on d, 'PostsInserted', -> $.queueTask ThreadStats.onPostsInserted
|
||||
$.on d, 'ThreadUpdate', ThreadStats.onUpdate
|
||||
|
||||
count: ->
|
||||
{posts} = ThreadStats.thread
|
||||
n = posts.keys.length
|
||||
for i in [ThreadStats.postIndex...n] by 1
|
||||
post = posts[posts.keys[i]]
|
||||
unless post.isFetchedQuote
|
||||
ThreadStats.postCount++
|
||||
ThreadStats.fileCount += post.files.length
|
||||
ThreadStats.postIndex = n
|
||||
|
||||
onUpdate: (e) ->
|
||||
return if e.detail[404]
|
||||
{postCount, fileCount, ipCount, newPosts} = e.detail
|
||||
ThreadStats.update postCount, fileCount, ipCount
|
||||
return unless ThreadStats.pageCountEl
|
||||
if newPosts.length
|
||||
ThreadStats.lastPost = g.posts[newPosts[newPosts.length - 1]].info.date
|
||||
if g.BOARD.ID isnt 'f' and ThreadStats.pageCountEl?.textContent isnt '1'
|
||||
{postCount, fileCount} = e.detail
|
||||
$.extend ThreadStats, {postCount, fileCount}
|
||||
ThreadStats.postIndex = ThreadStats.thread.posts.keys.length
|
||||
ThreadStats.update()
|
||||
if ThreadStats.showPage and ThreadStats.pageCountEl.textContent isnt '1'
|
||||
ThreadStats.fetchPage()
|
||||
|
||||
update: (postCount, fileCount, ipCount) ->
|
||||
onPostsInserted: ->
|
||||
return unless ThreadStats.thread.posts.keys.length > ThreadStats.postIndex
|
||||
ThreadStats.count()
|
||||
ThreadStats.update()
|
||||
if ThreadStats.showPage and ThreadStats.pageCountEl.textContent isnt '1'
|
||||
ThreadStats.fetchPage()
|
||||
|
||||
update: ->
|
||||
{thread, postCountEl, fileCountEl, ipCountEl} = ThreadStats
|
||||
postCountEl.textContent = postCount
|
||||
fileCountEl.textContent = fileCount
|
||||
if ipCount? and ipCountEl
|
||||
ipCountEl.textContent = ipCount
|
||||
(if thread.postLimit and !thread.isSticky then $.addClass else $.rmClass) postCountEl, 'warning'
|
||||
(if thread.fileLimit and !thread.isSticky then $.addClass else $.rmClass) fileCountEl, 'warning'
|
||||
postCountEl.textContent = ThreadStats.postCount
|
||||
fileCountEl.textContent = ThreadStats.fileCount
|
||||
ipCountEl?.textContent = thread.ipCount ? '?'
|
||||
postCountEl.classList.toggle 'warning', (thread.postLimit and !thread.isSticky)
|
||||
fileCountEl.classList.toggle 'warning', (thread.fileLimit and !thread.isSticky)
|
||||
|
||||
fetchPage: ->
|
||||
return unless ThreadStats.pageCountEl
|
||||
@ -76,15 +94,15 @@ ThreadStats =
|
||||
return
|
||||
ThreadStats.timeout = setTimeout ThreadStats.fetchPage, 2 * $.MINUTE
|
||||
$.whenModified(
|
||||
g.SITE.urls.threadsListJSON({boardID: ThreadStats.thread.board}),
|
||||
g.SITE.urls.threadsListJSON(ThreadStats.thread),
|
||||
'ThreadStats',
|
||||
ThreadStats.onThreadsLoad
|
||||
)
|
||||
|
||||
onThreadsLoad: ->
|
||||
if @status is 200
|
||||
for page in @response
|
||||
if g.BOARD.ID is 'f'
|
||||
for page, pageNum in @response
|
||||
if ThreadStats.showPurgePos
|
||||
purgePos = 1
|
||||
for thread in page.threads
|
||||
if thread.no < ThreadStats.thread.ID
|
||||
@ -92,9 +110,9 @@ ThreadStats =
|
||||
ThreadStats.pageCountEl.textContent = purgePos
|
||||
else
|
||||
for thread in page.threads when thread.no is ThreadStats.thread.ID
|
||||
ThreadStats.pageCountEl.textContent = page.page
|
||||
(if page.page is @response.length then $.addClass else $.rmClass) ThreadStats.pageCountEl, 'warning'
|
||||
ThreadStats.lastPageUpdate = new Date thread.last_modified * $.SECOND
|
||||
ThreadStats.pageCountEl.textContent = pageNum + 1
|
||||
ThreadStats.pageCountEl.classList.toggle 'warning', (pageNum is @response.length - 1)
|
||||
ThreadStats.lastPageUpdate = new Date(thread.last_modified * $.SECOND)
|
||||
ThreadStats.retry()
|
||||
return
|
||||
else if @status is 304
|
||||
@ -102,6 +120,12 @@ ThreadStats =
|
||||
|
||||
retry: ->
|
||||
# If thread data is stale (modification date given < time of last post), try again.
|
||||
if g.BOARD.ID isnt 'f' and ThreadStats.lastPost > ThreadStats.lastPageUpdate and ThreadStats.pageCountEl?.textContent isnt '1'
|
||||
clearTimeout ThreadStats.timeout
|
||||
ThreadStats.timeout = setTimeout ThreadStats.fetchPage, 5 * $.SECOND
|
||||
# Skip this on vichan sites due to sage posts not updating modification time in threads.json.
|
||||
return unless (
|
||||
ThreadStats.showPage and
|
||||
ThreadStats.pageCountEl.textContent isnt '1' and
|
||||
!g.SITE.threadModTimeIgnoresSage and
|
||||
ThreadStats.thread.posts[ThreadStats.thread.lastPost].info.date > ThreadStats.lastPageUpdate
|
||||
)
|
||||
clearTimeout ThreadStats.timeout
|
||||
ThreadStats.timeout = setTimeout ThreadStats.fetchPage, 5 * $.SECOND
|
||||
|
||||
@ -23,7 +23,6 @@ SW.tinyboard =
|
||||
'Thread Expansion'
|
||||
'Favicon'
|
||||
'Quote Threading'
|
||||
'Thread Stats'
|
||||
'Thread Updater'
|
||||
'Banner'
|
||||
'Flash Features'
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
SW.yotsuba =
|
||||
isOPContainerThread: false
|
||||
hasIPCount: true
|
||||
|
||||
urls:
|
||||
thread: ({boardID, threadID}) -> "#{location.protocol}//#{BoardConfig.domain(boardID)}/#{boardID}/thread/#{threadID}"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user