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

Api: Allow preserving timestamps when updating a note

This commit is contained in:
Laurent Cozic 2020-10-21 18:12:36 +01:00
parent 5e040c062c
commit ad7a80e260
2 changed files with 74 additions and 2 deletions

View File

@ -183,6 +183,59 @@ describe('services_rest_Api', function() {
expect(response.user_updated_time).toBe(updatedTime);
expect(response.user_created_time).toBe(createdTime);
const timeBefore = Date.now();
response = await api.route('POST', 'notes', null, JSON.stringify({
parent_id: f.id,
}));
const newNote = await Note.load(response.id);
expect(newNote.user_updated_time).toBeGreaterThanOrEqual(timeBefore);
expect(newNote.user_created_time).toBeGreaterThanOrEqual(timeBefore);
}));
it('should preserve user timestamps when updating notes', asyncTest(async () => {
const folder = await Folder.save({ title: 'mon carnet' });
const updatedTime = Date.now() - 1000;
const createdTime = Date.now() - 10000;
const response = await api.route('POST', 'notes', null, JSON.stringify({
parent_id: folder.id,
}));
const noteId = response.id;
{
// Check that if user timestamps are supplied, they are preserved by the API
await api.route('PUT', `notes/${noteId}`, null, JSON.stringify({
user_updated_time: updatedTime,
user_created_time: createdTime,
title: 'mod',
}));
const modNote = await Note.load(noteId);
expect(modNote.title).toBe('mod');
expect(modNote.user_updated_time).toBe(updatedTime);
expect(modNote.user_created_time).toBe(createdTime);
}
{
// Check if no user timestamps are supplied they are automatically updated.
const beforeTime = Date.now();
await api.route('PUT', `notes/${noteId}`, null, JSON.stringify({
title: 'mod2',
}));
const modNote = await Note.load(noteId);
expect(modNote.title).toBe('mod2');
expect(modNote.user_updated_time).toBeGreaterThanOrEqual(beforeTime);
expect(modNote.user_created_time).toBeGreaterThanOrEqual(createdTime);
}
}));
it('should create notes with supplied ID', asyncTest(async () => {

View File

@ -432,6 +432,8 @@ export default class Api {
const timestamp = Date.now();
note.updated_time = timestamp;
note.created_time = timestamp;
if (!('user_updated_time' in note)) note.user_updated_time = timestamp;
if (!('user_created_time' in note)) note.user_created_time = timestamp;
note = await Note.save(note, saveOptions);
@ -454,7 +456,24 @@ export default class Api {
if (!note) throw new ErrorNotFound();
const updatedNote = await this.defaultAction_(BaseModel.TYPE_NOTE, request, id, link);
const saveOptions = {
...this.defaultSaveOptions_(note, 'PUT'),
autoTimestamp: false, // No auto-timestamp because user may have provided them
userSideValidation: true,
};
const timestamp = Date.now();
const newProps = request.bodyJson(this.readonlyProperties('PUT'));
if (!('user_updated_time' in newProps)) newProps.user_updated_time = timestamp;
let newNote = {
...note,
...newProps,
updated_time: timestamp,
};
newNote = await Note.save(newNote, saveOptions);
const requestNote = JSON.parse(request.body);
if (requestNote.tags || requestNote.tags === '') {
@ -462,7 +481,7 @@ export default class Api {
await Tag.setNoteTagsByTitles(id, tagTitles);
}
return updatedNote;
return newNote;
}
return this.defaultAction_(BaseModel.TYPE_NOTE, request, id, link);