Skip to content

Commit

Permalink
All: Also duplicate resources when duplicating a note
Browse files Browse the repository at this point in the history
Ref: #5796
  • Loading branch information
laurent22 committed Nov 27, 2021
1 parent 4ce58fa commit c0a8c33
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
23 changes: 23 additions & 0 deletions packages/lib/models/Note.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import Folder from './Folder';
import Note from './Note';
import Tag from './Tag';
import ItemChange from './ItemChange';
import Resource from './Resource';
import { ResourceEntity } from '../services/database/types';
const ArrayUtils = require('../ArrayUtils.js');

async function allItems() {
Expand Down Expand Up @@ -143,6 +145,27 @@ describe('models/Note', function() {
expect(duplicatedNoteTags.length).toBe(2);
}));

it('should also duplicate resources when duplicating a note', (async () => {
const folder = await Folder.save({ title: 'folder' });
let note = await Note.save({ title: 'note', parent_id: folder.id });
await shim.attachFileToNote(note, `${supportDir}/photo.jpg`);

const resource = (await Resource.all())[0];
expect((await Resource.all()).length).toBe(1);

const duplicatedNote = await Note.duplicate(note.id);

const resources: ResourceEntity[] = await Resource.all();
expect(resources.length).toBe(2);

const duplicatedResource = resources.find(r => r.id !== resource.id);

note = await Note.load(note.id);

expect(note.body).toContain(resource.id);
expect(duplicatedNote.body).toContain(duplicatedResource.id);
}));

it('should delete a set of notes', (async () => {
const folder1 = await Folder.save({ title: 'folder1' });
const noOfNotes = 20;
Expand Down
23 changes: 19 additions & 4 deletions packages/lib/models/Note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,30 +595,45 @@ export default class Note extends BaseItem {
}
}

static async duplicate(noteId: string, options: any = null) {
private static async duplicateNoteResources(noteBody: string): Promise<string> {
const resourceIds = await this.linkedResourceIds(noteBody);
let newBody: string = noteBody;

for (const resourceId of resourceIds) {
const newResource = await Resource.duplicateResource(resourceId);
const regex = new RegExp(resourceId, 'gi');
newBody = newBody.replace(regex, newResource.id);
}

return newBody;
}

public static async duplicate(noteId: string, options: any = null) {
const changes = options && options.changes;
const uniqueTitle = options && options.uniqueTitle;

const originalNote = await Note.load(noteId);
const originalNote: NoteEntity = await Note.load(noteId);
if (!originalNote) throw new Error(`Unknown note: ${noteId}`);

const newNote = Object.assign({}, originalNote);
const fieldsToReset = ['id', 'created_time', 'updated_time', 'user_created_time', 'user_updated_time'];

for (const field of fieldsToReset) {
delete newNote[field];
delete (newNote as any)[field];
}

for (const n in changes) {
if (!changes.hasOwnProperty(n)) continue;
newNote[n] = changes[n];
(newNote as any)[n] = changes[n];
}

if (uniqueTitle) {
const title = await Note.findUniqueItemTitle(uniqueTitle);
newNote.title = title;
}

newNote.body = await this.duplicateNoteResources(newNote.body);

const newNoteSaved = await this.save(newNote);
const originalTags = await Tag.tagsByNoteId(noteId);
for (const tagToAdd of originalTags) {
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/models/Resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ export default class Resource extends BaseItem {
return await this.fsDriver().readFile(Resource.fullPath(resource), encoding);
}

static async duplicateResource(resourceId: string) {
public static async duplicateResource(resourceId: string): Promise<ResourceEntity> {
const resource = await Resource.load(resourceId);
const localState = await Resource.localState(resource);

Expand Down

0 comments on commit c0a8c33

Please sign in to comment.