From 19ba43ae3dfc6008893ad9ebdbd0e68ecb60de01 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Wed, 27 Jan 2016 10:04:19 -0800 Subject: [PATCH] Fetch boards.json data. --- Gruntfile.coffee | 1 + src/General/BoardsJSON.coffee | 32 ++++++++++++++++++++++++++++++++ src/General/Main.coffee | 2 ++ src/classes/Board.coffee | 1 + 4 files changed, 36 insertions(+) create mode 100644 src/General/BoardsJSON.coffee diff --git a/Gruntfile.coffee b/Gruntfile.coffee index 9ca14c14f..367218796 100644 --- a/Gruntfile.coffee +++ b/Gruntfile.coffee @@ -108,6 +108,7 @@ module.exports = (grunt) -> 'src/classes/Connection.coffee' 'src/classes/Fetcher.coffee' 'src/General/Polyfill.coffee' + 'src/General/BoardsJSON.coffee' 'src/General/Header.coffee' 'src/General/Index.coffee' 'src/General/Build.coffee' diff --git a/src/General/BoardsJSON.coffee b/src/General/BoardsJSON.coffee new file mode 100644 index 000000000..e73630f76 --- /dev/null +++ b/src/General/BoardsJSON.coffee @@ -0,0 +1,32 @@ +BoardsJSON = + cbs: [] + + init: -> + if (Conf['boardsJSON'].lastChecked or 0) < Date.now() - 2 * $.HOUR + $.ajax '//a.4cdn.org/boards.json', onloadend: @load + else + @set Conf['boardsJSON'].boards + + load: -> + if @status is 200 + boards = {} + for board in @response.boards + boards[board.board] = board + $.set 'boardsJSON', {boards, lastChecked: Date.now()} + else + {boards} = Conf['boardsJSON'] + new Notice 'warning', "Failed to load boards JSON. Error #{@statusText} (#{@status})", 20 + BoardsJSON.set boards + + set: (@boards) -> + for ID, board of @boards + g.boards[ID]?.json = board + for cb in @cbs + $.queueTask cb + return + + ready: (cb) -> + if @boards + cb() + else + @cbs.push cb diff --git a/src/General/Main.coffee b/src/General/Main.coffee index 7db610394..61d09af42 100644 --- a/src/General/Main.coffee +++ b/src/General/Main.coffee @@ -36,6 +36,7 @@ Main = for db in DataBoard.keys Conf[db] = boards: {} + Conf['boardsJSON'] = boards: {} Conf['selectedArchives'] = {} Conf['cooldowns'] = {} @@ -357,6 +358,7 @@ Main = features: [ ['Polyfill', Polyfill] + ['Boards JSON', BoardsJSON] ['Normalize URL', NormalizeURL] ['Captcha Configuration', Captcha.replace] ['Redirect', Redirect] diff --git a/src/classes/Board.coffee b/src/classes/Board.coffee index 7f811d463..073f24fd5 100644 --- a/src/classes/Board.coffee +++ b/src/classes/Board.coffee @@ -4,5 +4,6 @@ class Board constructor: (@ID) -> @threads = new SimpleDict() @posts = new SimpleDict() + @json = BoardsJSON.boards?[@ID] g.boards[@] = @