diff --git a/ajax/otpoll.php b/ajax/otpoll.php index 32d88036..509e50fc 100644 --- a/ajax/otpoll.php +++ b/ajax/otpoll.php @@ -62,7 +62,6 @@ try{ break; case 'sync-ops': $seqHead = $request->getParam('args/seq_head'); - if (!is_null($seqHead)){ $esId = $request->getParam('args/es_id'); $memberId = $request->getParam('args/member_id'); @@ -70,25 +69,39 @@ try{ $currentHead = OCA\Office\Op::getHeadSeq($esId); - if (!$currentHead || $seqHead == $currentHead) { // very first ops || match the current head - // Add incoming ops, response with a new head - foreach ($ops as $op) { - $op['opspec'] = json_encode($op['opspec']); - $lastSeq = OCA\Office\Op::add($op); + // TODO handle the case ($currentHead == "") && ($seqHead != "") + + if ($seqHead == $currentHead) { + // matching heads + if (count($ops) > 0) { + // incoming ops without conflict + // Add incoming ops, respond with a new head + $lastSeq = $currentHead; // empty op stack + foreach ($ops as $op) { + $op['opspec'] = json_encode($op); + $op['member'] = $memberId; + $lastSeq = OCA\Office\Op::add($esId, $op); + } + $response["result"] = 'added'; + $response["headSeq"] = $lastSeq; + } else { + // no incoming ops (just checking for new ops...) + $response["result"] = 'newOps'; + $response["ops"] = []; + $response["headSeq"] = $currentHead; } - $response["result"] = 'added'; - $response["headSeq"] = $lastSeq; - } else { - // result: 'conflict', - // ops: a list of all ops since $postobject['args']['seq_head'] - // headSeq: the most recent op-seq - $response["result"] = 'conflict'; + } else { // HEADs do not match $response["ops"] = OCA\Office\Op::getOpsAfter($esId, $seqHead); - $last = end($response["ops"]); - $response["headSeq"] = $last['seq']; + $response["headSeq"] = $currentHead; + if (count($ops) > 0) { // a conflict + $response["result"] = 'conflict'; + } else { // not a conflict + $response["result"] = 'newOps'; + + } } } else { - // Error - empty seq_head passed :) + // Error - no seq_head passed throw new BadRequestException(); } diff --git a/lib/op.php b/lib/op.php index 352236db..2aa31fda 100644 --- a/lib/op.php +++ b/lib/op.php @@ -4,19 +4,21 @@ namespace OCA\Office; class Op { - public static function add($op){ + public static function add($esId, $op){ $query = \OCP\DB::prepare('INSERT INTO `*PREFIX*office_op` (`es_id`, `member`, `opspec`) VALUES (?, ?, ?) '); $query->execute(array( - $result = $op['es_id'], + $esId, $op['member'], $op['opspec'], )); - if ($result){ - return \OCP\DB::insertid(`*PREFIX*office_op`); - } - return 0; + // throw something - if query fails - thats fatal + + return \OCP\DB::insertid(`*PREFIX*office_op`); } + /** + * @returns "" when there are no Ops, or the seq of the last Op + */ 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( @@ -24,17 +26,18 @@ class Op { )) ->fetchOne() ; - return $result; + return is_null($result) ? "" : $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; + if ($seq == "") $seq = -1; + $oplist = []; + $query = \OCP\DB::prepare('SELECT `opspec` FROM `*PREFIX*office_op` WHERE `es_id`=? AND `seq`>? ORDER BY `seq` ASC'); + $result = $query->execute(array($esId, $seq)); + while( $row = $result->fetchRow() ) { + $oplist[] = json_decode($row['opspec']); + } + return $oplist; } }