# HG changeset patch # User neko259 # Date 2014-12-16 19:39:56 # Node ID ffaaf49747fcf82adcc5179a0e1544145d910b88 # Parent 48247e19da90617403c7e55e58f0d53c8bdea227 Trigger post update on a new post instead of sending the post itself. This fixed issues with posts sent in a fixed locale instead of a different locale for each client diff --git a/boards/models/post.py b/boards/models/post.py --- a/boards/models/post.py +++ b/boards/models/post.py @@ -414,10 +414,7 @@ class Post(models.Model, Viewable): channel_name = WS_CHANNEL_THREAD + str(self.get_thread() .get_opening_post_id()) client.publish(channel_name, { - 'html': self.get_post_data( - format_type=DIFF_TYPE_HTML, - request=request), - 'diff_type': 'added' if recursive else 'updated', + 'notification_type': 'new_post', }) client.send() 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 @@ -3,7 +3,7 @@ JavaScript code in this page. - Copyright (C) 2013 neko259 + Copyright (C) 2013-2014 neko259 The JavaScript code in this page is free software: you can redistribute it and/or modify it under the terms of the GNU @@ -23,7 +23,6 @@ for the JavaScript code in this page. */ -var wsUrl = 'ws://localhost:9090/connection/websocket'; var wsUser = ''; var loading = false; @@ -33,6 +32,12 @@ var documentOriginalTitle = ''; // Thread ID does not change, can be stored one time var threadId = $('div.thread').children('.post').first().attr('id'); +/** + * Connect to websocket server and subscribe to thread updates. On any update we + * request a thread diff. + * + * @returns {boolean} true if connected, false otherwise + */ function connectWebsocket() { var metapanel = $('.metapanel')[0]; @@ -57,12 +62,7 @@ function connectWebsocket() { centrifuge.on('connect', function() { var channelName = 'thread:' + threadId; centrifuge.subscribe(channelName, function(message) { - var postHtml = message.data['html']; - var isAdded = (message.data['diff_type'] === 'added'); - - if (postHtml) { - updatePost(postHtml, isAdded); - } + getThreadDiff(); }); // For the case we closed the browser and missed some updates @@ -87,14 +87,13 @@ function getThreadDiff() { $.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) + updatePost(post) lastPost = post; } @@ -105,28 +104,37 @@ function getThreadDiff() { var postText = updatedPosts[i]; var post = $(postText); - updatePost(post, false) + updatePost(post) } // TODO Process removed posts if any + $('.metapanel').attr('data-last-update', data.last_update); }) } /** - * Add or update the post on thml page. + * Add or update the post on html page. */ -function updatePost(postHtml, isAdded) { +function updatePost(postHtml) { // This needs to be set on start because the page is scrolled after posts // are added or updated var bottom = isPageBottom(); var post = $(postHtml); - var threadPosts = $('div.thread').children('.post'); + var threadBlock = $('div.thread'); var lastUpdate = ''; - if (isAdded) { + var postId = post.attr('id'); + + // If the post already exists, replace it. Otherwise add as a new one. + var existingPosts = threadBlock.children('.post[id=' + postId + ']'); + + if (existingPosts.size() > 0) { + existingPosts.replaceWith(post); + } else { + var threadPosts = threadBlock.children('.post'); var lastPost = threadPosts.last(); post.appendTo(lastPost.parent()); @@ -140,12 +148,6 @@ function updatePost(postHtml, isAdded) { if (bottom) { scrollToBottom(); } - } else { - var postId = post.attr('id'); - - var oldPost = $('div.thread').children('.post[id=' + postId + ']'); - - oldPost.replaceWith(post); } processNewPost(post); diff --git a/boards/views/thread.py b/boards/views/thread.py --- a/boards/views/thread.py +++ b/boards/views/thread.py @@ -144,7 +144,7 @@ class ThreadView(BaseBoardView, PostMixi post = Post.objects.create_post(title=title, text=text, image=image, thread=post_thread, ip=ip, tags=tags) - post.send_to_websocket(request) + post.send_to_websocket(request, recursive=False) thread_to_show = (opening_post.id if opening_post else post.id)