1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-24 08:12:24 +02:00

Make sure synchronizer returns list of modified fields

This commit is contained in:
Laurent Cozic 2017-01-09 11:18:46 +01:00
parent 8973668957
commit 35e2fb8c6e
2 changed files with 33 additions and 3 deletions

View File

@ -30,6 +30,7 @@ class Change extends BaseModel {
if ($hasMore) array_pop($changes);
$itemIdToChange = array();
$itemIdToChangedFields = array();
$createdItems = array();
$deletedItems = array();
foreach ($changes as $change) {
@ -37,6 +38,9 @@ class Change extends BaseModel {
$createdItems[] = $change->item_id;
} else if ($change->type == Change::enumId('type', 'delete')) {
$deletedItems[] = $change->item_id;
} else { // Update
if (!isset($itemIdToChangedFields[$change->item_id])) $itemIdToChangedFields[$change->item_id] = array();
$itemIdToChangedFields[$change->item_id][] = $change->item_field;
}
$itemIdToChange[$change->item_id] = $change;
@ -49,15 +53,19 @@ class Change extends BaseModel {
continue;
}
$syncItem = $change->toSyncItem();
if (in_array($itemId, $deletedItems)) {
// Item was deleted at some point - just return one 'delete' event
$change->type = Change::enumId('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');
} else {
$syncItem['item_fields'] = $itemIdToChangedFields[$change->item_id];
}
$output[] = $change->toSyncItem();
$output[] = $syncItem;
}
// This is important so that the client knows that the last item in the list
@ -73,13 +81,13 @@ class Change extends BaseModel {
);
}
public function toSyncItem() {
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_field' => $this->item_field,
'item_fields' => array(), // This is populated by changesDoneAfterId()
);
}

View File

@ -229,5 +229,27 @@ class ChangeTest extends BaseTestCase {
$this->assertEquals(1, $foundCount);
}
}
public function testChangedFields() {
$n1 = new Note();
$n1->fromPublicArray(array('body' => 'test'));
$n1->owner_id = $this->userId();
$n1->save();
$r = Change::changesDoneAfterId($this->userId(), $this->clientId(2), 0);
$lastId = $r['items'][0]['id'];
$n1->latitude = 1;
$n1->save();
$n1->longitude = 1;
$n1->save();
$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']);
}
}