##// END OF EJS Templates
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
neko259 -
r895:ffaaf497 default
parent child Browse files
Show More
@@ -414,10 +414,7 b' class Post(models.Model, Viewable):'
414 414 channel_name = WS_CHANNEL_THREAD + str(self.get_thread()
415 415 .get_opening_post_id())
416 416 client.publish(channel_name, {
417 'html': self.get_post_data(
418 format_type=DIFF_TYPE_HTML,
419 request=request),
420 'diff_type': 'added' if recursive else 'updated',
417 'notification_type': 'new_post',
421 418 })
422 419 client.send()
423 420
@@ -3,7 +3,7 b''
3 3 JavaScript code in this page.
4 4
5 5
6 Copyright (C) 2013 neko259
6 Copyright (C) 2013-2014 neko259
7 7
8 8 The JavaScript code in this page is free software: you can
9 9 redistribute it and/or modify it under the terms of the GNU
@@ -23,7 +23,6 b''
23 23 for the JavaScript code in this page.
24 24 */
25 25
26 var wsUrl = 'ws://localhost:9090/connection/websocket';
27 26 var wsUser = '';
28 27
29 28 var loading = false;
@@ -33,6 +32,12 b" var documentOriginalTitle = '';"
33 32 // Thread ID does not change, can be stored one time
34 33 var threadId = $('div.thread').children('.post').first().attr('id');
35 34
35 /**
36 * Connect to websocket server and subscribe to thread updates. On any update we
37 * request a thread diff.
38 *
39 * @returns {boolean} true if connected, false otherwise
40 */
36 41 function connectWebsocket() {
37 42 var metapanel = $('.metapanel')[0];
38 43
@@ -57,12 +62,7 b' function connectWebsocket() {'
57 62 centrifuge.on('connect', function() {
58 63 var channelName = 'thread:' + threadId;
59 64 centrifuge.subscribe(channelName, function(message) {
60 var postHtml = message.data['html'];
61 var isAdded = (message.data['diff_type'] === 'added');
62
63 if (postHtml) {
64 updatePost(postHtml, isAdded);
65 }
65 getThreadDiff();
66 66 });
67 67
68 68 // For the case we closed the browser and missed some updates
@@ -87,14 +87,13 b' function getThreadDiff() {'
87 87
88 88 $.getJSON(diffUrl)
89 89 .success(function(data) {
90 var bottom = isPageBottom();
91 90 var addedPosts = data.added;
92 91
93 92 for (var i = 0; i < addedPosts.length; i++) {
94 93 var postText = addedPosts[i];
95 94 var post = $(postText);
96 95
97 updatePost(post, true)
96 updatePost(post)
98 97
99 98 lastPost = post;
100 99 }
@@ -105,28 +104,37 b' function getThreadDiff() {'
105 104 var postText = updatedPosts[i];
106 105 var post = $(postText);
107 106
108 updatePost(post, false)
107 updatePost(post)
109 108 }
110 109
111 110 // TODO Process removed posts if any
111 $('.metapanel').attr('data-last-update', data.last_update);
112 112 })
113 113 }
114 114
115 115 /**
116 * Add or update the post on thml page.
116 * Add or update the post on html page.
117 117 */
118 function updatePost(postHtml, isAdded) {
118 function updatePost(postHtml) {
119 119 // This needs to be set on start because the page is scrolled after posts
120 120 // are added or updated
121 121 var bottom = isPageBottom();
122 122
123 123 var post = $(postHtml);
124 124
125 var threadPosts = $('div.thread').children('.post');
125 var threadBlock = $('div.thread');
126 126
127 127 var lastUpdate = '';
128 128
129 if (isAdded) {
129 var postId = post.attr('id');
130
131 // If the post already exists, replace it. Otherwise add as a new one.
132 var existingPosts = threadBlock.children('.post[id=' + postId + ']');
133
134 if (existingPosts.size() > 0) {
135 existingPosts.replaceWith(post);
136 } else {
137 var threadPosts = threadBlock.children('.post');
130 138 var lastPost = threadPosts.last();
131 139
132 140 post.appendTo(lastPost.parent());
@@ -140,12 +148,6 b' function updatePost(postHtml, isAdded) {'
140 148 if (bottom) {
141 149 scrollToBottom();
142 150 }
143 } else {
144 var postId = post.attr('id');
145
146 var oldPost = $('div.thread').children('.post[id=' + postId + ']');
147
148 oldPost.replaceWith(post);
149 151 }
150 152
151 153 processNewPost(post);
@@ -144,7 +144,7 b' class ThreadView(BaseBoardView, PostMixi'
144 144
145 145 post = Post.objects.create_post(title=title, text=text, image=image,
146 146 thread=post_thread, ip=ip, tags=tags)
147 post.send_to_websocket(request)
147 post.send_to_websocket(request, recursive=False)
148 148
149 149 thread_to_show = (opening_post.id if opening_post else post.id)
150 150
General Comments 0
You need to be logged in to leave comments. Login now