# HG changeset patch # User neko259 # Date 2014-12-08 11:02:55 # Node ID 6155490bc755ee16f34ad3687a3b73ea8eff006c # Parent 72a583330abfe0b79035f83806f7c421ded6f794 Run thread update when connecting to websocket to get missed posts if the browser was closed diff --git a/boards/static/js/thread_update.js b/boards/static/js/thread_update.js --- a/boards/static/js/thread_update.js +++ b/boards/static/js/thread_update.js @@ -29,6 +29,7 @@ var wsUser = ''; var loading = false; var lastUpdateTime = null; var unreadPosts = 0; +var documentOriginalTitle = ''; // Thread ID does not change, can be stored one time var threadId = $('div.thread').children('.post').first().attr('id'); @@ -65,6 +66,8 @@ function connectWebsocket() { } }); + // For the case we closed the browser and missed some updates + getThreadDiff(); $('#autoupdate').text('[+]'); }); @@ -73,6 +76,51 @@ function connectWebsocket() { return true; } +/** + * Get diff of the posts from the current thread timestamp. + * This is required if the browser was closed and some post updates were + * missed. + */ +function getThreadDiff() { + var lastPost = threadPosts.last(); + var threadId = threadPosts.first().attr('id'); + + var diffUrl = '/api/diff_thread/' + threadId + '/' + lastUpdateTime + '/'; + + lastUpdateTime = $('.metapanel').attr('data-last-update'); + + $.getJSON(diffUrl) + .success(function(data) { + var bottom = isPageBottom(); + var addedPosts = data.added; + + for (var i = 0; i < addedPosts.length; i++) { + var postText = addedPosts[i]; + var post = $(postText); + + updatePost(post, true) + + lastPost = post; + } + + var updatedPosts = data.updated; + + for (var i = 0; i < updatedPosts.length; i++) { + var postText = updatedPosts[i]; + var post = $(postText); + + updatePost(post, false) + } + + // TODO Process removed posts if any + + lastUpdateTime = data.last_update; + }) +} + +/** + * Add or update the post on thml page. + */ function updatePost(postHtml, isAdded) { // This needs to be set on start because the page is scrolled after posts // are added or updated @@ -110,6 +158,9 @@ function updatePost(postHtml, isAdded) { updateMetadataPanel(lastUpdate) } +/** + * Initiate a blinking animation on a node to show it was updated. + */ function blink(node) { var blinkCount = 2; @@ -138,6 +189,10 @@ function getImageCount() { return $('.thread').find('img').length } +/** + * Update post count, images count and last update time in the metadata + * panel. + */ function updateMetadataPanel(lastUpdate) { var replyCountField = $('#reply-count'); var imageCountField = $('#image-count'); @@ -178,7 +233,6 @@ function updateBumplimitProgress(postDel } } -var documentOriginalTitle = ''; /** * Show 'new posts' text in the title if the document is not visible to a user */ diff --git a/boards/views/thread.py b/boards/views/thread.py --- a/boards/views/thread.py +++ b/boards/views/thread.py @@ -2,7 +2,6 @@ from django.core.urlresolvers import rev from django.db import transaction from django.http import Http404 from django.shortcuts import get_object_or_404, render, redirect -from django.views.decorators.cache import never_cache, cache_control from django.views.generic.edit import FormMixin from boards import utils, settings @@ -39,7 +38,6 @@ MODE_NORMAL = 'normal' class ThreadView(BaseBoardView, PostMixin, FormMixin): - @cache_control(no_cache=True) def get(self, request, post_id, mode=MODE_NORMAL, form=None): try: opening_post = Post.objects.filter(id=post_id).only('thread_new')[0]