From 6ee2595dce16837ce29610444934d97db57d3f19 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sat, 22 Jun 2019 12:31:04 +0100 Subject: [PATCH] Desktop: Fixes #1676: Preserve user timestamps when adding note via API --- CliClient/tests/services_rest_Api.js | 19 +++++++++++++++++++ ReactNativeClient/lib/services/rest/Api.js | 12 ++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CliClient/tests/services_rest_Api.js b/CliClient/tests/services_rest_Api.js index 6e985c2b9..815d6f08e 100644 --- a/CliClient/tests/services_rest_Api.js +++ b/CliClient/tests/services_rest_Api.js @@ -156,6 +156,25 @@ describe('services_rest_Api', function() { done(); }); + it('should preserve user timestamps when creating notes', async (done) => { + let response = null; + const f = await Folder.save({ title: "mon carnet" }); + + const updatedTime = Date.now() - 1000; + const createdTime = Date.now() - 10000; + + response = await api.route('POST', 'notes', null, JSON.stringify({ + parent_id: f.id, + user_updated_time: updatedTime, + user_created_time: createdTime, + })); + + expect(response.user_updated_time).toBe(updatedTime); + expect(response.user_created_time).toBe(createdTime); + + done(); + }); + it('should create notes with supplied ID', async (done) => { let response = null; const f = await Folder.save({ title: "mon carnet" }); diff --git a/ReactNativeClient/lib/services/rest/Api.js b/ReactNativeClient/lib/services/rest/Api.js index d125be344..562702fd1 100644 --- a/ReactNativeClient/lib/services/rest/Api.js +++ b/ReactNativeClient/lib/services/rest/Api.js @@ -1,4 +1,5 @@ const { ltrimSlashes } = require('lib/path-utils.js'); +const { Database } = require('lib/database.js'); const Folder = require('lib/models/Folder'); const Note = require('lib/models/Note'); const Tag = require('lib/models/Tag'); @@ -383,6 +384,11 @@ class Api { this.logger().info('Request (' + requestId + '): Saving note...'); const saveOptions = this.defaultSaveOptions_(note, 'POST'); + saveOptions.autoTimestamp = false; // No auto-timestamp because user may have provided them + const timestamp = Date.now(); + note.updated_time = timestamp; + note.created_time = timestamp; + note = await Note.save(note, saveOptions); if (requestNote.tags) { @@ -442,8 +448,10 @@ class Api { output.parent_id = folder.id; } - if (requestNote.source_url) output.source_url = requestNote.source_url; - if (requestNote.author) output.author = requestNote.author; + if ('source_url' in requestNote) output.source_url = requestNote.source_url; + if ('author' in requestNote) output.author = requestNote.author; + if ('user_updated_time' in requestNote) output.user_updated_time = Database.formatValue(Database.TYPE_INT, requestNote.user_updated_time); + if ('user_created_time' in requestNote) output.user_created_time = Database.formatValue(Database.TYPE_INT, requestNote.user_created_time); return output; }