Cleanup ops
This commit is contained in:
parent
9400fc8218
commit
accbdf7284
@ -66,39 +66,29 @@ try{
|
||||
$esId = $request->getParam('args/es_id');
|
||||
$memberId = $request->getParam('args/member_id');
|
||||
$ops = $request->getParam('args/client_ops');
|
||||
$hasOps = is_array($ops) && count($ops>0);
|
||||
|
||||
$currentHead = OCA\Office\Op::getHeadSeq($esId);
|
||||
|
||||
// TODO handle the case ($currentHead == "") && ($seqHead != "")
|
||||
|
||||
if ($seqHead == $currentHead) {
|
||||
// matching heads
|
||||
if (count($ops) > 0) {
|
||||
if ($hasOps) {
|
||||
// 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);
|
||||
}
|
||||
$newHead = OCA\Office\Op::addOpsArray($esId, $memberId, $ops);
|
||||
$response["result"] = 'added';
|
||||
$response["headSeq"] = $lastSeq;
|
||||
$response["headSeq"] = $newHead ? $newHead : $currentHead;
|
||||
} else {
|
||||
// no incoming ops (just checking for new ops...)
|
||||
$response["result"] = 'newOps';
|
||||
$response["ops"] = [];
|
||||
$response["ops"] = array();
|
||||
$response["headSeq"] = $currentHead;
|
||||
}
|
||||
} else { // HEADs do not match
|
||||
$response["ops"] = OCA\Office\Op::getOpsAfter($esId, $seqHead);
|
||||
$response["headSeq"] = $currentHead;
|
||||
if (count($ops) > 0) { // a conflict
|
||||
$response["result"] = 'conflict';
|
||||
} else { // not a conflict
|
||||
$response["result"] = 'newOps';
|
||||
|
||||
}
|
||||
$response["result"] = $hasOps ? 'conflict' : 'newOps';
|
||||
}
|
||||
} else {
|
||||
// Error - no seq_head passed
|
||||
|
35
lib/op.php
35
lib/op.php
@ -4,18 +4,27 @@ namespace OCA\Office;
|
||||
|
||||
class Op {
|
||||
|
||||
public static function add($esId, $op){
|
||||
public static function add($esId, $memberId, $opspec){
|
||||
$query = \OCP\DB::prepare('INSERT INTO `*PREFIX*office_op` (`es_id`, `member`, `opspec`) VALUES (?, ?, ?) ');
|
||||
$query->execute(array(
|
||||
$esId,
|
||||
$op['member'],
|
||||
$op['opspec'],
|
||||
$memberId,
|
||||
$opspec,
|
||||
));
|
||||
// throw something - if query fails - thats fatal
|
||||
|
||||
return \OCP\DB::insertid(`*PREFIX*office_op`);
|
||||
}
|
||||
|
||||
public static function addOpsArray($esId, $memberId, $ops){
|
||||
$lastSeq = false;
|
||||
foreach ($ops as $op) {
|
||||
$lastSeq = self::add($esId, $memberId, json_encode($op));
|
||||
}
|
||||
return $lastSeq;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @returns "" when there are no Ops, or the seq of the last Op
|
||||
*/
|
||||
@ -26,18 +35,24 @@ class Op {
|
||||
))
|
||||
->fetchOne()
|
||||
;
|
||||
return is_null($result) ? "" : $result;
|
||||
return !$result ? "" : $result;
|
||||
}
|
||||
|
||||
public static function getOpsAfterJson($esId, $seq){
|
||||
return array_map(
|
||||
json_decode,
|
||||
self::getOpsAfter($esId, $seq)
|
||||
);
|
||||
}
|
||||
|
||||
public static function getOpsAfter($esId, $seq){
|
||||
if ($seq == "") $seq = -1;
|
||||
$oplist = [];
|
||||
if ($seq == ""){
|
||||
$seq = -1;
|
||||
}
|
||||
$oplist = array();
|
||||
$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;
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user