Skip to content

Commit

Permalink
All: Fixes #593: Resource should not be auto-deleted if they've never…
Browse files Browse the repository at this point in the history
… been linked to any note
  • Loading branch information
laurent22 committed Jun 17, 2018
1 parent cf4331c commit f5a72ff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 10 additions & 0 deletions CliClient/tests/services_ResourceService.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,14 @@ describe('services_ResourceService', function() {
expect(!!(await Resource.load(resource1.id))).toBe(true);
}));

it('should not delete a resource that has never been associated with any note, because it probably means the resource came via sync, and associated note has not arrived yet', asyncTest(async () => {
const service = new ResourceService();
const resource = await shim.createResourceFromPath(__dirname + '/../tests/support/photo.jpg');

await service.indexNoteResources();
await service.deleteOrphanResources(0);

expect((await Resource.all()).length).toBe(1);
}));

});
8 changes: 7 additions & 1 deletion ReactNativeClient/lib/models/NoteResource.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const BaseModel = require('lib/BaseModel.js');

// - If is_associated = 1, note_resources indicates which note_id is currently associated with the given resource_id
// - If is_associated = 0, note_resources indicates which note_id *was* associated with the given resource_id
// - last_seen_time tells the last time that reosurce was associated with this note.
// - If last_seen_time is 0, it means the resource has never been associated with any note.

class NoteResource extends BaseModel {

static tableName() {
Expand Down Expand Up @@ -39,7 +44,7 @@ class NoteResource extends BaseModel {
const queries = [];
for (let i = 0; i < missingResources.length; i++) {
const id = missingResources[i].id;
queries.push({ sql: 'INSERT INTO note_resources (note_id, resource_id, is_associated, last_seen_time) VALUES (?, ?, ?, ?)', params: ["", id, 0, Date.now()] });
queries.push({ sql: 'INSERT INTO note_resources (note_id, resource_id, is_associated, last_seen_time) VALUES (?, ?, ?, ?)', params: ["", id, 0, 0] });
}
await this.db().transactionExecBatch(queries);
}
Expand All @@ -57,6 +62,7 @@ class NoteResource extends BaseModel {
GROUP BY resource_id
HAVING sum(is_associated) <= 0
AND last_seen_time < ?
AND last_seen_time != 0
`, [cutOffTime]);
return output.map(r => r.resource_id);
}
Expand Down

0 comments on commit f5a72ff

Please sign in to comment.