diff options
author | VicDeo <dubiniuk@owncloud.com> | 2014-11-13 22:04:09 +0300 |
---|---|---|
committer | VicDeo <dubiniuk@owncloud.com> | 2014-11-13 22:04:09 +0300 |
commit | da3705e8b78e9f85ed9a4762cd1f53a569f57cc8 (patch) | |
tree | 6c0ab901b07167eb408b01ca02a00225a72fcb0b /lib | |
parent | 28a5038fbdd341bdf99dd5b44c24e80f9c5042d5 (diff) | |
parent | 0f19d74f69bb814afb96c2efecc07d665b9b9c39 (diff) |
Merge pull request #402 from owncloud/optype-as-a-separate-field
Use a separate field for optype.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/db/op.php | 139 |
1 files changed, 83 insertions, 56 deletions
diff --git a/lib/db/op.php b/lib/db/op.php index 4cd37e09..cef01215 100644 --- a/lib/db/op.php +++ b/lib/db/op.php @@ -17,21 +17,24 @@ class Op extends \OCA\Documents\Db { protected $tableName = '`*PREFIX*documents_op`'; - protected $insertStatement = 'INSERT INTO `*PREFIX*documents_op` (`es_id`, `member`, `opspec`) VALUES (?, ?, ?)'; + protected $insertStatement = 'INSERT INTO `*PREFIX*documents_op` (`es_id`, `optype`, `member`, `opspec`) VALUES (?, ?, ?, ?)'; public static function addOpsArray($esId, $memberId, $ops){ - $lastSeq = ""; $opObj = new Op(); foreach ($ops as $op) { + if (!$opObj->canInsertOp($esId, $memberId, $op)){ + continue; + } $opObj->setData(array( - $esId, - $memberId, + $esId, + $op['optype'], + $memberId, json_encode($op) )); $opObj->insert(); - $lastSeq = $opObj->getLastInsertId(); } - return $lastSeq; + + return $opObj->getHeadSeq($esId); } /** @@ -84,76 +87,100 @@ class Op extends \OCA\Documents\Db { } public function addMember($esId, $memberId, $fullName, $color, $imageUrl){ - $op = '{"optype":"AddMember","memberid":"'. $memberId .'","timestamp":"'. time() .'", "setProperties":{"fullName":"'. $fullName .'","color":"'. $color .'","imageUrl":"'. $imageUrl .'"}}'; + $op = array( + 'optype' => 'AddMember', + 'memberid' => (string) $memberId, + 'timestamp' => (string) time(), + 'setProperties' => array( + 'fullName' => $fullName, + 'color' => $color, + 'imageUrl' => $imageUrl + ) + ); $this->insertOp($esId, $memberId, $op); } public function removeCursor($esId, $memberId){ - if ($this->hasOp($esId, $memberId, 'AddCursor') && !$this->hasLastOp($esId, $memberId, 'RemoveCursor')){ - $op = '{"optype":"RemoveCursor","memberid":"'. $memberId .'","reason":"server-idle","timestamp":'. time() .'}'; - $this->insertOp($esId, $memberId, $op); - } + $op = array( + 'optype' => 'RemoveCursor', + 'memberid' => (string) $memberId, + 'reason' => 'server-idle', + 'timestamp' => (string) time() + ); + $this->insertOp($esId, $memberId, $op); } public function removeMember($esId, $memberId){ - if ($this->hasOp($esId, $memberId, 'AddMember') && !$this->hasLastOp($esId, $memberId, 'RemoveMember')){ - $op ='{"optype":"RemoveMember","memberid":"'. $memberId .'","timestamp":'. time() .'}'; - $this->insertOp($esId, $memberId, $op); - } - } - - public function updateMember($esId, $memberId, $fullName, $color, $imageUrl){ - //TODO: Follow the spec https://github.com/kogmbh/WebODF/blob/master/webodf/lib/ops/OpUpdateMember.js#L95 - $op = '{"optype":"UpdateMember","memberid":"'. $memberId .'","fullName":"'. $fullName .'","color":"'. $color .'","imageUrl":"'. $imageUrl .'","timestamp":'. time() .'}' - ; + $op = array( + 'optype' => 'RemoveMember', + 'memberid' => (string) $memberId, + 'timestamp' => (string) time() + ); $this->insertOp($esId, $memberId, $op); } + //TODO: Implement https://github.com/kogmbh/WebODF/blob/master/webodf/lib/ops/OpUpdateMember.js#L95 public function changeNick($esId, $memberId, $fullName){ - $op = '{"optype":"UpdateMember","memberid":"'. $memberId .'", "setProperties":{"fullName":"'. $fullName .'"},"timestamp":'. time() .'}' - ; + $op = array( + 'optype' => 'UpdateMember', + 'memberid' => (string) $memberId, + 'timestamp' => (string) time(), + 'setProperties' => array( + 'fullName' => $fullName, + ) + ); $this->insertOp($esId, $memberId, $op); } protected function insertOp($esId, $memberId, $op){ - $op = new Op(array( - $esId, - $memberId, - $op - )); - $op->insert(); + if ($this->canInsertOp($esId, $memberId, $op)){ + $op = new Op(array( + $esId, + $op['optype'], + $memberId, + json_encode($op) + )); + $op->insert(); + } } - protected function hasLastOp($esId, $memberId, $opType){ - $query = \OCP\DB::prepare(' - SELECT `opspec` - FROM ' . self::DB_TABLE . ' - WHERE `es_id`=? - AND `member`=? - - ORDER BY `seq` DESC - ', - 2,0 - ); + protected function canInsertOp($esId, $memberId, $op){ + $cursorOps = array('AddCursor', 'RemoveCursor'); + $memberOps = array('AddMember', 'RemoveMember'); + $result = true; - $result = $query->execute(array($esId, $memberId)); - $ops = $result->fetchAll(); - foreach ($ops as $op){ - $decoded = json_decode($op['opspec'], true); - if ($decoded['optype']==$opType){ - return true; - } + switch ($op['optype']){ + case 'AddCursor': + $ops = $this->getFilteredMemberOps($esId, $memberId, $cursorOps); + $result = !count($ops) || $ops[0]['optype'] === 'RemoveCursor'; + break; + case 'RemoveCursor': + $ops = $this->getFilteredMemberOps($esId, $memberId, $cursorOps); + $result = count($ops) && $ops[0]['optype'] === 'AddCursor'; + break; + case 'AddMember': + $ops = $this->getFilteredMemberOps($esId, $memberId, $memberOps); + $result = !count($ops) || $ops[0]['optype'] === 'RemoveMember'; + break; + case 'RemoveMember': + $ops = $this->getFilteredMemberOps($esId, $memberId, $memberOps); + $result = count($ops) && $ops[0]['optype'] === 'AddMember'; + break; } - return false; + return $result; } - - protected function hasOp($esId, $memberId, $opType){ - $ops = $this->execute( - 'SELECT * FROM ' . $this->tableName . ' WHERE `es_id`=? AND `opspec` LIKE \'%"' . $opType . '","memberid":"' . $memberId .'"%\'', - array($esId) + + protected function getFilteredMemberOps($esId, $memberId, $targetOps){ + $stmt = $this->buildInQuery('optype', $targetOps); + $result = $this->execute(' + SELECT `optype` FROM ' . $this->tableName . ' + WHERE es_id=? AND member=? AND ' . $stmt . 'ORDER BY `seq` DESC', + array_merge(array($esId, $memberId), $targetOps) ); - $result = $ops->fetchAll(); - return is_array($result) && count($result)>0; + $ops = $result->fetchAll(); + if (!is_array($ops)){ + $ops = array(); + } + return $ops; } - } |