From f6e1c65ea41ab22c22365239842cbb6db1a9dbfa Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Tue, 6 Aug 2013 18:07:05 +0300 Subject: [PATCH] Ops sync --- ajax/otpoll.php | 39 ++++++++++++++++++++++++++------------- lib/op.php | 23 ++++++++++++++++++++++- lib/request.php | 27 +++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 14 deletions(-) create mode 100644 lib/request.php diff --git a/ajax/otpoll.php b/ajax/otpoll.php index ba0fba9d..83a18d65 100644 --- a/ajax/otpoll.php +++ b/ajax/otpoll.php @@ -50,10 +50,8 @@ function bogusSession($i){ return $bs; } -$postbody = file_get_contents('php://input'); -$postobject = json_decode($postbody, true); - -$command = $postobject['command']; +$request = new OCA\Office\Request(); +$command = $request->getParam('command'); $response = array(); switch ($command){ @@ -64,17 +62,35 @@ switch ($command){ $response = "true"; // should fail when session is non-existent break; case 'sync-ops': - // completely bogus + $seqHead = $request->getParam('args/seq_head'); + + if (!is_null($seqHead)){ + $esId = $request->getParam('args/es_id'); + $memberId = $request->getParam('args/member_id'); + $ops = $request->getParam('args/client_ops'); + + $currentHead = OCA\Office\Op::getHeadSeq($esId); - if $postobject['args']['seq_head'] is the most recent op in the ops-table: + //if $postobject['args']['seq_head'] is the most recent op in the ops-table: // append all ops in $postobject['args']['client_ops'] to the ops-table - else: - // reply with: - // { + if ($seqHead>$currentHead){ + foreach ($ops as $op){ + OCA\Office\Op::add($op); + } + } else { // result: 'conflict', // ops: a list of all ops since $postobject['args']['seq_head'] // headSeq: the most recent op-seq - // } + $response["result"] = 'conflict'; + $response["ops"] = OCA\Office\Op::getOpsAfter($esId, $seqHead); + $last = end($response["ops"]); + $response["headSeq"] = $last['seq']; + } + + } else { + // Error :) + } + /* * try { * OCA\Office\Op::add( @@ -89,9 +105,6 @@ switch ($command){ * * } */ - $response['result'] = 'newOps'; - $response['ops'] = array(); - $response['headSeq'] = -1; break; default: header('HTTP/1.1 400: BAD REQUEST'); diff --git a/lib/op.php b/lib/op.php index 04a918ad..0207b69c 100644 --- a/lib/op.php +++ b/lib/op.php @@ -4,7 +4,7 @@ namespace OCA\Office; class Op { - public function add($op){ + public static function add($op){ $query = \OCP\DB::prepare('INSERT INTO `*PREFIX*office_op` (`es_id`, `seq`, `member`, `opspec`) VALUES (?, ?, ?, ?) '); $result = $query->execute(array( $op['es_id'], @@ -12,6 +12,27 @@ class Op { $op['member'], $op['opspec'], )); + return $result; + } + + public static function getHeadSeq($esId){ + $query = \OCP\DB::prepare('SELECT `seq` FROM `*PREFIX*office_op` WHERE `es_id`=? ORDER BY `seq` DESC LIMIT 1'); + $result = $query->execute(array( + $esId + )) + ->fetchOne() + ; + return $result; + } + + public static function getOpsAfter($esId, $seq){ + $query = \OCP\DB::prepare('SELECT `seq` FROM `*PREFIX*office_op` WHERE `es_id`=? AND `seq`>? ORDER BY `seq` ASC'); + $result = $query->execute(array( + $esId, $seq + )) + ->fetchAll() + ; + return $result; } } diff --git a/lib/request.php b/lib/request.php new file mode 100644 index 00000000..83d6ce4d --- /dev/null +++ b/lib/request.php @@ -0,0 +1,27 @@ +data = json_decode(file_get_contents('php://input'), true); + } + public function getParam($name){ + $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; + } +} + \ No newline at end of file