From 661033c94812c4dbc8bd91be75cb7f71e1ebfd9a Mon Sep 17 00:00:00 2001 From: ccd0 Date: Sun, 5 Oct 2014 20:11:08 -0700 Subject: [PATCH] Move to push-push design for hidden threads sync with catalog. Only changes to the list are pushed. Fails at syncing with catalog across http/https, but fixes the issue where changes to the list would be reverted. --- src/Filtering/ThreadHiding.coffee | 46 +++++++++++++++++++++++++------ src/General/Settings.coffee | 5 +++- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/Filtering/ThreadHiding.coffee b/src/Filtering/ThreadHiding.coffee index ab7828283..f4d7da8ab 100755 --- a/src/Filtering/ThreadHiding.coffee +++ b/src/Filtering/ThreadHiding.coffee @@ -2,25 +2,32 @@ ThreadHiding = init: -> return if g.VIEW is 'thread' or !Conf['Thread Hiding Buttons'] and !Conf['Thread Hiding Link'] and !Conf['JSON Navigation'] @db = new DataBoard 'hiddenThreads' - return @catalogHide() if g.VIEW is 'catalog' + return @catalogWatch() if g.VIEW is 'catalog' + @cleanCatalog() Thread.callbacks.push name: 'Thread Hiding' cb: @node - catalogHide: -> - @hiddenThreads = ThreadHiding.db.get - boardID: g.BOARD.ID - defaultValue: {} - @hiddenThreads[threadID] = true for threadID of @hiddenThreads - localStorage.setItem "4chan-hide-t-#{g.BOARD}", JSON.stringify @hiddenThreads + getCatalogHidden: -> + JSON.parse(localStorage.getItem "4chan-hide-t-#{g.BOARD}") or {} + + setCatalogHidden: (threads) -> + if Object.keys(threads).length + localStorage.setItem "4chan-hide-t-#{g.BOARD}", JSON.stringify threads + else + localStorage.removeItem "4chan-hide-t-#{g.BOARD}" + + catalogWatch: -> + @hiddenThreads = ThreadHiding.getCatalogHidden() $.ready -> + # 4chan's catalog sets the style to "display: none;" when hiding or unhiding a thread. new MutationObserver(ThreadHiding.catalogSave).observe $.id('threads'), attributes: true subtree: true attributeFilter: ['style'] catalogSave: -> - hiddenThreads2 = JSON.parse localStorage.getItem "4chan-hide-t-#{g.BOARD}" + hiddenThreads2 = ThreadHiding.getCatalogHidden() for threadID of hiddenThreads2 when !(threadID of ThreadHiding.hiddenThreads) ThreadHiding.db.set boardID: g.BOARD.ID @@ -30,6 +37,7 @@ ThreadHiding = ThreadHiding.db.delete boardID: g.BOARD.ID threadID: threadID + ThreadHiding.hiddenThreads = hiddenThreads2 node: -> if data = ThreadHiding.db.get {boardID: @board.ID, threadID: @ID} @@ -44,6 +52,24 @@ ThreadHiding = ThreadHiding.makeStub thread, root return + cleanCatalog: -> + # We need to clean hidden threads on the catalog ourselves, + # otherwise if we don't visit the catalog regularly + # it will pollute the localStorage. + + # Only clean the catalog if our own hidden threads list was just cleaned. + return unless (ThreadHiding.db.data.lastChecked or 0) > Date.now() - $.MINUTE + + $.cache "//a.4cdn.org/#{g.BOARD}/threads.json", -> + return unless @status is 200 + hiddenThreadsOnCatalog = ThreadHiding.getCatalogHidden() + threads = {} + for page in @response + for thread in page.threads + if thread.no of hiddenThreadsOnCatalog + threads[thread.no] = hiddenThreadsOnCatalog[thread.no] + ThreadHiding.setCatalogHidden threads + menu: init: -> return if g.VIEW isnt 'index' or !Conf['Menu'] or !Conf['Thread Hiding Link'] @@ -145,15 +171,19 @@ ThreadHiding = $.prepend root, thread.stub saveHiddenState: (thread, makeStub) -> + hiddenThreadsOnCatalog = ThreadHiding.getCatalogHidden() if thread.isHidden ThreadHiding.db.set boardID: thread.board.ID threadID: thread.ID val: {makeStub} + hiddenThreadsOnCatalog[thread] = true else ThreadHiding.db.delete boardID: thread.board.ID threadID: thread.ID + delete hiddenThreadsOnCatalog[thread] + ThreadHiding.setCatalogHidden hiddenThreadsOnCatalog toggle: (thread) -> unless thread instanceof Thread diff --git a/src/General/Settings.coffee b/src/General/Settings.coffee index f7a0d4122..88e13eb63 100755 --- a/src/General/Settings.coffee +++ b/src/General/Settings.coffee @@ -123,7 +123,10 @@ Settings = button.textContent = "Hidden: #{hiddenNum}" $.on button, 'click', -> @textContent = 'Hidden: 0' - $.delete ['hiddenThreads', 'hiddenPosts'] + $.get 'hiddenThreads', {}, ({hiddenThreads}) -> + for boardID of hiddenThreads.boards + localStorage.removeItem "4chan-hide-t-#{boardID}" + $.delete ['hiddenThreads', 'hiddenPosts'] $.after $('input[name="Stubs"]', section).parentNode.parentNode, div export: -> # Make sure to export the most recent data.