mirror of
https://github.com/laurent22/joplin.git
synced 2025-01-02 12:47:41 +02:00
All: Ensure that timestamps are not changed when sharing or unsharing a note
This commit is contained in:
parent
b2de27b6fc
commit
cafaa9cac5
@ -1403,6 +1403,9 @@ packages/lib/services/searchengine/queryBuilder.js.map
|
||||
packages/lib/services/share/ShareService.d.ts
|
||||
packages/lib/services/share/ShareService.js
|
||||
packages/lib/services/share/ShareService.js.map
|
||||
packages/lib/services/share/ShareService.test.d.ts
|
||||
packages/lib/services/share/ShareService.test.js
|
||||
packages/lib/services/share/ShareService.test.js.map
|
||||
packages/lib/services/share/reducer.d.ts
|
||||
packages/lib/services/share/reducer.js
|
||||
packages/lib/services/share/reducer.js.map
|
||||
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -1389,6 +1389,9 @@ packages/lib/services/searchengine/queryBuilder.js.map
|
||||
packages/lib/services/share/ShareService.d.ts
|
||||
packages/lib/services/share/ShareService.js
|
||||
packages/lib/services/share/ShareService.js.map
|
||||
packages/lib/services/share/ShareService.test.d.ts
|
||||
packages/lib/services/share/ShareService.test.js
|
||||
packages/lib/services/share/ShareService.test.js.map
|
||||
packages/lib/services/share/reducer.d.ts
|
||||
packages/lib/services/share/reducer.js
|
||||
packages/lib/services/share/reducer.js.map
|
||||
|
66
packages/lib/services/share/ShareService.test.ts
Normal file
66
packages/lib/services/share/ShareService.test.ts
Normal file
@ -0,0 +1,66 @@
|
||||
import Note from '../../models/Note';
|
||||
import { msleep, setupDatabaseAndSynchronizer, switchClient } from '../../testing/test-utils';
|
||||
import ShareService from './ShareService';
|
||||
import reducer from '../../reducer';
|
||||
import { createStore } from 'redux';
|
||||
import { NoteEntity } from '../database/types';
|
||||
|
||||
function mockApi() {
|
||||
return {
|
||||
exec: (method: string, path: string = '', _query: Record<string, any> = null, _body: any = null, _headers: any = null, _options: any = null): Promise<any> => {
|
||||
if (method === 'GET' && path === 'api/shares') return { items: [] } as any;
|
||||
return null;
|
||||
},
|
||||
personalizedUserContentBaseUrl(_userId: string) {
|
||||
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function mockService() {
|
||||
const service = new ShareService();
|
||||
const store = createStore(reducer as any);
|
||||
service.initialize(store, mockApi() as any);
|
||||
return service;
|
||||
}
|
||||
|
||||
describe('ShareService', function() {
|
||||
|
||||
beforeEach(async (done) => {
|
||||
await setupDatabaseAndSynchronizer(1);
|
||||
await switchClient(1);
|
||||
done();
|
||||
});
|
||||
|
||||
it('should not change the note user timestamps when sharing or unsharing', (async () => {
|
||||
let note = await Note.save({});
|
||||
const service = mockService();
|
||||
await msleep(1);
|
||||
await service.shareNote(note.id);
|
||||
|
||||
function checkTimestamps(previousNote: NoteEntity, newNote: NoteEntity) {
|
||||
// After sharing or unsharing, only the updated_time property should
|
||||
// be updated, for sync purposes. All other timestamps shouldn't
|
||||
// change.
|
||||
expect(previousNote.user_created_time).toBe(newNote.user_created_time);
|
||||
expect(previousNote.user_updated_time).toBe(newNote.user_updated_time);
|
||||
expect(previousNote.updated_time < newNote.updated_time).toBe(true);
|
||||
expect(previousNote.created_time).toBe(newNote.created_time);
|
||||
}
|
||||
|
||||
{
|
||||
const noteReloaded = await Note.load(note.id);
|
||||
checkTimestamps(note, noteReloaded);
|
||||
note = noteReloaded;
|
||||
}
|
||||
|
||||
await msleep(1);
|
||||
await service.unshareNote(note.id);
|
||||
|
||||
{
|
||||
const noteReloaded = await Note.load(note.id);
|
||||
checkTimestamps(note, noteReloaded);
|
||||
}
|
||||
}));
|
||||
|
||||
});
|
@ -20,8 +20,9 @@ export default class ShareService {
|
||||
return this.instance_;
|
||||
}
|
||||
|
||||
public initialize(store: Store<any>) {
|
||||
public initialize(store: Store<any>, api: JoplinServerApi = null) {
|
||||
this.store_ = store;
|
||||
this.api_ = api;
|
||||
}
|
||||
|
||||
public get enabled(): boolean {
|
||||
@ -120,7 +121,14 @@ export default class ShareService {
|
||||
|
||||
const share = await this.api().exec('POST', 'api/shares', {}, { note_id: noteId });
|
||||
|
||||
await Note.save({ id: note.id, is_shared: 1 });
|
||||
await Note.save({
|
||||
id: note.id,
|
||||
parent_id: note.parent_id,
|
||||
is_shared: 1,
|
||||
updated_time: Date.now(),
|
||||
}, {
|
||||
autoTimestamp: false,
|
||||
});
|
||||
|
||||
return share;
|
||||
}
|
||||
@ -140,7 +148,14 @@ export default class ShareService {
|
||||
|
||||
await Promise.all(promises);
|
||||
|
||||
await Note.save({ id: note.id, is_shared: 0 });
|
||||
await Note.save({
|
||||
id: note.id,
|
||||
parent_id: note.parent_id,
|
||||
is_shared: 0,
|
||||
updated_time: Date.now(),
|
||||
}, {
|
||||
autoTimestamp: false,
|
||||
});
|
||||
}
|
||||
|
||||
public shareUrl(userId: string, share: StateShare): string {
|
||||
|
Loading…
Reference in New Issue
Block a user