1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00

Api: Added way to get the notes associated with a resource

This commit is contained in:
Laurent Cozic 2020-11-07 16:45:02 +00:00
parent 83b175bb5b
commit aa47ffb30f
8 changed files with 43 additions and 11 deletions

View File

@ -847,6 +847,9 @@ packages/lib/markupLanguageUtils.js.map
packages/lib/models/Alarm.d.ts
packages/lib/models/Alarm.js
packages/lib/models/Alarm.js.map
packages/lib/models/NoteResource.d.ts
packages/lib/models/NoteResource.js
packages/lib/models/NoteResource.js.map
packages/lib/models/Setting.d.ts
packages/lib/models/Setting.js
packages/lib/models/Setting.js.map

3
.gitignore vendored
View File

@ -839,6 +839,9 @@ packages/lib/markupLanguageUtils.js.map
packages/lib/models/Alarm.d.ts
packages/lib/models/Alarm.js
packages/lib/models/Alarm.js.map
packages/lib/models/NoteResource.d.ts
packages/lib/models/NoteResource.js
packages/lib/models/NoteResource.js.map
packages/lib/models/Setting.d.ts
packages/lib/models/Setting.js
packages/lib/models/Setting.js.map

View File

@ -794,6 +794,9 @@ packages/lib/markupLanguageUtils.js.map
packages/lib/models/Alarm.d.ts
packages/lib/models/Alarm.js
packages/lib/models/Alarm.js.map
packages/lib/models/NoteResource.d.ts
packages/lib/models/NoteResource.js
packages/lib/models/NoteResource.js.map
packages/lib/models/Setting.d.ts
packages/lib/models/Setting.js
packages/lib/models/Setting.js.map

View File

@ -10,7 +10,7 @@ const Tag = require('@joplin/lib/models/Tag.js');
const NoteTag = require('@joplin/lib/models/NoteTag.js');
const Resource = require('@joplin/lib/models/Resource.js');
const ItemChange = require('@joplin/lib/models/ItemChange.js');
const NoteResource = require('@joplin/lib/models/NoteResource.js');
const NoteResource = require('@joplin/lib/models/NoteResource').default;
const ResourceService = require('@joplin/lib/services/ResourceService.js');
const fs = require('fs-extra');
const ArrayUtils = require('@joplin/lib/ArrayUtils');

View File

@ -8,6 +8,7 @@ const Resource = require('@joplin/lib/models/Resource');
const Note = require('@joplin/lib/models/Note');
const Tag = require('@joplin/lib/models/Tag');
const NoteTag = require('@joplin/lib/models/NoteTag');
const ResourceService = require('@joplin/lib/services/ResourceService');
async function msleep(ms:number) {
return new Promise((resolve) => {
@ -653,4 +654,18 @@ describe('services_rest_Api', function() {
expect('title' in r).toBe(true);
}
}));
it('should return the notes associated with a resource', asyncTest(async () => {
const note = await Note.save({});
await shim.attachFileToNote(note, `${__dirname}/../tests/support/photo.jpg`);
const resource = (await Resource.all())[0];
const resourceService = new ResourceService();
await resourceService.indexNoteResources();
const r = await api.route(RequestMethod.GET, `resources/${resource.id}/notes`);
expect(r.items.length).toBe(1);
expect(r.items[0]).toBe(note.id);
}));
});

View File

@ -1,11 +1,11 @@
const BaseModel = require('../BaseModel').default;
import BaseModel from '../BaseModel';
// - 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 {
export default class NoteResource extends BaseModel {
static tableName() {
return 'note_resources';
}
@ -14,7 +14,12 @@ class NoteResource extends BaseModel {
return BaseModel.TYPE_NOTE_RESOURCE;
}
static async setAssociatedResources(noteId, resourceIds) {
static async associatedNoteIds(resourceId:string):Promise<string[]> {
const rows = await this.modelSelectAll('SELECT note_id FROM note_resources WHERE resource_id = ? AND is_associated = 1', [resourceId]);
return rows.map((r:any) => r.note_id);
}
static async setAssociatedResources(noteId:string, resourceIds:string[]) {
const existingRows = await this.modelSelectAll('SELECT * FROM note_resources WHERE note_id = ?', [noteId]);
const notProcessedResourceIds = resourceIds.slice();
@ -48,11 +53,11 @@ class NoteResource extends BaseModel {
await this.db().transactionExecBatch(queries);
}
static async remove(noteId) {
static async remove(noteId:string) {
await this.db().exec({ sql: 'UPDATE note_resources SET is_associated = 0 WHERE note_id = ?', params: [noteId] });
}
static async orphanResources(expiryDelay = null) {
static async orphanResources(expiryDelay:number = null) {
if (expiryDelay === null) expiryDelay = 1000 * 60 * 60 * 24 * 10;
const cutOffTime = Date.now() - expiryDelay;
const output = await this.modelSelectAll(
@ -66,12 +71,10 @@ class NoteResource extends BaseModel {
`,
[cutOffTime]
);
return output.map(r => r.resource_id);
return output.map((r:any) => r.resource_id);
}
static async deleteByResource(resourceId) {
static async deleteByResource(resourceId:string) {
await this.db().exec('DELETE FROM note_resources WHERE resource_id = ?', [resourceId]);
}
}
module.exports = NoteResource;

View File

@ -1,5 +1,5 @@
const ItemChange = require('../models/ItemChange');
const NoteResource = require('../models/NoteResource');
const NoteResource = require('../models/NoteResource').default;
const Note = require('../models/Note');
const Resource = require('../models/Resource');
const BaseModel = require('../BaseModel').default;

View File

@ -5,6 +5,7 @@ import defaultAction from '../utils/defaultAction';
import { ErrorBadRequest, ErrorNotFound } from '../utils/errors';
import readonlyProperties from '../utils/readonlyProperties';
import ApiResponse from '../ApiResponse';
import NoteResource from '../../../models/NoteResource';
const Resource = require('../../../models/Resource');
export default async function(request:Request, id:string = null, link:string = null) {
@ -30,6 +31,10 @@ export default async function(request:Request, id:string = null, link:string = n
return response;
}
if (link === 'notes') {
return { items: await NoteResource.associatedNoteIds(id) };
}
if (link) throw new ErrorNotFound();
}