#78 fixed. Null recomendations comments fixed. WS close messages in console.

This commit is contained in:
Alexey Skobkin 2016-05-10 01:22:43 +03:00
parent 072ece28cd
commit 6ee91fbb25

View file

@ -24,6 +24,9 @@ function PointPlus(ppVersion) {
// Getting username // Getting username
var point_username = $('#name h1').text(); var point_username = $('#name h1').text();
// Getting post id
var postId = $('#top-post').attr('data-id');
console.debug('Current post id detected as #%s', postId);
// Проверяем, загрузились ли мы // Проверяем, загрузились ли мы
var point_plus_debug = $('#point-plus-debug'); var point_plus_debug = $('#point-plus-debug');
@ -387,9 +390,6 @@ function PointPlus(ppVersion) {
ws = new WebSocket(((location.protocol == 'https:') ? 'wss' : 'ws') + '://point.im/ws'); ws = new WebSocket(((location.protocol == 'https:') ? 'wss' : 'ws') + '://point.im/ws');
console.log('WebSocket created: %O', ws); console.log('WebSocket created: %O', ws);
// Detecting post id if presented
var postId = $('#top-post').attr('data-id');
console.debug('Current post id detected as #%s', postId);
// Detecting view mode // Detecting view mode
treeSwitch = $('#tree-switch a.active').attr('href'); treeSwitch = $('#tree-switch a.active').attr('href');
console.debug('Comments view mode: %s', treeSwitch); console.debug('Comments view mode: %s', treeSwitch);
@ -399,6 +399,12 @@ function PointPlus(ppVersion) {
console.error('WebSocket error: %O', err); console.error('WebSocket error: %O', err);
}; };
// Close handler
ws.onclose = function(evt) {
console.error('WebSocket closed: %O', evt);
};
// todo: refactor to background service
// Message handler // Message handler
ws.onmessage = function(evt) { ws.onmessage = function(evt) {
try { try {
@ -430,14 +436,10 @@ function PointPlus(ppVersion) {
// Comments // Comments
case 'comment': case 'comment':
if (wsMessage.a === 'comment') { console.groupCollapsed('WS \'%s\' #%s/%s', wsMessage.a, wsMessage.post_id, wsMessage.comment_id);
console.groupCollapsed('WS comment #%s/%s', wsMessage.post_id, wsMessage.comment_id);
} else if (wsMessage.a === 'ok') {
console.groupCollapsed('WS comment rec #%s/%s', wsMessage.post_id, wsMessage.comment_id);
}
// Check option // Check option
if ( ! options.is('option_ws_comments')) { if (!options.is('option_ws_comments')) {
console.log('Comments processing disabled'); console.log('Comments processing disabled');
console.groupEnd(); console.groupEnd();
break; break;
@ -457,56 +459,58 @@ function PointPlus(ppVersion) {
break; break;
} }
// Generating comment from websocket message // If comment has text
create_comment_elements({ if (wsMessage.hasOwnProperty('html') && typeof wsMessage.html === 'string') {
id: (wsMessage.a === 'ok') ? wsMessage.rcid : wsMessage.comment_id, // Generating comment from websocket message
toId: wsMessage.to_comment_id, create_comment_elements({
postId: wsMessage.post_id, id: (wsMessage.a === 'ok') ? wsMessage.rcid : wsMessage.comment_id,
author: wsMessage.author, toId: wsMessage.to_comment_id,
html: wsMessage.html, postId: wsMessage.post_id,
fadeOut: options.is('option_ws_comments_color_fadeout'), author: wsMessage.author,
isRec: (wsMessage.a === 'ok') ? true : false, html: wsMessage.html,
unread: (point_username && point_username !== wsMessage.author) ? true : false, fadeOut: options.is('option_ws_comments_color_fadeout'),
}, function($comment) { isRec: (wsMessage.a === 'ok') ? true : false,
// It's time to DOM unread: (point_username && point_username !== wsMessage.author) ? true : false,
console.info('Inserting comment'); }, function($comment) {
// It's time to DOM
console.info('Inserting comment');
// Search for parent comment // Search for parent comment
$parentComment = (wsMessage.to_comment_id) ? ($('div.post[data-comment-id="' + wsMessage.to_comment_id + '"]')) : []; $parentComment = (wsMessage.to_comment_id) ? ($('div.post[data-comment-id="' + wsMessage.to_comment_id + '"]')) : [];
console.log('Parent comment: %O', $parentComment || null); console.log('Parent comment: %O', $parentComment || null);
// If list mode or not addressed to other comment // If list mode or not addressed to other comment
if ($('#comments #tree-switch a').eq(0).hasClass('active') || (wsMessage.to_comment_id === null) || (!$parentComment.length)) { if ($('#comments #tree-switch a').eq(0).hasClass('active') || (wsMessage.to_comment_id === null) || (!$parentComment.length)) {
// Adding to the end of the list // Adding to the end of the list
$('.content-wrap #comments #post-reply').before($comment); $('.content-wrap #comments #post-reply').before($comment);
} else {
// Check for children
$parentCommentChildren = $parentComment.next('.comments');
// If child comment already exist
if ($parentCommentChildren.length > 0) {
console.log('Child comments found. Appending...');
$parentCommentChildren.append($comment);
} else { } else {
console.log('No child comments found. Creating...'); // Check for children
$parentComment.after($('<div>').addClass('comments').append($comment)); $parentCommentChildren = $parentComment.next('.comments');
// If child comment already exist
if ($parentCommentChildren.length > 0) {
console.log('Child comments found. Appending...');
$parentCommentChildren.append($comment);
} else {
console.log('No child comments found. Creating...');
$parentComment.after($('<div>').addClass('comments').append($comment));
}
} }
}
// Desktop notifications // Desktop notifications
if (options.is('option_ws_comments_notifications')) { if (options.is('option_ws_comments_notifications')) {
console.log('Showing desktop notification'); console.log('Showing desktop notification');
messenger.sendMessage({ messenger.sendMessage({
type: 'showNotification', type: 'showNotification',
notificationId: 'comment_' + wsMessage.post_id + '#' + wsMessage.comment_id, notificationId: 'comment_' + wsMessage.post_id + '#' + wsMessage.comment_id,
avatarUrl: getProtocol() + '//point.im/avatar/' + wsMessage.author + '/80', avatarUrl: getProtocol() + '//point.im/avatar/' + wsMessage.author + '/80',
title: '@' + wsMessage.author + ' #' + wsMessage.post_id + '/' + wsMessage.comment_id, title: '@' + wsMessage.author + ' #' + wsMessage.post_id + '/' + wsMessage.comment_id,
text: wsMessage.text text: wsMessage.text
}, function(response) {}); }, function(response) {});
} }
console.groupEnd();
});
console.groupEnd();
});
}
break; break;