From 6b43ade77081ab17d4208491c1cc8bee2e43de52 Mon Sep 17 00:00:00 2001 From: Arkadiusz Waluk Date: Sun, 18 Jul 2021 23:32:54 +0200 Subject: [PATCH 1/7] Add endpoint with user id for node server --- .../async-lists/async-lists-page.php | 25 +++++++++++++++++++ forum/qa-plugin/async-lists/qa-plugin.php | 1 + 2 files changed, 26 insertions(+) create mode 100644 forum/qa-plugin/async-lists/async-lists-page.php diff --git a/forum/qa-plugin/async-lists/async-lists-page.php b/forum/qa-plugin/async-lists/async-lists-page.php new file mode 100644 index 00000000..e762d0a4 --- /dev/null +++ b/forum/qa-plugin/async-lists/async-lists-page.php @@ -0,0 +1,25 @@ + Date: Mon, 19 Jul 2021 17:20:50 +0200 Subject: [PATCH 2/7] Rename plugin "Async lists" to "Socket integration" --- forum/qa-plugin/async-lists/qa-plugin.php | 23 ------------------- .../event.php} | 2 +- .../js/async-questions-list.js | 0 .../layer.php} | 2 +- .../metadata.json | 4 ++-- .../page.php} | 2 +- .../socket-integration/qa-plugin.php | 23 +++++++++++++++++++ 7 files changed, 28 insertions(+), 28 deletions(-) delete mode 100644 forum/qa-plugin/async-lists/qa-plugin.php rename forum/qa-plugin/{async-lists/async-lists-event.php => socket-integration/event.php} (98%) rename forum/qa-plugin/{async-lists => socket-integration}/js/async-questions-list.js (100%) rename forum/qa-plugin/{async-lists/async-lists-layer.php => socket-integration/layer.php} (84%) rename forum/qa-plugin/{async-lists => socket-integration}/metadata.json (69%) rename forum/qa-plugin/{async-lists/async-lists-page.php => socket-integration/page.php} (93%) create mode 100644 forum/qa-plugin/socket-integration/qa-plugin.php diff --git a/forum/qa-plugin/async-lists/qa-plugin.php b/forum/qa-plugin/async-lists/qa-plugin.php deleted file mode 100644 index f45ab7f7..00000000 --- a/forum/qa-plugin/async-lists/qa-plugin.php +++ /dev/null @@ -1,23 +0,0 @@ -template, ['qa', 'activity'])) { + if (!empty(QA_WS_PORT) && in_array($this->template, ['qa', 'activity'])) { $path = qa_html(QA_HTML_THEME_LAYER_URLTOROOT . 'js/async-questions-list.js?v=' . QA_RESOURCE_VERSION); $this->content['script'][] = ''; $this->content['script'][] = ''; diff --git a/forum/qa-plugin/async-lists/metadata.json b/forum/qa-plugin/socket-integration/metadata.json similarity index 69% rename from forum/qa-plugin/async-lists/metadata.json rename to forum/qa-plugin/socket-integration/metadata.json index 720be45f..82cea73c 100644 --- a/forum/qa-plugin/async-lists/metadata.json +++ b/forum/qa-plugin/socket-integration/metadata.json @@ -1,7 +1,7 @@ { - "name": "Async lists", + "name": "Socket integration", "uri": "", - "description": "Asynchronous lists - home and activity", + "description": "Integration for Node server and socket", "version": "1.0", "date": "2021-03-27", "author": "CodersCommunity", diff --git a/forum/qa-plugin/async-lists/async-lists-page.php b/forum/qa-plugin/socket-integration/page.php similarity index 93% rename from forum/qa-plugin/async-lists/async-lists-page.php rename to forum/qa-plugin/socket-integration/page.php index e762d0a4..3ea3e68a 100644 --- a/forum/qa-plugin/async-lists/async-lists-page.php +++ b/forum/qa-plugin/socket-integration/page.php @@ -1,6 +1,6 @@ Date: Mon, 19 Jul 2021 19:47:43 +0200 Subject: [PATCH 3/7] Add sending details and recipients for event a_post and c_post --- forum/qa-plugin/socket-integration/event.php | 60 +++++++++++++++++--- 1 file changed, 53 insertions(+), 7 deletions(-) diff --git a/forum/qa-plugin/socket-integration/event.php b/forum/qa-plugin/socket-integration/event.php index 8b4450a6..96de1e76 100644 --- a/forum/qa-plugin/socket-integration/event.php +++ b/forum/qa-plugin/socket-integration/event.php @@ -16,33 +16,79 @@ public function process_event($event, $userid, $handle, $cookieid, $params) case 'q_close': case 'q_reopen': case 'q_move': - $this->send_to_websocket($event, ['question_id' => (int)$params['postid']]); + $this->send_to_websocket($event, ['questionId' => (int)$params['postid']]); break; - case 'a_post': case 'a_edit': case 'a_hide': case 'a_reshow': case 'a_select': case 'a_unselect': - $this->send_to_websocket($event, ['question_id' => (int)$params['parentid']]); + $this->send_to_websocket($event, ['questionId' => (int)$params['parentid']]); break; - case 'c_post': case 'c_edit': case 'c_hide': case 'c_reshow': case 'a_to_c': - $this->send_to_websocket($event, ['question_id' => (int)$params['questionid']]); + $this->send_to_websocket($event, ['questionId' => (int)$params['questionid']]); + break; + case 'a_post': + case 'c_post': + $questionParams = $params['question'] ?? $params['parent']; + $slug = explode('/', qa_q_request($questionParams['postid'], $questionParams['title']))[1] ?? null; + + $this->get_recipients($event, $params); + $this->send_to_websocket($event, [ + 'questionId' => (int)$questionParams['postid'], + 'questionTitle' => $slug, + 'postId' => (int)$params['postid'], + 'url' => qa_q_path( + $questionParams['postid'], + $questionParams['title'], + true, + isset($params['question']) ? 'C' : 'A', + $params['postid'] + ) + ], $this->get_recipients($event, $params)); + break; + } + } + + private function get_recipients($event, $params = []) + { + $users = []; + switch ($event) { + case 'a_post': + if (!empty($params['parent']['userid'])) { + $users = [$params['parent']['userid']]; + } + break; + case 'c_post': + $users = qa_db_read_all_values(qa_db_query_sub( + 'SELECT DISTINCT userid FROM `^posts` WHERE `parentid` = # AND `type` = "C" AND `userid` IS NOT NULL', + $params['parentid'] + )); + if (!empty($params['parent']['userid'])) { + $users[] = $params['parent']['userid']; + } break; } + + return array_values(array_filter(array_map('intval', $users), function ($id) { + return $id !== (int)qa_get_logged_in_userid(); + })); } - private function send_to_websocket($action, $data = []) + private function send_to_websocket($action, $data = [], $recipientIds = []) { $options = [ 'http' => [ 'header' => "Content-Type: application/json\r\n" . "token: " . QA_WS_TOKEN . "\r\n", 'method' => 'POST', - 'content' => json_encode(['action' => $action, 'data' => $data], JSON_UNESCAPED_UNICODE) + 'content' => json_encode([ + 'action' => $action, + 'recipientIds' => $recipientIds, + 'data' => $data, + ], JSON_UNESCAPED_UNICODE) ] ]; From 9ce7efdb82ab6e60376b45c9af3122b252c399b9 Mon Sep 17 00:00:00 2001 From: Arkadiusz Waluk Date: Tue, 20 Jul 2021 23:10:00 +0200 Subject: [PATCH 4/7] Add userId to all events --- forum/qa-plugin/socket-integration/event.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/forum/qa-plugin/socket-integration/event.php b/forum/qa-plugin/socket-integration/event.php index 96de1e76..9fcad952 100644 --- a/forum/qa-plugin/socket-integration/event.php +++ b/forum/qa-plugin/socket-integration/event.php @@ -16,20 +16,23 @@ public function process_event($event, $userid, $handle, $cookieid, $params) case 'q_close': case 'q_reopen': case 'q_move': - $this->send_to_websocket($event, ['questionId' => (int)$params['postid']]); + $this->send_to_websocket($event, ['userId' => (int)$userid, 'questionId' => (int)$params['postid']]); break; case 'a_edit': case 'a_hide': case 'a_reshow': case 'a_select': case 'a_unselect': - $this->send_to_websocket($event, ['questionId' => (int)$params['parentid']]); + $this->send_to_websocket($event, ['userId' => (int)$userid, 'questionId' => (int)$params['parentid']]); break; case 'c_edit': case 'c_hide': case 'c_reshow': case 'a_to_c': - $this->send_to_websocket($event, ['questionId' => (int)$params['questionid']]); + $this->send_to_websocket($event, [ + 'userId' => (int)$userid, + 'questionId' => (int)$params['questionid'] + ]); break; case 'a_post': case 'c_post': @@ -38,6 +41,7 @@ public function process_event($event, $userid, $handle, $cookieid, $params) $this->get_recipients($event, $params); $this->send_to_websocket($event, [ + 'userId' => (int)$userid, 'questionId' => (int)$questionParams['postid'], 'questionTitle' => $slug, 'postId' => (int)$params['postid'], From 9889500b2585d2c2134493d0a2298860a09b8f36 Mon Sep 17 00:00:00 2001 From: Arkadiusz Waluk Date: Tue, 20 Jul 2021 23:17:45 +0200 Subject: [PATCH 5/7] Rename `questionTitle` to `questionSlug` --- forum/qa-plugin/socket-integration/event.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/forum/qa-plugin/socket-integration/event.php b/forum/qa-plugin/socket-integration/event.php index 9fcad952..3717021f 100644 --- a/forum/qa-plugin/socket-integration/event.php +++ b/forum/qa-plugin/socket-integration/event.php @@ -43,7 +43,7 @@ public function process_event($event, $userid, $handle, $cookieid, $params) $this->send_to_websocket($event, [ 'userId' => (int)$userid, 'questionId' => (int)$questionParams['postid'], - 'questionTitle' => $slug, + 'questionSlug' => $slug, 'postId' => (int)$params['postid'], 'url' => qa_q_path( $questionParams['postid'], From 5cf7baf901d2b16876b9a324e75b464753a1af67 Mon Sep 17 00:00:00 2001 From: Krzysztof Pawelec Date: Wed, 21 Jul 2021 12:54:42 +0200 Subject: [PATCH 6/7] Rename script from 'async-questions-list' to 'websocket-integration' and it's event listener function from 'runAsyncQuestionsList' to 'runWebSocketIntegration'; --- .../js/{async-questions-list.js => websocket-integration.js} | 2 +- forum/qa-plugin/socket-integration/layer.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename forum/qa-plugin/socket-integration/js/{async-questions-list.js => websocket-integration.js} (98%) diff --git a/forum/qa-plugin/socket-integration/js/async-questions-list.js b/forum/qa-plugin/socket-integration/js/websocket-integration.js similarity index 98% rename from forum/qa-plugin/socket-integration/js/async-questions-list.js rename to forum/qa-plugin/socket-integration/js/websocket-integration.js index e725a728..7cdb2886 100644 --- a/forum/qa-plugin/socket-integration/js/async-questions-list.js +++ b/forum/qa-plugin/socket-integration/js/websocket-integration.js @@ -1,4 +1,4 @@ -document.addEventListener('DOMContentLoaded', function runAsyncQuestionsList() { +document.addEventListener('DOMContentLoaded', function runWebSocketIntegration() { const { pathname, hostname, protocol } = window.location; const PORT = window.WS_PORT || 3000; const isMainOrActivityPage = /^$|\/$|^(\/?)activity/.test(pathname); diff --git a/forum/qa-plugin/socket-integration/layer.php b/forum/qa-plugin/socket-integration/layer.php index c4bc8499..476ede6d 100644 --- a/forum/qa-plugin/socket-integration/layer.php +++ b/forum/qa-plugin/socket-integration/layer.php @@ -5,7 +5,7 @@ class qa_html_theme_layer extends qa_html_theme_base public function head_script() { if (!empty(QA_WS_PORT) && in_array($this->template, ['qa', 'activity'])) { - $path = qa_html(QA_HTML_THEME_LAYER_URLTOROOT . 'js/async-questions-list.js?v=' . QA_RESOURCE_VERSION); + $path = qa_html(QA_HTML_THEME_LAYER_URLTOROOT . 'js/websocket-integration.js?v=' . QA_RESOURCE_VERSION); $this->content['script'][] = ''; $this->content['script'][] = ''; } From 606d56304f473812a382d1661d3df48cb8f8e236 Mon Sep 17 00:00:00 2001 From: Arkadiusz Waluk Date: Wed, 21 Jul 2021 19:11:04 +0200 Subject: [PATCH 7/7] Remove unnecessary line --- forum/qa-plugin/socket-integration/event.php | 1 - 1 file changed, 1 deletion(-) diff --git a/forum/qa-plugin/socket-integration/event.php b/forum/qa-plugin/socket-integration/event.php index 3717021f..21bb0218 100644 --- a/forum/qa-plugin/socket-integration/event.php +++ b/forum/qa-plugin/socket-integration/event.php @@ -39,7 +39,6 @@ public function process_event($event, $userid, $handle, $cookieid, $params) $questionParams = $params['question'] ?? $params['parent']; $slug = explode('/', qa_q_request($questionParams['postid'], $questionParams['title']))[1] ?? null; - $this->get_recipients($event, $params); $this->send_to_websocket($event, [ 'userId' => (int)$userid, 'questionId' => (int)$questionParams['postid'],