1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-11 18:24:43 +02:00

Return objects that have been changed

This commit is contained in:
Laurent Cozic 2017-01-09 12:14:21 +01:00
parent 0e4a6f0fe6
commit 6a66c07a28
2 changed files with 33 additions and 14 deletions

View File

@ -53,16 +53,23 @@ class Change extends BaseModel {
continue;
}
$syncItem = $change->toSyncItem();
$syncItem = array(
'id' => (string)$change->id,
'type' => self::enumName('type', $change->type),
'item_id' => self::hex($change->item_id),
'item_type' => FolderItem::enumName('type', $change->item_type),
);
if (in_array($itemId, $deletedItems)) {
// Item was deleted at some point - just return one 'delete' event
$change->type = Change::enumId('type', 'delete');
$syncItem['type'] = 'delete';
} else if (in_array($itemId, $createdItems)) {
// Item was created then updated - just return one 'create' event with the latest changes
$change->type = Change::enumId('type', 'create');
$syncItem['type'] = 'create';
$syncItem['item'] = self::requireItemById($change->item_type, $change->item_id);
} else {
$syncItem['item_fields'] = $itemIdToChangedFields[$change->item_id];
$syncItem['item'] = self::requireItemById($change->item_type, $change->item_id);
}
$output[] = $syncItem;
@ -75,20 +82,31 @@ class Change extends BaseModel {
return strnatcmp($a['id'], $b['id']);
});
foreach ($output as $k => $syncItem) {
if ($syncItem['item']) {
$item = $syncItem['item']->toPublicArray();
if ($syncItem['type'] == 'update') {
foreach ($item as $field => $value) {
if (in_array($field, $syncItem['item_fields'])) continue;
unset($item[$field]);
}
unset($syncItem['item_fields']);
}
$syncItem['item'] = $item;
$output[$k] = $syncItem;
}
}
return array(
'has_more' => $hasMore,
'items' => $output,
);
}
private function toSyncItem() {
return array(
'id' => (string)$this->id,
'type' => self::enumName('type', $this->type),
'item_id' => self::hex($this->item_id),
'item_type' => FolderItem::enumName('type', $this->item_type),
'item_fields' => array(), // This is populated by changesDoneAfterId()
);
static private function requireItemById($itemTypeId, $itemId) {
$item = BaseItem::byId($itemTypeId, $itemId);
if (!$item) throw new \Exception('No such item: ' . $itemTypeId . ' ' . $itemId);
return $item;
}
static public function itemFieldHistory($itemId, $itemField, $toId = null) {

View File

@ -230,6 +230,7 @@ class ChangeTest extends BaseTestCase {
}
}
// Checks that when changing the fields of an object, the synchronizer returns the right fields.
public function testChangedFields() {
$n1 = new Note();
$n1->fromPublicArray(array('body' => 'test'));
@ -247,9 +248,9 @@ class ChangeTest extends BaseTestCase {
$r = Change::changesDoneAfterId($this->userId(), $this->clientId(2), $lastId);
$change = $r['items'][0];
$this->assertEquals(2, count($change['item_fields']));
$this->assertContains('latitude', $change['item_fields']);
$this->assertContains('longitude', $change['item_fields']);
$this->assertEquals(2, count($change['item']));
$this->assertEquals(1, $change['item']['latitude']);
$this->assertEquals(1, $change['item']['longitude']);
}
}