Browse Source

Add a third way to query widgets (websocket, ajax to daemon and pure ajax)

Convert some Widgets RPC calls to pure Ajax
pull/898/head
Timothée Jaussoin 6 years ago
parent
commit
b0b1f30c2d
  1. 2
      CHANGELOG.md
  2. 8
      app/widgets/Chat/Chat.php
  3. 2
      app/widgets/Chat/_chat_reactions.tpl
  4. 8
      app/widgets/Chat/chat.js
  5. 2
      app/widgets/Chats/Chats.php
  6. 4
      app/widgets/Chats/chats.js
  7. 2
      app/widgets/Communities/Communities.php
  8. 3
      app/widgets/Communities/communities.js
  9. 2
      app/widgets/CommunitiesServers/CommunitiesServers.php
  10. 4
      app/widgets/CommunitiesServers/communitiesservers.js
  11. 3
      app/widgets/Init/init.js
  12. 12
      app/widgets/Notifications/Notifications.php
  13. 2
      app/widgets/Notifications/notifications.js
  14. 2
      app/widgets/Presence/Presence.php
  15. 5
      app/widgets/Presence/presence.js
  16. 4
      app/widgets/PublishBrief/PublishBrief.php
  17. 4
      app/widgets/PublishBrief/_publishbrief.tpl
  18. 2
      app/widgets/PublishBrief/publishbrief.js
  19. 2
      app/widgets/Rooms/Rooms.php
  20. 6
      app/widgets/Rooms/_rooms.tpl
  21. 6
      app/widgets/Rooms/rooms.js
  22. 2
      app/widgets/SendTo/SendTo.php
  23. 2
      app/widgets/Stickers/stickers.js
  24. 7
      public/scripts/movim_base.js
  25. 26
      public/scripts/movim_websocket.js
  26. 15
      src/Movim/Controller/Ajax.php
  27. 13
      src/Movim/Controller/Front.php
  28. 17
      src/Movim/RPC.php
  29. 1
      src/Movim/Route.php
  30. 3
      src/Movim/Widget/Base.php

2
CHANGELOG.md

@ -6,6 +6,8 @@ v0.16.2 (trunk)
* Remove some paddings in the UI
* Set public url as a default body for shared Posts
* Request all the open Chats messages in one request (instead of looping)
* Add a third way to query widgets (websocket, ajax to daemon and pure ajax)
* Convert some Widgets RPC calls to pure Ajax
v0.16.1
---------------------------

8
app/widgets/Chat/Chat.php

@ -299,7 +299,7 @@ class Chat extends \Movim\Widget\Base
* @param string $message
* @return void
*/
public function ajaxHttpSendMessage($to, $message = false, $muc = false, $resource = false, $replace = false, $file = false)
public function ajaxHttpDaemonSendMessage($to, $message = false, $muc = false, $resource = false, $replace = false, $file = false)
{
$message = trim($message);
if (filter_var($message, FILTER_VALIDATE_URL)) {
@ -416,7 +416,7 @@ class Chat extends \Movim\Widget\Base
* @param string $message
* @return void
*/
public function ajaxHttpCorrect($to, $message, $mid = false)
public function ajaxHttpDaemonCorrect($to, $message, $mid = false)
{
$replace = $mid
? $this->user->messages()
@ -428,7 +428,7 @@ class Chat extends \Movim\Widget\Base
->first();
if ($replace) {
$this->ajaxHttpSendMessage($to, $message, false, false, $replace);
$this->ajaxHttpDaemonSendMessage($to, $message, false, false, $replace);
}
}
@ -437,7 +437,7 @@ class Chat extends \Movim\Widget\Base
*
* @
*/
public function ajaxHttpSendReaction($mid, string $emoji)
public function ajaxHttpDaemonSendReaction($mid, string $emoji)
{
$parentMessage = $this->user->messages()
->where('mid', $mid)

2
app/widgets/Chat/_chat_reactions.tpl

@ -1,7 +1,7 @@
{loop="$reactions"}
<li title="{$value|implodeCsv}"
{if="in_array($me, $value)"}class="reacted"{/if}
onclick="Chat_ajaxHttpSendReaction('{$message->mid}', '{$key}')">
onclick="Chat_ajaxHttpDaemonSendReaction('{$message->mid}', '{$key}')">
{autoescape="off"}
{$key|addEmojis:true}
{/autoescape}

8
app/widgets/Chat/chat.js

@ -121,13 +121,13 @@ var Chat = {
if (Chat.edit) {
Chat.edit = false;
if (textarea.dataset.mid) {
xhr = Chat_ajaxHttpCorrect(jid, text, textarea.dataset.mid);
xhr = Chat_ajaxHttpDaemonCorrect(jid, text, textarea.dataset.mid);
delete textarea.dataset.mid;
} else {
xhr = Chat_ajaxHttpCorrect(jid, text);
xhr = Chat_ajaxHttpDaemonCorrect(jid, text);
}
} else {
xhr = Chat_ajaxHttpSendMessage(jid, text, muc);
xhr = Chat_ajaxHttpDaemonSendMessage(jid, text, muc);
}
xhr.onreadystatechange = function() {
@ -907,7 +907,7 @@ MovimWebsocket.attach(function() {
if (typeof Upload != 'undefined') {
Upload.attach(function(file) {
Chat_ajaxHttpSendMessage(Chat.getTextarea().dataset.jid, false, Boolean(Chat.getTextarea().dataset.muc), false, false, file);
Chat_ajaxHttpDaemonSendMessage(Chat.getTextarea().dataset.jid, false, Boolean(Chat.getTextarea().dataset.muc), false, false, file);
});
}

2
app/widgets/Chats/Chats.php

@ -101,7 +101,7 @@ class Chats extends Base
}
}
public function ajaxGet()
public function ajaxHttpGet()
{
$this->rpc('MovimTpl.fill', '#chats_widget_list', $this->prepareChats());
$this->rpc('Chats.refresh');

4
app/widgets/Chats/chats.js

@ -83,6 +83,4 @@ var Chats = {
}
};
MovimWebsocket.attach(function() {
Chats_ajaxGet();
});
movim_add_onload(e => Chats_ajaxHttpGet());

2
app/widgets/Communities/Communities.php

@ -11,7 +11,7 @@ class Communities extends Base
$this->addjs('communities.js');
}
public function ajaxGet()
public function ajaxHttpGet()
{
$this->rpc('MovimTpl.fill', '#communities', $this->prepareCommunities());
}

3
app/widgets/Communities/communities.js

@ -1,4 +1,5 @@
MovimWebsocket.attach(function() {
Notification.current('community');
Communities_ajaxGet();
});
movim_add_onload(e => Communities_ajaxHttpGet());

2
app/widgets/CommunitiesServers/CommunitiesServers.php

@ -55,7 +55,7 @@ class CommunitiesServers extends Base
->request();
}
public function ajaxGet()
public function ajaxHttpGet()
{
$this->rpc('MovimTpl.fill', '#communities_servers', $this->prepareCommunities());
}

4
app/widgets/CommunitiesServers/communitiesservers.js

@ -1,3 +1 @@
MovimWebsocket.attach(function() {
CommunitiesServers_ajaxGet();
});
movim_add_onload(e => CommunitiesServers_ajaxHttpGet());

3
app/widgets/Init/init.js

@ -32,7 +32,6 @@ var Init = {
}
}
MovimWebsocket.attach(function()
{
MovimWebsocket.attach(function() {
Init.checkNode();
});

12
app/widgets/Notifications/Notifications.php

@ -27,7 +27,7 @@ class Notifications extends Base
{
$post = $packet->content;
if ($post->isComment() && !$post->isMine()) {
$this->ajaxSetCounter();
$this->ajaxHttpSetCounter();
}
}
@ -45,18 +45,18 @@ class Notifications extends Base
);
}
$this->ajaxSetCounter();
$this->ajaxHttpSetCounter();
}
public function ajaxRequest()
{
Drawer::fill($this->prepareNotifications());
\App\Cache::c('notifs_since', date(MOVIM_SQL_DATE));
$this->ajaxSetCounter();
$this->ajaxHttpSetCounter();
(new Notification)->ajaxClear('comments');
}
public function ajaxSetCounter()
public function ajaxHttpSetCounter()
{
$since = \App\Cache::c('notifs_since');
if (!$since) {
@ -109,7 +109,7 @@ class Notifications extends Base
$n->ajaxClear('invite|'.$jid);
Drawer::fill($this->prepareNotifications());
$this->ajaxSetCounter();
$this->ajaxHttpSetCounter();
}
public function ajaxRefuse($jid)
@ -132,7 +132,7 @@ class Notifications extends Base
$n->ajaxClear('invite|'.$jid);
Drawer::fill($this->prepareNotifications());
$this->ajaxSetCounter();
$this->ajaxHttpSetCounter();
}
/*

2
app/widgets/Notifications/notifications.js

@ -8,5 +8,5 @@ var Notifications = {
}
MovimWebsocket.attach(function() {
Notifications_ajaxSetCounter();
Notifications_ajaxHttpSetCounter();
});

2
app/widgets/Presence/Presence.php

@ -85,7 +85,7 @@ class Presence extends Base
Stream::end();
}
public function ajaxGetPresence()
public function ajaxHttpGetPresence()
{
$html = $this->preparePresence();
if ($html) {

5
app/widgets/Presence/presence.js

@ -7,7 +7,4 @@ var Presence = {
}
}
MovimWebsocket.attach(function()
{
Presence_ajaxGetPresence();
});
movim_add_onload(e => Presence_ajaxHttpGetPresence());

4
app/widgets/PublishBrief/PublishBrief.php

@ -78,7 +78,7 @@ class PublishBrief extends Base
$this->rpc('PublishBrief.checkEmbed');
}
public function ajaxHttpSaveDraft($form)
public function ajaxHttpDaemonSaveDraft($form)
{
$p = new \App\Post;
$p->title = $form->title->value;
@ -110,7 +110,7 @@ class PublishBrief extends Base
}
}
public function ajaxHttpPublish($form)
public function ajaxHttpDaemonPublish($form)
{
$this->rpc('PublishBrief.disableSend');

4
app/widgets/PublishBrief/_publishbrief.tpl

@ -26,7 +26,7 @@
</ul>
</header>
<a id="button_send" class="button action color"
onclick="PublishBrief.disableSend(); PublishBrief_ajaxHttpPublish(MovimUtils.formToJson('brief'));">
onclick="PublishBrief.disableSend(); PublishBrief_ajaxHttpDaemonPublish(MovimUtils.formToJson('brief'));">
<i class="material-icons">send</i>
</a>
{else}
@ -114,7 +114,7 @@
{else}
<span id="button_send"
class="control icon gray active"
onclick="PublishBrief.disableSend(); PublishBrief_ajaxHttpPublish(MovimUtils.formToJson('brief'));">
onclick="PublishBrief.disableSend(); PublishBrief_ajaxHttpDaemonPublish(MovimUtils.formToJson('brief'));">
<i class="material-icons">send</i>
</span>
{/if}

2
app/widgets/PublishBrief/publishbrief.js

@ -56,7 +56,7 @@ var PublishBrief = {
document.querySelector('input[name=embed]').onchange();
},
saveDraft: function() {
PublishBrief_ajaxHttpSaveDraft(MovimUtils.formToJson('brief'));
PublishBrief_ajaxHttpDaemonSaveDraft(MovimUtils.formToJson('brief'));
},
enableSend: function() {
document.querySelector('#button_send').classList.remove('disabled');

2
app/widgets/Rooms/Rooms.php

@ -198,7 +198,7 @@ class Rooms extends Base
/**
* @brief Get the Rooms
*/
public function ajaxDisplay($edit = false, $all = false)
public function ajaxHttpDisplay($edit = false, $all = false)
{
$this->refreshRooms($edit, $all);
}

6
app/widgets/Rooms/_rooms.tpl

@ -1,5 +1,5 @@
{if="$edit"}
<a class="button action color green" onclick="Rooms_ajaxDisplay(false, {if="$all"}true{else}false{/if})">
<a class="button action color green" onclick="Rooms_ajaxHttpDisplay(false, {if="$all"}true{else}false{/if})">
<i class="material-icons">check</i>
</a>
{/if}
@ -10,7 +10,7 @@
<ul class="list divided spaced middle {if="!$edit"}active{/if}">
<li class="subheader" title="{$c->__('page.configuration')}">
{if="$conferences->isNotEmpty() && !$edit"}
<span class="control icon active gray" onclick="Rooms_ajaxDisplay(true, {if="$all"}true{else}false{/if});">
<span class="control icon active gray" onclick="Rooms_ajaxHttpDisplay(true, {if="$all"}true{else}false{/if});">
<i class="material-icons">edit</i>
</span>
<span class="control icon active gray" onclick="Rooms_ajaxAdd()">
@ -149,7 +149,7 @@
<ul class="list thin active spaced divided">
{if="$disconnected > 0"}
<li onclick="Rooms_ajaxDisplay({if="$edit"}true{else}false{/if}, {if="$all"}false{else}true{/if})">
<li onclick="Rooms_ajaxHttpDisplay({if="$edit"}true{else}false{/if}, {if="$all"}false{else}true{/if})">
<span class="primary icon gray">
<i class="material-icons">
{if="$all"}expand_less{else}expand_more{/if}

6
app/widgets/Rooms/rooms.js

@ -78,7 +78,5 @@ var Rooms = {
}
}
MovimWebsocket.attach(function() {
Rooms.anonymousInit();
Rooms_ajaxDisplay();
});
//Rooms.anonymousInit();
movim_add_onload(e => Rooms_ajaxHttpDisplay());

2
app/widgets/SendTo/SendTo.php

@ -63,7 +63,7 @@ class SendTo extends Base
$this->rpc('Drawer.clear');
$c = new Chat;
$c->ajaxHttpSendMessage($to, !empty($message) ? $message : $this->__('sendto.shared_with'), $muc, false, false, $file);
$c->ajaxHttpDaemonSendMessage($to, !empty($message) ? $message : $this->__('sendto.shared_with'), $muc, false, false, $file);
}
public function ajaxGetMoreContacts(string $uri)

2
app/widgets/Stickers/stickers.js

@ -28,7 +28,7 @@ var Stickers = {
while (i < tds.length) {
tds[i].onclick = function() {
if (mid) {
Chat_ajaxHttpSendReaction(mid, this.dataset.emoji);
Chat_ajaxHttpDaemonSendReaction(mid, this.dataset.emoji);
Dialog_ajaxClear();
} else {
Stickers.addSmiley(this);

7
public/scripts/movim_base.js

@ -18,12 +18,9 @@ function movim_add_onload(func)
/**
* @brief Function that is run once the page is loaded.
*/
function movim_onload()
{
document.addEventListener("DOMContentLoaded", function() {
for (var i = 0; i < onloaders.length; i++) {
if (typeof(onloaders[i]) === "function")
onloaders[i]();
}
}
window.onload = function() { movim_onload(); };
});

26
public/scripts/movim_websocket.js

@ -13,12 +13,20 @@ WebSocket.prototype.register = function(host) {
};
/**
* Short function for MovimWebsocket.send
* Short functions
*/
function MWSs(widget, func, params) {
MovimWebsocket.send(widget, func, params);
}
function MWSa(widget, func, params) {
return MovimWebsocket.ajax(widget, func, params, false);
}
function MWSad(widget, func, params) {
return MovimWebsocket.ajax(widget, func, params, true);
}
/**
* @brief Definition of the MovimWebsocket object
* @param string error
@ -154,7 +162,8 @@ var MovimWebsocket = {
}
},
ajax : function(widget, func, params) {
// Ajax call that is going to the daemon
ajax : function(widget, func, params, daemon) {
let xhr = new XMLHttpRequest;
var body = {
@ -164,12 +173,23 @@ var MovimWebsocket = {
if (params) body.p = params;
xhr.open('POST', '?ajax');
xhr.open('POST', daemon ? '?ajaxd': '?ajax');
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
xhr.send(JSON.stringify(
{'func' : 'message', 'b' : body }
));
if (!daemon) {
xhr.addEventListener('readystatechange', function(e) {
if (this.readyState == 4 && this.status >= 200 && this.status < 400) {
var obj = JSON.parse(this.response);
for (funcall of obj) {
MovimWebsocket.handle(funcall);
}
}
});
}
return xhr;
},

15
src/Movim/Controller/Ajax.php

@ -29,15 +29,19 @@ class Ajax extends Base
if (empty($this->funclist)) {
return '';
}
$buffer = '<script type="text/javascript">';
foreach ($this->funclist as $key => $funcdef) {
$parlist = implode(',', $funcdef['params']);
$buffer .= 'function ' . $funcdef['object'] . '_'
. $funcdef['funcname'] . "(${parlist}){";
$function = "MWSs('";
if ($funcdef['http'] === 1) $function = " return MWSa('";
if ($funcdef['http'] === 2) $function = " return MWSad('";
$buffer .=
($funcdef['http'] ? " return MovimWebsocket.ajax('" : "MWSs('") .
$function .
$funcdef['object'] . "','" .
$funcdef['funcname'] . "'" .
(!empty($funcdef['params']) ? ",[${parlist}]" : '');
@ -58,9 +62,14 @@ class Ajax extends Base
/**
* Defines a new function.
*/
public function defun($widget, $funcname, array $params, $http = false)
public function defun($widget, $funcname, array $params)
{
array_push($this->widgetlist, $widget);
$http = 0;
if (preg_match('#^ajaxHttp#', $funcname)) $http = 1;
if (preg_match('#^ajaxHttpDaemon#', $funcname)) $http = 2;
$this->funclist[$widget.$funcname] = [
'object' => $widget,
'funcname' => $funcname,

13
src/Movim/Controller/Front.php

@ -5,6 +5,7 @@ use Monolog\Logger;
use Monolog\Handler\SyslogHandler;
use Movim\Route;
use Movim\Cookie;
use Movim\RPC;
class Front extends Base
{
@ -44,7 +45,19 @@ class Front extends Base
*/
public function runRequest($request)
{
// Simple ajax request to a Widget
if ($request === 'ajax') {
$payload = json_decode(file_get_contents('php://input'));
if ($payload) {
$rpc = new RPC;
$rpc->handleJSON($payload->b);
$rpc->writeJSON();
}
}
// Ajax request that is going to the daemon
if ($request === 'ajaxd') {
requestAPI('ajax', 2, [
'sid' => SESSION_ID,
'json' => rawurlencode(file_get_contents('php://input'))

17
src/Movim/RPC.php

@ -6,12 +6,25 @@ use Movim\Widget\Wrapper;
class RPC
{
private static $json = [];
public static function call($funcname, ...$args)
{
writeOut([
$payload = [
'func' => $funcname,
'params' => $args,
]);
];
if (php_sapi_name() != 'cli') {
array_push(self::$json, $payload);
} else {
writeOut($payload);
}
}
public function writeJSON()
{
echo json_encode(self::$json);
}
/**

1
src/Movim/Route.php

@ -18,6 +18,7 @@ class Route extends Base
'account' => false,
'accountnext' => ['s', 'err'],
'ajax' => false,
'ajaxd' => false,
'admin' => false,
'blog' => ['f', 'i'],
'chat' => ['f', 'r'],

3
src/Movim/Widget/Base.php

@ -62,8 +62,7 @@ class Base
$this->ajax->defun(
$this->name,
$method->name,
$params,
preg_match('#^ajaxHttp#', $method->name)
$params
);
}
}

Loading…
Cancel
Save