Skip to content

Plugin "socket integration" #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 0 additions & 52 deletions forum/qa-plugin/async-lists/async-lists-event.php

This file was deleted.

22 changes: 0 additions & 22 deletions forum/qa-plugin/async-lists/qa-plugin.php

This file was deleted.

101 changes: 101 additions & 0 deletions forum/qa-plugin/socket-integration/event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

class socket_integration_event
{
public function process_event($event, $userid, $handle, $cookieid, $params)
{
if (empty(QA_WS_URL) || empty(QA_WS_TOKEN)) {
return;
}

switch ($event) {
case 'q_post':
case 'q_edit':
case 'q_hide':
case 'q_reshow':
case 'q_close':
case 'q_reopen':
case 'q_move':
$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, ['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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'][] = '<script>window.WS_PORT = ' . QA_WS_PORT . ';</script>';
$this->content['script'][] = '<script src="' . $path . '" defer></script>';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
25 changes: 25 additions & 0 deletions forum/qa-plugin/socket-integration/page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

class socket_integration_page
{
public function match_request($request)
{
return $request === 'user-id';
}

public function process_request()
{
if (($_SERVER['HTTP_TOKEN'] ?? '') !== QA_WS_TOKEN) {
http_response_code(401);
return;
}

$id = qa_get_logged_in_userid();
if ($id === null) {
http_response_code(403);
return;
}

echo $id;
}
}
23 changes: 23 additions & 0 deletions forum/qa-plugin/socket-integration/qa-plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
Plugin Name: Socket integration
Plugin URI:
Plugin Description: Integration for Node server and socket
Plugin Version: 1.0
Plugin Date: 2021-03-27
Plugin Author: CodersCommunity
Plugin Author URI: https://forum.pasja-informatyki.pl
Plugin License:
Plugin Minimum Question2Answer Version: 1.5
Plugin Update Check URI:
*/

if (!defined('QA_VERSION')) {
header('Location: ../../');
exit();
}

qa_register_plugin_layer('layer.php', 'Socket integration layer');
qa_register_plugin_module('event', 'event.php', 'socket_integration_event', 'Socket integration event');
qa_register_plugin_module('page', 'page.php', 'socket_integration_page', 'Socket integration page');