fix sync-ops handling

pull/1/head
Tobias Hintze 11 years ago
parent f03ee0ee1c
commit fa96c76d5d

@ -62,7 +62,6 @@ try{
break; break;
case 'sync-ops': case 'sync-ops':
$seqHead = $request->getParam('args/seq_head'); $seqHead = $request->getParam('args/seq_head');
if (!is_null($seqHead)){ if (!is_null($seqHead)){
$esId = $request->getParam('args/es_id'); $esId = $request->getParam('args/es_id');
$memberId = $request->getParam('args/member_id'); $memberId = $request->getParam('args/member_id');
@ -70,25 +69,39 @@ try{
$currentHead = OCA\Office\Op::getHeadSeq($esId); $currentHead = OCA\Office\Op::getHeadSeq($esId);
if (!$currentHead || $seqHead == $currentHead) { // very first ops || match the current head // TODO handle the case ($currentHead == "") && ($seqHead != "")
// Add incoming ops, response with a new head
foreach ($ops as $op) { if ($seqHead == $currentHead) {
$op['opspec'] = json_encode($op['opspec']); // matching heads
$lastSeq = OCA\Office\Op::add($op); 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'; } else { // HEADs do not match
$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';
$response["ops"] = OCA\Office\Op::getOpsAfter($esId, $seqHead); $response["ops"] = OCA\Office\Op::getOpsAfter($esId, $seqHead);
$last = end($response["ops"]); $response["headSeq"] = $currentHead;
$response["headSeq"] = $last['seq']; if (count($ops) > 0) { // a conflict
$response["result"] = 'conflict';
} else { // not a conflict
$response["result"] = 'newOps';
}
} }
} else { } else {
// Error - empty seq_head passed :) // Error - no seq_head passed
throw new BadRequestException(); throw new BadRequestException();
} }

@ -4,19 +4,21 @@ namespace OCA\Office;
class Op { 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 = \OCP\DB::prepare('INSERT INTO `*PREFIX*office_op` (`es_id`, `member`, `opspec`) VALUES (?, ?, ?) ');
$query->execute(array( $query->execute(array(
$result = $op['es_id'], $esId,
$op['member'], $op['member'],
$op['opspec'], $op['opspec'],
)); ));
if ($result){ // throw something - if query fails - thats fatal
return \OCP\DB::insertid(`*PREFIX*office_op`);
} return \OCP\DB::insertid(`*PREFIX*office_op`);
return 0;
} }
/**
* @returns "" when there are no Ops, or the seq of the last Op
*/
public static function getHeadSeq($esId){ public static function getHeadSeq($esId){
$query = \OCP\DB::prepare('SELECT `seq` FROM `*PREFIX*office_op` WHERE `es_id`=? ORDER BY `seq` DESC LIMIT 1'); $query = \OCP\DB::prepare('SELECT `seq` FROM `*PREFIX*office_op` WHERE `es_id`=? ORDER BY `seq` DESC LIMIT 1');
$result = $query->execute(array( $result = $query->execute(array(
@ -24,17 +26,18 @@ class Op {
)) ))
->fetchOne() ->fetchOne()
; ;
return $result; return is_null($result) ? "" : $result;
} }
public static function getOpsAfter($esId, $seq){ 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'); if ($seq == "") $seq = -1;
$result = $query->execute(array( $oplist = [];
$esId, $seq $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));
->fetchAll() while( $row = $result->fetchRow() ) {
; $oplist[] = json_decode($row['opspec']);
return $result; }
return $oplist;
} }
} }

Loading…
Cancel
Save