diff --git a/forum/qa-plugin/async-lists/async-lists-event.php b/forum/qa-plugin/async-lists/async-lists-event.php deleted file mode 100644 index ccb89dc6..00000000 --- a/forum/qa-plugin/async-lists/async-lists-event.php +++ /dev/null @@ -1,52 +0,0 @@ -send_to_websocket($event, ['question_id' => (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']]); - 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']]); - break; - } - } - - private function send_to_websocket($action, $data = []) - { - $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) - ] - ]; - - $context = stream_context_create($options); - file_get_contents(QA_WS_URL, false, $context); - } -} 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 806d6ddc..00000000 --- a/forum/qa-plugin/async-lists/qa-plugin.php +++ /dev/null @@ -1,22 +0,0 @@ -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, ['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, [ + 'userId' => (int)$userid, + '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->send_to_websocket($event, [ + 'userId' => (int)$userid, + 'questionId' => (int)$questionParams['postid'], + 'questionSlug' => $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 = [], $recipientIds = []) + { + $options = [ + 'http' => [ + 'header' => "Content-Type: application/json\r\n" . "token: " . QA_WS_TOKEN . "\r\n", + 'method' => 'POST', + 'content' => json_encode([ + 'action' => $action, + 'recipientIds' => $recipientIds, + 'data' => $data, + ], JSON_UNESCAPED_UNICODE) + ] + ]; + + $context = stream_context_create($options); + file_get_contents(QA_WS_URL, false, $context); + } +} diff --git a/forum/qa-plugin/async-lists/js/async-questions-list.js b/forum/qa-plugin/socket-integration/js/websocket-integration.js similarity index 98% rename from forum/qa-plugin/async-lists/js/async-questions-list.js rename to forum/qa-plugin/socket-integration/js/websocket-integration.js index e725a728..7cdb2886 100644 --- a/forum/qa-plugin/async-lists/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/async-lists/async-lists-layer.php b/forum/qa-plugin/socket-integration/layer.php similarity index 75% rename from forum/qa-plugin/async-lists/async-lists-layer.php rename to forum/qa-plugin/socket-integration/layer.php index 6b9e44dc..476ede6d 100644 --- a/forum/qa-plugin/async-lists/async-lists-layer.php +++ b/forum/qa-plugin/socket-integration/layer.php @@ -4,8 +4,8 @@ class qa_html_theme_layer extends qa_html_theme_base { public function head_script() { - if (in_array($this->template, ['qa', 'activity'])) { - $path = qa_html(QA_HTML_THEME_LAYER_URLTOROOT . 'js/async-questions-list.js?v=' . QA_RESOURCE_VERSION); + if (!empty(QA_WS_PORT) && in_array($this->template, ['qa', 'activity'])) { + $path = qa_html(QA_HTML_THEME_LAYER_URLTOROOT . 'js/websocket-integration.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/socket-integration/page.php b/forum/qa-plugin/socket-integration/page.php new file mode 100644 index 00000000..3ea3e68a --- /dev/null +++ b/forum/qa-plugin/socket-integration/page.php @@ -0,0 +1,25 @@ +