##// 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 channel_name = WS_CHANNEL_THREAD + str(self.get_thread()
414 channel_name = WS_CHANNEL_THREAD + str(self.get_thread()
415 .get_opening_post_id())
415 .get_opening_post_id())
416 client.publish(channel_name, {
416 client.publish(channel_name, {
417 'html': self.get_post_data(
417 'notification_type': 'new_post',
418 format_type=DIFF_TYPE_HTML,
419 request=request),
420 'diff_type': 'added' if recursive else 'updated',
421 })
418 })
422 client.send()
419 client.send()
423
420
@@ -3,7 +3,7 b''
3 JavaScript code in this page.
3 JavaScript code in this page.
4
4
5
5
6 Copyright (C) 2013 neko259
6 Copyright (C) 2013-2014 neko259
7
7
8 The JavaScript code in this page is free software: you can
8 The JavaScript code in this page is free software: you can
9 redistribute it and/or modify it under the terms of the GNU
9 redistribute it and/or modify it under the terms of the GNU
@@ -23,7 +23,6 b''
23 for the JavaScript code in this page.
23 for the JavaScript code in this page.
24 */
24 */
25
25
26 var wsUrl = 'ws://localhost:9090/connection/websocket';
27 var wsUser = '';
26 var wsUser = '';
28
27
29 var loading = false;
28 var loading = false;
@@ -33,6 +32,12 b" var documentOriginalTitle = '';"
33 // Thread ID does not change, can be stored one time
32 // Thread ID does not change, can be stored one time
34 var threadId = $('div.thread').children('.post').first().attr('id');
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 function connectWebsocket() {
41 function connectWebsocket() {
37 var metapanel = $('.metapanel')[0];
42 var metapanel = $('.metapanel')[0];
38
43
@@ -57,12 +62,7 b' function connectWebsocket() {'
57 centrifuge.on('connect', function() {
62 centrifuge.on('connect', function() {
58 var channelName = 'thread:' + threadId;
63 var channelName = 'thread:' + threadId;
59 centrifuge.subscribe(channelName, function(message) {
64 centrifuge.subscribe(channelName, function(message) {
60 var postHtml = message.data['html'];
65 getThreadDiff();
61 var isAdded = (message.data['diff_type'] === 'added');
62
63 if (postHtml) {
64 updatePost(postHtml, isAdded);
65 }
66 });
66 });
67
67
68 // For the case we closed the browser and missed some updates
68 // For the case we closed the browser and missed some updates
@@ -87,14 +87,13 b' function getThreadDiff() {'
87
87
88 $.getJSON(diffUrl)
88 $.getJSON(diffUrl)
89 .success(function(data) {
89 .success(function(data) {
90 var bottom = isPageBottom();
91 var addedPosts = data.added;
90 var addedPosts = data.added;
92
91
93 for (var i = 0; i < addedPosts.length; i++) {
92 for (var i = 0; i < addedPosts.length; i++) {
94 var postText = addedPosts[i];
93 var postText = addedPosts[i];
95 var post = $(postText);
94 var post = $(postText);
96
95
97 updatePost(post, true)
96 updatePost(post)
98
97
99 lastPost = post;
98 lastPost = post;
100 }
99 }
@@ -105,28 +104,37 b' function getThreadDiff() {'
105 var postText = updatedPosts[i];
104 var postText = updatedPosts[i];
106 var post = $(postText);
105 var post = $(postText);
107
106
108 updatePost(post, false)
107 updatePost(post)
109 }
108 }
110
109
111 // TODO Process removed posts if any
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 // This needs to be set on start because the page is scrolled after posts
119 // This needs to be set on start because the page is scrolled after posts
120 // are added or updated
120 // are added or updated
121 var bottom = isPageBottom();
121 var bottom = isPageBottom();
122
122
123 var post = $(postHtml);
123 var post = $(postHtml);
124
124
125 var threadPosts = $('div.thread').children('.post');
125 var threadBlock = $('div.thread');
126
126
127 var lastUpdate = '';
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 var lastPost = threadPosts.last();
138 var lastPost = threadPosts.last();
131
139
132 post.appendTo(lastPost.parent());
140 post.appendTo(lastPost.parent());
@@ -140,12 +148,6 b' function updatePost(postHtml, isAdded) {'
140 if (bottom) {
148 if (bottom) {
141 scrollToBottom();
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 processNewPost(post);
153 processNewPost(post);
@@ -144,7 +144,7 b' class ThreadView(BaseBoardView, PostMixi'
144
144
145 post = Post.objects.create_post(title=title, text=text, image=image,
145 post = Post.objects.create_post(title=title, text=text, image=image,
146 thread=post_thread, ip=ip, tags=tags)
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 thread_to_show = (opening_post.id if opening_post else post.id)
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