Move otpoll to session controller
This commit is contained in:
parent
815194cddb
commit
d7b06b635a
119
ajax/otpoll.php
119
ajax/otpoll.php
@ -1,119 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ownCloud - Documents App
|
|
||||||
*
|
|
||||||
* @author Victor Dubiniuk
|
|
||||||
* @copyright 2013 Victor Dubiniuk victor.dubiniuk@gmail.com
|
|
||||||
*
|
|
||||||
* This file is licensed under the Affero General Public License version 3 or
|
|
||||||
* later.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace OCA\Documents;
|
|
||||||
|
|
||||||
class BadRequestException extends \Exception {
|
|
||||||
|
|
||||||
protected $body = "";
|
|
||||||
|
|
||||||
public function setBody($body){
|
|
||||||
$this->body = $body;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getBody(){
|
|
||||||
return $this->body;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
\OCP\JSON::checkAppEnabled('documents');
|
|
||||||
$response = array();
|
|
||||||
|
|
||||||
try{
|
|
||||||
$request = new Request();
|
|
||||||
$esId = $request->getParam('args/es_id');
|
|
||||||
|
|
||||||
$session = new Db\Session();
|
|
||||||
$session->load($esId);
|
|
||||||
|
|
||||||
$memberId = $request->getParam('args/member_id');
|
|
||||||
$member = new Db\Member();
|
|
||||||
$member->load($memberId);
|
|
||||||
|
|
||||||
if (!$member->getIsGuest()){
|
|
||||||
\OCP\JSON::checkLoggedIn();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
$file = new File($session->getFileId());
|
|
||||||
} catch (\Exception $e){
|
|
||||||
Helper::warnLog('Error. Session no longer exists. ' . $e->getMessage());
|
|
||||||
$ex = new BadRequestException();
|
|
||||||
$ex->setBody($request->getRawRequest());
|
|
||||||
throw $ex;
|
|
||||||
}
|
|
||||||
|
|
||||||
$command = $request->getParam('command');
|
|
||||||
switch ($command){
|
|
||||||
case 'sync_ops':
|
|
||||||
$seqHead = (string) $request->getParam('args/seq_head');
|
|
||||||
if (!is_null($seqHead)){
|
|
||||||
$ops = $request->getParam('args/client_ops');
|
|
||||||
$hasOps = is_array($ops) && count($ops)>0;
|
|
||||||
|
|
||||||
$op = new Db\Op();
|
|
||||||
$currentHead = $op->getHeadSeq($esId);
|
|
||||||
|
|
||||||
try {
|
|
||||||
$member->updateActivity($memberId);
|
|
||||||
} catch (\Exception $e){
|
|
||||||
//Db error. Not critical
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO handle the case ($currentHead == "") && ($seqHead != "")
|
|
||||||
if ($seqHead == $currentHead) {
|
|
||||||
// matching heads
|
|
||||||
if ($hasOps) {
|
|
||||||
// incoming ops without conflict
|
|
||||||
// Add incoming ops, respond with a new head
|
|
||||||
$newHead = Db\Op::addOpsArray($esId, $memberId, $ops);
|
|
||||||
$response["result"] = 'added';
|
|
||||||
$response["head_seq"] = $newHead ? $newHead : $currentHead;
|
|
||||||
} else {
|
|
||||||
// no incoming ops (just checking for new ops...)
|
|
||||||
$response["result"] = 'new_ops';
|
|
||||||
$response["ops"] = array();
|
|
||||||
$response["head_seq"] = $currentHead;
|
|
||||||
}
|
|
||||||
} else { // HEADs do not match
|
|
||||||
$response["ops"] = $op->getOpsAfterJson($esId, $seqHead);
|
|
||||||
$response["head_seq"] = $currentHead;
|
|
||||||
$response["result"] = $hasOps ? 'conflict' : 'new_ops';
|
|
||||||
}
|
|
||||||
|
|
||||||
$inactiveMembers = $member->updateByTimeout($esId);
|
|
||||||
foreach ($inactiveMembers as $inactive){
|
|
||||||
$op->removeCursor($esId, $inactive);
|
|
||||||
$op->removeMember($esId, $inactive);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
// Error - no seq_head passed
|
|
||||||
throw new BadRequestException();
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
$ex = new BadRequestException();
|
|
||||||
$ex->setBody($request->getRawRequest());
|
|
||||||
throw $ex;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
\OCP\JSON::success($response);
|
|
||||||
} catch (BadRequestException $e){
|
|
||||||
header('HTTP/1.1 400: BAD REQUEST');
|
|
||||||
\OCP\JSON::error( array(
|
|
||||||
'err' => 'bad request:[' . $e->getBody() . ']',
|
|
||||||
));
|
|
||||||
}
|
|
||||||
exit();
|
|
@ -23,6 +23,7 @@ $application->registerRoutes($this, array(
|
|||||||
array('name' => 'session#joinAsUser', 'url' => 'ajax/session/joinasuser/{fileId}', 'verb' => 'POST'),
|
array('name' => 'session#joinAsUser', 'url' => 'ajax/session/joinasuser/{fileId}', 'verb' => 'POST'),
|
||||||
array('name' => 'session#joinAsGuest', 'url' => 'ajax/session/joinasguest/{token}', 'verb' => 'POST'),
|
array('name' => 'session#joinAsGuest', 'url' => 'ajax/session/joinasguest/{token}', 'verb' => 'POST'),
|
||||||
array('name' => 'session#save', 'url' => 'ajax/session/save', 'verb' => 'POST'),
|
array('name' => 'session#save', 'url' => 'ajax/session/save', 'verb' => 'POST'),
|
||||||
|
array('name' => 'session#poll', 'url' => 'ajax/otpoll.php', 'verb' => 'POST'),
|
||||||
//documents
|
//documents
|
||||||
array('name' => 'document#create', 'url' => 'ajax/documents/create', 'verb' => 'POST'),
|
array('name' => 'document#create', 'url' => 'ajax/documents/create', 'verb' => 'POST'),
|
||||||
array('name' => 'document#serve', 'url' => 'ajax/genesis/{esId}', 'verb' => array('GET', 'HEAD')),
|
array('name' => 'document#serve', 'url' => 'ajax/genesis/{esId}', 'verb' => array('GET', 'HEAD')),
|
||||||
@ -37,20 +38,12 @@ $application->registerRoutes($this, array(
|
|||||||
)
|
)
|
||||||
));
|
));
|
||||||
|
|
||||||
|
|
||||||
/** @var $this \OC\Route\Router */
|
|
||||||
$this->create('documents_ajax_otpoll', 'ajax/otpoll.php')
|
|
||||||
->actionInclude('documents/ajax/otpoll.php');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Document routes
|
* Document routes
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/** @var $this \OC\Route\Router */
|
||||||
|
|
||||||
$this->create('documents_index', '')
|
$this->create('documents_index', '')
|
||||||
->get()
|
->get()
|
||||||
->actionInclude('documents/index.php');
|
->actionInclude('documents/index.php');
|
||||||
|
|
||||||
$this->create('documents_otpoll', 'ajax/otpoll.php')
|
|
||||||
->post()
|
|
||||||
->actionInclude('documents/ajax/otpoll.php')
|
|
||||||
;
|
|
||||||
|
@ -13,13 +13,28 @@ namespace OCA\Documents\Controller;
|
|||||||
|
|
||||||
use \OCP\AppFramework\Controller;
|
use \OCP\AppFramework\Controller;
|
||||||
use \OCP\IRequest;
|
use \OCP\IRequest;
|
||||||
|
use \OCP\AppFramework\Http;
|
||||||
use \OCP\AppFramework\Http\JSONResponse;
|
use \OCP\AppFramework\Http\JSONResponse;
|
||||||
|
|
||||||
|
|
||||||
use \OCA\Documents\Db;
|
use \OCA\Documents\Db;
|
||||||
use \OCA\Documents\File;
|
use \OCA\Documents\File;
|
||||||
use \OCA\Documents\Helper;
|
use \OCA\Documents\Helper;
|
||||||
use OCA\Documents\Filter;
|
use OCA\Documents\Filter;
|
||||||
|
|
||||||
|
class BadRequestException extends \Exception {
|
||||||
|
|
||||||
|
protected $body = "";
|
||||||
|
|
||||||
|
public function setBody($body){
|
||||||
|
$this->body = $body;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getBody(){
|
||||||
|
return $this->body;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class SessionController extends Controller{
|
class SessionController extends Controller{
|
||||||
|
|
||||||
protected $uid;
|
protected $uid;
|
||||||
@ -88,6 +103,112 @@ class SessionController extends Controller{
|
|||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function poll($command, $args){
|
||||||
|
$response = new JSONResponse();
|
||||||
|
|
||||||
|
try{
|
||||||
|
$esId = isset($args['es_id']) ? $args['es_id'] : null;
|
||||||
|
|
||||||
|
$session = new Db\Session();
|
||||||
|
$session->load($esId);
|
||||||
|
|
||||||
|
$memberId = isset($args['member_id']) ? $args['member_id'] : null;
|
||||||
|
$member = new Db\Member();
|
||||||
|
$member->load($memberId);
|
||||||
|
|
||||||
|
if (!$member->getIsGuest()){
|
||||||
|
\OCP\JSON::checkLoggedIn();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$file = new File($session->getFileId());
|
||||||
|
} catch (\Exception $e){
|
||||||
|
Helper::warnLog('Error. Session no longer exists. ' . $e->getMessage());
|
||||||
|
$ex = new BadRequestException();
|
||||||
|
$ex->setBody(
|
||||||
|
implode(',', $this->request->getParams())
|
||||||
|
);
|
||||||
|
throw $ex;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ($command){
|
||||||
|
case 'sync_ops':
|
||||||
|
$seqHead = (string) isset($args['seq_head']) ? $args['seq_head'] : null;
|
||||||
|
if (!is_null($seqHead)){
|
||||||
|
$ops = isset($args['client_ops']) ? $args['client_ops'] : null;
|
||||||
|
$hasOps = is_array($ops) && count($ops)>0;
|
||||||
|
|
||||||
|
$op = new Db\Op();
|
||||||
|
$currentHead = $op->getHeadSeq($esId);
|
||||||
|
|
||||||
|
try {
|
||||||
|
$member->updateActivity($memberId);
|
||||||
|
} catch (\Exception $e){
|
||||||
|
//Db error. Not critical
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO handle the case ($currentHead == "") && ($seqHead != "")
|
||||||
|
if ($seqHead == $currentHead) {
|
||||||
|
// matching heads
|
||||||
|
if ($hasOps) {
|
||||||
|
// incoming ops without conflict
|
||||||
|
// Add incoming ops, respond with a new head
|
||||||
|
$newHead = Db\Op::addOpsArray($esId, $memberId, $ops);
|
||||||
|
$response->setData(
|
||||||
|
array(
|
||||||
|
'result' => 'added',
|
||||||
|
'head_seq' => $newHead ? $newHead : $currentHead
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// no incoming ops (just checking for new ops...)
|
||||||
|
$response->setData(
|
||||||
|
array(
|
||||||
|
'result' => 'new_ops',
|
||||||
|
'ops' => array(),
|
||||||
|
'head_seq' => $currentHead
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else { // HEADs do not match
|
||||||
|
$response->setData(
|
||||||
|
array(
|
||||||
|
'result' => $hasOps ? 'conflict' : 'new_ops',
|
||||||
|
'ops' => $op->getOpsAfterJson($esId, $seqHead),
|
||||||
|
'head_seq' => $currentHead,
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$inactiveMembers = $member->updateByTimeout($esId);
|
||||||
|
foreach ($inactiveMembers as $inactive){
|
||||||
|
$op->removeCursor($esId, $inactive);
|
||||||
|
$op->removeMember($esId, $inactive);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Error - no seq_head passed
|
||||||
|
throw new BadRequestException();
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$ex = new BadRequestException();
|
||||||
|
$ex->setBody(
|
||||||
|
implode(',', $this->request->getParams())
|
||||||
|
);
|
||||||
|
throw $ex;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (BadRequestException $e){
|
||||||
|
$response->setStatus(Http::STATUS_BAD_REQUEST);
|
||||||
|
$response->setData(
|
||||||
|
array('err' => 'bad request:[' . $e->getBody() . ']')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @NoAdminRequired
|
* @NoAdminRequired
|
||||||
* @PublicPage
|
* @PublicPage
|
||||||
|
@ -1,48 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* ownCloud - Documents App
|
|
||||||
*
|
|
||||||
* @author Victor Dubiniuk
|
|
||||||
* @copyright 2013 Victor Dubiniuk victor.dubiniuk@gmail.com
|
|
||||||
*
|
|
||||||
* This file is licensed under the Affero General Public License version 3 or
|
|
||||||
* later.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace OCA\Documents;
|
|
||||||
|
|
||||||
class Request {
|
|
||||||
protected $data = array();
|
|
||||||
|
|
||||||
protected $rawRequest = '';
|
|
||||||
|
|
||||||
public function __construct(){
|
|
||||||
$this->rawRequest = file_get_contents('php://input');
|
|
||||||
$this->data = json_decode($this->rawRequest, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getRawRequest(){
|
|
||||||
return $this->rawRequest;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getParam($name){
|
|
||||||
if (empty($name)){
|
|
||||||
return $this->data;
|
|
||||||
}
|
|
||||||
|
|
||||||
$path = explode('/', $name);
|
|
||||||
|
|
||||||
reset($path);
|
|
||||||
$index = current($path);
|
|
||||||
$param = $this->data;
|
|
||||||
do {
|
|
||||||
if (!array_key_exists($index, $param)){
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
$param = $param[$index];
|
|
||||||
} while (($index = next($path)) !== false);
|
|
||||||
|
|
||||||
return $param;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user